Skip to content

Commit

Permalink
Start adding implementation for new builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroProofs committed Nov 25, 2024
1 parent 55c6907 commit 0087ec1
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 5 deletions.
13 changes: 13 additions & 0 deletions crates/aiken-lang/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
69 changes: 65 additions & 4 deletions crates/uplc/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> for DefaultFunction {
Expand Down Expand Up @@ -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}")),
}
}
Expand Down Expand Up @@ -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"),
}
}
}
Expand Down Expand Up @@ -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()
}
Expand Down
54 changes: 54 additions & 0 deletions crates/uplc/src/machine/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
})
}
}
Expand Down
56 changes: 55 additions & 1 deletion crates/uplc/src/machine/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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!(),
}
}

Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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!(),
}
}
}
Expand Down

0 comments on commit 0087ec1

Please sign in to comment.