diff --git a/src/program/constraints/boolean.rs b/src/program/constraints/boolean.rs index 3ec10c8edd..ecf9549b7d 100644 --- a/src/program/constraints/boolean.rs +++ b/src/program/constraints/boolean.rs @@ -97,39 +97,6 @@ impl> ResolvedProgram { ResolvedValue::Boolean(Boolean::Constant(bool)) } - // pub(crate) fn bool_from_variable(&mut self, scope: String, variable: Variable) -> Boolean { - // // Evaluate variable name in current function scope - // let variable_name = new_scope_from_variable(scope, &variable); - // - // match self.get(&variable_name) { - // Some(value) => match value { - // ResolvedValue::Boolean(boolean) => boolean.clone(), - // value => unimplemented!( - // "expected boolean for variable {}, got {}", - // variable_name, - // value - // ), - // }, - // None => unimplemented!("cannot resolve variable {} in program", variable_name), - // } - // } - - // fn get_bool_value( - // &mut self, - // cs: &mut CS, - // scope: String, - // expression: BooleanExpression, - // ) -> Boolean { - // match expression { - // BooleanExpression::Variable(variable) => self.bool_from_variable(scope, variable), - // BooleanExpression::Value(value) => Boolean::Constant(value), - // expression => match self.enforce_boolean_expression(cs, scope, expression) { - // ResolvedValue::Boolean(value) => value, - // _ => unimplemented!("boolean expression did not resolve to boolean"), - // }, - // } - // } - pub(crate) fn enforce_not(value: ResolvedValue) -> ResolvedValue { match value { ResolvedValue::Boolean(boolean) => ResolvedValue::Boolean(boolean.not()), @@ -184,86 +151,4 @@ impl> ResolvedProgram { ResolvedValue::Boolean(Boolean::Constant(true)) } - // - // pub(crate) fn enforce_boolean_expression( - // &mut self, - // cs: &mut CS, - // scope: String, - // expression: BooleanExpression, - // ) -> ResolvedValue { - // match expression { - // BooleanExpression::Variable(variable) => { - // ResolvedValue::Boolean(self.bool_from_variable(cs, scope, variable)) - // } - // BooleanExpression::Value(value) => ResolvedValue::Boolean(Boolean::Constant(value)), - // BooleanExpression::Not(expression) => { - // ResolvedValue::Boolean(self.enforce_not(cs, scope, *expression)) - // } - // BooleanExpression::Or(left, right) => { - // ResolvedValue::Boolean(self.enforce_or(cs, scope, *left, *right)) - // } - // BooleanExpression::And(left, right) => { - // ResolvedValue::Boolean(self.enforce_and(cs, scope, *left, *right)) - // } - // BooleanExpression::IntegerEq(left, right) => { - // ResolvedValue::Boolean(self.enforce_integer_equality(cs, scope, *left, *right)) - // } - // BooleanExpression::FieldEq(left, right) => { - // ResolvedValue::Boolean(self.enforce_field_equality(cs, scope, *left, *right)) - // } - // BooleanExpression::BoolEq(left, right) => { - // ResolvedValue::Boolean(self.enforce_bool_equality(cs, scope, *left, *right)) - // } - // BooleanExpression::IfElse(first, second, third) => { - // let resolved_first = - // match self.enforce_boolean_expression(cs, scope.clone(), *first) { - // ResolvedValue::Boolean(resolved) => resolved, - // _ => unimplemented!("if else conditional must resolve to boolean"), - // }; - // if resolved_first.eq(&Boolean::Constant(true)) { - // self.enforce_boolean_expression(cs, scope, *second) - // } else { - // self.enforce_boolean_expression(cs, scope, *third) - // } - // } - // BooleanExpression::Array(array) => { - // let mut result = vec![]; - // array.into_iter().for_each(|element| match *element { - // BooleanSpreadOrExpression::Spread(spread) => match spread { - // BooleanExpression::Variable(variable) => { - // let array_name = new_scope_from_variable(scope.clone(), &variable); - // match self.get(&array_name) { - // Some(value) => match value { - // ResolvedValue::BooleanArray(array) => { - // result.extend(array.clone()) - // } - // value => unimplemented!( - // "spreads only implemented for arrays, got {}", - // value - // ), - // }, - // None => unimplemented!( - // "cannot copy elements from array that does not exist {}", - // variable.name - // ), - // } - // } - // value => { - // unimplemented!("spreads only implemented for arrays, got {}", value) - // } - // }, - // BooleanSpreadOrExpression::Expression(expression) => { - // match self.enforce_boolean_expression(cs, scope.clone(), expression) { - // ResolvedValue::Boolean(value) => result.push(value), - // value => { - // unimplemented!("expected boolean for boolean array, got {}", value) - // } - // } - // } - // }); - // ResolvedValue::BooleanArray(result) - // } - // expression => unimplemented!("boolean expression {}", expression), - // } - // } } diff --git a/src/program/constraints/expression.rs b/src/program/constraints/expression.rs index f2561cff5b..21a9babc6b 100644 --- a/src/program/constraints/expression.rs +++ b/src/program/constraints/expression.rs @@ -211,46 +211,6 @@ impl> ResolvedProgram { } } } - // ResolvedValue::U32Array(field_array) => { - // match index { - // RangeOrExpression::Range(from, to) => { - // let from_resolved = match from { - // Some(from_index) => self.enforce_index(cs, scope.clone(), from_index), - // None => 0usize, // Array slice starts at index 0 - // }; - // let to_resolved = match to { - // Some(to_index) => self.enforce_index(cs, scope.clone(), to_index), - // None => field_array.len(), // Array slice ends at array length - // }; - // ResolvedValue::U32Array(field_array[from_resolved..to_resolved].to_owned()) - // } - // RangeOrExpression::Expression(index) => { - // let index_resolved = self.enforce_index(cs, scope.clone(), index); - // ResolvedValue::U32(field_array[index_resolved].to_owned()) - // } - // } - // } - // ResolvedValue::BooleanArray(bool_array) => { - // match index { - // RangeOrExpression::Range(from, to) => { - // let from_resolved = match from { - // Some(from_index) => self.enforce_index(cs, scope.clone(), from_index), - // None => 0usize, // Array slice starts at index 0 - // }; - // let to_resolved = match to { - // Some(to_index) => self.enforce_index(cs, scope.clone(), to_index), - // None => bool_array.len(), // Array slice ends at array length - // }; - // ResolvedValue::BooleanArray( - // bool_array[from_resolved..to_resolved].to_owned(), - // ) - // } - // RangeOrExpression::Expression(index) => { - // let index_resolved = self.enforce_index(cs, scope.clone(), index); - // ResolvedValue::Boolean(bool_array[index_resolved].to_owned()) - // } - // } - // } value => unimplemented!("Cannot access element of untyped array {}", value), } } @@ -439,15 +399,6 @@ impl> ResolvedProgram { Expression::FunctionCall(function, arguments) => { self.enforce_function_access_expression(cs, scope, function, arguments) } - // Expression::BooleanExp(boolean_expression) => { - // self.enforce_boolean_expression(cs, scope, boolean_expression) - // } - // Expression::IntegerExp(integer_expression) => { - // self.enforce_integer_expression(cs, scope, integer_expression) - // } - // Expression::FieldElementExp(field_expression) => { - // self.enforce_field_expression(cs, scope, field_expression) - // } _ => unimplemented!(), } } diff --git a/src/program/constraints/field_element.rs b/src/program/constraints/field_element.rs index fad93380be..7e6491c620 100644 --- a/src/program/constraints/field_element.rs +++ b/src/program/constraints/field_element.rs @@ -90,32 +90,6 @@ impl> ResolvedProgram { // parameter_variable } - // fn field_element_from_variable(&mut self, scope: String, variable: Variable) -> F { - // // Evaluate variable name in current function scope - // let variable_name = new_scope_from_variable(scope, &variable); - // - // match self.get(&variable_name) { - // Some(value) => match value { - // ResolvedValue::FieldElement(fe) => fe.clone(), - // value => unimplemented!( - // "expected field element for variable {}, got {}", - // variable_name, - // value - // ), - // }, - // None => unimplemented!("cannot resolve variable {} in program", variable_name), - // } - // } - - // fn get_field_value(&mut self, cs: &mut CS, scope: String, expression: FieldExpression) -> F { - // match expression { - // FieldExpression::Variable(variable) => { - // self.field_element_from_variable(scope, variable) - // } - // FieldExpression::Number(element) => element, - // } - // } - pub(crate) fn enforce_field_eq(&mut self, fe1: F, fe2: F) -> ResolvedValue { ResolvedValue::Boolean(Boolean::Constant(fe1.eq(&fe2))) } @@ -141,137 +115,4 @@ impl> ResolvedProgram { // ResolvedValue::FieldElement(fe1.pow(&fe2)) } - - // fn enforce_field_add_old( - // &mut self, - // cs: &mut CS, - // scope: String, - // left: FieldExpression, - // right: FieldExpression, - // ) -> ResolvedValue { - // let left = self.get_field_value(cs, scope.clone(), left); - // let right = self.get_field_value(cs, scope.clone(), right); - // - // ResolvedValue::FieldElement(left.add(&right)) - // } - // - // fn enforce_field_sub_old( - // &mut self, - // cs: &mut CS, - // scope: String, - // left: FieldExpression, - // right: FieldExpression, - // ) -> ResolvedValue { - // let left = self.get_field_value(cs, scope.clone(), left); - // let right = self.get_field_value(cs, scope.clone(), right); - // - // ResolvedValue::FieldElement(left.sub(&right)) - // } - // - // fn enforce_field_mul_old( - // &mut self, - // cs: &mut CS, - // scope: String, - // left: FieldExpression, - // right: FieldExpression, - // ) -> ResolvedValue { - // let left = self.get_field_value(cs, scope.clone(), left); - // let right = self.get_field_value(cs, scope.clone(), right); - // - // ResolvedValue::FieldElement(left.mul(&right)) - // } - // - // fn enforce_field_div_old( - // &mut self, - // cs: &mut CS, - // scope: String, - // left: FieldExpression, - // right: FieldExpression, - // ) -> ResolvedValue { - // let left = self.get_field_value(cs, scope.clone(), left); - // let right = self.get_field_value(cs, scope.clone(), right); - // - // ResolvedValue::FieldElement(left.div(&right)) - // } - // - // fn enforce_field_pow_old( - // &mut self, - // _cs: &mut CS, - // _scope: String, - // _left: FieldExpression, - // _right: FieldExpression, - // ) -> ResolvedValue { - // unimplemented!("field element exponentiation not supported") - // // let left = self.get_field_value(cs, scope.clone(), left); - // // let right = self.get_field_value(cs, scope.clone(), right); - // // - // // ResolvedValue::FieldElement(left.pow(&right)) - // } - - // pub(crate) fn enforce_field_expression( - // &mut self, - // cs: &mut CS, - // scope: String, - // expression: FieldExpression, - // ) -> ResolvedValue { - // match expression { - // FieldExpression::Variable(variable) => { - // ResolvedValue::FieldElement(self.field_element_from_variable(scope, variable)) - // } - // FieldExpression::Number(field) => ResolvedValue::FieldElement(field), - // FieldExpression::Add(left, right) => self.enforce_field_add_old(cs, scope, *left, *right), - // FieldExpression::Sub(left, right) => self.enforce_field_sub_old(cs, scope, *left, *right), - // FieldExpression::Mul(left, right) => self.enforce_field_mul_old(cs, scope, *left, *right), - // FieldExpression::Div(left, right) => self.enforce_field_div_old(cs, scope, *left, *right), - // FieldExpression::Pow(left, right) => self.enforce_field_pow_old(cs, scope, *left, *right), - // FieldExpression::IfElse(first, second, third) => { - // let resolved_first = - // match self.enforce_boolean_expression(cs, scope.clone(), *first) { - // ResolvedValue::Boolean(resolved) => resolved, - // _ => unimplemented!("if else conditional must resolve to boolean"), - // }; - // - // if resolved_first.eq(&Boolean::Constant(true)) { - // self.enforce_field_expression(cs, scope, *second) - // } else { - // self.enforce_field_expression(cs, scope, *third) - // } - // } - // FieldExpression::Array(array) => { - // let mut result = vec![]; - // array.into_iter().for_each(|element| match *element { - // FieldSpreadOrExpression::Spread(spread) => match spread { - // FieldExpression::Variable(variable) => { - // let array_name = new_scope_from_variable(scope.clone(), &variable); - // match self.get(&array_name) { - // Some(value) => match value { - // ResolvedValue::FieldElementArray(array) => { - // result.extend(array.clone()) - // } - // value => unimplemented!( - // "spreads only implemented for arrays, got {}", - // value - // ), - // }, - // None => unimplemented!( - // "cannot copy elements from array that does not exist {}", - // variable.name - // ), - // } - // } - // value => { - // unimplemented!("spreads only implemented for arrays, got {}", value) - // } - // }, - // FieldSpreadOrExpression::Expression(expression) => { - // match self.enforce_field_expression(cs, scope.clone(), expression) { - // ResolvedValue::FieldElement(value) => result.push(value), - // _ => unimplemented!("cannot resolve field"), - // } - // } - // }); - // ResolvedValue::FieldElementArray(result) - // } - // } - // } } diff --git a/src/program/constraints/integer.rs b/src/program/constraints/integer.rs index 8b88748361..3219311a87 100644 --- a/src/program/constraints/integer.rs +++ b/src/program/constraints/integer.rs @@ -93,34 +93,11 @@ impl> ResolvedProgram { // parameter_variable } - // pub(crate) fn integer_from_variable( - // &mut self, - // scope: String, - // variable: Variable, - // ) -> ResolvedValue { - // // Evaluate variable name in current function scope - // let variable_name = new_scope_from_variable(scope, &variable); - // - // match self.get(&variable_name) { - // Some(value) => value.clone(), - // None => unimplemented!("cannot resolve variable {} in program", variable_name), - // } - // } - pub(crate) fn get_integer_constant(integer: Integer) -> ResolvedValue { match integer { Integer::U32(u32_value) => ResolvedValue::U32(UInt32::constant(u32_value)), } } - // - // pub(crate) fn get_integer_value( - // integer: Integer - // ) -> ResolvedValue { - // match expression { - // IntegerExpression::Variable(variable) => self.integer_from_variable(scope, variable), - // IntegerExpression::Number(number) => Self::get_integer_constant(number), - // } - // } pub(crate) fn enforce_u32_eq(cs: &mut CS, left: UInt32, right: UInt32) -> ResolvedValue { left.conditional_enforce_equal( @@ -186,243 +163,4 @@ impl> ResolvedProgram { .unwrap(), ) } - - // pub(crate) fn enforce_integer_equality( - // &mut self, - // cs: &mut CS, - // scope: String, - // left: UInt32, - // right: UInt32, - // ) -> Boolean { - // let left = self.get_integer_value(cs, scope.clone(), left); - // let right = self.get_integer_value(cs, scope.clone(), right); - // - // match (left, right) { - // (ResolvedValue::U32(left_u32), ResolvedValue::U32(right_u32)) => { - // Self::enforce_u32_equality(cs, left_u32, right_u32) - // } - // (left_int, right_int) => { - // unimplemented!("equality not impl between {} == {}", left_int, right_int) - // } - // } - // } - - // fn enforce_integer_add_old( - // &mut self, - // cs: &mut CS, - // scope: String, - // left: IntegerExpression, - // right: IntegerExpression, - // ) -> ResolvedValue { - // let left = self.get_integer_value(cs, scope.clone(), left); - // let right = self.get_integer_value(cs, scope.clone(), right); - // - // match (left, right) { - // (ResolvedValue::U32(left_u32), ResolvedValue::U32(right_u32)) => { - // Self::enforce_u32_add(cs, left_u32, right_u32) - // } - // (left_int, right_int) => { - // unimplemented!("add not impl between {} + {}", left_int, right_int) - // } - // } - // } - - // fn enforce_u32_sub_old(cs: &mut CS, left: UInt32, right: UInt32) -> ResolvedValue { - // ResolvedValue::U32( - // left.sub( - // cs.ns(|| format!("enforce {} - {}", left.value.unwrap(), right.value.unwrap())), - // &right, - // ) - // .unwrap(), - // ) - // } - - // fn enforce_integer_sub( - // &mut self, - // cs: &mut CS, - // scope: String, - // left: IntegerExpression, - // right: IntegerExpression, - // ) -> ResolvedValue { - // let left = self.get_integer_value(cs, scope.clone(), left); - // let right = self.get_integer_value(cs, scope.clone(), right); - // - // match (left, right) { - // (ResolvedValue::U32(left_u32), ResolvedValue::U32(right_u32)) => { - // Self::enforce_u32_sub_old(cs, left_u32, right_u32) - // } - // (left_int, right_int) => { - // unimplemented!("add not impl between {} + {}", left_int, right_int) - // } - // } - // } - - // fn enforce_u32_mul_old(cs: &mut CS, left: UInt32, right: UInt32) -> ResolvedValue { - // ResolvedValue::U32( - // left.mul( - // cs.ns(|| format!("enforce {} * {}", left.value.unwrap(), right.value.unwrap())), - // &right, - // ) - // .unwrap(), - // ) - // } - // - // fn enforce_integer_mul( - // &mut self, - // cs: &mut CS, - // scope: String, - // left: IntegerExpression, - // right: IntegerExpression, - // ) -> ResolvedValue { - // let left = self.get_integer_value(cs, scope.clone(), left); - // let right = self.get_integer_value(cs, scope.clone(), right); - // - // match (left, right) { - // (ResolvedValue::U32(left_u32), ResolvedValue::U32(right_u32)) => { - // Self::enforce_u32_mul_old(cs, left_u32, right_u32) - // } - // (left_int, right_int) => { - // unimplemented!("add not impl between {} + {}", left_int, right_int) - // } - // } - // } - // - // fn enforce_u32_div_old(cs: &mut CS, left: UInt32, right: UInt32) -> ResolvedValue { - // ResolvedValue::U32( - // left.div( - // cs.ns(|| format!("enforce {} / {}", left.value.unwrap(), right.value.unwrap())), - // &right, - // ) - // .unwrap(), - // ) - // } - // - // fn enforce_integer_div( - // &mut self, - // cs: &mut CS, - // scope: String, - // left: IntegerExpression, - // right: IntegerExpression, - // ) -> ResolvedValue { - // let left = self.get_integer_value(cs, scope.clone(), left); - // let right = self.get_integer_value(cs, scope.clone(), right); - // - // match (left, right) { - // (ResolvedValue::U32(left_u32), ResolvedValue::U32(right_u32)) => { - // Self::enforce_u32_div_old(cs, left_u32, right_u32) - // } - // (left_int, right_int) => { - // unimplemented!("add not impl between {} + {}", left_int, right_int) - // } - // } - // } - // - // fn enforce_u32_pow_old(cs: &mut CS, left: UInt32, right: UInt32) -> ResolvedValue { - // ResolvedValue::U32( - // left.pow( - // cs.ns(|| { - // format!( - // "enforce {} ** {}", - // left.value.unwrap(), - // right.value.unwrap() - // ) - // }), - // &right, - // ) - // .unwrap(), - // ) - // } - // - // fn enforce_integer_pow( - // &mut self, - // cs: &mut CS, - // scope: String, - // left: IntegerExpression, - // right: IntegerExpression, - // ) -> ResolvedValue { - // let left = self.get_integer_value(cs, scope.clone(), left); - // let right = self.get_integer_value(cs, scope.clone(), right); - // - // match (left, right) { - // (ResolvedValue::U32(left_u32), ResolvedValue::U32(right_u32)) => { - // Self::enforce_u32_pow_old(cs, left_u32, right_u32) - // } - // (left_int, right_int) => { - // unimplemented!("add not impl between {} + {}", left_int, right_int) - // } - // } - // } - - // pub(crate) fn enforce_integer_expression( - // &mut self, - // cs: &mut CS, - // scope: String, - // expression: IntegerExpression, - // ) -> ResolvedValue { - // match expression { - // IntegerExpression::Variable(variable) => self.integer_from_variable(scope, variable), - // IntegerExpression::Number(number) => Self::get_integer_constant(number), - // IntegerExpression::Add(left, right) => { - // self.enforce_integer_add_old(cs, scope, *left, *right) - // } - // IntegerExpression::Sub(left, right) => { - // self.enforce_integer_sub(cs, scope, *left, *right) - // } - // IntegerExpression::Mul(left, right) => { - // self.enforce_integer_mul(cs, scope, *left, *right) - // } - // IntegerExpression::Div(left, right) => { - // self.enforce_integer_div(cs, scope, *left, *right) - // } - // IntegerExpression::Pow(left, right) => { - // self.enforce_integer_pow(cs, scope, *left, *right) - // } - // IntegerExpression::IfElse(first, second, third) => { - // let resolved_first = - // match self.enforce_boolean_expression(cs, scope.clone(), *first) { - // ResolvedValue::Boolean(resolved) => resolved, - // _ => unimplemented!("if else conditional must resolve to boolean"), - // }; - // - // if resolved_first.eq(&Boolean::Constant(true)) { - // self.enforce_integer_expression(cs, scope, *second) - // } else { - // self.enforce_integer_expression(cs, scope, *third) - // } - // } - // IntegerExpression::Array(array) => { - // let mut result = vec![]; - // array.into_iter().for_each(|element| match *element { - // IntegerSpreadOrExpression::Spread(spread) => match spread { - // IntegerExpression::Variable(variable) => { - // let array_name = new_scope_from_variable(scope.clone(), &variable); - // match self.get(&array_name) { - // Some(value) => match value { - // ResolvedValue::U32Array(array) => result.extend(array.clone()), - // value => unimplemented!( - // "spreads only implemented for arrays, got {}", - // value - // ), - // }, - // None => unimplemented!( - // "cannot copy elements from array that does not exist {}", - // variable.name - // ), - // } - // } - // value => { - // unimplemented!("spreads only implemented for arrays, got {}", value) - // } - // }, - // IntegerSpreadOrExpression::Expression(expression) => { - // match self.enforce_integer_expression(cs, scope.clone(), expression) { - // ResolvedValue::U32(value) => result.push(value), - // _ => unimplemented!("cannot resolve field"), - // } - // } - // }); - // ResolvedValue::U32Array(result) - // } - // } - // } } diff --git a/src/program/constraints/resolved_value.rs b/src/program/constraints/resolved_value.rs index 1a717764b8..a0b9397374 100644 --- a/src/program/constraints/resolved_value.rs +++ b/src/program/constraints/resolved_value.rs @@ -29,15 +29,6 @@ impl ResolvedValue { (ResolvedValue::FieldElement(ref _a), Type::FieldElement) => true, (ResolvedValue::Boolean(ref _a), Type::Boolean) => true, (ResolvedValue::Array(ref _arr), Type::Array(ref _ty, ref _len)) => true, // todo: add array types - // (ResolvedValue::U32Array(ref arr), Type::Array(ref arr_type, ref len)) => { - // (arr.len() == *len) & (**arr_type == Type::U32) - // } - // (ResolvedValue::FieldElementArray(ref arr), Type::Array(ref arr_type, ref len)) => { - // (arr.len() == *len) & (**arr_type == Type::FieldElement) - // } - // (ResolvedValue::BooleanArray(ref arr), Type::Array(ref arr_type, ref len)) => { - // (arr.len() == *len) & (**arr_type == Type::Boolean) - // } ( ResolvedValue::StructExpression(ref actual_name, ref _members), Type::Struct(ref expected_name), @@ -70,36 +61,6 @@ impl fmt::Display for ResolvedValue { } write!(f, "]") } - // ResolvedValue::U32Array(ref array) => { - // write!(f, "[")?; - // for (i, e) in array.iter().enumerate() { - // write!(f, "{}", e.value.unwrap())?; - // if i < array.len() - 1 { - // write!(f, ", ")?; - // } - // } - // write!(f, "]") - // } - // ResolvedValue::FieldElementArray(ref array) => { - // write!(f, "[")?; - // for (i, e) in array.iter().enumerate() { - // write!(f, "{}", e)?; - // if i < array.len() - 1 { - // write!(f, ", ")?; - // } - // } - // write!(f, "]") - // } - // ResolvedValue::BooleanArray(ref array) => { - // write!(f, "[")?; - // for (i, e) in array.iter().enumerate() { - // write!(f, "{}", e.get_value().unwrap())?; - // if i < array.len() - 1 { - // write!(f, ", ")?; - // } - // } - // write!(f, "]") - // } ResolvedValue::StructExpression(ref variable, ref members) => { write!(f, "{} {{", variable)?; for (i, member) in members.iter().enumerate() { diff --git a/src/program/types.rs b/src/program/types.rs index 60a7172205..d8fda43738 100644 --- a/src/program/types.rs +++ b/src/program/types.rs @@ -30,7 +30,8 @@ impl Integer { pub fn to_usize(&self) -> usize { match *self { // U8(u8) - Integer::U32(num) => num as usize, // U64(u64) + Integer::U32(num) => num as usize, + // U64(u64) } } } @@ -90,9 +91,6 @@ pub enum Expression { // Functions FunctionCall(Box>, Vec>), - // IntegerExp(IntegerExpression), - // FieldElementExp(FieldExpression), - // BooleanExp(BooleanExpression), } /// Definition assignee: v, arr[0..2], Point p.x @@ -185,95 +183,3 @@ impl<'ast, F: Field + PrimeField> Program<'ast, F> { self } } - -// /// Spread operator or u32 expression enum -// #[derive(Debug, Clone)] -// pub enum IntegerSpreadOrExpression { -// Spread(IntegerExpression), -// Expression(IntegerExpression), -// } - -// Expression that evaluates to a u32 value -// #[derive(Debug, Clone)] -// pub enum IntegerExpression { -// Variable(Variable), -// Number(Integer), -// Operators -// Add(Box>, Box>), -// Sub(Box>, Box>), -// Mul(Box>, Box>), -// Div(Box>, Box>), -// Pow(Box>, Box>), -// Conditionals -// IfElse( -// Box>, -// Box>, -// Box>, -// ), -// Arrays -// Array(Vec>>), -// // Unresolved -// Unresolved(Box>) // placeholder for array/struct access, function calls -// } - -// /// Spread or field expression enum -// #[derive(Debug, Clone)] -// pub enum FieldSpreadOrExpression { -// Spread(FieldExpression), -// Expression(FieldExpression), -// } - -// /// Expression that evaluates to a field value -// #[derive(Debug, Clone)] -// pub enum FieldExpression { -// Variable(Variable), -// Number(F), -// Operators -// Add(Box>, Box>), -// Sub(Box>, Box>), -// Mul(Box>, Box>), -// Div(Box>, Box>), -// Pow(Box>, Box>), -// Conditionals -// IfElse( -// Box>, -// Box>, -// Box>, -// ), -// Arrays -// Array(Vec>>), -// } - -// /// Spread or field expression enum -// #[derive(Debug, Clone)] -// pub enum BooleanSpreadOrExpression { -// Spread(BooleanExpression), -// Expression(BooleanExpression), -// } - -// Expression that evaluates to a boolean value -// #[derive(Debug, Clone)] -// pub enum BooleanExpression { -// Variable(Variable), -// Value(bool), -// Boolean operators -// Or(Box>, Box>), -// And(Box>, Box>), -// BoolEq(Box>, Box>), -// // Integer operators -// IntegerEq(Box>, Box>), -// Geq(Box>, Box>), -// Gt(Box>, Box>), -// Leq(Box>, Box>), -// Lt(Box>, Box>), -// // Field operators -// FieldEq(Box>, Box>), -// Conditionals -// IfElse( -// Box>, -// Box>, -// Box>, -// ), -// Arrays -// Array(Vec>>), -// } diff --git a/src/program/types_display.rs b/src/program/types_display.rs index 65464dffd4..baf974a6a2 100644 --- a/src/program/types_display.rs +++ b/src/program/types_display.rs @@ -282,117 +282,3 @@ impl fmt::Debug for Function { ) } } - -// impl fmt::Display for IntegerSpreadOrExpression { -// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { -// match *self { -// IntegerSpreadOrExpression::Spread(ref spread) => write!(f, "...{}", spread), -// IntegerSpreadOrExpression::Expression(ref expression) => write!(f, "{}", expression), -// } -// } -// } - -// impl<'ast, F: Field + PrimeField> fmt::Display for IntegerExpression { -// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { -// match *self { -// IntegerExpression::Variable(ref variable) => write!(f, "{}", variable), -// IntegerExpression::Number(ref number) => write!(f, "{}", number), -// IntegerExpression::Add(ref lhs, ref rhs) => write!(f, "{} + {}", lhs, rhs), -// IntegerExpression::Sub(ref lhs, ref rhs) => write!(f, "{} - {}", lhs, rhs), -// IntegerExpression::Mul(ref lhs, ref rhs) => write!(f, "{} * {}", lhs, rhs), -// IntegerExpression::Div(ref lhs, ref rhs) => write!(f, "{} / {}", lhs, rhs), -// IntegerExpression::Pow(ref lhs, ref rhs) => write!(f, "{} ** {}", lhs, rhs), -// IntegerExpression::IfElse(ref a, ref b, ref c) => { -// write!(f, "if {} then {} else {} fi", a, b, c) -// } -// IntegerExpression::Array(ref array) => { -// write!(f, "[")?; -// for (i, e) in array.iter().enumerate() { -// write!(f, "{}", e)?; -// if i < array.len() - 1 { -// write!(f, ", ")?; -// } -// } -// write!(f, "]") -// } -// } -// } -// } - -// impl fmt::Display for FieldSpreadOrExpression { -// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { -// match *self { -// FieldSpreadOrExpression::Spread(ref spread) => write!(f, "...{}", spread), -// FieldSpreadOrExpression::Expression(ref expression) => write!(f, "{}", expression), -// } -// } -// } -// -// impl<'ast, F: Field + PrimeField> fmt::Display for FieldExpression { -// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { -// match *self { -// FieldExpression::Variable(ref variable) => write!(f, "{}", variable), -// FieldExpression::Number(ref number) => write!(f, "{}", number), -// FieldExpression::Add(ref lhs, ref rhs) => write!(f, "{} + {}", lhs, rhs), -// FieldExpression::Sub(ref lhs, ref rhs) => write!(f, "{} - {}", lhs, rhs), -// FieldExpression::Mul(ref lhs, ref rhs) => write!(f, "{} * {}", lhs, rhs), -// FieldExpression::Div(ref lhs, ref rhs) => write!(f, "{} / {}", lhs, rhs), -// FieldExpression::Pow(ref lhs, ref rhs) => write!(f, "{} ** {}", lhs, rhs), -// FieldExpression::IfElse(ref a, ref b, ref c) => { -// write!(f, "if {} then {} else {} fi", a, b, c) -// } -// FieldExpression::Array(ref array) => { -// write!(f, "[")?; -// for (i, e) in array.iter().enumerate() { -// write!(f, "{}", e)?; -// if i < array.len() - 1 { -// write!(f, ", ")?; -// } -// } -// write!(f, "]") -// } // _ => unimplemented!("not all field expressions can be displayed") -// } -// } -// } - -// impl fmt::Display for BooleanSpreadOrExpression { -// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { -// match *self { -// BooleanSpreadOrExpression::Spread(ref spread) => write!(f, "...{}", spread), -// BooleanSpreadOrExpression::Expression(ref expression) => write!(f, "{}", expression), -// } -// } -// } - -// impl<'ast, F: Field + PrimeField> fmt::Display for BooleanExpression { -// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { -// match *self { -// BooleanExpression::Variable(ref variable) => write!(f, "{}", variable), -// BooleanExpression::Value(ref value) => write!(f, "{}", value), -// BooleanExpression::Not(ref expression) => write!(f, "!{}", expression), -// BooleanExpression::Or(ref lhs, ref rhs) => write!(f, "{} || {}", lhs, rhs), -// BooleanExpression::And(ref lhs, ref rhs) => write!(f, "{} && {}", lhs, rhs), -// BooleanExpression::BoolEq(ref lhs, ref rhs) => write!(f, "{} == {}", lhs, rhs), -// BooleanExpression::IntegerEq(ref lhs, ref rhs) => write!(f, "{} == {}", lhs, rhs), -// BooleanExpression::FieldEq(ref lhs, ref rhs) => write!(f, "{} == {}", lhs, rhs), -// // BooleanExpression::Neq(ref lhs, ref rhs) => write!(f, "{} != {}", lhs, rhs), -// BooleanExpression::Geq(ref lhs, ref rhs) => write!(f, "{} >= {}", lhs, rhs), -// BooleanExpression::Gt(ref lhs, ref rhs) => write!(f, "{} > {}", lhs, rhs), -// BooleanExpression::Leq(ref lhs, ref rhs) => write!(f, "{} <= {}", lhs, rhs), -// BooleanExpression::Lt(ref lhs, ref rhs) => write!(f, "{} < {}", lhs, rhs), -// BooleanExpression::IfElse(ref a, ref b, ref c) => { -// write!(f, "if {} then {} else {} fi", a, b, c) -// } -// BooleanExpression::Array(ref array) => { -// write!(f, "[")?; -// for (i, e) in array.iter().enumerate() { -// write!(f, "{}", e)?; -// if i < array.len() - 1 { -// write!(f, ", ")?; -// } -// } -// write!(f, "]") -// } -// } -// } -// } diff --git a/src/program/types_from.rs b/src/program/types_from.rs index c4c88f505b..7f7bb97fe5 100644 --- a/src/program/types_from.rs +++ b/src/program/types_from.rs @@ -42,33 +42,6 @@ impl<'ast, F: Field + PrimeField> From> for types::Expression } } -// impl<'ast, F: Field + PrimeField> From> for types::Expression { -// fn from(expression: ast::Expression<'ast>) -> Self { -// match types::Expression::from(expression) { -// types::Expression::Variable(variable) => types::IntegerExpression::Variable(variable), -// types::Expression::IntegerExp(integer_expression) => integer_expression, -// expression => unimplemented!("expected integer in integer expression, got {}", expression), -// } -// } -// } - -// impl<'ast, F: Field + PrimeField> From> -// for types::IntegerSpreadOrExpression -// { -// fn from(s_or_e: ast::SpreadOrExpression<'ast>) -> Self { -// match s_or_e { -// ast::SpreadOrExpression::Spread(spread) => types::IntegerSpreadOrExpression::Spread( -// types::IntegerExpression::from(spread.expression), -// ), -// ast::SpreadOrExpression::Expression(expression) => { -// types::IntegerSpreadOrExpression::Expression(types::IntegerExpression::from( -// expression, -// )) -// } -// } -// } -// } - impl<'ast, F: Field + PrimeField> From> for types::RangeOrExpression { @@ -99,12 +72,6 @@ impl<'ast, F: Field + PrimeField> From> } } -// -// impl<'ast, F: Field + PrimeField> From> for types::FieldExpression { -// fn from(variable: ast::Variable<'ast>) -> Self { -// types::FieldExpression::Variable(types::Variable::from(variable)) -// } -// } /// pest ast -> types::FieldExpression impl<'ast, F: Field + PrimeField> From> for types::Expression { @@ -113,37 +80,6 @@ impl<'ast, F: Field + PrimeField> From> for types::Expression From> for types::FieldExpression { -// fn from(expression: ast::Expression<'ast>) -> Self { -// match types::Expression::from(expression) { -// types::Expression::FieldElementExp(field_expression) => field_expression, -// types::Expression::Variable(variable) => types::FieldExpression::Variable(variable), -// expression => unimplemented!("expected field in field expression, got {}", expression), -// } -// } -// } - -// impl<'ast, F: Field + PrimeField> From> -// for types::FieldSpreadOrExpression -// { -// fn from(s_or_e: ast::SpreadOrExpression<'ast>) -> Self { -// match s_or_e { -// ast::SpreadOrExpression::Spread(spread) => types::FieldSpreadOrExpression::Spread( -// types::FieldExpression::from(spread.expression), -// ), -// ast::SpreadOrExpression::Expression(expression) => { -// types::FieldSpreadOrExpression::Expression(types::FieldExpression::from(expression)) -// } -// } -// } -// } - -// impl<'ast, F: Field + PrimeField> From> for types::BooleanExpression { -// fn from(variable: ast::Variable<'ast>) -> Self { -// types::BooleanExpression::Variable(types::Variable::from(variable)) -// } -// } - /// pest ast -> types::Boolean impl<'ast, F: Field + PrimeField> From> for types::Expression { @@ -157,33 +93,6 @@ impl<'ast, F: Field + PrimeField> From> for types::Expression } } -// impl<'ast, F: Field + PrimeField> From> for types::BooleanExpression { -// fn from(expression: ast::Expression<'ast>) -> Self { -// match types::Expression::from(expression) { -// types::Expression::BooleanExp(boolean_expression) => boolean_expression, -// types::Expression::Variable(variable) => types::BooleanExpression::Variable(variable), -// expression => unimplemented!("expected boolean in boolean expression, got {}", expression), -// } -// } -// } - -// impl<'ast, F: Field + PrimeField> From> -// for types::BooleanSpreadOrExpression -// { -// fn from(s_or_e: ast::SpreadOrExpression<'ast>) -> Self { -// match s_or_e { -// ast::SpreadOrExpression::Spread(spread) => types::BooleanSpreadOrExpression::Spread( -// types::BooleanExpression::from(spread.expression), -// ), -// ast::SpreadOrExpression::Expression(expression) => { -// types::BooleanSpreadOrExpression::Expression(types::BooleanExpression::from( -// expression, -// )) -// } -// } -// } -// } - /// pest ast -> types::Expression impl<'ast, F: Field + PrimeField> From> for types::Expression { @@ -196,107 +105,12 @@ impl<'ast, F: Field + PrimeField> From> for types::Expression From> for types::Expression { -// fn from(variable: ast::Variable<'ast>) -> Self { -// types::Expression::Variable(types::Variable::from(variable)) -// } -// } - impl<'ast, F: Field + PrimeField> From> for types::Expression { fn from(expression: ast::NotExpression<'ast>) -> Self { types::Expression::Not(Box::new(types::Expression::from(*expression.expression))) } } -// impl<'ast, F: Field + PrimeField> types::BooleanExpression { -// /// Find out which types we are comparing and output the corresponding expression. -// fn from_eq(expression: ast::BinaryExpression<'ast>) -> Self { -// let left = types::Expression::from(*expression.left); -// let right = types::Expression::from(*expression.right); -// -// // When matching a variable, look at the opposite side to see what we are comparing to and assume that variable type -// match (left, right) { -// // Boolean equality -// (types::Expression::BooleanExp(lhs), types::Expression::BooleanExp(rhs)) => { -// types::BooleanExpression::BoolEq(Box::new(lhs), Box::new(rhs)) -// } -// (types::Expression::BooleanExp(lhs), types::Expression::Variable(rhs)) => { -// types::BooleanExpression::BoolEq( -// Box::new(lhs), -// Box::new(types::BooleanExpression::Variable(rhs)), -// ) -// } -// (types::Expression::Variable(lhs), types::Expression::BooleanExp(rhs)) => { -// types::BooleanExpression::BoolEq( -// Box::new(types::BooleanExpression::Variable(lhs)), -// Box::new(rhs), -// ) -// } //TODO: check case for two variables? -// // Integer equality -// (types::Expression::IntegerExp(lhs), types::Expression::IntegerExp(rhs)) => { -// types::BooleanExpression::IntegerEq(Box::new(lhs), Box::new(rhs)) -// } -// (types::Expression::IntegerExp(lhs), types::Expression::Variable(rhs)) => { -// types::BooleanExpression::IntegerEq( -// Box::new(lhs), -// Box::new(types::IntegerExpression::Variable(rhs)), -// ) -// } -// (types::Expression::Variable(lhs), types::Expression::IntegerExp(rhs)) => { -// types::BooleanExpression::IntegerEq( -// Box::new(types::IntegerExpression::Variable(lhs)), -// Box::new(rhs), -// ) -// } -// // Field equality -// (types::Expression::FieldElementExp(lhs), types::Expression::FieldElementExp(rhs)) => { -// types::BooleanExpression::FieldEq(Box::new(lhs), Box::new(rhs)) -// } -// (types::Expression::FieldElementExp(lhs), types::Expression::Variable(rhs)) => { -// types::BooleanExpression::FieldEq( -// Box::new(lhs), -// Box::new(types::FieldExpression::Variable(rhs)), -// ) -// } -// (types::Expression::Variable(lhs), types::Expression::FieldElementExp(rhs)) => { -// types::BooleanExpression::FieldEq( -// Box::new(types::FieldExpression::Variable(lhs)), -// Box::new(rhs), -// ) -// } -// -// (lhs, rhs) => unimplemented!("pattern {} == {} unimplemented", lhs, rhs), -// } -// } -// -// fn from_neq(expression: ast::BinaryExpression<'ast>) -> Self { -// types::BooleanExpression::Not(Box::new(Self::from_eq(expression))) -// } -// } -// impl<'ast, F: Field + PrimeField> types::Type { -// fn resolve_type(left: &Box>, right: &Box>) -> Self { -// let left = types::Expression::::from(*left.clone()); -// let right = types::Expression::::from(*right.clone()); -// -// match (left, right) { -// // Integer operation -// (types::Expression::IntegerExp(_), _) | (_, types::Expression::IntegerExp(_)) => { -// types::Type::U32 -// } -// // Field operation -// (types::Expression::FieldElementExp(_), _) | (_, types::Expression::FieldElementExp(_)) => { -// types::Type::FieldElement -// } -// // Unmatched: two array accesses, two variables -// (lhs, rhs) => unimplemented!( -// "operand types {} and {} must match for binary expression", -// lhs, -// rhs -// ), -// } -// } -// } - impl<'ast, F: Field + PrimeField> From> for types::SpreadOrExpression { @@ -374,92 +188,11 @@ impl<'ast, F: Field + PrimeField> From> for types::E impl<'ast, F: Field + PrimeField> From> for types::Expression { fn from(expression: ast::TernaryExpression<'ast>) -> Self { - // Evaluate expressions to find out result type - // let first = ; - // let second = ; - // let third = ; - types::Expression::IfElse( Box::new(types::Expression::from(*expression.first)), Box::new(types::Expression::from(*expression.second)), Box::new(types::Expression::from(*expression.third)), ) - - // match (second, third) { - // // Boolean Result - // (types::Expression::BooleanExp(second), types::Expression::BooleanExp(third)) => { - // types::Expression::BooleanExp(types::Expression::IfElse( - // Box::new(first), - // Box::new(second), - // Box::new(third), - // )) - // } - // (types::Expression::BooleanExp(second), types::Expression::Variable(third)) => { - // types::Expression::BooleanExp(types::BooleanExpression::IfElse( - // Box::new(first), - // Box::new(second), - // Box::new(types::BooleanExpression::Variable(third)), - // )) - // } - // (types::Expression::Variable(second), types::Expression::BooleanExp(third)) => { - // types::Expression::BooleanExp(types::BooleanExpression::IfElse( - // Box::new(first), - // Box::new(types::BooleanExpression::Variable(second)), - // Box::new(third), - // )) - // } - // // Integer Result - // (types::Expression::IntegerExp(second), types::Expression::IntegerExp(third)) => { - // types::Expression::IntegerExp(types::IntegerExpression::IfElse( - // Box::new(first), - // Box::new(second), - // Box::new(third), - // )) - // } - // (types::Expression::IntegerExp(second), types::Expression::Variable(third)) => { - // types::Expression::IntegerExp(types::IntegerExpression::IfElse( - // Box::new(first), - // Box::new(second), - // Box::new(types::IntegerExpression::Variable(third)), - // )) - // } - // (types::Expression::Variable(second), types::Expression::IntegerExp(third)) => { - // types::Expression::IntegerExp(types::IntegerExpression::IfElse( - // Box::new(first), - // Box::new(types::IntegerExpression::Variable(second)), - // Box::new(third), - // )) - // } - // // Field Result - // (types::Expression::FieldElementExp(second), types::Expression::FieldElementExp(third)) => { - // types::Expression::FieldElementExp(types::FieldExpression::IfElse( - // Box::new(first), - // Box::new(second), - // Box::new(third), - // )) - // } - // (types::Expression::FieldElementExp(second), types::Expression::Variable(third)) => { - // types::Expression::FieldElementExp(types::FieldExpression::IfElse( - // Box::new(first), - // Box::new(second), - // Box::new(types::FieldExpression::Variable(third)), - // )) - // } - // (types::Expression::Variable(second), types::Expression::FieldElementExp(third)) => { - // types::Expression::FieldElementExp(types::FieldExpression::IfElse( - // Box::new(first), - // Box::new(types::FieldExpression::Variable(second)), - // Box::new(third), - // )) - // } - // - // (second, third) => unimplemented!( - // "pattern if {} then {} else {} unimplemented", - // first, - // second, - // third - // ), - // } } } @@ -545,10 +278,6 @@ impl<'ast, F: Field + PrimeField> From> for types::Express /// For defined types (ex: u32[4]) we manually construct the expression instead of implementing the From trait. /// This saves us from having to resolve things at a later point in time. impl<'ast, F: Field + PrimeField> types::Expression { - // fn from_basic(_ty: ast::BasicType<'ast>, expression: ast::Expression<'ast>) -> Self { - // types::Expression::from(expression) - // } - fn get_count(count: ast::Value<'ast>) -> usize { match count { ast::Value::U32(f) => f @@ -560,65 +289,6 @@ impl<'ast, F: Field + PrimeField> types::Expression { } } - // fn from_array(ty: ast::ArrayType<'ast>, expression: ast::Expression<'ast>) -> Self { - // match ty.ty { - // ast::BasicType::U32(_ty) => { - // let elements: Vec>> = match expression { - // ast::Expression::ArrayInline(array) => array - // .expressions - // .into_iter() - // .map(|s_or_e| Box::new(types::IntegerSpreadOrExpression::from(s_or_e))) - // .collect(), - // ast::Expression::ArrayInitializer(array) => { - // let count = types::Expression::::get_count(array.count); - // let expression = - // Box::new(types::IntegerSpreadOrExpression::from(*array.expression)); - // - // vec![expression; count] - // } - // _ => unimplemented!("expected array after array type"), - // }; - // types::Expression::IntegerExp(types::IntegerExpression::Array(elements)) - // } - // ast::BasicType::Field(_ty) => { - // let elements: Vec>> = match expression { - // ast::Expression::ArrayInline(array) => array - // .expressions - // .into_iter() - // .map(|s_or_e| Box::new(types::FieldSpreadOrExpression::from(s_or_e))) - // .collect(), - // ast::Expression::ArrayInitializer(array) => { - // let count = types::Expression::::get_count(array.count); - // let expression = - // Box::new(types::FieldSpreadOrExpression::from(*array.expression)); - // - // vec![expression; count] - // } - // _ => unimplemented!("expected array after array type"), - // }; - // types::Expression::FieldElementExp(types::FieldExpression::Array(elements)) - // } - // ast::BasicType::Boolean(_ty) => { - // let elements: Vec>> = match expression { - // ast::Expression::ArrayInline(array) => array - // .expressions - // .into_iter() - // .map(|s_or_e| Box::new(types::BooleanSpreadOrExpression::from(s_or_e))) - // .collect(), - // ast::Expression::ArrayInitializer(array) => { - // let count = types::Expression::::get_count(array.count); - // let expression = - // Box::new(types::BooleanSpreadOrExpression::from(*array.expression)); - // - // vec![expression; count] - // } - // _ => unimplemented!("expected array after array type"), - // }; - // types::Expression::BooleanExp(types::BooleanExpression::Array(elements)) - // } - // } - // } - fn from_struct(ty: ast::StructType<'ast>, expression: ast::Expression<'ast>) -> Self { let declaration_struct = ty.variable.value; match expression {