diff --git a/Cargo.lock b/Cargo.lock index 5073a553b..93cdda61a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2325,9 +2325,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.95" +version = "1.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" +checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1" dependencies = [ "indexmap", "itoa", @@ -3157,6 +3157,7 @@ dependencies = [ "byteorder", "cfg-if 0.1.10", "derivative", + "log", "num-bigint 0.2.6", "pairing_ce", "serde", diff --git a/changelogs/unreleased/1288-schaeff b/changelogs/unreleased/1288-schaeff new file mode 100644 index 000000000..a3758ebea --- /dev/null +++ b/changelogs/unreleased/1288-schaeff @@ -0,0 +1 @@ +Make ZoKrates build on stable rust \ No newline at end of file diff --git a/clippy.toml b/clippy.toml deleted file mode 100644 index 3f707a567..000000000 --- a/clippy.toml +++ /dev/null @@ -1 +0,0 @@ -blacklisted-names = [] \ No newline at end of file diff --git a/dev.Dockerfile b/dev.Dockerfile index 2bf8fed0e..bcb73b0d8 100644 --- a/dev.Dockerfile +++ b/dev.Dockerfile @@ -1,9 +1,9 @@ -FROM rustlang/rust:nightly +FROM rust:latest RUN useradd -u 1000 -m zokrates -COPY ./scripts/install_foundry_deb.sh /tmp/ -RUN /tmp/install_foundry_deb.sh +COPY ./scripts/install_foundry.sh /tmp/ +RUN /tmp/install_foundry.sh USER zokrates diff --git a/rust-toolchain.toml b/rust-toolchain.toml index cbfe2704f..292fe499e 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,2 +1,2 @@ [toolchain] -channel = "nightly-2022-07-01" +channel = "stable" diff --git a/zokrates_abi/src/lib.rs b/zokrates_abi/src/lib.rs index a85adb219..d12f65d8f 100644 --- a/zokrates_abi/src/lib.rs +++ b/zokrates_abi/src/lib.rs @@ -1,5 +1,3 @@ -#![feature(box_patterns, box_syntax)] - pub enum Inputs { Raw(Vec), Abi(Values), diff --git a/zokrates_analysis/src/boolean_array_comparator.rs b/zokrates_analysis/src/boolean_array_comparator.rs index f9696d276..72c158193 100644 --- a/zokrates_analysis/src/boolean_array_comparator.rs +++ b/zokrates_analysis/src/boolean_array_comparator.rs @@ -1,8 +1,8 @@ use zokrates_ast::{ common::WithSpan, typed::{ - folder::*, ArrayExpression, BooleanExpression, Conditional, ConditionalKind, Expr, - FieldElementExpression, Select, Type, TypedExpression, TypedProgram, UExpression, + folder::*, ArrayExpression, ArrayType, BooleanExpression, Conditional, ConditionalKind, + Expr, FieldElementExpression, Select, Type, TypedExpression, TypedProgram, UExpression, UExpressionInner, }, }; @@ -42,7 +42,7 @@ impl<'ast, T: Field> Folder<'ast, T> for BooleanArrayComparator { _ => unreachable!("array size should be known"), }; - let chunk_size = T::get_required_bits() as usize - 1; + let chunk_size = T::get_required_bits() - 1; let left_elements: Vec<_> = (0..len) .map(|i| BooleanExpression::select(*e.left.clone(), i as u32).span(span)) @@ -91,10 +91,10 @@ impl<'ast, T: Field> Folder<'ast, T> for BooleanArrayComparator { BooleanExpression::array_eq( ArrayExpression::value(left) - .annotate(Type::FieldElement, chunk_count as u32) + .annotate(ArrayType::new(Type::FieldElement, chunk_count as u32)) .span(span), ArrayExpression::value(right) - .annotate(Type::FieldElement, chunk_count as u32) + .annotate(ArrayType::new(Type::FieldElement, chunk_count as u32)) .span(span), ) } @@ -124,8 +124,8 @@ mod tests { // [x[0] ? 2**1 : 0 + x[1] ? 2**0 : 0] == [y[0] ? 2**1 : 0 + y[1] ? 2**0 : 0] // a single field is sufficient, as the prime we're working with is 3 bits long, so we can pack up to 2 bits - let x = a_id("x").annotate(Type::Boolean, 2u32); - let y = a_id("y").annotate(Type::Boolean, 2u32); + let x = a_id("x").annotate(ArrayType::new(Type::Boolean, 2u32)); + let y = a_id("y").annotate(ArrayType::new(Type::Boolean, 2u32)); let e: BooleanExpression = BooleanExpression::ArrayEq(BinaryExpression::new(x.clone(), y.clone())); @@ -152,8 +152,8 @@ mod tests { // should become // [x[0] ? 2**2 : 0 + x[1] ? 2**1 : 0, x[2] ? 2**0 : 0] == [y[0] ? 2**2 : 0 + y[1] ? 2**1 : 0 y[2] ? 2**0 : 0] - let x = a_id("x").annotate(Type::Boolean, 3u32); - let y = a_id("y").annotate(Type::Boolean, 3u32); + let x = a_id("x").annotate(ArrayType::new(Type::Boolean, 3u32)); + let y = a_id("y").annotate(ArrayType::new(Type::Boolean, 3u32)); let e: BooleanExpression = BooleanExpression::ArrayEq(BinaryExpression::new(x.clone(), y.clone())); diff --git a/zokrates_analysis/src/constant_resolver.rs b/zokrates_analysis/src/constant_resolver.rs index f39167d39..5ff42f4c2 100644 --- a/zokrates_analysis/src/constant_resolver.rs +++ b/zokrates_analysis/src/constant_resolver.rs @@ -113,7 +113,7 @@ mod tests { use zokrates_ast::typed::types::{DeclarationSignature, GTupleType}; use zokrates_ast::typed::{ DeclarationArrayType, DeclarationFunctionKey, DeclarationType, FieldElementExpression, - GType, Identifier, TypedConstant, TypedExpression, TypedFunction, TypedFunctionSymbol, + Identifier, TypedConstant, TypedExpression, TypedFunction, TypedFunctionSymbol, TypedStatement, }; use zokrates_field::Bn128Field; @@ -316,12 +316,12 @@ mod tests { FieldElementExpression::add( FieldElementExpression::select( ArrayExpression::identifier(Identifier::from(const_id.clone())) - .annotate(GType::FieldElement, 2u32), + .annotate(GArrayType::new(Type::FieldElement, 2u32)), UExpression::value(0u128).annotate(UBitwidth::B32), ), FieldElementExpression::select( ArrayExpression::identifier(Identifier::from(const_id.clone())) - .annotate(GType::FieldElement, 2u32), + .annotate(GArrayType::new(Type::FieldElement, 2u32)), UExpression::value(1u128).annotate(UBitwidth::B32), ), ) @@ -347,7 +347,7 @@ mod tests { FieldElementExpression::value(Bn128Field::from(2)).into(), FieldElementExpression::value(Bn128Field::from(2)).into(), ]) - .annotate(GType::FieldElement, 2u32), + .annotate(GArrayType::new(Type::FieldElement, 2u32)), ), DeclarationType::Array(DeclarationArrayType::new( DeclarationType::FieldElement, @@ -741,8 +741,9 @@ mod tests { main_baz_const_id.clone(), TypedConstantSymbol::Here(TypedConstant::new( TypedExpression::Array( - ArrayExpression::identifier(main_bar_const_id.clone().into()) - .annotate(Type::FieldElement, main_foo_const_id.clone()), + ArrayExpression::identifier(main_bar_const_id.clone().into()).annotate( + ArrayType::new(Type::FieldElement, main_foo_const_id.clone()), + ), ), DeclarationType::Array(DeclarationArrayType::new( DeclarationType::FieldElement, @@ -814,8 +815,9 @@ mod tests { main_baz_const_id.clone(), TypedConstantSymbol::Here(TypedConstant::new( TypedExpression::Array( - ArrayExpression::identifier(main_bar_const_id.into()) - .annotate(Type::FieldElement, main_foo_const_id.clone()), + ArrayExpression::identifier(main_bar_const_id.into()).annotate( + ArrayType::new(Type::FieldElement, main_foo_const_id.clone()), + ), ), DeclarationType::Array(DeclarationArrayType::new( DeclarationType::FieldElement, diff --git a/zokrates_analysis/src/flatten_complex_types.rs b/zokrates_analysis/src/flatten_complex_types.rs index 20cfa9436..64705d1ad 100644 --- a/zokrates_analysis/src/flatten_complex_types.rs +++ b/zokrates_analysis/src/flatten_complex_types.rs @@ -39,17 +39,14 @@ fn flatten_identifier_rec<'ast>( } typed::types::ConcreteType::Array(array_type) => (0..*array_type.size) .flat_map(|i| { - flatten_identifier_rec( - SourceIdentifier::Select(Box::new(id.clone()), i), - &array_type.ty, - ) + flatten_identifier_rec(SourceIdentifier::select(id.clone(), i), &array_type.ty) }) .collect(), typed::types::ConcreteType::Struct(members) => members .iter() .flat_map(|struct_member| { flatten_identifier_rec( - SourceIdentifier::Member(Box::new(id.clone()), struct_member.id.clone()), + SourceIdentifier::member(id.clone(), struct_member.id.clone()), &struct_member.ty, ) }) @@ -59,10 +56,7 @@ fn flatten_identifier_rec<'ast>( .iter() .enumerate() .flat_map(|(i, ty)| { - flatten_identifier_rec( - SourceIdentifier::Element(Box::new(id.clone()), i as u32), - ty, - ) + flatten_identifier_rec(SourceIdentifier::element(id.clone(), i as u32), ty) }) .collect(), } @@ -88,7 +82,7 @@ fn flatten_identifier_to_expression_rec<'ast, T: Field>( typed::ConcreteType::Array(array_type) => (0..*array_type.size) .flat_map(|i| { flatten_identifier_to_expression_rec( - SourceIdentifier::Select(box id.clone(), i), + SourceIdentifier::select(id.clone(), i), &array_type.ty, ) }) @@ -97,7 +91,7 @@ fn flatten_identifier_to_expression_rec<'ast, T: Field>( .iter() .flat_map(|struct_member| { flatten_identifier_to_expression_rec( - SourceIdentifier::Member(box id.clone(), struct_member.id.clone()), + SourceIdentifier::member(id.clone(), struct_member.id.clone()), &struct_member.ty, ) }) @@ -108,7 +102,7 @@ fn flatten_identifier_to_expression_rec<'ast, T: Field>( .enumerate() .flat_map(|(i, ty)| { flatten_identifier_to_expression_rec( - SourceIdentifier::Element(box id.clone(), i as u32), + SourceIdentifier::element(id.clone(), i as u32), ty, ) }) @@ -231,12 +225,12 @@ impl<'ast, T: Field> Flattener { fn fold_assignee(&mut self, a: typed::TypedAssignee<'ast, T>) -> Vec> { match a { typed::TypedAssignee::Identifier(v) => self.fold_variable(v), - typed::TypedAssignee::Select(box a, box i) => { + typed::TypedAssignee::Select(a, i) => { let count = match typed::ConcreteType::try_from(a.get_type()).unwrap() { typed::ConcreteType::Array(array_ty) => array_ty.ty.get_primitive_count(), _ => unreachable!(), }; - let a = self.fold_assignee(a); + let a = self.fold_assignee(*a); match i.as_inner() { typed::UExpressionInner::Value(index) => { @@ -245,7 +239,7 @@ impl<'ast, T: Field> Flattener { i => unreachable!("index {:?} not allowed, should be a constant", i), } } - typed::TypedAssignee::Member(box a, m) => { + typed::TypedAssignee::Member(a, m) => { let (offset, size) = match typed::ConcreteType::try_from(a.get_type()).unwrap() { typed::ConcreteType::Struct(struct_type) => struct_type @@ -263,11 +257,11 @@ impl<'ast, T: Field> Flattener { let size = size.unwrap(); - let a = self.fold_assignee(a); + let a = self.fold_assignee(*a); a[offset..offset + size].to_vec() } - typed::TypedAssignee::Element(box a, index) => { + typed::TypedAssignee::Element(a, index) => { let tuple_ty = typed::ConcreteTupleType::try_from( typed::ConcreteType::try_from(a.get_type()).unwrap(), ) @@ -282,7 +276,7 @@ impl<'ast, T: Field> Flattener { let size = &tuple_ty.elements[index as usize].get_primitive_count(); - let a = self.fold_assignee(a); + let a = self.fold_assignee(*a); a[offset..offset + size].to_vec() } diff --git a/zokrates_analysis/src/lib.rs b/zokrates_analysis/src/lib.rs index 539fe86cb..2cf0c24fc 100644 --- a/zokrates_analysis/src/lib.rs +++ b/zokrates_analysis/src/lib.rs @@ -1,5 +1,3 @@ -#![feature(box_patterns, box_syntax)] - //! Module containing static analysis //! //! @file mod.rs diff --git a/zokrates_analysis/src/out_of_bounds.rs b/zokrates_analysis/src/out_of_bounds.rs index 5be99af50..7ed8613a7 100644 --- a/zokrates_analysis/src/out_of_bounds.rs +++ b/zokrates_analysis/src/out_of_bounds.rs @@ -46,10 +46,10 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for OutOfBoundsChecker { a: TypedAssignee<'ast, T>, ) -> Result, Error> { match a { - TypedAssignee::Select(box array, box index) => { + TypedAssignee::Select(array, index) => { use zokrates_ast::typed::Typed; - let array = self.fold_assignee(array)?; + let array = self.fold_assignee(*array)?; let size = match array.get_type() { Type::Array(array_ty) => match array_ty.size.as_inner() { @@ -62,13 +62,13 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for OutOfBoundsChecker { match index.as_inner() { UExpressionInner::Value(i) if i.value >= size => Err(Error(format!( "Out of bounds write to `{}` because `{}` has size {}", - TypedAssignee::Select(box array.clone(), box index), + TypedAssignee::select(array.clone(), *index), array, size ))), - _ => Ok(TypedAssignee::Select( - box self.fold_assignee(array)?, - box self.fold_uint_expression(index)?, + _ => Ok(TypedAssignee::select( + array, + self.fold_uint_expression(*index)?, )), } } diff --git a/zokrates_analysis/src/panic_extractor.rs b/zokrates_analysis/src/panic_extractor.rs index 312abab05..dd2628cd5 100644 --- a/zokrates_analysis/src/panic_extractor.rs +++ b/zokrates_analysis/src/panic_extractor.rs @@ -1,6 +1,6 @@ use std::ops::*; use zokrates_ast::{ - common::{expressions::BinaryExpression, Fold, WithSpan}, + common::{Fold, WithSpan}, zir::{ folder::*, BooleanExpression, Conditional, ConditionalExpression, ConditionalOrExpression, Expr, FieldElementExpression, IfElseStatement, RuntimeError, UBitwidth, UExpression, @@ -160,14 +160,12 @@ impl<'ast, T: Field> Folder<'ast, T> for PanicExtractor<'ast, T> { ) -> BooleanExpression<'ast, T> { match e { // constant range checks are complete, so no panic needs to be extracted - e @ BooleanExpression::FieldLt(BinaryExpression { - left: box FieldElementExpression::Value(_), - .. - }) - | e @ BooleanExpression::FieldLt(BinaryExpression { - right: box FieldElementExpression::Value(_), - .. - }) => fold_boolean_expression_cases(self, e), + BooleanExpression::FieldLt(b) + if matches!(b.left.as_ref(), FieldElementExpression::Value(_)) + || matches!(b.right.as_ref(), FieldElementExpression::Value(_)) => + { + fold_boolean_expression_cases(self, BooleanExpression::FieldLt(b)) + } BooleanExpression::FieldLt(e) => { let span = e.get_span(); diff --git a/zokrates_analysis/src/propagation.rs b/zokrates_analysis/src/propagation.rs index 06e8c5b43..4fc9e6651 100644 --- a/zokrates_analysis/src/propagation.rs +++ b/zokrates_analysis/src/propagation.rs @@ -73,30 +73,28 @@ impl<'ast, T: Field> Propagator<'ast, T> { .get_mut(&var.id) .map(|c| Ok((var, c))) .unwrap_or(Err(var)), - TypedAssignee::Select(box assignee, box index) => { - match self.try_get_constant_mut(assignee) { - Ok((variable, constant)) => match index.as_inner() { - UExpressionInner::Value(n) => match constant { - TypedExpression::Array(a) => match a.as_inner_mut() { - ArrayExpressionInner::Value(value) => { - match value.value.get_mut(n.value as usize) { - Some(TypedExpressionOrSpread::Expression(ref mut e)) => { - Ok((variable, e)) - } - None => Err(variable), - _ => unreachable!(), + TypedAssignee::Select(assignee, index) => match self.try_get_constant_mut(assignee) { + Ok((variable, constant)) => match index.as_inner() { + UExpressionInner::Value(n) => match constant { + TypedExpression::Array(a) => match a.as_inner_mut() { + ArrayExpressionInner::Value(value) => { + match value.value.get_mut(n.value as usize) { + Some(TypedExpressionOrSpread::Expression(ref mut e)) => { + Ok((variable, e)) } + None => Err(variable), + _ => unreachable!(), } - _ => unreachable!("should be an array value"), - }, - _ => unreachable!("should be an array expression"), + } + _ => unreachable!("should be an array value"), }, - _ => Err(variable), + _ => unreachable!("should be an array expression"), }, - e => e, - } - } - TypedAssignee::Member(box assignee, m) => match self.try_get_constant_mut(assignee) { + _ => Err(variable), + }, + e => e, + }, + TypedAssignee::Member(assignee, m) => match self.try_get_constant_mut(assignee) { Ok((v, c)) => { let ty = assignee.get_type(); @@ -119,20 +117,18 @@ impl<'ast, T: Field> Propagator<'ast, T> { } e => e, }, - TypedAssignee::Element(box assignee, index) => { - match self.try_get_constant_mut(assignee) { - Ok((v, c)) => match c { - TypedExpression::Tuple(a) => match a.as_inner_mut() { - TupleExpressionInner::Value(value) => { - Ok((v, &mut value.value[*index as usize])) - } - _ => unreachable!("should be a tuple value"), - }, - _ => unreachable!("should be a tuple expression"), + TypedAssignee::Element(assignee, index) => match self.try_get_constant_mut(assignee) { + Ok((v, c)) => match c { + TypedExpression::Tuple(a) => match a.as_inner_mut() { + TupleExpressionInner::Value(value) => { + Ok((v, &mut value.value[*index as usize])) + } + _ => unreachable!("should be a tuple value"), }, - e => e, - } - } + _ => unreachable!("should be a tuple expression"), + }, + e => e, + }, } } } @@ -418,7 +414,7 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for Propagator<'ast, T> { .map(|v| BooleanExpression::value(v).into()) .collect::>(), ) - .annotate(Type::Boolean, bitwidth.to_usize() as u32) + .annotate(ArrayType::new(Type::Boolean, bitwidth.to_usize() as u32)) .into() } _ => unreachable!("should be a uint value"), @@ -526,7 +522,7 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for Propagator<'ast, T> { }) .collect::>(), ) - .annotate(Type::Boolean, bit_width) + .annotate(ArrayType::new(Type::Boolean, bit_width)) .span(span) .into(), )) @@ -1192,7 +1188,7 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for Propagator<'ast, T> { None => Ok(SelectOrExpression::Expression( E::select( ArrayExpressionInner::Identifier(id) - .annotate(inner_type, size.value as u32), + .annotate(ArrayType::new(inner_type, size.value as u32)), UExpressionInner::Value(n).annotate(UBitwidth::B32), ) .into_inner(), @@ -1200,7 +1196,7 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for Propagator<'ast, T> { } } (a, i) => Ok(SelectOrExpression::Select(SelectExpression::new( - a.annotate(inner_type, size.value as u32), + a.annotate(ArrayType::new(inner_type, size.value as u32)), i.annotate(UBitwidth::B32), ))), }, @@ -1706,7 +1702,7 @@ mod tests { FieldElementExpression::value(Bn128Field::from(2)).into(), FieldElementExpression::value(Bn128Field::from(3)).into(), ]) - .annotate(Type::FieldElement, 3u32), + .annotate(ArrayType::new(Type::FieldElement, 3u32)), UExpression::add(1u32.into(), 1u32.into()), ); @@ -1837,34 +1833,38 @@ mod tests { ArrayExpression::value(vec![TypedExpressionOrSpread::Expression( FieldElementExpression::value(Bn128Field::from(2usize)).into(), )]) - .annotate(Type::FieldElement, 1u32), + .annotate(ArrayType::new(Type::FieldElement, 1u32)), ArrayExpression::value(vec![TypedExpressionOrSpread::Expression( FieldElementExpression::value(Bn128Field::from(2usize)).into(), )]) - .annotate(Type::FieldElement, 1u32), + .annotate(ArrayType::new(Type::FieldElement, 1u32)), )); let e_constant_false = BooleanExpression::ArrayEq(BinaryExpression::new( ArrayExpression::value(vec![TypedExpressionOrSpread::Expression( FieldElementExpression::value(Bn128Field::from(2usize)).into(), )]) - .annotate(Type::FieldElement, 1u32), + .annotate(ArrayType::new(Type::FieldElement, 1u32)), ArrayExpression::value(vec![TypedExpressionOrSpread::Expression( FieldElementExpression::value(Bn128Field::from(4usize)).into(), )]) - .annotate(Type::FieldElement, 1u32), + .annotate(ArrayType::new(Type::FieldElement, 1u32)), )); let e_identifier_true: BooleanExpression = BooleanExpression::ArrayEq(BinaryExpression::new( - ArrayExpression::identifier("a".into()).annotate(Type::FieldElement, 1u32), - ArrayExpression::identifier("a".into()).annotate(Type::FieldElement, 1u32), + ArrayExpression::identifier("a".into()) + .annotate(ArrayType::new(Type::FieldElement, 1u32)), + ArrayExpression::identifier("a".into()) + .annotate(ArrayType::new(Type::FieldElement, 1u32)), )); let e_identifier_unchanged: BooleanExpression = BooleanExpression::ArrayEq(BinaryExpression::new( - ArrayExpression::identifier("a".into()).annotate(Type::FieldElement, 1u32), - ArrayExpression::identifier("b".into()).annotate(Type::FieldElement, 1u32), + ArrayExpression::identifier("a".into()) + .annotate(ArrayType::new(Type::FieldElement, 1u32)), + ArrayExpression::identifier("b".into()) + .annotate(ArrayType::new(Type::FieldElement, 1u32)), )); let e_non_canonical_true = BooleanExpression::ArrayEq(BinaryExpression::new( @@ -1872,14 +1872,14 @@ mod tests { ArrayExpression::value(vec![TypedExpressionOrSpread::Expression( FieldElementExpression::value(Bn128Field::from(2usize)).into(), )]) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), )]) - .annotate(Type::FieldElement, 1u32), + .annotate(ArrayType::new(Type::FieldElement, 1u32)), ArrayExpression::value(vec![TypedExpressionOrSpread::Expression( FieldElementExpression::value(Bn128Field::from(2usize)).into(), )]) - .annotate(Type::FieldElement, 1u32), + .annotate(ArrayType::new(Type::FieldElement, 1u32)), )); let e_non_canonical_false = BooleanExpression::ArrayEq(BinaryExpression::new( @@ -1887,14 +1887,14 @@ mod tests { ArrayExpression::value(vec![TypedExpressionOrSpread::Expression( FieldElementExpression::value(Bn128Field::from(2usize)).into(), )]) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), )]) - .annotate(Type::FieldElement, 1u32), + .annotate(ArrayType::new(Type::FieldElement, 1u32)), ArrayExpression::value(vec![TypedExpressionOrSpread::Expression( FieldElementExpression::value(Bn128Field::from(4usize)).into(), )]) - .annotate(Type::FieldElement, 1u32), + .annotate(ArrayType::new(Type::FieldElement, 1u32)), )); assert_eq!( diff --git a/zokrates_analysis/src/reducer/mod.rs b/zokrates_analysis/src/reducer/mod.rs index 3328bf5cb..c248cccf2 100644 --- a/zokrates_analysis/src/reducer/mod.rs +++ b/zokrates_analysis/src/reducer/mod.rs @@ -437,7 +437,7 @@ mod tests { use zokrates_ast::typed::types::DeclarationSignature; use zokrates_ast::typed::types::{DeclarationConstant, GTupleType}; use zokrates_ast::typed::{ - ArrayExpression, DeclarationFunctionKey, DeclarationType, DeclarationVariable, + ArrayExpression, ArrayType, DeclarationFunctionKey, DeclarationType, DeclarationVariable, FieldElementExpression, GenericIdentifier, Identifier, Select, TupleExpression, TupleType, Type, TypedExpression, TypedExpressionOrSpread, UBitwidth, Variable, }; @@ -650,7 +650,7 @@ mod tests { .into()], statements: vec![TypedStatement::ret( ArrayExpression::identifier("a".into()) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), )], signature: foo_signature.clone(), @@ -674,7 +674,7 @@ mod tests { ArrayExpression::value(vec![ FieldElementExpression::identifier("a".into()).into() ]) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), TypedStatement::definition( @@ -684,10 +684,10 @@ mod tests { .signature(foo_signature.clone()), vec![None], vec![ArrayExpression::identifier("b".into()) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into()], ) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), TypedStatement::definition( @@ -700,7 +700,7 @@ mod tests { (FieldElementExpression::identifier("a".into()) + FieldElementExpression::select( ArrayExpression::identifier("b".into()) - .annotate(Type::FieldElement, 1u32), + .annotate(ArrayType::new(Type::FieldElement, 1u32)), 0u32, )) .into(), @@ -750,28 +750,28 @@ mod tests { ArrayExpression::value(vec![ FieldElementExpression::identifier("a".into()).into() ]) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), TypedStatement::definition( Variable::array(Identifier::from("a").in_frame(1), Type::FieldElement, 1u32) .into(), ArrayExpression::identifier("b".into()) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), TypedStatement::definition( Variable::array(Identifier::from("b").version(1), Type::FieldElement, 1u32) .into(), ArrayExpression::identifier(Identifier::from("a").in_frame(1)) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), TypedStatement::ret( (FieldElementExpression::identifier("a".into()) + FieldElementExpression::select( ArrayExpression::identifier(Identifier::from("b").version(1)) - .annotate(Type::FieldElement, 1u32), + .annotate(ArrayType::new(Type::FieldElement, 1u32)), 0u32, )) .into(), @@ -850,7 +850,7 @@ mod tests { .into()], statements: vec![TypedStatement::ret( ArrayExpression::identifier("a".into()) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), )], signature: foo_signature.clone(), @@ -884,7 +884,7 @@ mod tests { ArrayExpression::value(vec![ FieldElementExpression::identifier("a".into()).into() ]) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), TypedStatement::definition( @@ -894,10 +894,10 @@ mod tests { .signature(foo_signature.clone()), vec![None], vec![ArrayExpression::identifier("b".into()) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into()], ) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), TypedStatement::definition( @@ -910,7 +910,7 @@ mod tests { (FieldElementExpression::identifier("a".into()) + FieldElementExpression::select( ArrayExpression::identifier("b".into()) - .annotate(Type::FieldElement, 1u32), + .annotate(ArrayType::new(Type::FieldElement, 1u32)), 0u32, )) .into(), @@ -960,28 +960,28 @@ mod tests { ArrayExpression::value(vec![ FieldElementExpression::identifier("a".into()).into() ]) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), TypedStatement::definition( Variable::array(Identifier::from("a").in_frame(1), Type::FieldElement, 1u32) .into(), ArrayExpression::identifier("b".into()) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), TypedStatement::definition( Variable::array(Identifier::from("b").version(1), Type::FieldElement, 1u32) .into(), ArrayExpression::identifier(Identifier::from("a").in_frame(1)) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), TypedStatement::ret( (FieldElementExpression::identifier("a".into()) + FieldElementExpression::select( ArrayExpression::identifier(Identifier::from("b").version(1)) - .annotate(Type::FieldElement, 1u32), + .annotate(ArrayType::new(Type::FieldElement, 1u32)), 0u32, )) .into(), @@ -1076,23 +1076,23 @@ mod tests { vec![ArrayExpression::value(vec![ TypedExpressionOrSpread::Spread( ArrayExpression::identifier("a".into()) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), FieldElementExpression::value(Bn128Field::from(0)).into(), ]) - .annotate( + .annotate(ArrayType::new( Type::FieldElement, UExpression::identifier("K".into()).annotate(UBitwidth::B32) + 1u32.into(), - ) + )) .into()], ) - .annotate( + .annotate(ArrayType::new( Type::FieldElement, UExpression::identifier("K".into()).annotate(UBitwidth::B32) + 1u32.into(), - ), + )), 0u32.into(), UExpression::identifier("K".into()).annotate(UBitwidth::B32), ) @@ -1100,10 +1100,10 @@ mod tests { ), TypedStatement::ret( ArrayExpression::identifier("ret".into()) - .annotate( + .annotate(ArrayType::new( Type::FieldElement, UExpression::identifier("K".into()).annotate(UBitwidth::B32), - ) + )) .into(), ), ], @@ -1121,10 +1121,10 @@ mod tests { .into()], statements: vec![TypedStatement::ret( ArrayExpression::identifier("a".into()) - .annotate( + .annotate(ArrayType::new( Type::FieldElement, UExpression::identifier("K".into()).annotate(UBitwidth::B32), - ) + )) .into(), )], signature: bar_signature.clone(), @@ -1143,10 +1143,10 @@ mod tests { Bn128Field::from(1), ) .into()]) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into()], ) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), TypedStatement::ret( @@ -1257,7 +1257,7 @@ mod tests { .into()], statements: vec![TypedStatement::ret( ArrayExpression::identifier("a".into()) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), )], signature: foo_signature.clone(), @@ -1273,10 +1273,10 @@ mod tests { .signature(foo_signature.clone()), vec![None], vec![ArrayExpression::value(vec![]) - .annotate(Type::FieldElement, 0u32) + .annotate(ArrayType::new(Type::FieldElement, 0u32)) .into()], ) - .annotate(Type::FieldElement, 1u32) + .annotate(ArrayType::new(Type::FieldElement, 1u32)) .into(), ), TypedStatement::ret( diff --git a/zokrates_analysis/src/reducer/shallow_ssa.rs b/zokrates_analysis/src/reducer/shallow_ssa.rs index 8bb244a73..46f9cd962 100644 --- a/zokrates_analysis/src/reducer/shallow_ssa.rs +++ b/zokrates_analysis/src/reducer/shallow_ssa.rs @@ -103,15 +103,15 @@ impl<'ast> ShallowTransformer<'ast> { ) -> TypedAssignee<'ast, T> { match a { TypedAssignee::Identifier(v) => TypedAssignee::Identifier(self.fold_variable(v)), - TypedAssignee::Select(box a, box index) => TypedAssignee::Select( - box self.fold_assignee_no_ssa_increase(a), - box self.fold_uint_expression(index), + TypedAssignee::Select(a, index) => TypedAssignee::select( + self.fold_assignee_no_ssa_increase(*a), + self.fold_uint_expression(*index), ), - TypedAssignee::Member(box s, m) => { - TypedAssignee::Member(box self.fold_assignee_no_ssa_increase(s), m) + TypedAssignee::Member(s, m) => { + TypedAssignee::member(self.fold_assignee_no_ssa_increase(*s), m) } - TypedAssignee::Element(box s, index) => { - TypedAssignee::Element(box self.fold_assignee_no_ssa_increase(s), index) + TypedAssignee::Element(s, index) => { + TypedAssignee::element(self.fold_assignee_no_ssa_increase(*s), index) } } } @@ -409,7 +409,7 @@ mod tests { FieldElementExpression::value(Bn128Field::from(1)).into(), FieldElementExpression::value(Bn128Field::from(1)).into(), ]) - .annotate(Type::FieldElement, 2u32) + .annotate(ArrayType::new(Type::FieldElement, 2u32)) .into(), ); @@ -425,15 +425,19 @@ mod tests { FieldElementExpression::value(Bn128Field::from(1)).into(), FieldElementExpression::value(Bn128Field::from(1)).into() ]) - .annotate(Type::FieldElement, 2u32) + .annotate(ArrayType::new(Type::FieldElement, 2u32)) .into() )] ); let s: TypedStatement = TypedStatement::definition( TypedAssignee::Select( - box TypedAssignee::Identifier(Variable::array("a", Type::FieldElement, 2u32)), - box UExpression::from(1u32), + Box::new(TypedAssignee::Identifier(Variable::array( + "a", + Type::FieldElement, + 2u32, + ))), + Box::new(UExpression::from(1u32)), ), FieldElementExpression::value(Bn128Field::from(2)).into(), ); @@ -461,16 +465,19 @@ mod tests { FieldElementExpression::value(Bn128Field::from(0)).into(), FieldElementExpression::value(Bn128Field::from(1)).into(), ]) - .annotate(Type::FieldElement, 2u32) + .annotate(ArrayType::new(Type::FieldElement, 2u32)) .into(), ArrayExpression::value(vec![ FieldElementExpression::value(Bn128Field::from(2)).into(), FieldElementExpression::value(Bn128Field::from(3)).into(), ]) - .annotate(Type::FieldElement, 2u32) + .annotate(ArrayType::new(Type::FieldElement, 2u32)) .into(), ]) - .annotate(Type::array((Type::FieldElement, 2u32)), 2u32) + .annotate(ArrayType::new( + Type::array((Type::FieldElement, 2u32)), + 2u32, + )) .into(), ); @@ -486,30 +493,33 @@ mod tests { FieldElementExpression::value(Bn128Field::from(0)).into(), FieldElementExpression::value(Bn128Field::from(1)).into(), ]) - .annotate(Type::FieldElement, 2u32) + .annotate(ArrayType::new(Type::FieldElement, 2u32)) .into(), ArrayExpression::value(vec![ FieldElementExpression::value(Bn128Field::from(2)).into(), FieldElementExpression::value(Bn128Field::from(3)).into(), ]) - .annotate(Type::FieldElement, 2u32) + .annotate(ArrayType::new(Type::FieldElement, 2u32)) .into(), ]) - .annotate(Type::array((Type::FieldElement, 2u32)), 2u32) + .annotate(ArrayType::new( + Type::array((Type::FieldElement, 2u32)), + 2u32 + )) .into(), )] ); let s: TypedStatement = TypedStatement::definition( - TypedAssignee::Select( - box TypedAssignee::Identifier(Variable::new("a", array_of_array_ty.clone())), - box UExpression::from(1u32), + TypedAssignee::select( + TypedAssignee::Identifier(Variable::new("a", array_of_array_ty.clone())), + UExpression::from(1u32), ), ArrayExpression::value(vec![ FieldElementExpression::value(Bn128Field::from(4)).into(), FieldElementExpression::value(Bn128Field::from(5)).into(), ]) - .annotate(Type::FieldElement, 2u32) + .annotate(ArrayType::new(Type::FieldElement, 2u32)) .into(), ); diff --git a/zokrates_analysis/src/struct_concretizer.rs b/zokrates_analysis/src/struct_concretizer.rs index 932a92670..6e57c0484 100644 --- a/zokrates_analysis/src/struct_concretizer.rs +++ b/zokrates_analysis/src/struct_concretizer.rs @@ -70,7 +70,7 @@ impl<'ast, T: Field> Folder<'ast, T> for StructConcretizer<'ast, T> { .collect(), generics: concrete_generics .into_iter() - .map(|g| Some(DeclarationConstant::Concrete(g as u32))) + .map(|g| Some(DeclarationConstant::Concrete(g))) .collect(), ..ty } @@ -82,9 +82,9 @@ impl<'ast, T: Field> Folder<'ast, T> for StructConcretizer<'ast, T> { ) -> DeclarationArrayType<'ast, T> { let size = ty.size.map_concrete(&self.generics).unwrap(); - DeclarationArrayType { - size: box DeclarationConstant::Concrete(size), - ty: box self.fold_declaration_type(*ty.ty), - } + DeclarationArrayType::new( + self.fold_declaration_type(*ty.ty), + DeclarationConstant::Concrete(size), + ) } } diff --git a/zokrates_analysis/src/variable_write_remover.rs b/zokrates_analysis/src/variable_write_remover.rs index 9defeeaf5..2d9ce8685 100644 --- a/zokrates_analysis/src/variable_write_remover.rs +++ b/zokrates_analysis/src/variable_write_remover.rs @@ -56,7 +56,7 @@ impl<'ast> VariableWriteRemover { let tail = indices; match head { - Access::Select(box head) => { + Access::Select(head) => { statements.insert(TypedStatement::assertion( BooleanExpression::uint_lt( head.clone(), @@ -266,7 +266,7 @@ impl<'ast> VariableWriteRemover { }) .collect::>(), ) - .annotate(inner_ty.clone(), size) + .annotate(ArrayType::new(inner_ty.clone(), size)) .into() } _ => unreachable!(), @@ -525,27 +525,28 @@ impl<'ast> VariableWriteRemover { #[derive(Clone, Debug)] enum Access<'ast, T: Field> { - Select(Box>), + Select(UExpression<'ast, T>), Member(MemberId), Element(u32), } + /// Turn an assignee into its representation as a base variable and a list accesses /// a[2][3][4] -> (a, [2, 3, 4]) fn linear(a: TypedAssignee) -> (Variable, Vec>) { match a { TypedAssignee::Identifier(v) => (v, vec![]), - TypedAssignee::Select(box array, box index) => { - let (v, mut indices) = linear(array); - indices.push(Access::Select(box index)); + TypedAssignee::Select(array, index) => { + let (v, mut indices) = linear(*array); + indices.push(Access::Select(*index)); (v, indices) } - TypedAssignee::Member(box s, m) => { - let (v, mut indices) = linear(s); + TypedAssignee::Member(s, m) => { + let (v, mut indices) = linear(*s); indices.push(Access::Member(m)); (v, indices) } - TypedAssignee::Element(box s, i) => { - let (v, mut indices) = linear(s); + TypedAssignee::Element(s, i) => { + let (v, mut indices) = linear(*s); indices.push(Access::Element(i)); (v, indices) } @@ -555,12 +556,12 @@ fn linear(a: TypedAssignee) -> (Variable, Vec>) { fn is_constant(assignee: &TypedAssignee) -> bool { match assignee { TypedAssignee::Identifier(_) => true, - TypedAssignee::Select(box assignee, box index) => match index.as_inner() { + TypedAssignee::Select(assignee, index) => match index.as_inner() { UExpressionInner::Value(_) => is_constant(assignee), _ => false, }, - TypedAssignee::Member(box assignee, _) => is_constant(assignee), - TypedAssignee::Element(box assignee, _) => is_constant(assignee), + TypedAssignee::Member(ref assignee, _) => is_constant(assignee), + TypedAssignee::Element(ref assignee, _) => is_constant(assignee), } } @@ -616,7 +617,7 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for VariableWriteRemover { .annotate(bitwidth) .into(), Type::Array(array_type) => ArrayExpression::identifier(variable.id.clone()) - .annotate(*array_type.ty, *array_type.size) + .annotate(array_type) .into(), Type::Struct(members) => StructExpression::identifier(variable.id.clone()) .annotate(members) @@ -633,9 +634,7 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for VariableWriteRemover { let indices = indices .into_iter() .map(|a| match a { - Access::Select(box i) => { - Ok(Access::Select(box self.fold_uint_expression(i)?)) - } + Access::Select(i) => Ok(Access::Select(self.fold_uint_expression(i)?)), a => Ok(a), }) .collect::>()?; diff --git a/zokrates_ast/Cargo.toml b/zokrates_ast/Cargo.toml index 39ba46946..38d79d775 100644 --- a/zokrates_ast/Cargo.toml +++ b/zokrates_ast/Cargo.toml @@ -9,6 +9,7 @@ bellman = ["zokrates_field/bellman", "pairing_ce", "zokrates_embed/bellman"] ark = ["ark-bls12-377", "zokrates_embed/ark"] [dependencies] +log = "0.4" byteorder = "1.4.3" zokrates_pest_ast = { version = "0.3.0", path = "../zokrates_pest_ast" } cfg-if = "0.1" diff --git a/zokrates_ast/src/common/embed.rs b/zokrates_ast/src/common/embed.rs index 18e902716..de5d3ad07 100644 --- a/zokrates_ast/src/common/embed.rs +++ b/zokrates_ast/src/common/embed.rs @@ -301,7 +301,7 @@ impl FlatEmbed { ); assert_eq!(gen.len(), assignment.0.len()); - gen.map(|g| *assignment.0.get(&g).unwrap() as u32).collect() + gen.map(|g| *assignment.0.get(&g).unwrap()).collect() } pub fn id(&self) -> &'static str { @@ -355,15 +355,11 @@ pub fn sha256_round<'ast, T: Field>( let cs_indices = 0..variable_count; // indices of the arguments to the function // apply an offset of `variable_count` to get the indice of our dummy `input` argument - let input_argument_indices: Vec<_> = input_indices - .clone() - .into_iter() - .map(|i| i + variable_count) - .collect(); + let input_argument_indices: Vec<_> = + input_indices.clone().map(|i| i + variable_count).collect(); // apply an offset of `variable_count` to get the indice of our dummy `current_hash` argument let current_hash_argument_indices: Vec<_> = current_hash_indices .clone() - .into_iter() .map(|i| i + variable_count) .collect(); // define parameters to the function based on the variables diff --git a/zokrates_ast/src/common/expressions.rs b/zokrates_ast/src/common/expressions.rs index 4b61b1f82..01481ce6a 100644 --- a/zokrates_ast/src/common/expressions.rs +++ b/zokrates_ast/src/common/expressions.rs @@ -23,8 +23,8 @@ impl BinaryExpression { pub fn new(left: L, right: R) -> Self { Self { span: None, - left: box left, - right: box right, + left: Box::new(left), + right: Box::new(right), operator: PhantomData, output: PhantomData, } @@ -72,7 +72,7 @@ impl UnaryExpression { pub fn new(inner: In) -> Self { Self { span: None, - inner: box inner, + inner: Box::new(inner), operator: PhantomData, output: PhantomData, } diff --git a/zokrates_ast/src/flat/mod.rs b/zokrates_ast/src/flat/mod.rs index e1a86e7a9..44bd42573 100644 --- a/zokrates_ast/src/flat/mod.rs +++ b/zokrates_ast/src/flat/mod.rs @@ -386,16 +386,10 @@ impl FlatExpression { FlatExpression::Add(ref e) => e.left.is_linear() && e.right.is_linear(), FlatExpression::Sub(ref e) => e.left.is_linear() && e.right.is_linear(), FlatExpression::Mult(ref e) => matches!( - (&e.left, &e.right), - (box FlatExpression::Value(_), box FlatExpression::Value(_)) - | ( - box FlatExpression::Value(_), - box FlatExpression::Identifier(_) - ) - | ( - box FlatExpression::Identifier(_), - box FlatExpression::Value(_) - ) + (&*e.left, &*e.right), + (FlatExpression::Value(_), FlatExpression::Value(_)) + | (FlatExpression::Value(_), FlatExpression::Identifier(_)) + | (FlatExpression::Identifier(_), FlatExpression::Value(_)) ), } } diff --git a/zokrates_ast/src/ir/expression.rs b/zokrates_ast/src/ir/expression.rs index 5394b6eac..b3dd59478 100644 --- a/zokrates_ast/src/ir/expression.rs +++ b/zokrates_ast/src/ir/expression.rs @@ -29,6 +29,7 @@ impl WithSpan for QuadComb { } impl QuadComb { + #[allow(clippy::result_large_err)] pub fn try_linear(self) -> Result, Self> { // identify `(k * ~ONE) * (lincomb)` and `(lincomb) * (k * ~ONE)` and return (k * lincomb) // if not, error out with the input diff --git a/zokrates_ast/src/ir/mod.rs b/zokrates_ast/src/ir/mod.rs index 2adbd802c..e3aecebf1 100644 --- a/zokrates_ast/src/ir/mod.rs +++ b/zokrates_ast/src/ir/mod.rs @@ -115,6 +115,7 @@ impl<'ast, T: Field> fmt::Display for BlockStatement<'ast, T> { } } +#[allow(clippy::large_enum_variant)] #[derive(Debug, Serialize, Deserialize, Clone, Derivative)] #[derivative(Hash, PartialEq, Eq)] pub enum Statement<'ast, T> { diff --git a/zokrates_ast/src/lib.rs b/zokrates_ast/src/lib.rs index 797b85619..084173f84 100644 --- a/zokrates_ast/src/lib.rs +++ b/zokrates_ast/src/lib.rs @@ -1,5 +1,3 @@ -#![feature(box_patterns, box_syntax)] - pub mod common; pub mod flat; pub mod ir; diff --git a/zokrates_ast/src/typed/abi.rs b/zokrates_ast/src/typed/abi.rs index 7c9d995fa..88a0da9e4 100644 --- a/zokrates_ast/src/typed/abi.rs +++ b/zokrates_ast/src/typed/abi.rs @@ -22,7 +22,7 @@ impl Abi { ConcreteSignature { generics: vec![], inputs: self.inputs.iter().map(|i| i.ty.clone()).collect(), - output: box self.output.clone(), + output: Box::new(self.output.clone()), } } } diff --git a/zokrates_ast/src/typed/folder.rs b/zokrates_ast/src/typed/folder.rs index bc05d026f..424109bf3 100644 --- a/zokrates_ast/src/typed/folder.rs +++ b/zokrates_ast/src/typed/folder.rs @@ -167,8 +167,8 @@ pub trait Folder<'ast, T: Field>: Sized { fn fold_array_type(&mut self, t: ArrayType<'ast, T>) -> ArrayType<'ast, T> { ArrayType { - ty: box self.fold_type(*t.ty), - size: box self.fold_uint_expression(*t.size), + ty: Box::new(self.fold_type(*t.ty)), + size: Box::new(self.fold_uint_expression(*t.size)), } } @@ -189,7 +189,7 @@ pub trait Folder<'ast, T: Field>: Sized { .members .into_iter() .map(|m| StructMember { - ty: box self.fold_type(*m.ty), + ty: Box::new(self.fold_type(*m.ty)), ..m }) .collect(), @@ -213,8 +213,8 @@ pub trait Folder<'ast, T: Field>: Sized { t: DeclarationArrayType<'ast, T>, ) -> DeclarationArrayType<'ast, T> { DeclarationArrayType { - ty: box self.fold_declaration_type(*t.ty), - size: box self.fold_declaration_constant(*t.size), + ty: Box::new(self.fold_declaration_type(*t.ty)), + size: Box::new(self.fold_declaration_constant(*t.size)), } } @@ -245,7 +245,7 @@ pub trait Folder<'ast, T: Field>: Sized { .members .into_iter() .map(|m| DeclarationStructMember { - ty: box self.fold_declaration_type(*m.ty), + ty: Box::new(self.fold_declaration_type(*m.ty)), ..m }) .collect(), @@ -1559,7 +1559,7 @@ fn fold_signature<'ast, T: Field, F: Folder<'ast, T>>( .into_iter() .map(|o| f.fold_declaration_type(o)) .collect(), - output: box f.fold_declaration_type(*s.output), + output: Box::new(f.fold_declaration_type(*s.output)), } } @@ -1584,7 +1584,7 @@ pub fn fold_array_expression<'ast, T: Field, F: Folder<'ast, T>>( ArrayExpression { inner: f.fold_array_expression_inner(&ty, e.inner), - ty: box ty, + ty: Box::new(ty), } } @@ -1709,12 +1709,13 @@ pub fn fold_assignee<'ast, T: Field, F: Folder<'ast, T>>( ) -> TypedAssignee<'ast, T> { match a { TypedAssignee::Identifier(v) => TypedAssignee::Identifier(f.fold_variable(v)), - TypedAssignee::Select(box a, box index) => { - TypedAssignee::Select(box f.fold_assignee(a), box f.fold_uint_expression(index)) - } - TypedAssignee::Member(box s, m) => TypedAssignee::Member(box f.fold_assignee(s), m), - TypedAssignee::Element(box s, index) => { - TypedAssignee::Element(box f.fold_assignee(s), index) + TypedAssignee::Select(a, index) => TypedAssignee::Select( + Box::new(f.fold_assignee(*a)), + Box::new(f.fold_uint_expression(*index)), + ), + TypedAssignee::Member(s, m) => TypedAssignee::Member(Box::new(f.fold_assignee(*s)), m), + TypedAssignee::Element(s, index) => { + TypedAssignee::Element(Box::new(f.fold_assignee(*s)), index) } } } diff --git a/zokrates_ast/src/typed/integer.rs b/zokrates_ast/src/typed/integer.rs index 8f8b11915..d5ba9ad58 100644 --- a/zokrates_ast/src/typed/integer.rs +++ b/zokrates_ast/src/typed/integer.rs @@ -110,7 +110,7 @@ impl<'ast, T: Clone> IntegerInference for StructType<'ast, T> { .zip(other.members.into_iter()) .map(|(m_t, m_u)| match m_t.ty.get_common_pattern(*m_u.ty) { Ok(ty) => DeclarationStructMember { - ty: box ty, + ty: Box::new(ty), id: m_t.id, }, Err(..) => unreachable!( @@ -590,7 +590,8 @@ impl<'ast, T: Field> FieldElementExpression<'ast, T> { }) .collect::, _>>()?; Ok(FieldElementExpression::select( - ArrayExpression::value(values).annotate(Type::FieldElement, size), + ArrayExpression::value(values) + .annotate(ArrayType::new(Type::FieldElement, size)), index, )) } @@ -712,7 +713,8 @@ impl<'ast, T: Field> UExpression<'ast, T> { }) .collect::, _>>()?; Ok(UExpression::select( - ArrayExpression::value(values).annotate(Type::Uint(*bitwidth), size), + ArrayExpression::value(values) + .annotate(ArrayType::new(Type::Uint(*bitwidth), size)), index, )) } @@ -772,12 +774,16 @@ impl<'ast, T: Field> ArrayExpression<'ast, T> { let inner_ty = res.value[0].get_type().0; - Ok(ArrayExpressionInner::Value(res).annotate(inner_ty, *array_ty.size)) + let array_ty = ArrayType::new(inner_ty, *array_ty.size); + + Ok(ArrayExpressionInner::Value(res).annotate(array_ty)) } ArrayExpressionInner::Repeat(r) => { match &*target_array_ty.ty { GType::Int => { - Ok(ArrayExpressionInner::Repeat(r).annotate(Type::Int, *array_ty.size)) + let array_ty = ArrayType::new(Type::Int, *array_ty.size); + + Ok(ArrayExpressionInner::Repeat(r).annotate(array_ty)) } // try to align the repeated element to the target type t => TypedExpression::align_to_type(*r.e, t) @@ -785,16 +791,16 @@ impl<'ast, T: Field> ArrayExpression<'ast, T> { let ty = e.get_type().clone(); ArrayExpressionInner::Repeat(RepeatExpression::new(e, *r.count)) - .annotate(ty, *array_ty.size) + .annotate(ArrayType::new(ty, *array_ty.size)) }) .map_err(|(e, _)| e), } } a => { if *target_array_ty.ty == *array_ty.ty { - Ok(a.annotate(*array_ty.ty, *array_ty.size)) + Ok(a.annotate(array_ty)) } else { - Err(a.annotate(*array_ty.ty, *array_ty.size).into()) + Err(a.annotate(array_ty).into()) } } } @@ -920,11 +926,12 @@ mod tests { let n: IntExpression = BigUint::from(42usize).into(); let n_a: ArrayExpression = ArrayExpressionInner::Value(ValueExpression::new(vec![n.clone().into()])) - .annotate(Type::Int, 1u32); + .annotate(ArrayType::new(Type::Int, 1u32)); let t: FieldElementExpression = Bn128Field::from(42).into(); let t_a: ArrayExpression = ArrayExpressionInner::Value(ValueExpression::new(vec![t.clone().into()])) - .annotate(Type::FieldElement, 1u32); + .annotate(ArrayType::new(Type::FieldElement, 1u32)); + let i: UExpression = 42u32.into(); let c: BooleanExpression = true.into(); @@ -981,11 +988,11 @@ mod tests { let n: IntExpression = BigUint::from(42usize).into(); let n_a: ArrayExpression = ArrayExpressionInner::Value(ValueExpression::new(vec![n.clone().into()])) - .annotate(Type::Int, 1u32); + .annotate(ArrayType::new(Type::Int, 1u32)); let t: UExpression = 42u32.into(); let t_a: ArrayExpression = ArrayExpressionInner::Value(ValueExpression::new(vec![t.clone().into()])) - .annotate(Type::Uint(UBitwidth::B32), 1u32); + .annotate(ArrayType::new(Type::Uint(UBitwidth::B32), 1u32)); let i: UExpression = 0u32.into(); let c: BooleanExpression = true.into(); diff --git a/zokrates_ast/src/typed/mod.rs b/zokrates_ast/src/typed/mod.rs index 53e2096c5..77789fe06 100644 --- a/zokrates_ast/src/typed/mod.rs +++ b/zokrates_ast/src/typed/mod.rs @@ -405,6 +405,20 @@ pub enum TypedAssignee<'ast, T> { Element(Box>, u32), } +impl<'ast, T> TypedAssignee<'ast, T> { + pub fn select(self, index: UExpression<'ast, T>) -> Self { + Self::Select(Box::new(self), Box::new(index)) + } + + pub fn member(self, member: MemberId) -> Self { + Self::Member(Box::new(self), member) + } + + pub fn element(self, index: u32) -> Self { + Self::Element(Box::new(self), index) + } +} + #[derive(Clone, PartialEq, Hash, Eq, Debug, PartialOrd, Ord)] pub struct TypedSpread<'ast, T> { pub array: ArrayExpression<'ast, T>, @@ -1072,7 +1086,7 @@ impl<'ast, T, E> BlockExpression<'ast, T, E> { BlockExpression { span: None, statements, - value: box value, + value: Box::new(value), } } } @@ -1172,7 +1186,7 @@ impl<'ast, T, E> MemberExpression<'ast, T, E> { pub fn new(struc: StructExpression<'ast, T>, id: MemberId) -> Self { MemberExpression { span: None, - struc: box struc, + struc: Box::new(struc), id, ty: PhantomData, } @@ -1200,8 +1214,8 @@ impl<'ast, T, E> SelectExpression<'ast, T, E> { pub fn new(array: ArrayExpression<'ast, T>, index: UExpression<'ast, T>) -> Self { SelectExpression { span: None, - array: box array, - index: box index, + array: Box::new(array), + index: Box::new(index), ty: PhantomData, } } @@ -1228,7 +1242,7 @@ impl<'ast, T, E> ElementExpression<'ast, T, E> { pub fn new(tuple: TupleExpression<'ast, T>, index: u32) -> Self { ElementExpression { span: None, - tuple: box tuple, + tuple: Box::new(tuple), index, ty: PhantomData, } @@ -1268,9 +1282,9 @@ impl<'ast, T, E> ConditionalExpression<'ast, T, E> { ) -> Self { ConditionalExpression { span: None, - condition: box condition, - consequence: box consequence, - alternative: box alternative, + condition: Box::new(condition), + consequence: Box::new(consequence), + alternative: Box::new(alternative), kind, } } @@ -1387,9 +1401,9 @@ impl<'ast, T: Field> From> for TupleExpression<'ast, T> { _ => unreachable!(), } } - TypedAssignee::Select(box a, box index) => TupleExpression::select(a.into(), index), - TypedAssignee::Member(box a, id) => TupleExpression::member(a.into(), id), - TypedAssignee::Element(box a, index) => TupleExpression::element(a.into(), index), + TypedAssignee::Select(a, index) => TupleExpression::select((*a).into(), *index), + TypedAssignee::Member(a, id) => TupleExpression::member((*a).into(), id), + TypedAssignee::Element(a, index) => TupleExpression::element((*a).into(), index), } } } @@ -1404,9 +1418,9 @@ impl<'ast, T: Field> From> for StructExpression<'ast, T> _ => unreachable!(), } } - TypedAssignee::Select(box a, box index) => StructExpression::select(a.into(), index), - TypedAssignee::Member(box a, id) => StructExpression::member(a.into(), id), - TypedAssignee::Element(box a, index) => StructExpression::element(a.into(), index), + TypedAssignee::Select(a, index) => StructExpression::select((*a).into(), *index), + TypedAssignee::Member(a, id) => StructExpression::member((*a).into(), id), + TypedAssignee::Element(a, index) => StructExpression::element((*a).into(), index), } } } @@ -1417,13 +1431,13 @@ impl<'ast, T: Field> From> for ArrayExpression<'ast, T> { TypedAssignee::Identifier(v) => { let inner = ArrayExpression::identifier(v.id); match v.ty { - GType::Array(array_ty) => inner.annotate(*array_ty.ty, *array_ty.size), + GType::Array(array_ty) => inner.annotate(array_ty), _ => unreachable!(), } } - TypedAssignee::Select(box a, box index) => ArrayExpression::select(a.into(), index), - TypedAssignee::Member(box a, id) => ArrayExpression::member(a.into(), id), - TypedAssignee::Element(box a, index) => ArrayExpression::element(a.into(), index), + TypedAssignee::Select(a, index) => ArrayExpression::select((*a).into(), *index), + TypedAssignee::Member(a, id) => ArrayExpression::member((*a).into(), id), + TypedAssignee::Element(a, index) => ArrayExpression::element((*a).into(), index), } } } @@ -1432,13 +1446,9 @@ impl<'ast, T: Field> From> for FieldElementExpression<'as fn from(assignee: TypedAssignee<'ast, T>) -> Self { match assignee { TypedAssignee::Identifier(v) => FieldElementExpression::identifier(v.id), - TypedAssignee::Element(box a, index) => { - FieldElementExpression::element(a.into(), index) - } - TypedAssignee::Member(box a, id) => FieldElementExpression::member(a.into(), id), - TypedAssignee::Select(box a, box index) => { - FieldElementExpression::select(a.into(), index) - } + TypedAssignee::Element(a, index) => FieldElementExpression::element((*a).into(), index), + TypedAssignee::Member(a, id) => FieldElementExpression::member((*a).into(), id), + TypedAssignee::Select(a, index) => FieldElementExpression::select((*a).into(), *index), } } } @@ -1737,11 +1747,7 @@ impl<'ast, T: Field> ArrayValueExpression<'ast, T> { } a => (0..size.value) .map(|i| { - Some(U::select( - a.clone() - .annotate(*array_ty.ty.clone(), *array_ty.size.clone()), - i as u32, - )) + Some(U::select(a.clone().annotate(array_ty.clone()), i as u32)) }) .collect(), } @@ -1780,13 +1786,9 @@ pub enum ArrayExpressionInner<'ast, T> { } impl<'ast, T> ArrayExpressionInner<'ast, T> { - pub fn annotate>>( - self, - ty: Type<'ast, T>, - size: S, - ) -> ArrayExpression<'ast, T> { + pub fn annotate(self, ty: ArrayType<'ast, T>) -> ArrayExpression<'ast, T> { ArrayExpression { - ty: box (ty, size.into()).into(), + ty: Box::new(ty), inner: self, } } @@ -1808,13 +1810,15 @@ impl<'ast, T: Clone> ArrayExpression<'ast, T> { ) -> Self { let inner = array.inner_type().clone(); let size = to.clone() - from.clone(); - ArrayExpressionInner::Slice(SliceExpression::new(array, from, to)).annotate(inner, size) + let array_ty = ArrayType::new(inner, size); + ArrayExpressionInner::Slice(SliceExpression::new(array, from, to)).annotate(array_ty) } pub fn repeat(e: TypedExpression<'ast, T>, count: UExpression<'ast, T>) -> Self { let inner = e.get_type().clone(); let size = count.clone(); - ArrayExpressionInner::Repeat(RepeatExpression::new(e, count)).annotate(inner, size) + let array_ty = ArrayType::new(inner, size); + ArrayExpressionInner::Repeat(RepeatExpression::new(e, count)).annotate(array_ty) } } @@ -2205,9 +2209,7 @@ impl<'ast, T: Field> From> for TypedExpression<'ast, T> { match v.get_type() { Type::FieldElement => FieldElementExpression::identifier(v.id).into(), Type::Boolean => BooleanExpression::identifier(v.id).into(), - Type::Array(ty) => ArrayExpression::identifier(v.id) - .annotate(*ty.ty, *ty.size) - .into(), + Type::Array(ty) => ArrayExpression::identifier(v.id).annotate(ty).into(), Type::Struct(ty) => StructExpression::identifier(v.id).annotate(ty).into(), Type::Tuple(ty) => TupleExpression::identifier(v.id).annotate(ty).into(), Type::Uint(w) => UExpression::identifier(v.id).annotate(w).into(), @@ -2835,7 +2837,7 @@ impl<'ast, T: fmt::Display + Clone> Expr<'ast, T> for StructExpression<'ast, T> } } -impl<'ast, T: fmt::Display + Clone> Expr<'ast, T> for ArrayExpression<'ast, T> { +impl<'ast, T: Clone + fmt::Display> Expr<'ast, T> for ArrayExpression<'ast, T> { type Inner = ArrayExpressionInner<'ast, T>; type Ty = ArrayType<'ast, T>; type ConcreteTy = ConcreteArrayType; @@ -3040,22 +3042,21 @@ impl<'ast, T> Conditional<'ast, T> for UExpression<'ast, T> { } } -impl<'ast, T: Clone> Conditional<'ast, T> for ArrayExpression<'ast, T> { +impl<'ast, T: Clone + fmt::Display> Conditional<'ast, T> for ArrayExpression<'ast, T> { fn conditional( condition: BooleanExpression<'ast, T>, consequence: Self, alternative: Self, kind: ConditionalKind, ) -> Self { - let ty = consequence.inner_type().clone(); - let size = consequence.size(); + let ty = consequence.ty().clone(); ArrayExpressionInner::Conditional(ConditionalExpression::new( condition, consequence, alternative, kind, )) - .annotate(ty, size) + .annotate(ty) } } @@ -3144,13 +3145,9 @@ impl<'ast, T: Clone> Select<'ast, T> for UExpression<'ast, T> { impl<'ast, T: Clone> Select<'ast, T> for ArrayExpression<'ast, T> { fn select>>(array: ArrayExpression<'ast, T>, index: I) -> Self { - let (ty, size) = match array.inner_type() { - Type::Array(array_type) => (array_type.ty.clone(), array_type.size.clone()), - _ => unreachable!(), - }; + let array_ty = array.inner_type().clone().try_into().unwrap(); - ArrayExpressionInner::Select(SelectExpression::new(array, index.into())) - .annotate(*ty, *size) + ArrayExpressionInner::Select(SelectExpression::new(array, index.into())).annotate(array_ty) } } @@ -3194,56 +3191,60 @@ impl<'ast, T> Member<'ast, T> for BooleanExpression<'ast, T> { impl<'ast, T: Clone> Member<'ast, T> for UExpression<'ast, T> { fn member(s: StructExpression<'ast, T>, id: MemberId) -> Self { - let ty = s.ty().members.iter().find(|member| id == member.id); - let bitwidth = match ty { - Some(crate::typed::types::StructMember { - ty: box Type::Uint(bitwidth), - .. - }) => *bitwidth, - _ => unreachable!(), - }; + let ty = *s + .ty() + .members + .iter() + .find(|member| id == member.id) + .unwrap() + .ty + .clone(); + let bitwidth: UBitwidth = ty.try_into().unwrap(); UExpressionInner::Member(MemberExpression::new(s, id)).annotate(bitwidth) } } impl<'ast, T: Clone> Member<'ast, T> for ArrayExpression<'ast, T> { fn member(s: StructExpression<'ast, T>, id: MemberId) -> Self { - let ty = s.ty().members.iter().find(|member| id == member.id); - let (ty, size) = match ty { - Some(crate::typed::types::StructMember { - ty: box Type::Array(array_ty), - .. - }) => (*array_ty.ty.clone(), array_ty.size.clone()), - _ => unreachable!(), - }; - ArrayExpressionInner::Member(MemberExpression::new(s, id)).annotate(ty, *size) + let ty = *s + .ty() + .members + .iter() + .find(|member| id == member.id) + .unwrap() + .ty + .clone(); + let array_ty: ArrayType<'ast, T> = ty.try_into().unwrap(); + ArrayExpressionInner::Member(MemberExpression::new(s, id)).annotate(array_ty) } } impl<'ast, T: Clone> Member<'ast, T> for StructExpression<'ast, T> { fn member(s: StructExpression<'ast, T>, id: MemberId) -> Self { - let ty = s.ty().members.iter().find(|member| id == member.id); - let struct_ty = match ty { - Some(crate::typed::types::StructMember { - ty: box Type::Struct(struct_ty), - .. - }) => struct_ty.clone(), - _ => unreachable!(), - }; + let ty = *s + .ty() + .members + .iter() + .find(|member| id == member.id) + .unwrap() + .ty + .clone(); + let struct_ty = ty.try_into().unwrap(); StructExpressionInner::Member(MemberExpression::new(s, id)).annotate(struct_ty) } } impl<'ast, T: Clone> Member<'ast, T> for TupleExpression<'ast, T> { fn member(s: StructExpression<'ast, T>, id: MemberId) -> Self { - let ty = s.ty().members.iter().find(|member| id == member.id); - let tuple_ty = match ty { - Some(crate::typed::types::StructMember { - ty: box Type::Tuple(tuple_ty), - .. - }) => tuple_ty.clone(), - _ => unreachable!(), - }; + let ty = *s + .ty() + .members + .iter() + .find(|member| id == member.id) + .unwrap() + .ty + .clone(); + let tuple_ty = ty.try_into().unwrap(); TupleExpressionInner::Member(MemberExpression::new(s, id)).annotate(tuple_ty) } } @@ -3277,12 +3278,9 @@ impl<'ast, T: Clone> Element<'ast, T> for UExpression<'ast, T> { impl<'ast, T: Clone> Element<'ast, T> for ArrayExpression<'ast, T> { fn element(s: TupleExpression<'ast, T>, id: u32) -> Self { - let ty = &s.ty().elements[id as usize]; - let (ty, size) = match ty { - Type::Array(array_ty) => (*array_ty.ty.clone(), array_ty.size.clone()), - _ => unreachable!(), - }; - ArrayExpressionInner::Element(ElementExpression::new(s, id)).annotate(ty, *size) + let ty = s.ty().elements[id as usize].clone(); + let array_ty = ty.try_into().unwrap(); + ArrayExpressionInner::Element(ElementExpression::new(s, id)).annotate(array_ty) } } @@ -3442,8 +3440,7 @@ impl<'ast, T: Field> Block<'ast, T> for UExpression<'ast, T> { impl<'ast, T: Field> Block<'ast, T> for ArrayExpression<'ast, T> { fn block(statements: Vec>, value: Self) -> Self { let array_ty = value.ty().clone(); - ArrayExpressionInner::Block(BlockExpression::new(statements, value)) - .annotate(*array_ty.ty, *array_ty.size) + ArrayExpressionInner::Block(BlockExpression::new(statements, value)).annotate(array_ty) } } @@ -3561,7 +3558,7 @@ impl<'ast, T: Field> Constant for ArrayExpression<'ast, T> { .map(|e| e.into()) .collect::>(), ) - .annotate(*array_ty.ty, *array_ty.size), + .annotate(array_ty), ArrayExpressionInner::Slice(e) => { let from = match e.from.into_inner() { UExpressionInner::Value(from) => from.value as usize, @@ -3586,7 +3583,7 @@ impl<'ast, T: Field> Constant for ArrayExpression<'ast, T> { .take(to - from) .collect::>(), ) - .annotate(*array_ty.ty, *array_ty.size) + .annotate(array_ty) } ArrayExpressionInner::Repeat(e) => { let count = match e.count.into_inner() { @@ -3597,7 +3594,7 @@ impl<'ast, T: Field> Constant for ArrayExpression<'ast, T> { let e = e.e.into_canonical_constant(); ArrayExpression::value(vec![TypedExpressionOrSpread::Expression(e); count]) - .annotate(*array_ty.ty, *array_ty.size) + .annotate(array_ty) } _ => unreachable!(), } diff --git a/zokrates_ast/src/typed/result_folder.rs b/zokrates_ast/src/typed/result_folder.rs index 531d45275..d8541add0 100644 --- a/zokrates_ast/src/typed/result_folder.rs +++ b/zokrates_ast/src/typed/result_folder.rs @@ -334,8 +334,8 @@ pub trait ResultFolder<'ast, T: Field>: Sized { t: ArrayType<'ast, T>, ) -> Result, Self::Error> { Ok(ArrayType { - ty: box self.fold_type(*t.ty)?, - size: box self.fold_uint_expression(*t.size)?, + ty: Box::new(self.fold_type(*t.ty)?), + size: Box::new(self.fold_uint_expression(*t.size)?), }) } @@ -367,8 +367,10 @@ pub trait ResultFolder<'ast, T: Field>: Sized { .into_iter() .map(|m| { let id = m.id; - self.fold_type(*m.ty) - .map(|ty| StructMember { ty: box ty, id }) + self.fold_type(*m.ty).map(|ty| StructMember { + ty: Box::new(ty), + id, + }) }) .collect::>()?, ..t @@ -394,8 +396,8 @@ pub trait ResultFolder<'ast, T: Field>: Sized { t: DeclarationArrayType<'ast, T>, ) -> Result, Self::Error> { Ok(DeclarationArrayType { - ty: box self.fold_declaration_type(*t.ty)?, - size: box self.fold_declaration_constant(*t.size)?, + ty: Box::new(self.fold_declaration_type(*t.ty)?), + size: Box::new(self.fold_declaration_constant(*t.size)?), }) } @@ -428,7 +430,10 @@ pub trait ResultFolder<'ast, T: Field>: Sized { .map(|m| { let id = m.id; self.fold_declaration_type(*m.ty) - .map(|ty| DeclarationStructMember { ty: box ty, id }) + .map(|ty| DeclarationStructMember { + ty: Box::new(ty), + id, + }) }) .collect::>()?, ..t @@ -958,14 +963,15 @@ pub fn fold_assignee<'ast, T: Field, F: ResultFolder<'ast, T>>( ) -> Result, F::Error> { match a { TypedAssignee::Identifier(v) => Ok(TypedAssignee::Identifier(f.fold_variable(v)?)), - TypedAssignee::Select(box a, box index) => Ok(TypedAssignee::Select( - box f.fold_assignee(a)?, - box f.fold_uint_expression(index)?, + TypedAssignee::Select(a, index) => Ok(TypedAssignee::Select( + Box::new(f.fold_assignee(*a)?), + Box::new(f.fold_uint_expression(*index)?), + )), + TypedAssignee::Member(s, m) => Ok(TypedAssignee::Member(Box::new(f.fold_assignee(*s)?), m)), + TypedAssignee::Element(s, index) => Ok(TypedAssignee::Element( + Box::new(f.fold_assignee(*s)?), + index, )), - TypedAssignee::Member(box s, m) => Ok(TypedAssignee::Member(box f.fold_assignee(s)?, m)), - TypedAssignee::Element(box s, index) => { - Ok(TypedAssignee::Element(box f.fold_assignee(s)?, index)) - } } } @@ -1683,7 +1689,7 @@ pub fn fold_signature<'ast, T: Field, F: ResultFolder<'ast, T>>( .into_iter() .map(|o| f.fold_declaration_type(o)) .collect::>()?, - output: box f.fold_declaration_type(*s.output)?, + output: Box::new(f.fold_declaration_type(*s.output)?), }) } @@ -1725,7 +1731,7 @@ pub fn fold_array_expression<'ast, T: Field, F: ResultFolder<'ast, T>>( Ok(ArrayExpression { inner: f.fold_array_expression_inner(&ty, e.inner)?, - ty: box ty, + ty: Box::new(ty), }) } diff --git a/zokrates_ast/src/typed/types.rs b/zokrates_ast/src/typed/types.rs index 8ae15de8b..ece3547e7 100644 --- a/zokrates_ast/src/typed/types.rs +++ b/zokrates_ast/src/typed/types.rs @@ -282,7 +282,7 @@ impl<'ast, T> TryInto for DeclarationConstant<'ast, T> { pub type MemberId = String; -#[allow(clippy::derive_hash_xor_eq)] +#[allow(clippy::derived_hash_with_manual_eq)] #[derive(Debug, Clone, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)] pub struct GStructMember { #[serde(rename = "name")] @@ -306,7 +306,7 @@ fn try_from_g_struct_member, U>( ) -> Result, SpecializationError> { Ok(GStructMember { id: t.id, - ty: box try_from_g_type(*t.ty)?, + ty: Box::new(try_from_g_type(*t.ty)?), }) } @@ -324,7 +324,7 @@ impl<'ast, T> From for StructMember<'ast, T> { } } -#[allow(clippy::derive_hash_xor_eq)] +#[allow(clippy::derived_hash_with_manual_eq)] #[derive(Clone, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord, Debug)] pub struct GArrayType { pub size: Box, @@ -372,8 +372,8 @@ fn try_from_g_array_type, U>( t: GArrayType, ) -> Result, SpecializationError> { Ok(GArrayType { - size: box (*t.size).try_into().map_err(|_| SpecializationError)?, - ty: box try_from_g_type(*t.ty)?, + size: Box::new((*t.size).try_into().map_err(|_| SpecializationError)?), + ty: Box::new(try_from_g_type(*t.ty)?), }) } @@ -391,7 +391,7 @@ impl<'ast, T> From for ArrayType<'ast, T> { } } -#[allow(clippy::derive_hash_xor_eq)] +#[allow(clippy::derived_hash_with_manual_eq)] #[derive(Clone, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord, Debug)] pub struct GTupleType { pub elements: Vec>, @@ -470,6 +470,42 @@ impl TryFrom> for GTupleType { } } +impl TryFrom> for GStructType { + type Error = (); + + fn try_from(t: GType) -> Result { + if let GType::Struct(t) = t { + Ok(t) + } else { + Err(()) + } + } +} + +impl TryFrom> for GArrayType { + type Error = (); + + fn try_from(t: GType) -> Result { + if let GType::Array(t) = t { + Ok(t) + } else { + Err(()) + } + } +} + +impl TryFrom> for UBitwidth { + type Error = (); + + fn try_from(t: GType) -> Result { + if let GType::Uint(t) = t { + Ok(t) + } else { + Err(()) + } + } +} + #[derive(Debug, Clone, Hash, Serialize, Deserialize, PartialOrd, Ord, Eq, PartialEq)] pub struct StructLocation { #[serde(skip)] @@ -643,7 +679,7 @@ impl fmt::Display for UBitwidth { } } -#[allow(clippy::derive_hash_xor_eq)] +#[allow(clippy::derived_hash_with_manual_eq)] #[derive(Clone, Eq, Hash, PartialOrd, Ord, Debug)] pub enum GType { FieldElement, @@ -821,8 +857,8 @@ impl<'ast, T> From for DeclarationType<'ast, T> { impl> From<(GType, U)> for GArrayType { fn from(tup: (GType, U)) -> Self { GArrayType { - ty: box tup.0, - size: box tup.1.into(), + ty: Box::new(tup.0), + size: Box::new(tup.1.into()), } } } @@ -830,8 +866,8 @@ impl> From<(GType, U)> for GArrayType { impl GArrayType { pub fn new>(ty: GType, size: U) -> Self { GArrayType { - ty: box ty, - size: box size.into(), + ty: Box::new(ty), + size: Box::new(size.into()), } } } @@ -1208,8 +1244,12 @@ pub fn specialize_declaration_type< .into_iter() .map(|m| { let id = m.id; - specialize_declaration_type(*m.ty, &inside_generics) - .map(|ty| GStructMember { ty: box ty, id }) + specialize_declaration_type(*m.ty, &inside_generics).map(|ty| { + GStructMember { + ty: Box::new(ty), + id, + } + }) }) .collect::>()?, generics: s0 @@ -1250,7 +1290,7 @@ pub mod signature { Self { generics: vec![], inputs: vec![], - output: box GType::Tuple(GTupleType::new(vec![])), + output: Box::new(GType::Tuple(GTupleType::new(vec![]))), } } } @@ -1399,7 +1439,7 @@ pub mod signature { .into_iter() .map(try_from_g_type) .collect::>()?, - output: box try_from_g_type(*t.output)?, + output: Box::new(try_from_g_type(*t.output)?), }) } diff --git a/zokrates_ast/src/typed/utils/mod.rs b/zokrates_ast/src/typed/utils/mod.rs index 36b6d6aea..ad2c406f0 100644 --- a/zokrates_ast/src/typed/utils/mod.rs +++ b/zokrates_ast/src/typed/utils/mod.rs @@ -1,6 +1,6 @@ use super::{ ArrayExpression, ArrayExpressionInner, BooleanExpression, Conditional, ConditionalKind, Expr, - FieldElementExpression, Id, Identifier, Select, Typed, TypedExpression, + FieldElementExpression, GArrayType, Id, Identifier, Select, Typed, TypedExpression, TypedExpressionOrSpread, UBitwidth, UExpression, ValueExpression, }; @@ -23,13 +23,15 @@ pub fn a< values: [E; N], ) -> ArrayExpression<'ast, T> { let ty = values[0].get_type(); + + let array_ty = GArrayType::new(ty, N as u32); ArrayExpression::value( values .into_iter() .map(|e| TypedExpressionOrSpread::Expression(e.into())) .collect(), ) - .annotate(ty, N as u32) + .annotate(array_ty) } pub fn u_32<'ast, T: Field, U: TryInto>(v: U) -> UExpression<'ast, T> { diff --git a/zokrates_ast/src/untyped/from_ast.rs b/zokrates_ast/src/untyped/from_ast.rs index 88c12d6ce..78e02b7d0 100644 --- a/zokrates_ast/src/untyped/from_ast.rs +++ b/zokrates_ast/src/untyped/from_ast.rs @@ -390,85 +390,85 @@ impl<'ast> From> for untyped::ExpressionNode<'ast> use crate::untyped::NodeValue; match expression.op { pest::BinaryOperator::Add => untyped::Expression::Add( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::Sub => untyped::Expression::Sub( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::Mul => untyped::Expression::Mult( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::Div => untyped::Expression::Div( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::Rem => untyped::Expression::Rem( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::Eq => untyped::Expression::Eq( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::Lt => untyped::Expression::Lt( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::Lte => untyped::Expression::Le( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::Gt => untyped::Expression::Gt( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::Gte => untyped::Expression::Ge( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::And => untyped::Expression::And( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::Or => untyped::Expression::Or( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::Pow => untyped::Expression::Pow( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::BitXor => untyped::Expression::BitXor( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::LeftShift => untyped::Expression::LeftShift( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::RightShift => untyped::Expression::RightShift( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::BitAnd => untyped::Expression::BitAnd( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), pest::BinaryOperator::BitOr => untyped::Expression::BitOr( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ), // rewrite (a != b)` as `!(a == b)` - pest::BinaryOperator::NotEq => untyped::Expression::Not( - box untyped::Expression::Eq( - box untyped::ExpressionNode::from(*expression.left), - box untyped::ExpressionNode::from(*expression.right), + pest::BinaryOperator::NotEq => untyped::Expression::Not(Box::new( + untyped::Expression::Eq( + Box::new(untyped::ExpressionNode::from(*expression.left)), + Box::new(untyped::ExpressionNode::from(*expression.right)), ) .span(expression.span.clone()), - ), + )), } .span(expression.span) } @@ -477,22 +477,22 @@ impl<'ast> From> for untyped::ExpressionNode<'ast> impl<'ast> From> for untyped::ExpressionNode<'ast> { fn from(expression: pest::IfElseExpression<'ast>) -> untyped::ExpressionNode<'ast> { use crate::untyped::NodeValue; - untyped::Expression::Conditional(box ConditionalExpression { - condition: box untyped::ExpressionNode::from(*expression.condition), + untyped::Expression::Conditional(Box::new(ConditionalExpression { + condition: Box::new(untyped::ExpressionNode::from(*expression.condition)), consequence_statements: expression .consequence_statements .into_iter() .map(untyped::StatementNode::from) .collect(), - consequence: box untyped::ExpressionNode::from(*expression.consequence), + consequence: Box::new(untyped::ExpressionNode::from(*expression.consequence)), alternative_statements: expression .alternative_statements .into_iter() .map(untyped::StatementNode::from) .collect(), - alternative: box untyped::ExpressionNode::from(*expression.alternative), + alternative: Box::new(untyped::ExpressionNode::from(*expression.alternative)), kind: untyped::ConditionalKind::IfElse, - }) + })) .span(expression.span) } } @@ -500,14 +500,14 @@ impl<'ast> From> for untyped::ExpressionNode<'ast> impl<'ast> From> for untyped::ExpressionNode<'ast> { fn from(expression: pest::TernaryExpression<'ast>) -> untyped::ExpressionNode<'ast> { use crate::untyped::NodeValue; - untyped::Expression::Conditional(box ConditionalExpression { - condition: box untyped::ExpressionNode::from(*expression.condition), + untyped::Expression::Conditional(Box::new(ConditionalExpression { + condition: Box::new(untyped::ExpressionNode::from(*expression.condition)), consequence_statements: vec![], - consequence: box untyped::ExpressionNode::from(*expression.consequence), + consequence: Box::new(untyped::ExpressionNode::from(*expression.consequence)), alternative_statements: vec![], - alternative: box untyped::ExpressionNode::from(*expression.alternative), + alternative: Box::new(untyped::ExpressionNode::from(*expression.alternative)), kind: untyped::ConditionalKind::Ternary, - }) + })) .span(expression.span) } } @@ -617,7 +617,8 @@ impl<'ast> From> for untyped::ExpressionN let value = untyped::ExpressionNode::from(*initializer.value); let count = untyped::ExpressionNode::from(*initializer.count); - untyped::Expression::ArrayInitializer(box value, box count).span(initializer.span) + untyped::Expression::ArrayInitializer(Box::new(value), Box::new(count)) + .span(initializer.span) } } @@ -674,18 +675,20 @@ impl<'ast> From> for untyped::ExpressionNode<'ast> ) .span(a.span), pest::Access::Select(a) => untyped::Expression::Select( - box acc, - box untyped::RangeOrExpression::from(a.expression), + Box::new(acc), + Box::new(untyped::RangeOrExpression::from(a.expression)), ) .span(a.span), pest::Access::Dot(m) => match m.inner { pest::IdentifierOrDecimal::Identifier(id) => { - untyped::Expression::Member(box acc, box id.span.as_str()).span(m.span) - } - pest::IdentifierOrDecimal::Decimal(id) => { - untyped::Expression::Element(box acc, id.span.as_str().parse().unwrap()) + untyped::Expression::Member(Box::new(acc), Box::new(id.span.as_str())) .span(m.span) } + pest::IdentifierOrDecimal::Decimal(id) => untyped::Expression::Element( + Box::new(acc), + id.span.as_str().parse().unwrap(), + ) + .span(m.span), }, }) } @@ -783,15 +786,15 @@ impl<'ast> From> for untyped::AssigneeNode<'ast> { assignee.accesses.into_iter().fold(a, |acc, s| { match s { pest::AssigneeAccess::Select(s) => untyped::Assignee::Select( - box acc, - box untyped::RangeOrExpression::from(s.expression), + Box::new(acc), + Box::new(untyped::RangeOrExpression::from(s.expression)), ), pest::AssigneeAccess::Dot(a) => match a.inner { pest::IdentifierOrDecimal::Identifier(id) => { - untyped::Assignee::Member(box acc, box id.span.as_str()) + untyped::Assignee::Member(Box::new(acc), Box::new(id.span.as_str())) } pest::IdentifierOrDecimal::Decimal(id) => { - untyped::Assignee::Element(box acc, id.span.as_str().parse().unwrap()) + untyped::Assignee::Element(Box::new(acc), id.span.as_str().parse().unwrap()) } }, } @@ -1032,29 +1035,33 @@ mod tests { ( "field[2]", untyped::UnresolvedType::Array( - box untyped::UnresolvedType::FieldElement.mock(), + Box::new(untyped::UnresolvedType::FieldElement.mock()), untyped::Expression::IntConstant(2usize.into()).mock(), ), ), ( "field[2][3]", untyped::UnresolvedType::Array( - box untyped::UnresolvedType::Array( - box untyped::UnresolvedType::FieldElement.mock(), - untyped::Expression::IntConstant(3usize.into()).mock(), - ) - .mock(), + Box::new( + untyped::UnresolvedType::Array( + Box::new(untyped::UnresolvedType::FieldElement.mock()), + untyped::Expression::IntConstant(3usize.into()).mock(), + ) + .mock(), + ), untyped::Expression::IntConstant(2usize.into()).mock(), ), ), ( "bool[2][3u32]", untyped::UnresolvedType::Array( - box untyped::UnresolvedType::Array( - box untyped::UnresolvedType::Boolean.mock(), - untyped::Expression::U32Constant(3u32).mock(), - ) - .mock(), + Box::new( + untyped::UnresolvedType::Array( + Box::new(untyped::UnresolvedType::Boolean.mock()), + untyped::Expression::U32Constant(3u32).mock(), + ) + .mock(), + ), untyped::Expression::IntConstant(2usize.into()).mock(), ), ), @@ -1099,59 +1106,59 @@ mod tests { ( "a[3]", untyped::Expression::Select( - box untyped::Expression::Identifier("a").into(), - box untyped::RangeOrExpression::Expression( + Box::new(untyped::Expression::Identifier("a").into()), + Box::new(untyped::RangeOrExpression::Expression( untyped::Expression::IntConstant(3usize.into()).into(), ), - ), + )), ), ( "a[3][4]", untyped::Expression::Select( - box untyped::Expression::Select( - box untyped::Expression::Identifier("a").into(), - box untyped::RangeOrExpression::Expression( + Box::new(untyped::Expression::Select( + Box::new(untyped::Expression::Identifier("a").into()), + Box::new(untyped::RangeOrExpression::Expression( untyped::Expression::IntConstant(3usize.into()).into(), - ), + )), ) - .into(), - box untyped::RangeOrExpression::Expression( + .into()), + Box::new(untyped::RangeOrExpression::Expression( untyped::Expression::IntConstant(4usize.into()).into(), - ), + )), ), ), ( "a(3)[4]", untyped::Expression::Select( - box untyped::Expression::FunctionCall( - box untyped::Expression::Identifier("a").mock(), + Box::new(untyped::Expression::FunctionCall( + Box::new(untyped::Expression::Identifier("a").mock()), None, vec![untyped::Expression::IntConstant(3usize.into()).into()], ) - .into(), - box untyped::RangeOrExpression::Expression( + .into()), + Box::new(untyped::RangeOrExpression::Expression( untyped::Expression::IntConstant(4usize.into()).into(), - ), + )), ), ), ( "a(3)[4][5]", untyped::Expression::Select( - box untyped::Expression::Select( - box untyped::Expression::FunctionCall( - box untyped::Expression::Identifier("a").mock(), + Box::new(untyped::Expression::Select( + Box::new(untyped::Expression::FunctionCall( + Box::new(untyped::Expression::Identifier("a").mock()), None, vec![untyped::Expression::IntConstant(3usize.into()).into()], ) - .into(), - box untyped::RangeOrExpression::Expression( + .into()), + Box::new(untyped::RangeOrExpression::Expression( untyped::Expression::IntConstant(4usize.into()).into(), - ), + )), ) - .into(), - box untyped::RangeOrExpression::Expression( + .into()), + Box::new(untyped::RangeOrExpression::Expression( untyped::Expression::IntConstant(5usize.into()).into(), - ), + )), ), ), ]; @@ -1172,13 +1179,15 @@ mod tests { assert_eq!( untyped::Module::from(ast), wrap(untyped::Expression::FunctionCall( - box untyped::Expression::Select( - box untyped::Expression::Identifier("a").mock(), - box untyped::RangeOrExpression::Expression( - untyped::Expression::IntConstant(2u32.into()).mock() + Box::new( + untyped::Expression::Select( + Box::new(untyped::Expression::Identifier("a").mock()), + Box::new(untyped::RangeOrExpression::Expression( + untyped::Expression::IntConstant(2u32.into()).mock() + )) ) - ) - .mock(), + .mock() + ), None, vec![untyped::Expression::IntConstant(3u32.into()).mock()], )) @@ -1194,12 +1203,14 @@ mod tests { assert_eq!( untyped::Module::from(ast), wrap(untyped::Expression::FunctionCall( - box untyped::Expression::FunctionCall( - box untyped::Expression::Identifier("a").mock(), - None, - vec![untyped::Expression::IntConstant(2u32.into()).mock()] - ) - .mock(), + Box::new( + untyped::Expression::FunctionCall( + Box::new(untyped::Expression::Identifier("a").mock()), + None, + vec![untyped::Expression::IntConstant(2u32.into()).mock()] + ) + .mock() + ), None, vec![untyped::Expression::IntConstant(3u32.into()).mock()], )) @@ -1261,10 +1272,10 @@ mod tests { span: span.clone(), }), expression: pest::Expression::Postfix(pest::PostfixExpression { - base: box pest::Expression::Identifier(pest::IdentifierExpression { + base: Box::new(pest::Expression::Identifier(pest::IdentifierExpression { value: String::from("foo"), span: span.clone(), - }), + })), accesses: vec![pest::Access::Call(pest::CallAccess { explicit_generics: None, arguments: pest::Arguments { diff --git a/zokrates_ast/src/untyped/types.rs b/zokrates_ast/src/untyped/types.rs index 0f6c4ba45..f2a87ad86 100644 --- a/zokrates_ast/src/untyped/types.rs +++ b/zokrates_ast/src/untyped/types.rs @@ -69,7 +69,7 @@ impl<'ast> fmt::Display for UnresolvedType<'ast> { impl<'ast> UnresolvedType<'ast> { pub fn array(ty: UnresolvedTypeNode<'ast>, size: ExpressionNode<'ast>) -> Self { - UnresolvedType::Array(box ty, size) + UnresolvedType::Array(Box::new(ty), size) } } diff --git a/zokrates_ast/src/zir/identifier.rs b/zokrates_ast/src/zir/identifier.rs index a7ae6b959..3d56b07ef 100644 --- a/zokrates_ast/src/zir/identifier.rs +++ b/zokrates_ast/src/zir/identifier.rs @@ -26,6 +26,20 @@ impl<'ast> Identifier<'ast> { } } +impl<'ast> SourceIdentifier<'ast> { + pub fn select(self, index: u32) -> Self { + Self::Select(Box::new(self), index) + } + + pub fn member(self, member: MemberId) -> Self { + Self::Member(Box::new(self), member) + } + + pub fn element(self, index: u32) -> Self { + Self::Element(Box::new(self), index) + } +} + impl<'ast> fmt::Display for Identifier<'ast> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { @@ -39,9 +53,9 @@ impl<'ast> fmt::Display for SourceIdentifier<'ast> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { SourceIdentifier::Basic(i) => write!(f, "{}", i), - SourceIdentifier::Select(box i, index) => write!(f, "{}~{}", i, index), - SourceIdentifier::Member(box i, m) => write!(f, "{}.{}", i, m), - SourceIdentifier::Element(box i, index) => write!(f, "{}.{}", i, index), + SourceIdentifier::Select(i, index) => write!(f, "{}~{}", i, index), + SourceIdentifier::Member(i, m) => write!(f, "{}.{}", i, m), + SourceIdentifier::Element(i, index) => write!(f, "{}.{}", i, index), } } } diff --git a/zokrates_ast/src/zir/mod.rs b/zokrates_ast/src/zir/mod.rs index dcb4cefaf..a7cc08019 100644 --- a/zokrates_ast/src/zir/mod.rs +++ b/zokrates_ast/src/zir/mod.rs @@ -490,9 +490,9 @@ impl<'ast, T, E> ConditionalExpression<'ast, T, E> { pub fn new(condition: BooleanExpression<'ast, T>, consequence: E, alternative: E) -> Self { ConditionalExpression { span: None, - condition: box condition, - consequence: box consequence, - alternative: box alternative, + condition: Box::new(condition), + consequence: Box::new(consequence), + alternative: Box::new(alternative), } } } @@ -534,7 +534,7 @@ impl<'ast, T, E> SelectExpression<'ast, T, E> { SelectExpression { span: None, array, - index: box index, + index: Box::new(index), } } } diff --git a/zokrates_ast/src/zir/uint.rs b/zokrates_ast/src/zir/uint.rs index dfb0b3ec9..3ace2a152 100644 --- a/zokrates_ast/src/zir/uint.rs +++ b/zokrates_ast/src/zir/uint.rs @@ -153,7 +153,7 @@ impl UMetadata { } pub fn bitwidth(&self) -> u32 { - self.max.bits() as u32 + self.max.bits() } // issue the metadata for a parameter of a given bitwidth diff --git a/zokrates_book/src/gettingstarted.md b/zokrates_book/src/gettingstarted.md index 93caf96c2..d0aa512f3 100644 --- a/zokrates_book/src/gettingstarted.md +++ b/zokrates_book/src/gettingstarted.md @@ -25,7 +25,7 @@ You can build ZoKrates from [source](https://github.com/ZoKrates/ZoKrates/) with git clone https://github.com/ZoKrates/ZoKrates cd ZoKrates export ZOKRATES_STDLIB=$PWD/zokrates_stdlib/stdlib -cargo +nightly build -p zokrates_cli --release +cargo build -p zokrates_cli --release cd target/release ``` diff --git a/zokrates_cli/src/bin.rs b/zokrates_cli/src/bin.rs index a0aa12882..68e7d5ec4 100644 --- a/zokrates_cli/src/bin.rs +++ b/zokrates_cli/src/bin.rs @@ -1,5 +1,3 @@ -#![feature(panic_info_message)] -#![feature(backtrace)] // // @file bin.rs // @author Jacob Eberhardt @@ -86,21 +84,8 @@ fn cli() -> Result<(), String> { } fn panic_hook(pi: &std::panic::PanicInfo) { - let location = pi - .location() - .map(|l| format!("({})", l)) - .unwrap_or_default(); - - let message = pi - .message() - .map(|m| format!("{}", m)) - .or_else(|| pi.payload().downcast_ref::<&str>().map(|p| p.to_string())); - - if let Some(s) = message { - println!("{} {}", s, location); - } else { - println!("The compiler unexpectedly panicked {}", location); - } + println!("The compiler unexpectedly panicked"); + println!("{}", pi); #[cfg(debug_assertions)] { diff --git a/zokrates_cli/src/ops/compile.rs b/zokrates_cli/src/ops/compile.rs index 35d47b3f8..c7bc25a15 100644 --- a/zokrates_cli/src/ops/compile.rs +++ b/zokrates_cli/src/ops/compile.rs @@ -145,10 +145,10 @@ fn cli_compile(sub_matches: &ArgMatches) -> Result<(), String> { // serialize flattened program and write to binary file log::debug!("Serialize program"); - let bin_output_file = File::create(&bin_output_path) + let bin_output_file = File::create(bin_output_path) .map_err(|why| format!("Could not create {}: {}", bin_output_path.display(), why))?; - let r1cs_output_file = File::create(&r1cs_output_path) + let r1cs_output_file = File::create(r1cs_output_path) .map_err(|why| format!("Could not create {}: {}", r1cs_output_path.display(), why))?; let mut bin_writer = BufWriter::new(bin_output_file); @@ -170,7 +170,7 @@ fn cli_compile(sub_matches: &ArgMatches) -> Result<(), String> { Ok(constraint_count) => { // serialize ABI spec and write to JSON file log::debug!("Serialize ABI"); - let abi_spec_file = File::create(&abi_spec_path) + let abi_spec_file = File::create(abi_spec_path) .map_err(|why| format!("Could not create {}: {}", abi_spec_path.display(), why))?; let mut writer = BufWriter::new(abi_spec_file); diff --git a/zokrates_cli/src/ops/compute_witness.rs b/zokrates_cli/src/ops/compute_witness.rs index 0a886523b..466c88dfc 100644 --- a/zokrates_cli/src/ops/compute_witness.rs +++ b/zokrates_cli/src/ops/compute_witness.rs @@ -77,7 +77,7 @@ pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { // read compiled program let path = Path::new(sub_matches.value_of("input").unwrap()); let file = - File::open(&path).map_err(|why| format!("Could not open {}: {}", path.display(), why))?; + File::open(path).map_err(|why| format!("Could not open {}: {}", path.display(), why))?; let mut reader = BufReader::new(file); @@ -106,7 +106,7 @@ fn cli_compute<'a, T: Field, I: Iterator>>( let signature = match is_abi { true => { let path = Path::new(sub_matches.value_of("abi-spec").unwrap()); - let file = File::open(&path) + let file = File::open(path) .map_err(|why| format!("Could not open {}: {}", path.display(), why))?; let mut reader = BufReader::new(file); @@ -190,7 +190,7 @@ fn cli_compute<'a, T: Field, I: Iterator>>( // write witness to file let output_path = Path::new(sub_matches.value_of("output").unwrap()); - let output_file = File::create(&output_path) + let output_file = File::create(output_path) .map_err(|why| format!("Could not create {}: {}", output_path.display(), why))?; let writer = BufWriter::new(output_file); @@ -214,7 +214,7 @@ fn cli_compute<'a, T: Field, I: Iterator>>( // write circom witness to file let wtns_path = Path::new(sub_matches.value_of("circom-witness").unwrap()); - let wtns_file = File::create(&wtns_path) + let wtns_file = File::create(wtns_path) .map_err(|why| format!("Could not create {}: {}", output_path.display(), why))?; let mut writer = BufWriter::new(wtns_file); diff --git a/zokrates_cli/src/ops/export_verifier.rs b/zokrates_cli/src/ops/export_verifier.rs index 08b35a44a..f343a7374 100644 --- a/zokrates_cli/src/ops/export_verifier.rs +++ b/zokrates_cli/src/ops/export_verifier.rs @@ -35,7 +35,7 @@ pub fn subcommand() -> App<'static, 'static> { pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { let vk_path = Path::new(sub_matches.value_of("input").unwrap()); - let vk_file = File::open(&vk_path) + let vk_file = File::open(vk_path) .map_err(|why| format!("Could not open {}: {}", vk_path.display(), why))?; // deserialize vk to JSON @@ -84,7 +84,7 @@ fn cli_export_verifier App<'static, 'static> { pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { let program_path = Path::new(sub_matches.value_of("input").unwrap()); - let program_file = File::open(&program_path) + let program_file = File::open(program_path) .map_err(|why| format!("Could not open {}: {}", program_path.display(), why))?; let mut reader = BufReader::new(program_file); @@ -160,7 +160,7 @@ fn cli_generate_proof< // deserialize witness let witness_path = Path::new(sub_matches.value_of("witness").unwrap()); - let witness_file = File::open(&witness_path) + let witness_file = File::open(witness_path) .map_err(|why| format!("Could not open {}: {}", witness_path.display(), why))?; let witness_reader = BufReader::new(witness_file); @@ -171,7 +171,7 @@ fn cli_generate_proof< let pk_path = Path::new(sub_matches.value_of("proving-key-path").unwrap()); let proof_path = Path::new(sub_matches.value_of("proof-path").unwrap()); - let pk_file = File::open(&pk_path) + let pk_file = File::open(pk_path) .map_err(|why| format!("Could not open {}: {}", pk_path.display(), why))?; let pk_reader = BufReader::new(pk_file); diff --git a/zokrates_cli/src/ops/generate_smtlib2.rs b/zokrates_cli/src/ops/generate_smtlib2.rs index ac58f8e56..389372df9 100644 --- a/zokrates_cli/src/ops/generate_smtlib2.rs +++ b/zokrates_cli/src/ops/generate_smtlib2.rs @@ -35,7 +35,7 @@ pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { // read compiled program let path = Path::new(sub_matches.value_of("input").unwrap()); let file = - File::open(&path).map_err(|why| format!("Could not open {}: {}", path.display(), why))?; + File::open(path).map_err(|why| format!("Could not open {}: {}", path.display(), why))?; let mut reader = BufReader::new(file); diff --git a/zokrates_cli/src/ops/inspect.rs b/zokrates_cli/src/ops/inspect.rs index b8ca37545..1db259b6a 100644 --- a/zokrates_cli/src/ops/inspect.rs +++ b/zokrates_cli/src/ops/inspect.rs @@ -31,7 +31,7 @@ pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { // read compiled program let path = Path::new(sub_matches.value_of("input").unwrap()); let file = - File::open(&path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; + File::open(path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; let mut reader = BufReader::new(file); diff --git a/zokrates_cli/src/ops/mpc/beacon.rs b/zokrates_cli/src/ops/mpc/beacon.rs index 619a95ed9..71412868b 100644 --- a/zokrates_cli/src/ops/mpc/beacon.rs +++ b/zokrates_cli/src/ops/mpc/beacon.rs @@ -73,7 +73,7 @@ fn cli_mpc_beacon, B: MpcBack ) -> Result<(), String> { let path = Path::new(sub_matches.value_of("input").unwrap()); let file = - File::open(&path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; + File::open(path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; let mut reader = BufReader::new(file); @@ -134,7 +134,7 @@ fn cli_mpc_beacon, B: MpcBack }; let output_path = Path::new(sub_matches.value_of("output").unwrap()); - let output_file = File::create(&output_path) + let output_file = File::create(output_path) .map_err(|why| format!("Could not create `{}`: {}", output_path.display(), why))?; let mut writer = BufWriter::new(output_file); diff --git a/zokrates_cli/src/ops/mpc/contribute.rs b/zokrates_cli/src/ops/mpc/contribute.rs index fcb7e5461..96128768c 100644 --- a/zokrates_cli/src/ops/mpc/contribute.rs +++ b/zokrates_cli/src/ops/mpc/contribute.rs @@ -70,12 +70,12 @@ pub fn cli_mpc_contribute< ) -> Result<(), String> { let path = Path::new(sub_matches.value_of("input").unwrap()); let file = - File::open(&path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; + File::open(path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; let mut reader = BufReader::new(file); let output_path = Path::new(sub_matches.value_of("output").unwrap()); - let output_file = File::create(&output_path) + let output_file = File::create(output_path) .map_err(|why| format!("Could not create `{}`: {}", output_path.display(), why))?; let mut writer = BufWriter::new(output_file); diff --git a/zokrates_cli/src/ops/mpc/export.rs b/zokrates_cli/src/ops/mpc/export.rs index cf640fa1c..87c780052 100644 --- a/zokrates_cli/src/ops/mpc/export.rs +++ b/zokrates_cli/src/ops/mpc/export.rs @@ -66,7 +66,7 @@ pub fn cli_mpc_export, B: Mpc ) -> Result<(), String> { let path = Path::new(sub_matches.value_of("input").unwrap()); let file = - File::open(&path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; + File::open(path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; let mut reader = BufReader::new(file); diff --git a/zokrates_cli/src/ops/mpc/init.rs b/zokrates_cli/src/ops/mpc/init.rs index 92a972482..72e2283c6 100644 --- a/zokrates_cli/src/ops/mpc/init.rs +++ b/zokrates_cli/src/ops/mpc/init.rs @@ -46,7 +46,7 @@ pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { // read compiled program let path = Path::new(sub_matches.value_of("input").unwrap()); let file = - File::open(&path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; + File::open(path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; let mut reader = BufReader::new(file); @@ -76,7 +76,7 @@ fn cli_mpc_init< let mut radix_reader = BufReader::new(radix_file); let output_path = Path::new(sub_matches.value_of("output").unwrap()); - let output_file = File::create(&output_path) + let output_file = File::create(output_path) .map_err(|why| format!("Could not create `{}`: {}", output_path.display(), why))?; let mut writer = BufWriter::new(output_file); diff --git a/zokrates_cli/src/ops/mpc/verify.rs b/zokrates_cli/src/ops/mpc/verify.rs index d0520e64c..ef0371127 100644 --- a/zokrates_cli/src/ops/mpc/verify.rs +++ b/zokrates_cli/src/ops/mpc/verify.rs @@ -46,7 +46,7 @@ pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { // read compiled program let path = Path::new(sub_matches.value_of("circuit").unwrap()); let file = - File::open(&path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; + File::open(path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; let mut reader = BufReader::new(file); @@ -71,7 +71,7 @@ fn cli_mpc_verify< let path = Path::new(sub_matches.value_of("input").unwrap()); let file = - File::open(&path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; + File::open(path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; let reader = BufReader::new(file); diff --git a/zokrates_cli/src/ops/print_proof.rs b/zokrates_cli/src/ops/print_proof.rs index a6df505af..cf0d25a7c 100644 --- a/zokrates_cli/src/ops/print_proof.rs +++ b/zokrates_cli/src/ops/print_proof.rs @@ -38,7 +38,7 @@ pub fn subcommand() -> App<'static, 'static> { pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { let proof_path = Path::new(sub_matches.value_of("proof-path").unwrap()); - let proof_file = File::open(&proof_path) + let proof_file = File::open(proof_path) .map_err(|why| format!("Could not open {}: {}", proof_path.display(), why))?; // deserialize proof to JSON diff --git a/zokrates_cli/src/ops/profile.rs b/zokrates_cli/src/ops/profile.rs index 79e0b1f71..a866beee8 100644 --- a/zokrates_cli/src/ops/profile.rs +++ b/zokrates_cli/src/ops/profile.rs @@ -26,7 +26,7 @@ pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { // read compiled program let path = Path::new(sub_matches.value_of("input").unwrap()); let file = - File::open(&path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; + File::open(path).map_err(|why| format!("Could not open `{}`: {}", path.display(), why))?; let mut reader = BufReader::new(file); diff --git a/zokrates_cli/src/ops/setup.rs b/zokrates_cli/src/ops/setup.rs index 27ddc94c1..25bca25c8 100644 --- a/zokrates_cli/src/ops/setup.rs +++ b/zokrates_cli/src/ops/setup.rs @@ -95,7 +95,7 @@ pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { // read compiled program let path = Path::new(sub_matches.value_of("input").unwrap()); let file = - File::open(&path).map_err(|why| format!("Couldn't open {}: {}", path.display(), why))?; + File::open(path).map_err(|why| format!("Couldn't open {}: {}", path.display(), why))?; let mut reader = BufReader::new(file); let prog = ProgEnum::deserialize(&mut reader)?; @@ -146,7 +146,7 @@ pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { #[cfg(feature = "ark")] Parameters(BackendParameter::Ark, _, SchemeParameter::MARLIN) => { let setup_path = Path::new(sub_matches.value_of("universal-setup-path").unwrap()); - let setup_file = File::open(&setup_path) + let setup_file = File::open(setup_path) .map_err(|why| format!("Couldn't open {}: {}\nExpected an universal setup, make sure `zokrates universal-setup` was run`", setup_path.display(), why))?; let mut reader = BufReader::new(setup_file); diff --git a/zokrates_cli/src/ops/verify.rs b/zokrates_cli/src/ops/verify.rs index 24a96c387..568b13383 100644 --- a/zokrates_cli/src/ops/verify.rs +++ b/zokrates_cli/src/ops/verify.rs @@ -51,7 +51,7 @@ pub fn subcommand() -> App<'static, 'static> { pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { let vk_path = Path::new(sub_matches.value_of("verification-key-path").unwrap()); - let vk_file = File::open(&vk_path) + let vk_file = File::open(vk_path) .map_err(|why| format!("Could not open {}: {}", vk_path.display(), why))?; // deserialize vk to JSON @@ -60,7 +60,7 @@ pub fn exec(sub_matches: &ArgMatches) -> Result<(), String> { .map_err(|why| format!("Could not deserialize verification key: {}", why))?; let proof_path = Path::new(sub_matches.value_of("proof-path").unwrap()); - let proof_file = File::open(&proof_path) + let proof_file = File::open(proof_path) .map_err(|why| format!("Could not open {}: {}", proof_path.display(), why))?; // deserialize proof to JSON diff --git a/zokrates_cli/tests/integration.rs b/zokrates_cli/tests/integration.rs index 628dfaf18..68bc0abf6 100644 --- a/zokrates_cli/tests/integration.rs +++ b/zokrates_cli/tests/integration.rs @@ -313,7 +313,7 @@ mod integration { .unwrap(); // load the expected witness - let expected_witness_file = File::open(&expected_witness_path).unwrap(); + let expected_witness_file = File::open(expected_witness_path).unwrap(); let expected_witness: Witness = helpers::parse_witness_json(expected_witness_file).unwrap(); @@ -638,7 +638,7 @@ mod integration { .unwrap(); // load the expected smtlib2 - let mut expected_smtlib2_file = File::open(&expected_smtlib2_path).unwrap(); + let mut expected_smtlib2_file = File::open(expected_smtlib2_path).unwrap(); let mut expected_smtlib2 = String::new(); expected_smtlib2_file .read_to_string(&mut expected_smtlib2) diff --git a/zokrates_codegen/src/lib.rs b/zokrates_codegen/src/lib.rs index 8484d8b62..46faf587e 100644 --- a/zokrates_codegen/src/lib.rs +++ b/zokrates_codegen/src/lib.rs @@ -1,5 +1,3 @@ -#![feature(box_patterns, box_syntax)] - //! Module containing the `Flattener` to process a program that is R1CS-able. //! //! @file flatten.rs @@ -11,10 +9,7 @@ mod utils; use self::utils::flat_expression_from_bits; use zokrates_ast::{ - common::{ - expressions::{BinaryExpression, UnaryExpression, ValueExpression}, - Span, - }, + common::{expressions::ValueExpression, Span}, zir::{ canonicalizer::ZirCanonicalizer, ConditionalExpression, Expr, Folder, SelectExpression, ShouldReduce, UMetadata, ZirAssemblyStatement, ZirExpressionList, ZirProgram, @@ -2658,133 +2653,71 @@ impl<'ast, T: Field> Flattener<'ast, T> { error.into(), ) } - // `!(x == 0)` can be asserted by giving the inverse of `x` - BooleanExpression::Not(UnaryExpression { - inner: - box BooleanExpression::UintEq(BinaryExpression { - left: - box UExpression { - inner: - UExpressionInner::Value(ValueExpression { - value: 0, .. - }), + BooleanExpression::Not(u) => { + let inner_span = u.get_span(); + + match *u.inner { + BooleanExpression::UintEq(b) => { + if let UExpressionInner::Value(ValueExpression { + value: 0, .. + }) = b.left.inner + { + let x = self + .flatten_uint_expression(statements_flattened, *b.right) + .get_field_unchecked(); + self.enforce_not_zero_assertion(statements_flattened, x) + } else if let UExpressionInner::Value(ValueExpression { + value: 0, + .. + }) = b.right.inner + { + let x = self + .flatten_uint_expression(statements_flattened, *b.left) + .get_field_unchecked(); + self.enforce_not_zero_assertion(statements_flattened, x) + } else { + self.enforce_naive_assertion( + statements_flattened, + BooleanExpression::not(BooleanExpression::UintEq(b)), + error, + ); + } + } + BooleanExpression::FieldEq(b) => match (*b.left, *b.right) { + ( + FieldElementExpression::Value(ValueExpression { + value: zero, .. - }, - right: box x, - .. - }), - .. - }) - | BooleanExpression::Not(UnaryExpression { - inner: - box BooleanExpression::UintEq(BinaryExpression { - right: - box UExpression { - inner: - UExpressionInner::Value(ValueExpression { - value: 0, .. - }), + }), + x, + ) + | ( + x, + FieldElementExpression::Value(ValueExpression { + value: zero, .. - }, - left: box x, - .. - }), - .. - }) => { - let x = self - .flatten_uint_expression(statements_flattened, x) - .get_field_unchecked(); - - // introduce intermediate variable - let x_id = self.define(x, statements_flattened); - - // check that `x` is not 0 by giving its inverse - let invx = self.use_sym(); - - // # invx = 1/x - statements_flattened.push_back(FlatStatement::Directive( - FlatDirective::new( - vec![invx], - Solver::Div, - vec![FlatExpression::value(T::one()), x_id.into()], - ), - )); - - // assert(invx * x == 1) - statements_flattened.push_back(FlatStatement::condition( - FlatExpression::value(T::one()), - FlatExpression::mul(invx.into(), x_id.into()), - RuntimeError::Inverse, - )); - } - // `!(x == 0)` can be asserted by giving the inverse of `x` - BooleanExpression::Not(UnaryExpression { - inner: - box BooleanExpression::FieldEq( - BinaryExpression { - left: box FieldElementExpression::Value(zero), - right: box x, - .. - }, - .., - ), - .. - }) - | BooleanExpression::Not(UnaryExpression { - inner: - box BooleanExpression::FieldEq( - BinaryExpression { - left: box x, - right: box FieldElementExpression::Value(zero), - .. - }, - .., - ), - .. - }) if zero.value == T::from(0) => { - let x = self.flatten_field_expression(statements_flattened, x); - - // introduce intermediate variable - let x_id = self.define(x, statements_flattened); - - // check that `x` is not 0 by giving its inverse - let invx = self.use_sym(); - - // # invx = 1/x - statements_flattened.push_back(FlatStatement::Directive( - FlatDirective::new( - vec![invx], - Solver::Div, - vec![FlatExpression::value(T::one()), x_id.into()], + }), + ) if zero == T::from(0) => { + let x = self.flatten_field_expression(statements_flattened, x); + self.enforce_not_zero_assertion(statements_flattened, x) + } + (left, right) => self.enforce_naive_assertion( + statements_flattened, + BooleanExpression::not( + BooleanExpression::field_eq(left, right).span(inner_span), + ) + .span(span), + error, + ), + }, + e => self.enforce_naive_assertion( + statements_flattened, + BooleanExpression::not(e.span(inner_span)).span(span), + error, ), - )); - - // assert(invx * x == 1) - statements_flattened.push_back(FlatStatement::condition( - FlatExpression::value(T::one()), - FlatExpression::mul(invx.into(), x_id.into()), - RuntimeError::Inverse, - )); - } - e => { - // naive approach: flatten the boolean to a single field element and constrain it to 1 - let e = self.flatten_boolean_expression(statements_flattened, e); - - if e.is_linear() { - statements_flattened.push_back(FlatStatement::condition( - e, - FlatExpression::value(T::from(1)), - error.into(), - )); - } else { - // swap so that left side is linear - statements_flattened.push_back(FlatStatement::condition( - FlatExpression::value(T::from(1)), - e, - error.into(), - )); } } + e => self.enforce_naive_assertion(statements_flattened, e, error), } } ZirStatement::MultipleDefinition(s) => { @@ -2868,6 +2801,63 @@ impl<'ast, T: Field> Flattener<'ast, T> { statements_flattened.set_span(span_backup); } + fn enforce_naive_assertion( + &mut self, + statements_flattened: &mut FlatStatements<'ast, T>, + e: BooleanExpression<'ast, T>, + error: zokrates_ast::zir::RuntimeError, + ) { + // naive approach: flatten the boolean to a single field element and constrain it to 1 + let e = self.flatten_boolean_expression(statements_flattened, e); + + if e.is_linear() { + statements_flattened.push_back(FlatStatement::condition( + e, + FlatExpression::value(T::from(1)), + error.into(), + )); + } else { + // swap so that left side is linear + statements_flattened.push_back(FlatStatement::condition( + FlatExpression::value(T::from(1)), + e, + error.into(), + )); + } + } + + /// Enforce that x is not zero + /// + /// # Arguments + /// + /// * `statements_flattened` - `FlatStatements<'ast, T>` Vector where new flattened statements can be added. + /// * `x` - `FlatExpression` The expression to be constrained to not be zero. + fn enforce_not_zero_assertion( + &mut self, + statements_flattened: &mut FlatStatements<'ast, T>, + x: FlatExpression, + ) { + // introduce intermediate variable + let x_id = self.define(x, statements_flattened); + + // check that `x` is not 0 by giving its inverse + let invx = self.use_sym(); + + // # invx = 1/x + statements_flattened.push_back(FlatStatement::Directive(FlatDirective::new( + vec![invx], + Solver::Div, + vec![FlatExpression::value(T::one()), x_id.into()], + ))); + + // assert(invx * x == 1) + statements_flattened.push_back(FlatStatement::condition( + FlatExpression::value(T::one()), + FlatExpression::mul(invx.into(), x_id.into()), + RuntimeError::Inverse, + )); + } + /// Flattens an equality assertion, enforcing it in the circuit. /// /// # Arguments diff --git a/zokrates_core/src/compile.rs b/zokrates_core/src/compile.rs index d1a57705b..4a0505b7d 100644 --- a/zokrates_core/src/compile.rs +++ b/zokrates_core/src/compile.rs @@ -153,18 +153,12 @@ impl fmt::Display for CompileErrorInner { CompileErrorInner::ParserError(ref e) => write!(f, "\n\t{}", e), CompileErrorInner::MacroError(ref e) => write!(f, "\n\t{}", e), CompileErrorInner::SemanticError(ref e) => { - let location = e - .pos() - .map(|p| format!("{}", p.from)) - .unwrap_or_else(|| "".to_string()); + let location = e.pos().map(|p| format!("{}", p.from)).unwrap_or_default(); write!(f, "{}\n\t{}", location, e.message()) } CompileErrorInner::ReadError(ref e) => write!(f, "\n\t{}", e), CompileErrorInner::ImportError(ref e) => { - let location = e - .span() - .map(|p| format!("{}", p.from)) - .unwrap_or_else(|| "".to_string()); + let location = e.span().map(|p| format!("{}", p.from)).unwrap_or_default(); write!(f, "{}\n\t{}", location, e.message()) } CompileErrorInner::AnalysisError(ref e) => write!(f, "\n\t{}", e), @@ -327,7 +321,7 @@ mod test { assert!(e.0[0] .value() .to_string() - .contains(&"Cannot resolve import without a resolver")); + .contains("Cannot resolve import without a resolver")); } #[test] @@ -448,15 +442,15 @@ struct Bar { field a; } vec![], vec![ConcreteStructMember { id: "b".into(), - ty: box ConcreteType::Struct(ConcreteStructType::new( + ty: Box::new(ConcreteType::Struct(ConcreteStructType::new( "bar".into(), "Bar".into(), vec![], - vec![ConcreteStructMember { - id: "a".into(), - ty: box ConcreteType::FieldElement - }] - )) + vec![ConcreteStructMember::new( + "a".into(), + ConcreteType::FieldElement + )] + ))) }] )) }], diff --git a/zokrates_core/src/lib.rs b/zokrates_core/src/lib.rs index 51de21db8..874b996ac 100644 --- a/zokrates_core/src/lib.rs +++ b/zokrates_core/src/lib.rs @@ -1,5 +1,3 @@ -#![feature(box_patterns, box_syntax)] - pub mod compile; pub mod imports; mod macros; diff --git a/zokrates_core/src/optimizer/redefinition.rs b/zokrates_core/src/optimizer/redefinition.rs index 770e9c025..55880465a 100644 --- a/zokrates_core/src/optimizer/redefinition.rs +++ b/zokrates_core/src/optimizer/redefinition.rs @@ -63,7 +63,6 @@ impl RedefinitionOptimizer { .into_iter() .chain(p.arguments.iter().map(|p| p.id)) .chain(p.returns()) - .into_iter() .collect(), } } diff --git a/zokrates_core/src/semantics.rs b/zokrates_core/src/semantics.rs index a0d7c5081..5be636cbb 100644 --- a/zokrates_core/src/semantics.rs +++ b/zokrates_core/src/semantics.rs @@ -372,7 +372,7 @@ impl<'ast, T: Field> Checker<'ast, T> { Ok(TypedProgram { main: program.main, - module_map: ModuleMap::new(state.typed_modules.iter().map(|(id, _)| id).cloned()), + module_map: ModuleMap::new(state.typed_modules.keys().cloned()), modules: state.typed_modules, }) } @@ -804,7 +804,6 @@ impl<'ast, T: Field> Checker<'ast, T> { .get(&import.module_id) .unwrap() .functions_iter() - .into_iter() .filter(|d| d.key.id == import.symbol_id) .map(|d| DeclarationFunctionKey { module: import.module_id.to_path_buf(), @@ -1052,7 +1051,6 @@ impl<'ast, T: Field> Checker<'ast, T> { fn check_single_main(module: &TypedModule) -> Result<(), ErrorInner> { match module .functions_iter() - .into_iter() .filter(|d| d.key.id == "main") .count() { @@ -1220,7 +1218,7 @@ impl<'ast, T: Field> Checker<'ast, T> { } if !found_return { - match (&*s.output).is_empty_tuple() { + match (*s.output).is_empty_tuple() { true => statements_checked.push( TypedStatement::ret(TypedExpression::empty_tuple()).with_span(span), ), @@ -1769,9 +1767,9 @@ impl<'ast, T: Field> Checker<'ast, T> { ) -> Result, ErrorInner> { match expr.value { // for function calls, check the rhs with the expected type - Expression::FunctionCall(box fun_id_expression, generics, arguments) => self + Expression::FunctionCall(fun_id_expression, generics, arguments) => self .check_function_call_expression( - fun_id_expression, + *fun_id_expression, generics, arguments, Some(return_type), @@ -1932,11 +1930,7 @@ impl<'ast, T: Field> Checker<'ast, T> { let e_checked = e .map(|e| { match e.value { - Expression::FunctionCall( - box fun_id_expression, - generics, - arguments, - ) => { + Expression::FunctionCall(fun_id_expression, generics, arguments) => { let ty = zokrates_ast::typed::types::try_from_g_type( return_type.clone(), ) @@ -1944,7 +1938,7 @@ impl<'ast, T: Field> Checker<'ast, T> { .unwrap(); self.check_function_call_expression( - fun_id_expression, + *fun_id_expression, generics, arguments, ty, @@ -2156,13 +2150,13 @@ impl<'ast, T: Field> Checker<'ast, T> { message: format!("Variable `{}` is undeclared", variable_name), }), }, - Assignee::Select(box assignee, box index) => { - let checked_assignee = self.check_assignee(assignee, module_id, types)?; + Assignee::Select(assignee, index) => { + let checked_assignee = self.check_assignee(*assignee, module_id, types)?; let ty = checked_assignee.get_type(); match ty { Type::Array(..) => { - let checked_index = match index { + let checked_index = match *index { RangeOrExpression::Expression(e) => { self.check_expression(e, module_id, types)? } @@ -2184,10 +2178,7 @@ impl<'ast, T: Field> Checker<'ast, T> { }, )?; - Ok(TypedAssignee::Select( - box checked_assignee, - box checked_typed_index, - )) + Ok(TypedAssignee::select(checked_assignee, checked_typed_index)) } ty => Err(ErrorInner { span: Some(span), @@ -2198,13 +2189,13 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Assignee::Member(box assignee, box member) => { - let checked_assignee = self.check_assignee(assignee, module_id, types)?; + Assignee::Member(assignee, member) => { + let checked_assignee = self.check_assignee(*assignee, module_id, types)?; let ty = checked_assignee.get_type(); match &ty { - Type::Struct(members) => match members.iter().find(|m| m.id == member) { - Some(_) => Ok(TypedAssignee::Member(box checked_assignee, member.into())), + Type::Struct(members) => match members.iter().find(|m| m.id == *member) { + Some(_) => Ok(TypedAssignee::member(checked_assignee, (*member).into())), None => Err(ErrorInner { span: Some(span), message: format!( @@ -2229,13 +2220,13 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Assignee::Element(box assignee, index) => { - let checked_assignee = self.check_assignee(assignee, module_id, types)?; + Assignee::Element(assignee, index) => { + let checked_assignee = self.check_assignee(*assignee, module_id, types)?; let ty = checked_assignee.get_type(); match &ty { Type::Tuple(tuple_ty) => match tuple_ty.elements.get(index as usize) { - Some(_) => Ok(TypedAssignee::Element(box checked_assignee, index)), + Some(_) => Ok(TypedAssignee::element(checked_assignee, index)), None => Err(ErrorInner { span: Some(span), message: format!( @@ -2413,7 +2404,7 @@ impl<'ast, T: Field> Checker<'ast, T> { function_key, generics_checked, arguments_checked, - ).annotate(*array_ty.ty, *array_ty.size).into()), + ).annotate(array_ty).into()), Type::Tuple(tuple_ty) => Ok(TupleExpression::function_call( function_key, generics_checked, @@ -2462,7 +2453,7 @@ impl<'ast, T: Field> Checker<'ast, T> { Ok(FieldElementExpression::identifier(id.into()).into()) } Type::Array(array_type) => Ok(ArrayExpression::identifier(id.into()) - .annotate(*array_type.ty, *array_type.size) + .annotate(array_type) .into()), Type::Struct(members) => Ok(StructExpression::identifier(id.into()) .annotate(members) @@ -2479,9 +2470,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Add(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::Add(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; use self::TypedExpression::*; @@ -2515,9 +2506,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Sub(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::Sub(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; use self::TypedExpression::*; @@ -2544,9 +2535,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Mult(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::Mult(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; use self::TypedExpression::*; @@ -2579,9 +2570,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Div(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::Div(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; use self::TypedExpression::*; @@ -2612,9 +2603,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Rem(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::Rem(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; let (e1_checked, e2_checked) = TypedExpression::align_without_integers( e1_checked, e2_checked, @@ -2641,9 +2632,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Pow(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::Pow(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; let e1_checked = match FieldElementExpression::try_from_typed(e1_checked) { Ok(e) => e.into(), @@ -2669,8 +2660,8 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Neg(box e) => { - let e = self.check_expression(e, module_id, types)?; + Expression::Neg(e) => { + let e = self.check_expression(*e, module_id, types)?; match e { TypedExpression::Int(e) => Ok(IntExpression::neg(e).into()), @@ -2688,8 +2679,8 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Pos(box e) => { - let e = self.check_expression(e, module_id, types)?; + Expression::Pos(e) => { + let e = self.check_expression(*e, module_id, types)?; match e { TypedExpression::Int(e) => Ok(IntExpression::pos(e).into()), @@ -2707,7 +2698,7 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Conditional(box conditional) => { + Expression::Conditional(conditional) => { let condition_checked = self.check_expression(*conditional.condition, module_id, types)?; @@ -2800,18 +2791,18 @@ impl<'ast, T: Field> Checker<'ast, T> { Expression::U16Constant(n) => Ok(UExpression::value(n.into()).annotate(16).with_span(span).into()), Expression::U32Constant(n) => Ok(UExpression::value(n.into()).annotate(32).with_span(span).into()), Expression::U64Constant(n) => Ok(UExpression::value(n.into()).annotate(64).with_span(span).into()), - Expression::FunctionCall(box fun_id_expression, generics, arguments) => self + Expression::FunctionCall(fun_id_expression, generics, arguments) => self .check_function_call_expression( - fun_id_expression, + *fun_id_expression, generics, arguments, None, module_id, types, ), - Expression::Lt(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::Lt(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; let (e1_checked, e2_checked) = TypedExpression::align_without_integers( e1_checked, e2_checked, @@ -2859,9 +2850,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Le(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::Le(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; let (e1_checked, e2_checked) = TypedExpression::align_without_integers( e1_checked, e2_checked, @@ -2909,9 +2900,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Eq(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::Eq(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; let (e1_checked, e2_checked) = TypedExpression::align_without_integers( e1_checked, e2_checked, @@ -2960,9 +2951,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Ge(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::Ge(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; let (e1_checked, e2_checked) = TypedExpression::align_without_integers( e1_checked, e2_checked, @@ -3010,9 +3001,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Gt(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::Gt(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; let (e1_checked, e2_checked) = TypedExpression::align_without_integers( e1_checked, e2_checked, @@ -3060,10 +3051,10 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Select(box array, box index) => { - let array = self.check_expression(array, module_id, types)?; + Expression::Select(array, index) => { + let array = self.check_expression(*array, module_id, types)?; - match index { + match *index { RangeOrExpression::Range(r) => { match array { TypedExpression::Array(array) => { @@ -3108,7 +3099,7 @@ impl<'ast, T: Field> Checker<'ast, T> { from.clone(), to.clone(), )) - .annotate(inner_type, UExpression::floor_sub(to, from)) + .annotate(ArrayType::new(inner_type, UExpression::floor_sub(to, from))) .into()) } e => Err(ErrorInner { @@ -3163,8 +3154,8 @@ impl<'ast, T: Field> Checker<'ast, T> { } } } - Expression::Element(box e, index) => { - let e = self.check_expression(e, module_id, types)?; + Expression::Element(e, index) => { + let e = self.check_expression(*e, module_id, types)?; match e { TypedExpression::Tuple(t) => { let ty = t.ty().elements.get(index as usize); @@ -3201,13 +3192,13 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Member(box e, box id) => { - let e = self.check_expression(e, module_id, types)?; + Expression::Member(e, id) => { + let e = self.check_expression(*e, module_id, types)?; match e { TypedExpression::Struct(s) => { // check that the struct has that field and return the type if it does - let ty = s.ty().iter().find(|m| m.id == id).map(|m| *m.ty.clone()); + let ty = s.ty().iter().find(|m| m.id == *id).map(|m| *m.ty.clone()); match ty { Some(ty) => match ty { @@ -3326,7 +3317,7 @@ impl<'ast, T: Field> Checker<'ast, T> { Ok( ArrayExpression::value(unwrapped_expressions_or_spreads) - .annotate(inferred_type, size) + .annotate(ArrayType::new(inferred_type, size)) .into(), ) } @@ -3338,11 +3329,11 @@ impl<'ast, T: Field> Checker<'ast, T> { let ty = TupleType::new(elements.iter().map(|e| e.get_type()).collect()); Ok(TupleExpression::value(elements).annotate(ty).into()) } - Expression::ArrayInitializer(box e, box count) => { - let e = self.check_expression(e, module_id, types)?; + Expression::ArrayInitializer(e, count) => { + let e = self.check_expression(*e, module_id, types)?; let ty = e.get_type(); - let count = self.check_expression(count, module_id, types)?; + let count = self.check_expression(*count, module_id, types)?; let count = UExpression::try_from_typed(count, &UBitwidth::B32).map_err(|e| { ErrorInner { @@ -3356,7 +3347,7 @@ impl<'ast, T: Field> Checker<'ast, T> { })?; Ok(ArrayExpressionInner::Repeat(RepeatExpression::new(e, count.clone())) - .annotate(ty, count) + .annotate(ArrayType::new(ty, count)) .into()) } Expression::InlineStruct(id, inline_members) => { @@ -3467,10 +3458,7 @@ impl<'ast, T: Field> Checker<'ast, T> { ), }) } else { - Ok(StructMember { - id: m.id.clone(), - ty: box v.get_type().clone(), - }) + Ok(StructMember::new(m.id.clone(), v.get_type().clone())) } }) .collect::, _>>()?; @@ -3488,9 +3476,9 @@ impl<'ast, T: Field> Checker<'ast, T> { .annotate(inferred_struct_type) .into()) } - Expression::And(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::And(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; let (e1_checked, e2_checked) = TypedExpression::align_without_integers( e1_checked, e2_checked, @@ -3522,9 +3510,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Or(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::Or(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; match (e1_checked, e2_checked) { (TypedExpression::Boolean(e1), TypedExpression::Boolean(e2)) => { Ok(BooleanExpression::bitor(e1, e2).into()) @@ -3539,9 +3527,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::LeftShift(box e1, box e2) => { - let e1 = self.check_expression(e1, module_id, types)?; - let e2 = self.check_expression(e2, module_id, types)?; + Expression::LeftShift(e1, e2) => { + let e1 = self.check_expression(*e1, module_id, types)?; + let e2 = self.check_expression(*e2, module_id, types)?; let e2 = UExpression::try_from_typed(e2, &UBitwidth::B32).map_err(|e| ErrorInner { @@ -3568,9 +3556,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::RightShift(box e1, box e2) => { - let e1 = self.check_expression(e1, module_id, types)?; - let e2 = self.check_expression(e2, module_id, types)?; + Expression::RightShift(e1, e2) => { + let e1 = self.check_expression(*e1, module_id, types)?; + let e2 = self.check_expression(*e2, module_id, types)?; let e2 = UExpression::try_from_typed(e2, &UBitwidth::B32).map_err(|e| ErrorInner { @@ -3599,9 +3587,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::BitOr(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::BitOr(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; let (e1_checked, e2_checked) = TypedExpression::align_without_integers( e1_checked, e2_checked, @@ -3633,9 +3621,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::BitAnd(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::BitAnd(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; let (e1_checked, e2_checked) = TypedExpression::align_without_integers( e1_checked, e2_checked, @@ -3667,9 +3655,9 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::BitXor(box e1, box e2) => { - let e1_checked = self.check_expression(e1, module_id, types)?; - let e2_checked = self.check_expression(e2, module_id, types)?; + Expression::BitXor(e1, e2) => { + let e1_checked = self.check_expression(*e1, module_id, types)?; + let e2_checked = self.check_expression(*e2, module_id, types)?; let (e1_checked, e2_checked) = TypedExpression::align_without_integers( e1_checked, e2_checked, @@ -3701,8 +3689,8 @@ impl<'ast, T: Field> Checker<'ast, T> { }), } } - Expression::Not(box e) => { - let e_checked = self.check_expression(e, module_id, types)?; + Expression::Not(e) => { + let e_checked = self.check_expression(*e, module_id, types)?; match e_checked { TypedExpression::Int(e) => Ok(IntExpression::not(e).into()), TypedExpression::Boolean(e) => Ok(BooleanExpression::not(e).into()), @@ -3766,7 +3754,7 @@ mod tests { // The value of `P - 1` is a valid field literal let expr = Expression::FieldConstant(Bn128Field::max_value().to_biguint()).mock(); assert!(Checker::::default() - .check_expression(expr, &*MODULE_ID, &TypeMap::new()) + .check_expression(expr, &MODULE_ID, &TypeMap::new()) .is_ok()); } @@ -3777,7 +3765,7 @@ mod tests { let expr = Expression::FieldConstant(value).mock(); assert!(Checker::::default() - .check_expression(expr, &*MODULE_ID, &TypeMap::new()) + .check_expression(expr, &MODULE_ID, &TypeMap::new()) .is_err()); } } @@ -3800,7 +3788,7 @@ mod tests { ]) .mock(); assert!(Checker::::default() - .check_expression(a, &*MODULE_ID, &types) + .check_expression(a, &MODULE_ID, &types) .is_err()); // [[0f], [0f, 0f]] @@ -3820,7 +3808,7 @@ mod tests { ]) .mock(); assert!(Checker::::default() - .check_expression(a, &*MODULE_ID, &types) + .check_expression(a, &MODULE_ID, &types) .is_ok()); // [[0f], true] @@ -3836,7 +3824,7 @@ mod tests { ]) .mock(); assert!(Checker::::default() - .check_expression(a, &*MODULE_ID, &types) + .check_expression(a, &MODULE_ID, &types) .is_err()); } } @@ -4051,7 +4039,7 @@ mod tests { let mut checker: Checker = Checker::default(); assert_eq!( - checker.check_module(&*MODULE_ID, &mut state).unwrap_err()[0] + checker.check_module(&MODULE_ID, &mut state).unwrap_err()[0] .inner .message, "foo conflicts with another symbol" @@ -4134,7 +4122,7 @@ mod tests { ); let mut checker: Checker = Checker::default(); - assert!(checker.check_module(&*MODULE_ID, &mut state).is_ok()); + assert!(checker.check_module(&MODULE_ID, &mut state).is_ok()); } mod generics { @@ -4176,7 +4164,7 @@ mod tests { ); let mut checker: Checker = Checker::default(); - assert!(checker.check_module(&*MODULE_ID, &mut state).is_ok()); + assert!(checker.check_module(&MODULE_ID, &mut state).is_ok()); } #[test] @@ -4233,7 +4221,7 @@ mod tests { let mut checker: Checker = Checker::default(); assert_eq!( - checker.check_module(&*MODULE_ID, &mut state).unwrap_err()[0] + checker.check_module(&MODULE_ID, &mut state).unwrap_err()[0] .inner .message, "Undeclared symbol `P`" @@ -4273,7 +4261,7 @@ mod tests { ); let mut checker: Checker = Checker::default(); - assert_eq!(checker.check_module(&*MODULE_ID, &mut state), Ok(())); + assert_eq!(checker.check_module(&MODULE_ID, &mut state), Ok(())); assert!(state .typed_modules .get(&*MODULE_ID) @@ -4324,7 +4312,7 @@ mod tests { let mut checker: Checker = Checker::default(); assert_eq!( - checker.check_module(&*MODULE_ID, &mut state).unwrap_err()[0] + checker.check_module(&MODULE_ID, &mut state).unwrap_err()[0] .inner .message, "foo conflicts with another symbol" @@ -4368,7 +4356,7 @@ mod tests { let mut checker: Checker = Checker::default(); assert_eq!( - checker.check_module(&*MODULE_ID, &mut state).unwrap_err()[0] + checker.check_module(&MODULE_ID, &mut state).unwrap_err()[0] .inner .message, "foo conflicts with another symbol" @@ -4420,7 +4408,7 @@ mod tests { let mut checker: Checker = Checker::default(); assert_eq!( - checker.check_module(&*MODULE_ID, &mut state).unwrap_err()[0] + checker.check_module(&MODULE_ID, &mut state).unwrap_err()[0] .inner .message, "foo conflicts with another symbol" @@ -4469,7 +4457,7 @@ mod tests { let mut checker: Checker = Checker::default(); assert_eq!( - checker.check_module(&*MODULE_ID, &mut state).unwrap_err()[0] + checker.check_module(&MODULE_ID, &mut state).unwrap_err()[0] .inner .message, "foo conflicts with another symbol" @@ -4498,12 +4486,12 @@ mod tests { let state = State::new(modules, (*MODULE_ID).clone()); let signature = UnresolvedSignature::new().inputs(vec![UnresolvedType::Array( - box UnresolvedType::FieldElement.mock(), + Box::new(UnresolvedType::FieldElement.mock()), Expression::Identifier("K").mock(), ) .mock()]); assert_eq!( - Checker::::default().check_signature(signature, &*MODULE_ID, &state), + Checker::::default().check_signature(signature, &MODULE_ID, &state), Err(vec![ErrorInner { span: Some(SourceSpan::mock()), message: "Undeclared symbol `K`".to_string() @@ -4520,27 +4508,31 @@ mod tests { let signature = UnresolvedSignature::new() .generics(vec!["K".mock(), "L".mock(), "M".mock()]) .inputs(vec![UnresolvedType::Array( - box UnresolvedType::Array( - box UnresolvedType::FieldElement.mock(), - Expression::Identifier("K").mock(), - ) - .mock(), + Box::new( + UnresolvedType::Array( + Box::new(UnresolvedType::FieldElement.mock()), + Expression::Identifier("K").mock(), + ) + .mock(), + ), Expression::Identifier("L").mock(), ) .mock()]) .output( UnresolvedType::Array( - box UnresolvedType::Array( - box UnresolvedType::FieldElement.mock(), - Expression::Identifier("L").mock(), - ) - .mock(), + Box::new( + UnresolvedType::Array( + Box::new(UnresolvedType::FieldElement.mock()), + Expression::Identifier("L").mock(), + ) + .mock(), + ), Expression::Identifier("K").mock(), ) .mock(), ); assert_eq!( - Checker::::default().check_signature(signature, &*MODULE_ID, &state), + Checker::::default().check_signature(signature, &MODULE_ID, &state), Ok(DeclarationSignature::new() .inputs(vec![DeclarationType::array(( DeclarationType::array(( @@ -4575,7 +4567,7 @@ mod tests { checker.enter_scope(); assert_eq!( - checker.check_statement(statement, &*MODULE_ID, &TypeMap::new()), + checker.check_statement(statement, &MODULE_ID, &TypeMap::new()), Err(vec![ErrorInner { span: Some(SourceSpan::mock()), message: "Identifier \"b\" is undefined".into() @@ -4606,7 +4598,7 @@ mod tests { let mut checker: Checker = new_with_args(scope, HashSet::new()); checker.enter_scope(); assert_eq!( - checker.check_statement(statement, &*MODULE_ID, &TypeMap::new()), + checker.check_statement(statement, &MODULE_ID, &TypeMap::new()), Ok(TypedStatement::definition( typed::Variable::field_element("a").into(), FieldElementExpression::identifier("b".into()).into() @@ -4675,7 +4667,7 @@ mod tests { let mut checker: Checker = Checker::default(); assert_eq!( - checker.check_module(&*MODULE_ID, &mut state), + checker.check_module(&MODULE_ID, &mut state), Err(vec![Error { inner: ErrorInner { span: Some(SourceSpan::mock()), @@ -4771,7 +4763,7 @@ mod tests { ); let mut checker: Checker = Checker::default(); - assert!(checker.check_module(&*MODULE_ID, &mut state).is_ok()); + assert!(checker.check_module(&MODULE_ID, &mut state).is_ok()); } #[test] @@ -4805,7 +4797,7 @@ mod tests { let mut checker: Checker = Checker::default(); assert_eq!( - checker.check_function("foo", foo, &*MODULE_ID, &state), + checker.check_function("foo", foo, &MODULE_ID, &state), Err(vec![ErrorInner { span: Some(SourceSpan::mock()), message: "Identifier \"i\" is undefined".into() @@ -4884,7 +4876,7 @@ mod tests { let mut checker: Checker = Checker::default(); assert_eq!( - checker.check_function("foo", foo, &*MODULE_ID, &state), + checker.check_function("foo", foo, &MODULE_ID, &state), Ok(foo_checked) ); } @@ -4902,8 +4894,12 @@ mod tests { let bar_statements: Vec = vec![ Statement::Definition( untyped::Variable::immutable("a", UnresolvedType::FieldElement.mock()).mock(), - Expression::FunctionCall(box Expression::Identifier("foo").mock(), None, vec![]) - .mock(), + Expression::FunctionCall( + Box::new(Expression::Identifier("foo").mock()), + None, + vec![], + ) + .mock(), ) .mock(), Statement::Return(None).mock(), @@ -4931,7 +4927,7 @@ mod tests { let mut checker: Checker = new_with_args(Scope::default(), functions); assert_eq!( - checker.check_function("bar", bar, &*MODULE_ID, &state), + checker.check_function("bar", bar, &MODULE_ID, &state), Err(vec![ErrorInner { span: Some(SourceSpan::mock()), message: @@ -4951,8 +4947,12 @@ mod tests { let bar_statements: Vec = vec![ Statement::Definition( untyped::Variable::immutable("a", UnresolvedType::FieldElement.mock()).mock(), - Expression::FunctionCall(box Expression::Identifier("foo").mock(), None, vec![]) - .mock(), + Expression::FunctionCall( + Box::new(Expression::Identifier("foo").mock()), + None, + vec![], + ) + .mock(), ) .mock(), Statement::Return(None).mock(), @@ -4970,7 +4970,7 @@ mod tests { let mut checker: Checker = new_with_args(Scope::default(), HashSet::new()); assert_eq!( - checker.check_function("bar", bar, &*MODULE_ID, &state), + checker.check_function("bar", bar, &MODULE_ID, &state), Err(vec![ErrorInner { span: Some(SourceSpan::mock()), @@ -5007,8 +5007,12 @@ mod tests { let main_statements: Vec = vec![ Statement::Assignment( Assignee::Identifier("a").mock(), - Expression::FunctionCall(box Expression::Identifier("foo").mock(), None, vec![]) - .mock(), + Expression::FunctionCall( + Box::new(Expression::Identifier("foo").mock()), + None, + vec![], + ) + .mock(), ) .mock(), Statement::Return(None).mock(), @@ -5045,7 +5049,7 @@ mod tests { let mut checker: Checker = new_with_args(Scope::default(), HashSet::new()); assert_eq!( - checker.check_module(&*MODULE_ID, &mut state), + checker.check_module(&MODULE_ID, &mut state), Err(vec![Error { inner: ErrorInner { span: Some(SourceSpan::mock()), @@ -5099,14 +5103,18 @@ mod tests { .mock(), Statement::Assignment( Assignee::Select( - box Assignee::Identifier("a").mock(), - box RangeOrExpression::Expression( + Box::new(Assignee::Identifier("a").mock()), + Box::new(RangeOrExpression::Expression( untyped::Expression::IntConstant(0usize.into()).mock(), - ), + )), + ) + .mock(), + Expression::FunctionCall( + Box::new(Expression::Identifier("foo").mock()), + None, + vec![], ) .mock(), - Expression::FunctionCall(box Expression::Identifier("foo").mock(), None, vec![]) - .mock(), ) .mock(), Statement::Return(None).mock(), @@ -5142,7 +5150,7 @@ mod tests { ); let mut checker: Checker = new_with_args(Scope::default(), HashSet::new()); - assert!(checker.check_module(&*MODULE_ID, &mut state).is_ok()); + assert!(checker.check_module(&MODULE_ID, &mut state).is_ok()); } #[test] @@ -5156,13 +5164,15 @@ mod tests { let bar_statements: Vec = vec![ Statement::Assertion( Expression::Eq( - box Expression::IntConstant(1usize.into()).mock(), - box Expression::FunctionCall( - box Expression::Identifier("foo").mock(), - None, - vec![], - ) - .mock(), + Box::new(Expression::IntConstant(1usize.into()).mock()), + Box::new( + Expression::FunctionCall( + Box::new(Expression::Identifier("foo").mock()), + None, + vec![], + ) + .mock(), + ), ) .mock(), None, @@ -5183,7 +5193,7 @@ mod tests { let mut checker: Checker = new_with_args(Scope::default(), HashSet::new()); assert_eq!( - checker.check_function("bar", bar, &*MODULE_ID, &state), + checker.check_function("bar", bar, &MODULE_ID, &state), Err(vec![ErrorInner { span: Some(SourceSpan::mock()), @@ -5216,7 +5226,7 @@ mod tests { let mut checker: Checker = new_with_args(Scope::default(), HashSet::new()); assert_eq!( - checker.check_function("bar", bar, &*MODULE_ID, &state), + checker.check_function("bar", bar, &MODULE_ID, &state), Err(vec![ErrorInner { span: Some(SourceSpan::mock()), message: "Identifier \"a\" is undefined".into() @@ -5254,7 +5264,7 @@ mod tests { let mut checker: Checker = new_with_args(Scope::default(), HashSet::new()); assert_eq!( checker - .check_function("main", f, &*MODULE_ID, &state) + .check_function("main", f, &MODULE_ID, &state) .unwrap_err()[0] .message, "Duplicate name in function definition: `a` was previously declared as an argument, a generic parameter or a constant" @@ -5356,7 +5366,7 @@ mod tests { untyped::Expression::IntConstant(2usize.into()).mock(), ) .mock(), - &*MODULE_ID, + &MODULE_ID, &TypeMap::new(), ); let s2_checked: Result, Vec> = checker @@ -5367,7 +5377,7 @@ mod tests { untyped::Expression::IntConstant(2usize.into()).mock(), ) .mock(), - &*MODULE_ID, + &MODULE_ID, &TypeMap::new(), ); assert!(s2_checked.is_ok()); @@ -5388,7 +5398,7 @@ mod tests { untyped::Expression::IntConstant(2usize.into()).mock(), ) .mock(), - &*MODULE_ID, + &MODULE_ID, &TypeMap::new(), ); let s2_checked: Result, Vec> = checker @@ -5398,7 +5408,7 @@ mod tests { untyped::Expression::BooleanConstant(true).mock(), ) .mock(), - &*MODULE_ID, + &MODULE_ID, &TypeMap::new(), ); assert!(s2_checked.is_ok()); @@ -5507,7 +5517,7 @@ mod tests { .into_iter() .map(|s| { checker - .check_statement(s, &*MODULE_ID, &TypeMap::default()) + .check_statement(s, &MODULE_ID, &TypeMap::default()) .unwrap() }) .collect(); @@ -5539,7 +5549,7 @@ mod tests { let mut checker: Checker = Checker::default(); - checker.check_module(&*MODULE_ID, &mut state).unwrap(); + checker.check_module(&MODULE_ID, &mut state).unwrap(); (checker, state) } @@ -5567,7 +5577,7 @@ mod tests { Checker::::default().check_struct_type_declaration( "Foo".into(), declaration, - &*MODULE_ID, + &MODULE_ID, &state ), Ok(expected_type) @@ -5611,7 +5621,7 @@ mod tests { Checker::::default().check_struct_type_declaration( "Foo".into(), declaration, - &*MODULE_ID, + &MODULE_ID, &state ), Ok(expected_type) @@ -5646,7 +5656,7 @@ mod tests { .check_struct_type_declaration( "Foo".into(), declaration, - &*MODULE_ID, + &MODULE_ID, &state ) .unwrap_err()[0] @@ -5703,7 +5713,7 @@ mod tests { ); assert!(Checker::default() - .check_module(&*MODULE_ID, &mut state) + .check_module(&MODULE_ID, &mut state) .is_ok()); assert_eq!( state @@ -5763,7 +5773,7 @@ mod tests { ); assert!(Checker::default() - .check_module(&*MODULE_ID, &mut state) + .check_module(&MODULE_ID, &mut state) .is_err()); } @@ -5797,7 +5807,7 @@ mod tests { ); assert!(Checker::default() - .check_module(&*MODULE_ID, &mut state) + .check_module(&MODULE_ID, &mut state) .is_err()); } @@ -5849,7 +5859,7 @@ mod tests { ); assert!(Checker::default() - .check_module(&*MODULE_ID, &mut state) + .check_module(&MODULE_ID, &mut state) .is_err()); } } @@ -5879,7 +5889,7 @@ mod tests { assert_eq!( checker.check_type( UnresolvedType::User("Foo".into(), None).mock(), - &*MODULE_ID, + &MODULE_ID, &state.types ), Ok(Type::Struct(StructType::new( @@ -5894,7 +5904,7 @@ mod tests { checker .check_type( UnresolvedType::User("Bar".into(), None).mock(), - &*MODULE_ID, + &MODULE_ID, &state.types ) .unwrap_err() @@ -5927,15 +5937,17 @@ mod tests { assert_eq!( checker.check_expression( Expression::Member( - box Expression::InlineStruct( - "Foo".into(), - vec![("foo", Expression::IntConstant(42usize.into()).mock())] - ) - .mock(), + Box::new( + Expression::InlineStruct( + "Foo".into(), + vec![("foo", Expression::IntConstant(42usize.into()).mock())] + ) + .mock() + ), "foo".into() ) .mock(), - &*MODULE_ID, + &MODULE_ID, &state.types ), Ok(FieldElementExpression::member( @@ -5975,15 +5987,20 @@ mod tests { checker .check_expression( Expression::Member( - box Expression::InlineStruct( - "Foo".into(), - vec![("foo", Expression::IntConstant(42usize.into()).mock())] - ) - .mock(), + Box::new( + Expression::InlineStruct( + "Foo".into(), + vec![( + "foo", + Expression::IntConstant(42usize.into()).mock() + )] + ) + .mock() + ), "bar".into() ) .mock(), - &*MODULE_ID, + &MODULE_ID, &state.types ) .unwrap_err() @@ -6018,7 +6035,7 @@ mod tests { vec![("foo", Expression::IntConstant(42usize.into()).mock())] ) .mock(), - &*MODULE_ID, + &MODULE_ID, &state.types ) .unwrap_err() @@ -6060,7 +6077,7 @@ mod tests { ] ) .mock(), - &*MODULE_ID, + &MODULE_ID, &state.types ), Ok(StructExpression::value(vec![ @@ -6113,7 +6130,7 @@ mod tests { ] ) .mock(), - &*MODULE_ID, + &MODULE_ID, &state.types ), Ok(StructExpression::value(vec![ @@ -6164,7 +6181,7 @@ mod tests { vec![("foo", Expression::IntConstant(42usize.into()).mock())] ) .mock(), - &*MODULE_ID, + &MODULE_ID, &state.types ) .unwrap_err() @@ -6212,7 +6229,7 @@ mod tests { )] ) .mock(), - &*MODULE_ID, + &MODULE_ID, &state.types ).unwrap_err() .message, @@ -6230,7 +6247,7 @@ mod tests { ] ) .mock(), - &*MODULE_ID, + &MODULE_ID, &state.types ) .unwrap_err() @@ -6290,7 +6307,7 @@ mod tests { main.value.statements = vec![Statement::Return(Some( Expression::FunctionCall( - box Expression::Identifier("foo").mock(), + Box::new(Expression::Identifier("foo").mock()), None, vec![Expression::IntConstant(0usize.into()).mock()], ) @@ -6354,13 +6371,13 @@ mod tests { Expression::FieldConstant(42u32.into()).mock(), ) .mock(), - &*MODULE_ID, + &MODULE_ID, &TypeMap::new(), ) .unwrap(); assert_eq!( - checker.check_assignee(a, &*MODULE_ID, &TypeMap::new()), + checker.check_assignee(a, &MODULE_ID, &TypeMap::new()), Ok(TypedAssignee::Identifier(typed::Variable::new( "a", Type::FieldElement, @@ -6373,8 +6390,10 @@ mod tests { // field[3] a = [1, 2, 3] // a[2] = 42 let a = Assignee::Select( - box Assignee::Identifier("a").mock(), - box RangeOrExpression::Expression(Expression::IntConstant(2usize.into()).mock()), + Box::new(Assignee::Identifier("a").mock()), + Box::new(RangeOrExpression::Expression( + Expression::IntConstant(2usize.into()).mock(), + )), ) .mock(); @@ -6405,19 +6424,19 @@ mod tests { .mock(), ) .mock(), - &*MODULE_ID, + &MODULE_ID, &TypeMap::new(), ) .unwrap(); assert_eq!( - checker.check_assignee(a, &*MODULE_ID, &TypeMap::new()), + checker.check_assignee(a, &MODULE_ID, &TypeMap::new()), Ok(TypedAssignee::Select( - box TypedAssignee::Identifier(typed::Variable::new( + Box::new(TypedAssignee::Identifier(typed::Variable::new( "a", Type::array((Type::FieldElement, 3u32)), - )), - box 2u32.into() + ))), + Box::new(2u32.into()) )) ); } @@ -6427,14 +6446,18 @@ mod tests { // field[1][1] a = [[1]] // a[0][0] let a: AssigneeNode = Assignee::Select( - box Assignee::Select( - box Assignee::Identifier("a").mock(), - box RangeOrExpression::Expression( - Expression::IntConstant(0usize.into()).mock(), - ), - ) - .mock(), - box RangeOrExpression::Expression(Expression::IntConstant(0usize.into()).mock()), + Box::new( + Assignee::Select( + Box::new(Assignee::Identifier("a").mock()), + Box::new(RangeOrExpression::Expression( + Expression::IntConstant(0usize.into()).mock(), + )), + ) + .mock(), + ), + Box::new(RangeOrExpression::Expression( + Expression::IntConstant(0usize.into()).mock(), + )), ) .mock(); @@ -6465,22 +6488,22 @@ mod tests { .mock(), ) .mock(), - &*MODULE_ID, + &MODULE_ID, &TypeMap::new(), ) .unwrap(); assert_eq!( - checker.check_assignee(a, &*MODULE_ID, &TypeMap::new()), + checker.check_assignee(a, &MODULE_ID, &TypeMap::new()), Ok(TypedAssignee::Select( - box TypedAssignee::Select( - box TypedAssignee::Identifier(typed::Variable::new( + Box::new(TypedAssignee::Select( + Box::new(TypedAssignee::Identifier(typed::Variable::new( "a", Type::array((Type::array((Type::FieldElement, 1u32)), 1u32)), - )), - box 0u32.into() - ), - box 0u32.into() + ))), + Box::new(0u32.into()) + )), + Box::new(0u32.into()) )) ); } diff --git a/zokrates_embed/src/ark.rs b/zokrates_embed/src/ark.rs index 40ce41ddd..fa8544962 100644 --- a/zokrates_embed/src/ark.rs +++ b/zokrates_embed/src/ark.rs @@ -279,8 +279,8 @@ fn var_to_index(var: &FpVar, offset: usize) -> usize { fn new_g1(flat: &[T]) -> G1 { assert_eq!(flat.len(), 2); G1::new( - BLS12Fq::from_str(&*flat[0].to_dec_string()).unwrap(), - BLS12Fq::from_str(&*flat[1].to_dec_string()).unwrap(), + BLS12Fq::from_str(&flat[0].to_dec_string()).unwrap(), + BLS12Fq::from_str(&flat[1].to_dec_string()).unwrap(), false, ) } @@ -290,12 +290,12 @@ fn new_g2(flat: &[T]) -> G2 { assert_eq!(flat.len(), 4); G2::new( BLS12Fq2::new( - BLS12Fq::from_str(&*flat[0].to_dec_string()).unwrap(), - BLS12Fq::from_str(&*flat[1].to_dec_string()).unwrap(), + BLS12Fq::from_str(&flat[0].to_dec_string()).unwrap(), + BLS12Fq::from_str(&flat[1].to_dec_string()).unwrap(), ), BLS12Fq2::new( - BLS12Fq::from_str(&*flat[2].to_dec_string()).unwrap(), - BLS12Fq::from_str(&*flat[3].to_dec_string()).unwrap(), + BLS12Fq::from_str(&flat[2].to_dec_string()).unwrap(), + BLS12Fq::from_str(&flat[3].to_dec_string()).unwrap(), ), false, ) diff --git a/zokrates_js/src/lib.rs b/zokrates_js/src/lib.rs index ed3f26502..bc7a666c6 100644 --- a/zokrates_js/src/lib.rs +++ b/zokrates_js/src/lib.rs @@ -153,7 +153,7 @@ impl<'a> Resolver for JsResolver<'a> { Some(Component::Normal(_)) => { let path_normalized = normalize_path(path); let source = STDLIB - .get(&path_normalized.to_str().unwrap()) + .get(path_normalized.to_str().unwrap()) .ok_or_else(|| { Error::new(format!( "module `{}` not found in stdlib", diff --git a/zokrates_parser/src/lib.rs b/zokrates_parser/src/lib.rs index b4f9cb85c..eb61ce2d8 100644 --- a/zokrates_parser/src/lib.rs +++ b/zokrates_parser/src/lib.rs @@ -12,6 +12,7 @@ use pest::Parser; #[grammar = "zokrates.pest"] struct ZoKratesParser; +#[allow(clippy::result_large_err)] pub fn parse(input: &str) -> Result, Error> { ZoKratesParser::parse(Rule::file, input) } diff --git a/zokrates_pest_ast/src/lib.rs b/zokrates_pest_ast/src/lib.rs index 65268b304..8772e82f4 100644 --- a/zokrates_pest_ast/src/lib.rs +++ b/zokrates_pest_ast/src/lib.rs @@ -1173,6 +1173,7 @@ impl fmt::Display for Error { } } +#[allow(clippy::result_large_err)] pub fn generate_ast(input: &str) -> Result { let parse_tree = parse(input).map_err(Error)?; Ok(Prog::from(parse_tree).0) diff --git a/zokrates_proof_systems/src/rng.rs b/zokrates_proof_systems/src/rng.rs index 9e2d51df5..814217f27 100644 --- a/zokrates_proof_systems/src/rng.rs +++ b/zokrates_proof_systems/src/rng.rs @@ -5,7 +5,7 @@ use rand_0_8::{rngs::StdRng, SeedableRng}; pub fn get_rng_from_entropy(entropy: &str) -> StdRng { let h = { let mut h = Blake2b::default(); - h.input(&entropy.as_bytes()); + h.input(entropy.as_bytes()); h.result() }; diff --git a/zokrates_proof_systems/src/to_token.rs b/zokrates_proof_systems/src/to_token.rs index fbefdc400..a2be8a963 100644 --- a/zokrates_proof_systems/src/to_token.rs +++ b/zokrates_proof_systems/src/to_token.rs @@ -8,8 +8,8 @@ use super::{ /// Helper methods for parsing group structure pub fn encode_g1_element(g: &G1Affine) -> (U256, U256) { ( - U256::from(&hex::decode(&g.0.trim_start_matches("0x")).unwrap()[..]), - U256::from(&hex::decode(&g.1.trim_start_matches("0x")).unwrap()[..]), + U256::from(&hex::decode(g.0.trim_start_matches("0x")).unwrap()[..]), + U256::from(&hex::decode(g.1.trim_start_matches("0x")).unwrap()[..]), ) } @@ -17,12 +17,12 @@ pub fn encode_g2_element(g: &G2Affine) -> ((U256, U256), (U256, U256)) { match g { G2Affine::Fq2(g) => ( ( - U256::from(&hex::decode(&g.0 .0.trim_start_matches("0x")).unwrap()[..]), - U256::from(&hex::decode(&g.0 .1.trim_start_matches("0x")).unwrap()[..]), + U256::from(&hex::decode(g.0 .0.trim_start_matches("0x")).unwrap()[..]), + U256::from(&hex::decode(g.0 .1.trim_start_matches("0x")).unwrap()[..]), ), ( - U256::from(&hex::decode(&g.1 .0.trim_start_matches("0x")).unwrap()[..]), - U256::from(&hex::decode(&g.1 .1.trim_start_matches("0x")).unwrap()[..]), + U256::from(&hex::decode(g.1 .0.trim_start_matches("0x")).unwrap()[..]), + U256::from(&hex::decode(g.1 .1.trim_start_matches("0x")).unwrap()[..]), ), ), _ => unreachable!(), @@ -30,7 +30,7 @@ pub fn encode_g2_element(g: &G2Affine) -> ((U256, U256), (U256, U256)) { } pub fn encode_fr_element(f: &Fr) -> U256 { - U256::from(&hex::decode(&f.trim_start_matches("0x")).unwrap()[..]) + U256::from(&hex::decode(f.trim_start_matches("0x")).unwrap()[..]) } pub trait ToToken: SolidityCompatibleScheme { diff --git a/zokrates_test_derive/src/lib.rs b/zokrates_test_derive/src/lib.rs index 5f7f10f9a..110a02753 100644 --- a/zokrates_test_derive/src/lib.rs +++ b/zokrates_test_derive/src/lib.rs @@ -9,7 +9,7 @@ pub fn write_tests(base: &str) { let base = Path::new(&base); let out_dir = env::var("OUT_DIR").unwrap(); let destination = Path::new(&out_dir).join("tests.rs"); - let test_file = File::create(&destination).unwrap(); + let test_file = File::create(destination).unwrap(); let mut writer = BufWriter::new(test_file); for p in glob(base.join("**/*.json").to_str().unwrap()).unwrap() {