feat: add conversion to data and from data for new primitive types

This commit is contained in:
microproofs 2023-11-07 00:10:42 -05:00 committed by Lucas
parent 3675762c3e
commit d51374aac1
3 changed files with 126 additions and 6 deletions

View File

@ -1,4 +1,4 @@
use std::{collections::HashMap, rc::Rc}; use std::{collections::HashMap, ops::Deref, rc::Rc};
use indexmap::{IndexMap, IndexSet}; use indexmap::{IndexMap, IndexSet};
use itertools::Itertools; use itertools::Itertools;
@ -7,7 +7,7 @@ use uplc::{
builder::{CONSTR_FIELDS_EXPOSER, CONSTR_INDEX_EXPOSER}, builder::{CONSTR_FIELDS_EXPOSER, CONSTR_INDEX_EXPOSER},
builtins::DefaultFunction, builtins::DefaultFunction,
machine::{ machine::{
runtime::{convert_constr_to_tag, ANY_TAG}, runtime::{convert_constr_to_tag, Compressable, ANY_TAG},
value::to_pallas_bigint, value::to_pallas_bigint,
}, },
Constr, KeyValuePairs, PlutusData, Constr, KeyValuePairs, PlutusData,
@ -1213,6 +1213,10 @@ pub fn convert_data_to_type(term: Term<Name>, field_type: &Rc<Type>) -> Term<Nam
Term::equals_integer() Term::equals_integer()
.apply(Term::integer(1.into())) .apply(Term::integer(1.into()))
.apply(Term::fst_pair().apply(Term::unconstr_data().apply(term))) .apply(Term::fst_pair().apply(Term::unconstr_data().apply(term)))
} else if field_type.is_bls381_12_g1() {
Term::bls12_381_g1_uncompress().apply(Term::un_b_data().apply(term))
} else if field_type.is_bls381_12_g2() {
Term::bls12_381_g2_uncompress().apply(Term::un_b_data().apply(term))
} else { } else {
term term
} }
@ -1292,9 +1296,13 @@ pub fn convert_constants_to_data(constants: Vec<Rc<UplcConstant>>) -> Vec<UplcCo
any_constructor: None, any_constructor: None,
fields: vec![], fields: vec![],
})), })),
UplcConstant::Bls12_381G1Element(_) => todo!(), UplcConstant::Bls12_381G1Element(b) => UplcConstant::Data(PlutusData::BoundedBytes(
UplcConstant::Bls12_381G2Element(_) => todo!(), b.deref().clone().compress().into(),
UplcConstant::Bls12_381MlResult(_) => todo!(), )),
UplcConstant::Bls12_381G2Element(b) => UplcConstant::Data(PlutusData::BoundedBytes(
b.deref().clone().compress().into(),
)),
UplcConstant::Bls12_381MlResult(_) => unreachable!(),
}; };
new_constants.push(constant); new_constants.push(constant);
} }
@ -1353,6 +1361,10 @@ pub fn convert_type_to_data(term: Term<Name>, field_type: &Rc<Type>) -> Term<Nam
.into(), .into(),
), ),
) )
} else if field_type.is_bls381_12_g1() {
Term::bls12_381_g1_compress().apply(Term::b_data().apply(term))
} else if field_type.is_bls381_12_g2() {
Term::bls12_381_g2_compress().apply(Term::b_data().apply(term))
} else { } else {
term term
} }

View File

@ -1,6 +1,7 @@
use self::{environment::Environment, pretty::Printer}; use self::{environment::Environment, pretty::Printer};
use crate::{ use crate::{
ast::{Constant, DefinitionLocation, ModuleKind, Span}, ast::{Constant, DefinitionLocation, ModuleKind, Span},
builtins::{G1_ELEMENT, G2_ELEMENT, MILLER_LOOP_RESULT},
tipo::fields::FieldMap, tipo::fields::FieldMap,
}; };
use std::{cell::RefCell, collections::HashMap, ops::Deref, rc::Rc}; use std::{cell::RefCell, collections::HashMap, ops::Deref, rc::Rc};
@ -130,6 +131,33 @@ impl Type {
} }
} }
pub fn is_bls381_12_g1(&self) -> 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 { pub fn is_string(&self) -> bool {
match self { match self {
Self::App { module, name, .. } if "String" == name && module.is_empty() => true, 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(), Self::Var { tipo } => tipo.borrow().get_uplc_type().unwrap(),
_ => todo!(), _ => unreachable!(),
} }
} else { } else {
UplcType::Data 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 { pub fn is_string(&self) -> bool {
match self { match self {
Self::Link { tipo } => tipo.is_string(), Self::Link { tipo } => tipo.is_string(),

View File

@ -97,10 +97,66 @@ impl<T> Term<T> {
Term::Builtin(DefaultFunction::BData) Term::Builtin(DefaultFunction::BData)
} }
pub fn blake2b_224() -> Self {
Term::Builtin(DefaultFunction::Blake2b_224)
}
pub fn blake2b_256() -> Self { pub fn blake2b_256() -> Self {
Term::Builtin(DefaultFunction::Blake2b_256) 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( pub fn choose_data(
self, self,
constr_case: Self, constr_case: Self,
@ -199,6 +255,10 @@ impl<T> Term<T> {
Term::Builtin(DefaultFunction::IndexByteString) Term::Builtin(DefaultFunction::IndexByteString)
} }
pub fn keccak_256() -> Self {
Term::Builtin(DefaultFunction::Keccak_256)
}
pub fn length_of_bytearray() -> Self { pub fn length_of_bytearray() -> Self {
Term::Builtin(DefaultFunction::LengthOfByteString) Term::Builtin(DefaultFunction::LengthOfByteString)
} }