From 0087ec1a5006e4ea517c5a6db3d6b9378d755ce9 Mon Sep 17 00:00:00 2001 From: microproofs Date: Mon, 25 Nov 2024 08:36:01 +0700 Subject: [PATCH] Start adding implementation for new builtins --- crates/aiken-lang/src/builtins.rs | 13 +++++ crates/uplc/src/builtins.rs | 69 +++++++++++++++++++++++++-- crates/uplc/src/machine/cost_model.rs | 54 +++++++++++++++++++++ crates/uplc/src/machine/runtime.rs | 56 +++++++++++++++++++++- 4 files changed, 187 insertions(+), 5 deletions(-) diff --git a/crates/aiken-lang/src/builtins.rs b/crates/aiken-lang/src/builtins.rs index 2a9c53c14..fc24c7368 100644 --- a/crates/aiken-lang/src/builtins.rs +++ b/crates/aiken-lang/src/builtins.rs @@ -908,6 +908,19 @@ pub fn from_default_function(builtin: DefaultFunction, id_gen: &IdGenerator) -> (tipo, 2) } + DefaultFunction::AndByteString => todo!(), + DefaultFunction::OrByteString => todo!(), + DefaultFunction::XorByteString => todo!(), + DefaultFunction::ComplementByteString => todo!(), + DefaultFunction::ReadBit => todo!(), + DefaultFunction::WriteBits => todo!(), + DefaultFunction::ReplicateByte => todo!(), + DefaultFunction::ShiftByteString => todo!(), + DefaultFunction::RotateByteString => todo!(), + DefaultFunction::CountSetBits => todo!(), + DefaultFunction::FindFirstSetBit => todo!(), + DefaultFunction::Ripemd_160 => todo!(), + DefaultFunction::ExpModInteger => todo!(), }; ValueConstructor::public( diff --git a/crates/uplc/src/builtins.rs b/crates/uplc/src/builtins.rs index 2a777c41c..4464b3123 100644 --- a/crates/uplc/src/builtins.rs +++ b/crates/uplc/src/builtins.rs @@ -101,9 +101,28 @@ pub enum DefaultFunction { Bls12_381_MulMlResult = 69, Bls12_381_FinalVerify = 70, - // Bitwise + // Conversions IntegerToByteString = 73, ByteStringToInteger = 74, + // Logical + AndByteString = 75, + OrByteString = 76, + XorByteString = 77, + ComplementByteString = 78, + ReadBit = 79, + WriteBits = 80, + ReplicateByte = 81, + // Bitwise + ShiftByteString = 82, + RotateByteString = 83, + CountSetBits = 84, + FindFirstSetBit = 85, + // Ripemd_160 + Ripemd_160 = 86, + ExpModInteger = 87, + // Match + // CaseList = 88, + // CaseData = 89, } impl TryFrom for DefaultFunction { @@ -362,11 +381,23 @@ impl FromStr for DefaultFunction { "bls12_381_millerLoop" => Ok(Bls12_381_MillerLoop), "bls12_381_mulMlResult" => Ok(Bls12_381_MulMlResult), "bls12_381_finalVerify" => Ok(Bls12_381_FinalVerify), - - // Bitwise "integerToByteString" => Ok(IntegerToByteString), "byteStringToInteger" => Ok(ByteStringToInteger), - + "andByteString" => Ok(AndByteString), + "orByteString" => Ok(OrByteString), + "xorByteString" => Ok(XorByteString), + "complementByteString" => Ok(ComplementByteString), + "readBit" => Ok(ReadBit), + "writeBits" => Ok(WriteBits), + "replicateByte" => Ok(ReplicateByte), + "shiftByteString" => Ok(ShiftByteString), + "rotateByteString" => Ok(RotateByteString), + "countSetBits" => Ok(CountSetBits), + "findFirstSetBit" => Ok(FindFirstSetBit), + "ripemd160" => Ok(Ripemd_160), + "expModInteger" => Ok(ExpModInteger), + // "caseList" => Ok(CaseList), + // "caseData" => Ok(CaseData), rest => Err(format!("Default Function not found - {rest}")), } } @@ -452,6 +483,21 @@ impl Display for DefaultFunction { Bls12_381_FinalVerify => write!(f, "bls12_381_finalVerify"), IntegerToByteString => write!(f, "integerToByteString"), ByteStringToInteger => write!(f, "byteStringToInteger"), + AndByteString => write!(f, "andByteString"), + OrByteString => write!(f, "orByteString"), + XorByteString => write!(f, "xorByteString"), + ComplementByteString => write!(f, "complementByteString"), + ReadBit => write!(f, "readBit"), + WriteBits => write!(f, "writeBits"), + ReplicateByte => write!(f, "replicateByte"), + ShiftByteString => write!(f, "shiftByteString"), + RotateByteString => write!(f, "rotateByteString"), + CountSetBits => write!(f, "countSetBits"), + FindFirstSetBit => write!(f, "findFirstSetBit"), + Ripemd_160 => write!(f, "ripemd160"), + ExpModInteger => write!(f, "expModInteger"), + // CaseList => write!(f, "caseList"), + // CaseData => write!(f, "caseData"), } } } @@ -536,6 +582,21 @@ impl DefaultFunction { Bls12_381_FinalVerify => "bls12_381_final_verify", IntegerToByteString => "integer_to_bytearray", ByteStringToInteger => "bytearray_to_integer", + AndByteString => "and_bytearray", + OrByteString => "or_bytearray", + XorByteString => "xor_bytearray", + ComplementByteString => "complement_bytearray", + ReadBit => "read_bit", + WriteBits => "write_bits", + ReplicateByte => "replicate_byte", + ShiftByteString => "shift_bytearray", + RotateByteString => "rotate_bytearray", + CountSetBits => "count_set_bits", + FindFirstSetBit => "find_first_set_bit", + Ripemd_160 => "ripemd160", + ExpModInteger => "exp_mod_integer", + // CaseList => "case_list", + // CaseData => "case_data", } .to_string() } diff --git a/crates/uplc/src/machine/cost_model.rs b/crates/uplc/src/machine/cost_model.rs index e7bd36da4..51c28b0cc 100644 --- a/crates/uplc/src/machine/cost_model.rs +++ b/crates/uplc/src/machine/cost_model.rs @@ -2566,6 +2566,60 @@ impl BuiltinCosts { .cpu .cost(args[0].to_ex_mem(), args[1].to_ex_mem()), }, + DefaultFunction::AndByteString => ExBudget { + mem: self + .and_byte_string + .mem + .cost(args[0].to_ex_mem(), args[1].to_ex_mem()), + cpu: self + .and_byte_string + .cpu + .cost(args[0].to_ex_mem(), args[1].to_ex_mem()), + }, + DefaultFunction::OrByteString => ExBudget { + mem: self + .or_byte_string + .mem + .cost(args[0].to_ex_mem(), args[1].to_ex_mem()), + cpu: self + .or_byte_string + .cpu + .cost(args[0].to_ex_mem(), args[1].to_ex_mem()), + }, + DefaultFunction::XorByteString => ExBudget { + mem: self + .xor_byte_string + .mem + .cost(args[0].to_ex_mem(), args[1].to_ex_mem()), + cpu: self + .xor_byte_string + .cpu + .cost(args[0].to_ex_mem(), args[1].to_ex_mem()), + }, + DefaultFunction::ComplementByteString => ExBudget { + mem: self.complement_byte_string.mem.cost(args[0].to_ex_mem()), + cpu: self.complement_byte_string.cpu.cost(args[0].to_ex_mem()), + }, + DefaultFunction::ReadBit => ExBudget { + mem: self + .read_bit + .mem + .cost(args[0].to_ex_mem(), args[1].to_ex_mem()), + cpu: self + .read_bit + .cpu + .cost(args[0].to_ex_mem(), args[1].to_ex_mem()), + }, + DefaultFunction::WriteBits => todo!(), + DefaultFunction::ReplicateByte => todo!(), + DefaultFunction::ShiftByteString => todo!(), + DefaultFunction::RotateByteString => todo!(), + DefaultFunction::CountSetBits => todo!(), + DefaultFunction::FindFirstSetBit => todo!(), + DefaultFunction::Ripemd_160 => todo!(), + DefaultFunction::ExpModInteger => todo!(), + // DefaultFunction::CaseList => todo!(), + // DefaultFunction::CaseData => todo!(), }) } } diff --git a/crates/uplc/src/machine/runtime.rs b/crates/uplc/src/machine/runtime.rs index 124212a8a..305eec7f3 100644 --- a/crates/uplc/src/machine/runtime.rs +++ b/crates/uplc/src/machine/runtime.rs @@ -177,7 +177,22 @@ impl DefaultFunction { | DefaultFunction::Bls12_381_MulMlResult | DefaultFunction::Bls12_381_FinalVerify | DefaultFunction::IntegerToByteString - | DefaultFunction::ByteStringToInteger => false, + | DefaultFunction::ByteStringToInteger + | DefaultFunction::AndByteString + | DefaultFunction::OrByteString + | DefaultFunction::XorByteString + | DefaultFunction::ComplementByteString + | DefaultFunction::ReadBit + | DefaultFunction::WriteBits + | DefaultFunction::ReplicateByte + | DefaultFunction::ShiftByteString + | DefaultFunction::RotateByteString + | DefaultFunction::CountSetBits + | DefaultFunction::FindFirstSetBit + | DefaultFunction::Ripemd_160 + | DefaultFunction::ExpModInteger => false, + // | DefaultFunction::CaseList + // | DefaultFunction::CaseData } } @@ -258,6 +273,19 @@ impl DefaultFunction { DefaultFunction::Bls12_381_FinalVerify => 2, DefaultFunction::IntegerToByteString => 3, DefaultFunction::ByteStringToInteger => 2, + DefaultFunction::AndByteString => 2, + DefaultFunction::OrByteString => 2, + DefaultFunction::XorByteString => 2, + DefaultFunction::ComplementByteString => todo!(), + DefaultFunction::ReadBit => todo!(), + DefaultFunction::WriteBits => todo!(), + DefaultFunction::ReplicateByte => todo!(), + DefaultFunction::ShiftByteString => todo!(), + DefaultFunction::RotateByteString => todo!(), + DefaultFunction::CountSetBits => todo!(), + DefaultFunction::FindFirstSetBit => todo!(), + DefaultFunction::Ripemd_160 => todo!(), + DefaultFunction::ExpModInteger => todo!(), } } @@ -338,6 +366,19 @@ impl DefaultFunction { DefaultFunction::Bls12_381_FinalVerify => 0, DefaultFunction::IntegerToByteString => 0, DefaultFunction::ByteStringToInteger => 0, + DefaultFunction::AndByteString => 0, + DefaultFunction::OrByteString => 0, + DefaultFunction::XorByteString => 0, + DefaultFunction::ComplementByteString => 0, + DefaultFunction::ReadBit => 0, + DefaultFunction::WriteBits => 0, + DefaultFunction::ReplicateByte => 0, + DefaultFunction::ShiftByteString => 0, + DefaultFunction::RotateByteString => 0, + DefaultFunction::CountSetBits => 0, + DefaultFunction::FindFirstSetBit => 0, + DefaultFunction::Ripemd_160 => 0, + DefaultFunction::ExpModInteger => 0, } } @@ -1454,6 +1495,19 @@ impl DefaultFunction { Ok(Value::Con(constant.into())) } + DefaultFunction::AndByteString => todo!(), + DefaultFunction::OrByteString => todo!(), + DefaultFunction::XorByteString => todo!(), + DefaultFunction::ComplementByteString => todo!(), + DefaultFunction::ReadBit => todo!(), + DefaultFunction::WriteBits => todo!(), + DefaultFunction::ReplicateByte => todo!(), + DefaultFunction::ShiftByteString => todo!(), + DefaultFunction::RotateByteString => todo!(), + DefaultFunction::CountSetBits => todo!(), + DefaultFunction::FindFirstSetBit => todo!(), + DefaultFunction::Ripemd_160 => todo!(), + DefaultFunction::ExpModInteger => todo!(), } } }