From d51374aac1fbbc299b648ffa1dba97f252d66764 Mon Sep 17 00:00:00 2001 From: microproofs Date: Tue, 7 Nov 2023 00:10:42 -0500 Subject: [PATCH] feat: add conversion to data and from data for new primitive types --- crates/aiken-lang/src/gen_uplc/builder.rs | 22 +++++++-- crates/aiken-lang/src/tipo.rs | 50 ++++++++++++++++++- crates/uplc/src/builder.rs | 60 +++++++++++++++++++++++ 3 files changed, 126 insertions(+), 6 deletions(-) diff --git a/crates/aiken-lang/src/gen_uplc/builder.rs b/crates/aiken-lang/src/gen_uplc/builder.rs index ed4e8a28..14731960 100644 --- a/crates/aiken-lang/src/gen_uplc/builder.rs +++ b/crates/aiken-lang/src/gen_uplc/builder.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, rc::Rc}; +use std::{collections::HashMap, ops::Deref, rc::Rc}; use indexmap::{IndexMap, IndexSet}; use itertools::Itertools; @@ -7,7 +7,7 @@ use uplc::{ builder::{CONSTR_FIELDS_EXPOSER, CONSTR_INDEX_EXPOSER}, builtins::DefaultFunction, machine::{ - runtime::{convert_constr_to_tag, ANY_TAG}, + runtime::{convert_constr_to_tag, Compressable, ANY_TAG}, value::to_pallas_bigint, }, Constr, KeyValuePairs, PlutusData, @@ -1213,6 +1213,10 @@ pub fn convert_data_to_type(term: Term, field_type: &Rc) -> Term>) -> Vec todo!(), - UplcConstant::Bls12_381G2Element(_) => todo!(), - UplcConstant::Bls12_381MlResult(_) => todo!(), + UplcConstant::Bls12_381G1Element(b) => UplcConstant::Data(PlutusData::BoundedBytes( + b.deref().clone().compress().into(), + )), + UplcConstant::Bls12_381G2Element(b) => UplcConstant::Data(PlutusData::BoundedBytes( + b.deref().clone().compress().into(), + )), + UplcConstant::Bls12_381MlResult(_) => unreachable!(), }; new_constants.push(constant); } @@ -1353,6 +1361,10 @@ pub fn convert_type_to_data(term: Term, field_type: &Rc) -> Term bool { + match self { + Self::App { module, name, .. } => G1_ELEMENT == name && module.is_empty(), + + Self::Var { tipo } => tipo.borrow().is_bls381_12_g1(), + _ => false, + } + } + + pub fn is_bls381_12_g2(&self) -> bool { + match self { + Self::App { module, name, .. } => G2_ELEMENT == name && module.is_empty(), + + Self::Var { tipo } => tipo.borrow().is_bls381_12_g2(), + _ => false, + } + } + + pub fn is_ml_result(&self) -> bool { + match self { + Self::App { module, name, .. } => MILLER_LOOP_RESULT == name && module.is_empty(), + + Self::Var { tipo } => tipo.borrow().is_ml_result(), + _ => false, + } + } + pub fn is_string(&self) -> bool { match self { Self::App { module, name, .. } if "String" == name && module.is_empty() => true, @@ -292,7 +320,7 @@ impl Type { } } Self::Var { tipo } => tipo.borrow().get_uplc_type().unwrap(), - _ => todo!(), + _ => unreachable!(), } } else { UplcType::Data @@ -456,6 +484,26 @@ impl TypeVar { } } + pub fn is_bls381_12_g1(&self) -> bool { + match self { + Self::Link { tipo } => tipo.is_bls381_12_g1(), + _ => false, + } + } + + pub fn is_bls381_12_g2(&self) -> bool { + match self { + Self::Link { tipo } => tipo.is_bls381_12_g2(), + _ => false, + } + } + pub fn is_ml_result(&self) -> bool { + match self { + Self::Link { tipo } => tipo.is_ml_result(), + _ => false, + } + } + pub fn is_string(&self) -> bool { match self { Self::Link { tipo } => tipo.is_string(), diff --git a/crates/uplc/src/builder.rs b/crates/uplc/src/builder.rs index d996da53..d0703b0c 100644 --- a/crates/uplc/src/builder.rs +++ b/crates/uplc/src/builder.rs @@ -97,10 +97,66 @@ impl Term { Term::Builtin(DefaultFunction::BData) } + pub fn blake2b_224() -> Self { + Term::Builtin(DefaultFunction::Blake2b_224) + } + pub fn blake2b_256() -> Self { Term::Builtin(DefaultFunction::Blake2b_256) } + pub fn bls12_381_g1_add() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G1_Add) + } + pub fn bls12_381_g1_neg() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G1_Neg) + } + pub fn bls12_381_g1_scalar_mul() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G1_ScalarMul) + } + pub fn bls12_381_g1_equal() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G1_Equal) + } + pub fn bls12_381_g1_compress() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G1_Compress) + } + pub fn bls12_381_g1_uncompress() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G1_Uncompress) + } + pub fn bls12_381_g1_hash_to_group() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G1_HashToGroup) + } + pub fn bls12_381_g2_add() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G2_Add) + } + pub fn bls12_381_g2_neg() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G2_Neg) + } + pub fn bls12_381_g2_scalar_mul() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G2_ScalarMul) + } + pub fn bls12_381_g2_equal() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G2_Equal) + } + pub fn bls12_381_g2_compress() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G2_Compress) + } + pub fn bls12_381_g2_uncompress() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G2_Uncompress) + } + pub fn bls12_381_g2_hash_to_group() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_G2_HashToGroup) + } + pub fn bls12_381_miller_loop() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_MillerLoop) + } + pub fn bls12_381_mul_ml_result() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_MulMlResult) + } + pub fn bls12_381_final_verify() -> Self { + Term::Builtin(DefaultFunction::Bls12_381_FinalVerify) + } + pub fn choose_data( self, constr_case: Self, @@ -199,6 +255,10 @@ impl Term { Term::Builtin(DefaultFunction::IndexByteString) } + pub fn keccak_256() -> Self { + Term::Builtin(DefaultFunction::Keccak_256) + } + pub fn length_of_bytearray() -> Self { Term::Builtin(DefaultFunction::LengthOfByteString) }