merge clippy
This commit is contained in:
commit
05e91acc27
24 changed files with 174 additions and 178 deletions
|
@ -116,15 +116,16 @@ impl<T: Field> Value<T> {
|
|||
Ok(CheckedValue::Array(a))
|
||||
}
|
||||
}
|
||||
(Value::Struct(mut s), ConcreteType::Struct(members)) => {
|
||||
if s.len() != members.len() {
|
||||
(Value::Struct(mut s), ConcreteType::Struct(struc)) => {
|
||||
if s.len() != struc.members_count() {
|
||||
Err(format!(
|
||||
"Expected {} member(s), found {}",
|
||||
members.len(),
|
||||
struc.members_count(),
|
||||
s.len()
|
||||
))
|
||||
} else {
|
||||
let s = members
|
||||
let s = struc
|
||||
.members
|
||||
.into_iter()
|
||||
.map(|member| {
|
||||
s.remove(&member.id)
|
||||
|
|
|
@ -299,7 +299,7 @@ fn cli_compile<T: Field>(sub_matches: &ArgMatches) -> Result<(), String> {
|
|||
format!(
|
||||
"{}:{}",
|
||||
file.strip_prefix(std::env::current_dir().unwrap())
|
||||
.unwrap_or(file.as_path())
|
||||
.unwrap_or_else(|_| file.as_path())
|
||||
.display(),
|
||||
e.value()
|
||||
)
|
||||
|
@ -347,7 +347,7 @@ fn cli_compile<T: Field>(sub_matches: &ArgMatches) -> Result<(), String> {
|
|||
.map_err(|why| format!("Couldn't create {}: {}", hr_output_path.display(), why))?;
|
||||
|
||||
let mut hrofb = BufWriter::new(hr_output_file);
|
||||
write!(&mut hrofb, "{}\n", program_flattened)
|
||||
writeln!(&mut hrofb, "{}", program_flattened)
|
||||
.map_err(|_| "Unable to write data to file.".to_string())?;
|
||||
hrofb
|
||||
.flush()
|
||||
|
@ -385,7 +385,7 @@ fn cli_check<T: Field>(sub_matches: &ArgMatches) -> Result<(), String> {
|
|||
format!(
|
||||
"{}:{}",
|
||||
file.strip_prefix(std::env::current_dir().unwrap())
|
||||
.unwrap_or(file.as_path())
|
||||
.unwrap_or_else(|_| file.as_path())
|
||||
.display(),
|
||||
e.value()
|
||||
)
|
||||
|
@ -448,9 +448,11 @@ fn cli() -> Result<(), String> {
|
|||
const VERIFICATION_CONTRACT_DEFAULT_PATH: &str = "verifier.sol";
|
||||
const WITNESS_DEFAULT_PATH: &str = "witness";
|
||||
const JSON_PROOF_PATH: &str = "proof.json";
|
||||
let default_curve = env::var("ZOKRATES_CURVE").unwrap_or(constants::BN128.into());
|
||||
let default_backend = env::var("ZOKRATES_BACKEND").unwrap_or(constants::BELLMAN.into());
|
||||
let default_scheme = env::var("ZOKRATES_PROVING_SCHEME").unwrap_or(constants::G16.into());
|
||||
let default_curve = env::var("ZOKRATES_CURVE").unwrap_or_else(|_| constants::BN128.into());
|
||||
let default_backend =
|
||||
env::var("ZOKRATES_BACKEND").unwrap_or_else(|_| constants::BELLMAN.into());
|
||||
let default_scheme =
|
||||
env::var("ZOKRATES_PROVING_SCHEME").unwrap_or_else(|_| constants::G16.into());
|
||||
let default_solidity_abi = "v1";
|
||||
let default_stdlib_path = dirs::home_dir()
|
||||
.map(|p| p.join(".zokrates/stdlib"))
|
||||
|
|
|
@ -458,11 +458,11 @@ impl<'ast> fmt::Display for Range<'ast> {
|
|||
self.from
|
||||
.as_ref()
|
||||
.map(|e| e.to_string())
|
||||
.unwrap_or("".to_string()),
|
||||
.unwrap_or_else(|| "".to_string()),
|
||||
self.to
|
||||
.as_ref()
|
||||
.map(|e| e.to_string())
|
||||
.unwrap_or("".to_string())
|
||||
.unwrap_or_else(|| "".to_string())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -651,21 +651,13 @@ impl<'ast> fmt::Debug for Expression<'ast> {
|
|||
}
|
||||
|
||||
/// A list of expressions, used in return statements
|
||||
#[derive(Clone, PartialEq)]
|
||||
#[derive(Clone, PartialEq, Default)]
|
||||
pub struct ExpressionList<'ast> {
|
||||
pub expressions: Vec<ExpressionNode<'ast>>,
|
||||
}
|
||||
|
||||
pub type ExpressionListNode<'ast> = Node<ExpressionList<'ast>>;
|
||||
|
||||
impl<'ast> ExpressionList<'ast> {
|
||||
pub fn new() -> ExpressionList<'ast> {
|
||||
ExpressionList {
|
||||
expressions: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for ExpressionList<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
for (i, param) in self.expressions.iter().enumerate() {
|
||||
|
|
|
@ -181,7 +181,7 @@ pub fn compile<T: Field, E: Into<imports::Error>>(
|
|||
})
|
||||
}
|
||||
|
||||
pub fn check<'ast, T: Field, E: Into<imports::Error>>(
|
||||
pub fn check<T: Field, E: Into<imports::Error>>(
|
||||
source: String,
|
||||
location: FilePath,
|
||||
resolver: Option<&dyn Resolver<E>>,
|
||||
|
|
|
@ -249,12 +249,18 @@ impl<T: Field> FlatExpression<T> {
|
|||
FlatExpression::Add(ref x, ref y) | FlatExpression::Sub(ref x, ref y) => {
|
||||
x.is_linear() && y.is_linear()
|
||||
}
|
||||
FlatExpression::Mult(ref x, ref y) => match (x.clone(), y.clone()) {
|
||||
FlatExpression::Mult(ref x, ref y) => matches!(
|
||||
(x.clone(), y.clone()),
|
||||
(box FlatExpression::Number(_), box FlatExpression::Number(_))
|
||||
| (box FlatExpression::Number(_), box FlatExpression::Identifier(_))
|
||||
| (box FlatExpression::Identifier(_), box FlatExpression::Number(_)) => true,
|
||||
_ => false,
|
||||
},
|
||||
| (
|
||||
box FlatExpression::Number(_),
|
||||
box FlatExpression::Identifier(_)
|
||||
)
|
||||
| (
|
||||
box FlatExpression::Identifier(_),
|
||||
box FlatExpression::Number(_)
|
||||
)
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,13 +29,13 @@ impl<T: Field> QuadComb<T> {
|
|||
|
||||
match self.left.try_summand() {
|
||||
Some((ref variable, ref coefficient)) if *variable == FlatVariable::one() => {
|
||||
return Some(self.right.clone() * &coefficient);
|
||||
return Some(self.right.clone() * coefficient);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
match self.right.try_summand() {
|
||||
Some((ref variable, ref coefficient)) if *variable == FlatVariable::one() => {
|
||||
return Some(self.left.clone() * &coefficient);
|
||||
return Some(self.left.clone() * coefficient);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
@ -263,6 +263,7 @@ impl<T: Field> Mul<&T> for LinComb<T> {
|
|||
impl<T: Field> Div<&T> for LinComb<T> {
|
||||
type Output = LinComb<T>;
|
||||
|
||||
#[allow(clippy::suspicious_arithmetic_impl)]
|
||||
fn div(self, scalar: &T) -> LinComb<T> {
|
||||
self * &scalar.inverse_mul().unwrap()
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ impl Interpreter {
|
|||
}
|
||||
|
||||
impl Interpreter {
|
||||
pub fn execute<T: Field>(&self, program: &Prog<T>, inputs: &Vec<T>) -> ExecutionResult<T> {
|
||||
pub fn execute<T: Field>(&self, program: &Prog<T>, inputs: &[T]) -> ExecutionResult<T> {
|
||||
let main = &program.main;
|
||||
self.check_inputs(&program, &inputs)?;
|
||||
let mut witness = BTreeMap::new();
|
||||
|
@ -124,7 +124,7 @@ impl Interpreter {
|
|||
}
|
||||
}
|
||||
|
||||
fn check_inputs<T: Field, U>(&self, program: &Prog<T>, inputs: &Vec<U>) -> Result<(), Error> {
|
||||
fn check_inputs<T: Field, U>(&self, program: &Prog<T>, inputs: &[U]) -> Result<(), Error> {
|
||||
if program.main.arguments.len() == inputs.len() {
|
||||
Ok(())
|
||||
} else {
|
||||
|
@ -148,7 +148,7 @@ impl Interpreter {
|
|||
true => vec![T::zero(), T::one()],
|
||||
false => vec![
|
||||
T::one(),
|
||||
T::one().checked_div(&inputs[0]).unwrap_or(T::one()),
|
||||
T::one().checked_div(&inputs[0]).unwrap_or_else(T::one),
|
||||
],
|
||||
},
|
||||
Solver::Bits(bit_width) => {
|
||||
|
@ -201,7 +201,7 @@ impl Interpreter {
|
|||
let n = inputs[0].clone().to_biguint();
|
||||
let d = inputs[1].clone().to_biguint();
|
||||
|
||||
let q = n.checked_div(&d).unwrap_or(0u32.into());
|
||||
let q = n.checked_div(&d).unwrap_or_else(|| 0u32.into());
|
||||
let r = n - d * &q;
|
||||
vec![T::try_from(q).unwrap(), T::try_from(r).unwrap()]
|
||||
}
|
||||
|
@ -213,11 +213,19 @@ impl Interpreter {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EvaluationError;
|
||||
|
||||
impl<T: Field> LinComb<T> {
|
||||
fn evaluate(&self, witness: &BTreeMap<FlatVariable, T>) -> Result<T, ()> {
|
||||
fn evaluate(&self, witness: &BTreeMap<FlatVariable, T>) -> Result<T, EvaluationError> {
|
||||
self.0
|
||||
.iter()
|
||||
.map(|(var, mult)| witness.get(var).map(|v| v.clone() * mult).ok_or(())) // get each term
|
||||
.map(|(var, mult)| {
|
||||
witness
|
||||
.get(var)
|
||||
.map(|v| v.clone() * mult)
|
||||
.ok_or(EvaluationError)
|
||||
}) // get each term
|
||||
.collect::<Result<Vec<_>, _>>() // fail if any term isn't found
|
||||
.map(|v| v.iter().fold(T::from(0), |acc, t| acc + t)) // return the sum
|
||||
}
|
||||
|
@ -230,7 +238,7 @@ impl<T: Field> LinComb<T> {
|
|||
}
|
||||
|
||||
impl<T: Field> QuadComb<T> {
|
||||
pub fn evaluate(&self, witness: &BTreeMap<FlatVariable, T>) -> Result<T, ()> {
|
||||
pub fn evaluate(&self, witness: &BTreeMap<FlatVariable, T>) -> Result<T, EvaluationError> {
|
||||
let left = self.left.evaluate(&witness)?;
|
||||
let right = self.right.evaluate(&witness)?;
|
||||
Ok(left * right)
|
||||
|
@ -291,7 +299,10 @@ mod tests {
|
|||
let r = interpreter
|
||||
.execute_solver(
|
||||
&cond_eq,
|
||||
&inputs.iter().map(|&i| Bn128Field::from(i)).collect(),
|
||||
&inputs
|
||||
.iter()
|
||||
.map(|&i| Bn128Field::from(i))
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.unwrap();
|
||||
let res: Vec<Bn128Field> = vec![0, 1].iter().map(|&i| Bn128Field::from(i)).collect();
|
||||
|
@ -306,7 +317,10 @@ mod tests {
|
|||
let r = interpreter
|
||||
.execute_solver(
|
||||
&cond_eq,
|
||||
&inputs.iter().map(|&i| Bn128Field::from(i)).collect(),
|
||||
&inputs
|
||||
.iter()
|
||||
.map(|&i| Bn128Field::from(i))
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.unwrap();
|
||||
let res: Vec<Bn128Field> = vec![1, 1].iter().map(|&i| Bn128Field::from(i)).collect();
|
||||
|
|
|
@ -155,10 +155,7 @@ impl<T: Field> Prog<T> {
|
|||
self.main
|
||||
.statements
|
||||
.iter()
|
||||
.filter(|s| match s {
|
||||
Statement::Constraint(..) => true,
|
||||
_ => false,
|
||||
})
|
||||
.filter(|s| matches!(s, Statement::Constraint(..)))
|
||||
.count()
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@ impl<T: Field> Prog<T> {
|
|||
// // deduplicate directives which take the same input
|
||||
let r = DirectiveOptimizer::optimize(r);
|
||||
// remove duplicate constraints
|
||||
let r = DuplicateOptimizer::optimize(r);
|
||||
r
|
||||
DuplicateOptimizer::optimize(r)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -139,7 +139,7 @@ impl<T: Field> Folder<T> for RedefinitionOptimizer<T> {
|
|||
match inputs.iter().all(|r| r.is_ok()) {
|
||||
true => {
|
||||
// unwrap inputs to their constant value
|
||||
let inputs = inputs.into_iter().map(|i| i.unwrap()).collect();
|
||||
let inputs = inputs.into_iter().map(|i| i.unwrap()).collect::<Vec<_>>();
|
||||
// run the interpereter
|
||||
let outputs = Interpreter::default()
|
||||
.execute_solver(&d.solver, &inputs)
|
||||
|
|
|
@ -49,39 +49,33 @@ fn ark_combination<T: Field + ArkFieldExtensions>(
|
|||
cs: &mut ConstraintSystem<<<T as ArkFieldExtensions>::ArkEngine as PairingEngine>::Fr>,
|
||||
symbols: &mut BTreeMap<FlatVariable, Variable>,
|
||||
witness: &mut Witness<T>,
|
||||
) -> Result<
|
||||
LinearCombination<<<T as ArkFieldExtensions>::ArkEngine as PairingEngine>::Fr>,
|
||||
SynthesisError,
|
||||
> {
|
||||
let lc =
|
||||
l.0.into_iter()
|
||||
.map(|(k, v)| {
|
||||
(
|
||||
v.into_ark(),
|
||||
*symbols.entry(k).or_insert_with(|| {
|
||||
match k.is_output() {
|
||||
true => cs.new_input_variable(|| {
|
||||
Ok(witness
|
||||
.0
|
||||
.remove(&k)
|
||||
.ok_or(SynthesisError::AssignmentMissing)?
|
||||
.into_ark())
|
||||
}),
|
||||
false => cs.new_witness_variable(|| {
|
||||
Ok(witness
|
||||
.0
|
||||
.remove(&k)
|
||||
.ok_or(SynthesisError::AssignmentMissing)?
|
||||
.into_ark())
|
||||
}),
|
||||
}
|
||||
.unwrap()
|
||||
}),
|
||||
)
|
||||
})
|
||||
.fold(LinearCombination::zero(), |acc, e| acc + e);
|
||||
|
||||
Ok(lc)
|
||||
) -> LinearCombination<<<T as ArkFieldExtensions>::ArkEngine as PairingEngine>::Fr> {
|
||||
l.0.into_iter()
|
||||
.map(|(k, v)| {
|
||||
(
|
||||
v.into_ark(),
|
||||
*symbols.entry(k).or_insert_with(|| {
|
||||
match k.is_output() {
|
||||
true => cs.new_input_variable(|| {
|
||||
Ok(witness
|
||||
.0
|
||||
.remove(&k)
|
||||
.ok_or(SynthesisError::AssignmentMissing)?
|
||||
.into_ark())
|
||||
}),
|
||||
false => cs.new_witness_variable(|| {
|
||||
Ok(witness
|
||||
.0
|
||||
.remove(&k)
|
||||
.ok_or(SynthesisError::AssignmentMissing)?
|
||||
.into_ark())
|
||||
}),
|
||||
}
|
||||
.unwrap()
|
||||
}),
|
||||
)
|
||||
})
|
||||
.fold(LinearCombination::zero(), |acc, e| acc + e)
|
||||
}
|
||||
|
||||
impl<T: Field + ArkFieldExtensions> Prog<T> {
|
||||
|
@ -93,7 +87,7 @@ impl<T: Field + ArkFieldExtensions> Prog<T> {
|
|||
// mapping from IR variables
|
||||
let mut symbols = BTreeMap::new();
|
||||
|
||||
let mut witness = witness.unwrap_or(Witness::empty());
|
||||
let mut witness = witness.unwrap_or_else(Witness::empty);
|
||||
|
||||
assert!(symbols.insert(FlatVariable::one(), ConstraintSystem::<<<T as ArkFieldExtensions>::ArkEngine as PairingEngine>::Fr>::one()).is_none());
|
||||
|
||||
|
@ -131,30 +125,27 @@ impl<T: Field + ArkFieldExtensions> Prog<T> {
|
|||
let main = self.main;
|
||||
|
||||
for statement in main.statements {
|
||||
match statement {
|
||||
Statement::Constraint(quad, lin) => {
|
||||
let a = ark_combination(
|
||||
quad.left.clone().into_canonical(),
|
||||
&mut cs,
|
||||
&mut symbols,
|
||||
&mut witness,
|
||||
)?;
|
||||
let b = ark_combination(
|
||||
quad.right.clone().into_canonical(),
|
||||
&mut cs,
|
||||
&mut symbols,
|
||||
&mut witness,
|
||||
)?;
|
||||
let c = ark_combination(
|
||||
lin.into_canonical(),
|
||||
&mut cs,
|
||||
&mut symbols,
|
||||
&mut witness,
|
||||
)?;
|
||||
if let Statement::Constraint(quad, lin) = statement {
|
||||
let a = ark_combination(
|
||||
quad.left.clone().into_canonical(),
|
||||
&mut cs,
|
||||
&mut symbols,
|
||||
&mut witness,
|
||||
);
|
||||
let b = ark_combination(
|
||||
quad.right.clone().into_canonical(),
|
||||
&mut cs,
|
||||
&mut symbols,
|
||||
&mut witness,
|
||||
);
|
||||
let c = ark_combination(
|
||||
lin.into_canonical(),
|
||||
&mut cs,
|
||||
&mut symbols,
|
||||
&mut witness,
|
||||
);
|
||||
|
||||
cs.enforce_constraint(a, b, c)?;
|
||||
}
|
||||
_ => {}
|
||||
cs.enforce_constraint(a, b, c)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -90,7 +90,7 @@ impl<T: BellmanFieldExtensions + Field> Prog<T> {
|
|||
// mapping from IR variables
|
||||
let mut symbols = BTreeMap::new();
|
||||
|
||||
let mut witness = witness.unwrap_or(Witness::empty());
|
||||
let mut witness = witness.unwrap_or_else(Witness::empty);
|
||||
|
||||
assert!(symbols.insert(FlatVariable::one(), CS::one()).is_none());
|
||||
|
||||
|
@ -131,26 +131,22 @@ impl<T: BellmanFieldExtensions + Field> Prog<T> {
|
|||
let main = self.main;
|
||||
|
||||
for statement in main.statements {
|
||||
match statement {
|
||||
Statement::Constraint(quad, lin) => {
|
||||
let a = &bellman_combination(
|
||||
quad.left.into_canonical(),
|
||||
cs,
|
||||
&mut symbols,
|
||||
&mut witness,
|
||||
);
|
||||
let b = &bellman_combination(
|
||||
quad.right.into_canonical(),
|
||||
cs,
|
||||
&mut symbols,
|
||||
&mut witness,
|
||||
);
|
||||
let c =
|
||||
&bellman_combination(lin.into_canonical(), cs, &mut symbols, &mut witness);
|
||||
if let Statement::Constraint(quad, lin) = statement {
|
||||
let a = &bellman_combination(
|
||||
quad.left.into_canonical(),
|
||||
cs,
|
||||
&mut symbols,
|
||||
&mut witness,
|
||||
);
|
||||
let b = &bellman_combination(
|
||||
quad.right.into_canonical(),
|
||||
cs,
|
||||
&mut symbols,
|
||||
&mut witness,
|
||||
);
|
||||
let c = &bellman_combination(lin.into_canonical(), cs, &mut symbols, &mut witness);
|
||||
|
||||
cs.enforce(|| "Constraint", |lc| lc + a, |lc| lc + b, |lc| lc + c);
|
||||
}
|
||||
_ => {}
|
||||
cs.enforce(|| "Constraint", |lc| lc + a, |lc| lc + b, |lc| lc + c);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2840,7 +2840,7 @@ impl<'ast, T: Field> Checker<'ast, T> {
|
|||
|
||||
// check that we provided the required number of values
|
||||
|
||||
if struct_type.len() != inline_members.len() {
|
||||
if struct_type.members_count() != inline_members.len() {
|
||||
return Err(ErrorInner {
|
||||
pos: Some(pos),
|
||||
message: format!(
|
||||
|
|
|
@ -70,7 +70,6 @@ impl<T: Field> Analyse for FlatProg<T> {
|
|||
|
||||
impl<T: Field> Analyse for Prog<T> {
|
||||
fn analyse(self) -> Self {
|
||||
let r = UnconstrainedVariableDetector::detect(self);
|
||||
r
|
||||
UnconstrainedVariableDetector::detect(self)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,7 +92,7 @@ impl<'ast, 'a, T: Field> Propagator<'ast, 'a, T> {
|
|||
}
|
||||
}
|
||||
|
||||
fn is_constant<'ast, T: Field>(e: &TypedExpression<'ast, T>) -> bool {
|
||||
fn is_constant<T: Field>(e: &TypedExpression<T>) -> bool {
|
||||
match e {
|
||||
TypedExpression::FieldElement(FieldElementExpression::Number(..)) => true,
|
||||
TypedExpression::Boolean(BooleanExpression::Value(..)) => true,
|
||||
|
|
|
@ -107,7 +107,7 @@ impl<'ast, T: Field> Folder<'ast, T> for Unroller<'ast> {
|
|||
current = T::one() + ¤t;
|
||||
}
|
||||
|
||||
let res = values
|
||||
values
|
||||
.into_iter()
|
||||
.map(|index| {
|
||||
vec![
|
||||
|
@ -125,9 +125,7 @@ impl<'ast, T: Field> Folder<'ast, T> for Unroller<'ast> {
|
|||
})
|
||||
.flatten()
|
||||
.flat_map(|x| self.fold_statement(x))
|
||||
.collect();
|
||||
|
||||
res
|
||||
.collect()
|
||||
}
|
||||
(from, to) => {
|
||||
self.complete = false;
|
||||
|
|
|
@ -171,7 +171,6 @@ pub fn fold_module<'ast, T: Field, F: Folder<'ast, T>>(
|
|||
.into_iter()
|
||||
.map(|(key, fun)| (key, f.fold_function_symbol(fun)))
|
||||
.collect(),
|
||||
..p
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -358,7 +358,7 @@ impl<S> GStructType<S> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
pub fn members_count(&self) -> usize {
|
||||
self.members.len()
|
||||
}
|
||||
|
||||
|
|
|
@ -111,7 +111,6 @@ pub fn fold_module<'ast, T: Field, F: Folder<'ast, T>>(
|
|||
.into_iter()
|
||||
.map(|(key, fun)| (key, f.fold_function_symbol(fun)))
|
||||
.collect(),
|
||||
..p
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -140,7 +140,7 @@ pub mod signature {
|
|||
use super::*;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Ord, PartialOrd)]
|
||||
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Ord, PartialOrd, Default)]
|
||||
pub struct Signature {
|
||||
pub inputs: Vec<Type>,
|
||||
pub outputs: Vec<Type>,
|
||||
|
@ -226,10 +226,7 @@ pub mod signature {
|
|||
}
|
||||
|
||||
pub fn new() -> Signature {
|
||||
Signature {
|
||||
inputs: vec![],
|
||||
outputs: vec![],
|
||||
}
|
||||
Signature::default()
|
||||
}
|
||||
|
||||
pub fn inputs(mut self, inputs: Vec<Type>) -> Self {
|
||||
|
|
|
@ -105,10 +105,7 @@ impl ShouldReduce {
|
|||
}
|
||||
|
||||
pub fn is_unknown(&self) -> bool {
|
||||
match self {
|
||||
ShouldReduce::Unknown => true,
|
||||
_ => false,
|
||||
}
|
||||
*self == ShouldReduce::Unknown
|
||||
}
|
||||
|
||||
// we can always enable a reduction
|
||||
|
|
|
@ -16,6 +16,7 @@ use num_bigint::BigUint;
|
|||
use num_traits::{CheckedDiv, One, Zero};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::convert::{From, TryFrom};
|
||||
use std::fmt;
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::hash::Hash;
|
||||
use std::ops::{Add, Div, Mul, Sub};
|
||||
|
@ -44,6 +45,14 @@ pub trait ArkFieldExtensions {
|
|||
fn into_ark(self) -> <Self::ArkEngine as ark_ec::PairingEngine>::Fr;
|
||||
}
|
||||
|
||||
pub struct FieldParseError;
|
||||
|
||||
impl fmt::Debug for FieldParseError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Failed to parse to field element")
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Field:
|
||||
From<i32>
|
||||
+ From<u32>
|
||||
|
@ -94,8 +103,8 @@ pub trait Field:
|
|||
/// Returns the number of bits required to represent any element of this field type.
|
||||
fn get_required_bits() -> usize;
|
||||
/// Tries to parse a string into this representation
|
||||
fn try_from_dec_str(s: &str) -> Result<Self, ()>;
|
||||
fn try_from_str(s: &str, radix: u32) -> Result<Self, ()>;
|
||||
fn try_from_dec_str(s: &str) -> Result<Self, FieldParseError>;
|
||||
fn try_from_str(s: &str, radix: u32) -> Result<Self, FieldParseError>;
|
||||
/// Returns a decimal string representing a the member of the equivalence class of this `Field` in Z/pZ
|
||||
/// which lies in [-(p-1)/2, (p-1)/2]
|
||||
fn to_compact_dec_string(&self) -> String;
|
||||
|
@ -130,7 +139,7 @@ pub trait Field:
|
|||
mod prime_field {
|
||||
macro_rules! prime_field {
|
||||
($modulus:expr, $name:expr) => {
|
||||
use crate::{Field, Pow};
|
||||
use crate::{Field, FieldParseError, Pow};
|
||||
use lazy_static::lazy_static;
|
||||
use num_bigint::{BigInt, BigUint, Sign, ToBigInt};
|
||||
use num_integer::Integer;
|
||||
|
@ -208,11 +217,11 @@ mod prime_field {
|
|||
fn get_required_bits() -> usize {
|
||||
(*P).bits()
|
||||
}
|
||||
fn try_from_dec_str(s: &str) -> Result<Self, ()> {
|
||||
fn try_from_dec_str(s: &str) -> Result<Self, FieldParseError> {
|
||||
Self::try_from_str(s, 10)
|
||||
}
|
||||
fn try_from_str(s: &str, radix: u32) -> Result<Self, ()> {
|
||||
let x = BigInt::parse_bytes(s.as_bytes(), radix).ok_or(())?;
|
||||
fn try_from_str(s: &str, radix: u32) -> Result<Self, FieldParseError> {
|
||||
let x = BigInt::parse_bytes(s.as_bytes(), radix).ok_or(FieldParseError)?;
|
||||
Ok(FieldPrime {
|
||||
value: &x - x.div_floor(&*P) * &*P,
|
||||
})
|
||||
|
|
|
@ -364,6 +364,7 @@ mod ast {
|
|||
#[pest_ast(rule(Rule::vis_private))]
|
||||
pub struct PrivateVisibility {}
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
#[derive(Debug, FromPest, PartialEq, Clone)]
|
||||
#[pest_ast(rule(Rule::statement))]
|
||||
pub enum Statement<'ast> {
|
||||
|
@ -560,6 +561,7 @@ mod ast {
|
|||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
#[derive(Debug, FromPest, PartialEq, Clone)]
|
||||
#[pest_ast(rule(Rule::access))]
|
||||
pub enum Access<'ast> {
|
||||
|
|
|
@ -93,7 +93,7 @@ pub fn test_inner(test_path: &str) {
|
|||
let t: Tests =
|
||||
serde_json::from_reader(BufReader::new(File::open(Path::new(test_path)).unwrap())).unwrap();
|
||||
|
||||
let curves = t.curves.clone().unwrap_or(vec![Curve::Bn128]);
|
||||
let curves = t.curves.clone().unwrap_or_else(|| vec![Curve::Bn128]);
|
||||
|
||||
let t = Tests {
|
||||
entry_point: Some(
|
||||
|
@ -124,17 +124,14 @@ fn compile_and_run<T: Field>(t: Tests) {
|
|||
|
||||
let bin = artifacts.prog();
|
||||
|
||||
match t.max_constraint_count {
|
||||
Some(target_count) => {
|
||||
let count = bin.constraint_count();
|
||||
if let Some(target_count) = t.max_constraint_count {
|
||||
let count = bin.constraint_count();
|
||||
|
||||
println!(
|
||||
"{} at {}% of max",
|
||||
entry_point.display(),
|
||||
(count as f32) / (target_count as f32) * 100_f32
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
println!(
|
||||
"{} at {}% of max",
|
||||
entry_point.display(),
|
||||
(count as f32) / (target_count as f32) * 100_f32
|
||||
);
|
||||
};
|
||||
|
||||
let interpreter = zokrates_core::ir::Interpreter::default();
|
||||
|
@ -142,25 +139,25 @@ fn compile_and_run<T: Field>(t: Tests) {
|
|||
for test in t.tests.into_iter() {
|
||||
let input = &test.input.values;
|
||||
|
||||
let output = interpreter.execute(bin, &(input.iter().cloned().map(parse_val).collect()));
|
||||
let output = interpreter.execute(
|
||||
bin,
|
||||
&(input.iter().cloned().map(parse_val).collect::<Vec<_>>()),
|
||||
);
|
||||
|
||||
match compare(output, test.output) {
|
||||
Err(e) => {
|
||||
let mut code = File::open(&entry_point).unwrap();
|
||||
let mut s = String::new();
|
||||
code.read_to_string(&mut s).unwrap();
|
||||
let context = format!(
|
||||
"\n{}\nCalled with input ({})\n",
|
||||
s,
|
||||
input
|
||||
.iter()
|
||||
.map(|i| i.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
);
|
||||
panic!("{}{}", context, e)
|
||||
}
|
||||
Ok(..) => {}
|
||||
};
|
||||
if let Err(e) = compare(output, test.output) {
|
||||
let mut code = File::open(&entry_point).unwrap();
|
||||
let mut s = String::new();
|
||||
code.read_to_string(&mut s).unwrap();
|
||||
let context = format!(
|
||||
"\n{}\nCalled with input ({})\n",
|
||||
s,
|
||||
input
|
||||
.iter()
|
||||
.map(|i| i.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
);
|
||||
panic!("{}{}", context, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue