mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-23 18:21:38 +03:00
add from type conversion method
This commit is contained in:
parent
5c005c4b8f
commit
fd51ee28e2
@ -265,7 +265,7 @@ impl<'ast> fmt::Display for NumberImplicit<'ast> {
|
||||
#[pest_ast(rule(Rule::value_integer))]
|
||||
pub struct Integer<'ast> {
|
||||
pub number: Number<'ast>,
|
||||
pub _type: Option<IntegerType>,
|
||||
pub _type: IntegerType,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
@ -645,6 +645,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
||||
Expression::FieldElement(fe) => Ok(Self::get_field_element_constant(fe)),
|
||||
Expression::GroupElement(gr) => Ok(ConstrainedValue::GroupElement(gr)),
|
||||
Expression::Boolean(bool) => Ok(Self::get_boolean_constant(bool)),
|
||||
Expression::Implicit(string) => unimplemented!(),
|
||||
|
||||
// Binary operations
|
||||
Expression::Add(left, right) => {
|
||||
|
@ -2,13 +2,15 @@
|
||||
|
||||
use crate::{
|
||||
errors::ValueError,
|
||||
types::{Circuit, FieldElement, Function, Identifier, Type},
|
||||
Integer,
|
||||
types::{Circuit, FieldElement, Function, Identifier, Integer, IntegerType, Type},
|
||||
};
|
||||
|
||||
use snarkos_models::{
|
||||
curves::{Field, Group, PrimeField},
|
||||
gadgets::utilities::boolean::Boolean,
|
||||
gadgets::utilities::{
|
||||
boolean::Boolean, uint128::UInt128, uint16::UInt16, uint32::UInt32, uint64::UInt64,
|
||||
uint8::UInt8,
|
||||
},
|
||||
};
|
||||
use std::fmt;
|
||||
|
||||
@ -106,6 +108,30 @@ impl<F: Field + PrimeField, G: Group> ConstrainedValue<F, G> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn from_type(value: String, _type: &Type<F, G>) -> Result<Self, ValueError> {
|
||||
Ok(match _type {
|
||||
Type::IntegerType(integer_type) => ConstrainedValue::Integer(match integer_type {
|
||||
IntegerType::U8 => Integer::U8(UInt8::constant(value.parse::<u8>()?)),
|
||||
IntegerType::U16 => Integer::U16(UInt16::constant(value.parse::<u16>()?)),
|
||||
IntegerType::U32 => Integer::U32(UInt32::constant(value.parse::<u32>()?)),
|
||||
IntegerType::U64 => Integer::U64(UInt64::constant(value.parse::<u64>()?)),
|
||||
IntegerType::U128 => Integer::U128(UInt128::constant(value.parse::<u128>()?)),
|
||||
}),
|
||||
Type::FieldElement => ConstrainedValue::FieldElement(FieldElement::Constant(
|
||||
F::from_str(&value).unwrap_or_default(),
|
||||
)),
|
||||
Type::GroupElement => ConstrainedValue::GroupElement({
|
||||
use std::str::FromStr;
|
||||
|
||||
let scalar = G::ScalarField::from_str(&value).unwrap_or_default();
|
||||
let point = G::default().mul(&scalar);
|
||||
point
|
||||
}),
|
||||
Type::Boolean => ConstrainedValue::Boolean(Boolean::Constant(value.parse::<bool>()?)),
|
||||
_ => unimplemented!("Cannot implicitly create type"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field + PrimeField, G: Group> fmt::Display for ConstrainedValue<F, G> {
|
||||
|
@ -1,7 +1,18 @@
|
||||
use crate::errors::IntegerError;
|
||||
|
||||
use std::num::ParseIntError;
|
||||
use std::str::ParseBoolError;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ValueError {
|
||||
#[error("{}", _0)]
|
||||
ParseIntError(ParseIntError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ParseBoolError(ParseBoolError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
IntegerError(IntegerError),
|
||||
/// Unexpected array length
|
||||
#[error("{}", _0)]
|
||||
ArrayLength(String),
|
||||
@ -9,9 +20,6 @@ pub enum ValueError {
|
||||
#[error("Expected type array, got {}", _0)]
|
||||
ArrayModel(String),
|
||||
|
||||
#[error("{}", _0)]
|
||||
IntegerError(IntegerError),
|
||||
|
||||
/// Unexpected circuit name
|
||||
#[error("Expected circuit name {} got {}", _0, _1)]
|
||||
CircuitName(String, String),
|
||||
@ -21,6 +29,18 @@ pub enum ValueError {
|
||||
TypeError(String),
|
||||
}
|
||||
|
||||
impl From<ParseIntError> for ValueError {
|
||||
fn from(error: ParseIntError) -> Self {
|
||||
ValueError::ParseIntError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ParseBoolError> for ValueError {
|
||||
fn from(error: ParseBoolError) -> Self {
|
||||
ValueError::ParseBoolError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IntegerError> for ValueError {
|
||||
fn from(error: IntegerError) -> Self {
|
||||
ValueError::IntegerError(error)
|
||||
|
@ -90,7 +90,7 @@ type_list = _{(_type ~ ("," ~ _type)*)?}
|
||||
|
||||
value_number = @{ "0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* }
|
||||
value_implicit = { value_number }
|
||||
value_integer = { value_number ~ type_integer? }
|
||||
value_integer = { value_number ~ type_integer }
|
||||
value_field = { value_number ~ type_field }
|
||||
value_group = { value_number ~ type_group }
|
||||
value_boolean = { "true" | "false" }
|
||||
|
@ -95,13 +95,6 @@ pub enum FieldElement<F: Field + PrimeField> {
|
||||
Allocated(Option<F>, R1CSVariable),
|
||||
}
|
||||
|
||||
// /// A constant or allocated element in the field
|
||||
// #[derive(Clone, PartialEq, Eq)]
|
||||
// pub enum GroupElement<G: Field + PrimeField> {
|
||||
// Constant(G),
|
||||
// Allocated(Option<G>, R1CSVariable),
|
||||
// }
|
||||
|
||||
/// Range or expression enum
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum RangeOrExpression<F: Field + PrimeField, G: Group> {
|
||||
@ -127,6 +120,7 @@ pub enum Expression<F: Field + PrimeField, G: Group> {
|
||||
FieldElement(FieldElement<F>),
|
||||
GroupElement(G),
|
||||
Boolean(Boolean),
|
||||
Implicit(String),
|
||||
|
||||
// Number operations
|
||||
Add(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
||||
|
@ -104,9 +104,10 @@ impl<'ast, F: Field + PrimeField, G: Group> fmt::Display for Expression<F, G> {
|
||||
|
||||
// Values
|
||||
Expression::Integer(ref integer) => write!(f, "{}", integer),
|
||||
Expression::FieldElement(ref fe) => write!(f, "{}", fe),
|
||||
Expression::GroupElement(ref gr) => write!(f, "{}", gr),
|
||||
Expression::FieldElement(ref field) => write!(f, "{}", field),
|
||||
Expression::GroupElement(ref group) => write!(f, "{}", group),
|
||||
Expression::Boolean(ref bool) => write!(f, "{}", bool.get_value().unwrap()),
|
||||
Expression::Implicit(ref value) => write!(f, "{}", value),
|
||||
|
||||
// Number operations
|
||||
Expression::Add(ref left, ref right) => write!(f, "{} + {}", left, right),
|
||||
|
@ -65,17 +65,7 @@ impl<'ast> types::Integer {
|
||||
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Integer<'ast>> for types::Expression<F, G> {
|
||||
fn from(field: ast::Integer<'ast>) -> Self {
|
||||
types::Expression::Integer(match field._type {
|
||||
Some(_type) => types::Integer::from(field.number, _type),
|
||||
// default integer type is u32
|
||||
None => types::Integer::U32(UInt32::constant(
|
||||
field
|
||||
.number
|
||||
.value
|
||||
.parse::<u32>()
|
||||
.expect("unable to parse u32"),
|
||||
)),
|
||||
})
|
||||
types::Expression::Integer(types::Integer::from(field.number, field._type))
|
||||
}
|
||||
}
|
||||
|
||||
@ -152,8 +142,8 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Boolean<'ast>> for types::
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::NumberImplicit<'ast>>
|
||||
for types::Expression<F, G>
|
||||
{
|
||||
fn from(_number: ast::NumberImplicit<'ast>) -> Self {
|
||||
unimplemented!()
|
||||
fn from(number: ast::NumberImplicit<'ast>) -> Self {
|
||||
types::Expression::Implicit(number.number.value)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user