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 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<Name>, field_type: &Rc<Type>) -> Term<Nam
Term::equals_integer()
.apply(Term::integer(1.into()))
.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 {
term
}
@@ -1292,9 +1296,13 @@ pub fn convert_constants_to_data(constants: Vec<Rc<UplcConstant>>) -> Vec<UplcCo
any_constructor: None,
fields: vec![],
})),
UplcConstant::Bls12_381G1Element(_) => 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<Name>, field_type: &Rc<Type>) -> Term<Nam
.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 {
term
}

View File

@@ -1,6 +1,7 @@
use self::{environment::Environment, pretty::Printer};
use crate::{
ast::{Constant, DefinitionLocation, ModuleKind, Span},
builtins::{G1_ELEMENT, G2_ELEMENT, MILLER_LOOP_RESULT},
tipo::fields::FieldMap,
};
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 {
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(),