diff --git a/crypto-primitives/Cargo.toml b/crypto-primitives/Cargo.toml index 3b937c8..4d3566d 100644 --- a/crypto-primitives/Cargo.toml +++ b/crypto-primitives/Cargo.toml @@ -32,6 +32,7 @@ bench-utils = { path = "../bench-utils" } digest = "0.7" blake2 = "0.7" +num-traits = { version = "0.2.11" } rand = { version = "0.7" } derivative = "1" rayon = "1" diff --git a/crypto-primitives/src/merkle_tree/mod.rs b/crypto-primitives/src/merkle_tree/mod.rs index df65dd0..ea1f8df 100644 --- a/crypto-primitives/src/merkle_tree/mod.rs +++ b/crypto-primitives/src/merkle_tree/mod.rs @@ -372,6 +372,7 @@ mod test { merkle_tree::*, }; use algebra::curves::jubjub::JubJubAffine as JubJub; + use num_traits::Zero; use rand::SeedableRng; use rand_xorshift::XorShiftRng; @@ -419,7 +420,6 @@ mod test { } fn bad_merkle_tree_verify(leaves: &[L]) -> () { - use algebra::groups::Group; let mut rng = XorShiftRng::seed_from_u64(13423423u64); let crh_parameters = Rc::new(H::setup(&mut rng).unwrap()); diff --git a/crypto-primitives/src/nizk/mod.rs b/crypto-primitives/src/nizk/mod.rs index 048c51c..1c30bc7 100644 --- a/crypto-primitives/src/nizk/mod.rs +++ b/crypto-primitives/src/nizk/mod.rs @@ -59,7 +59,8 @@ mod test { #[test] fn test_gm17() { use crate::nizk::{gm17::Gm17, NIZK}; - use algebra::{curves::bls12_381::Bls12_381, fields::bls12_381::Fr, Field}; + use algebra::{curves::bls12_381::Bls12_381, fields::bls12_381::Fr}; + use num_traits::One; use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, SynthesisError}; #[derive(Copy, Clone)] diff --git a/crypto-primitives/src/signature/schnorr/mod.rs b/crypto-primitives/src/signature/schnorr/mod.rs index 29a8c25..9e03e05 100644 --- a/crypto-primitives/src/signature/schnorr/mod.rs +++ b/crypto-primitives/src/signature/schnorr/mod.rs @@ -6,6 +6,7 @@ use algebra::{ to_bytes, ToConstraintField, UniformRand, }; use digest::Digest; +use num_traits::{One, Zero}; use rand::Rng; use std::{ hash::Hash, diff --git a/r1cs-std/Cargo.toml b/r1cs-std/Cargo.toml index a797398..b72a8f0 100644 --- a/r1cs-std/Cargo.toml +++ b/r1cs-std/Cargo.toml @@ -25,6 +25,7 @@ edition = "2018" algebra = { path = "../algebra" } r1cs-core = { path = "../r1cs-core" } derivative = "1" +num-traits = { version = "0.2.11" } radix_trie = "0.1" [dev-dependencies] diff --git a/r1cs-std/src/bits/boolean.rs b/r1cs-std/src/bits/boolean.rs index 0bcbacf..64f9f44 100644 --- a/r1cs-std/src/bits/boolean.rs +++ b/r1cs-std/src/bits/boolean.rs @@ -832,13 +832,12 @@ impl CondSelectGadget for Boolean { } } - - #[cfg(test)] mod test { use super::{AllocatedBit, Boolean}; use crate::{prelude::*, test_constraint_system::TestConstraintSystem}; use algebra::{fields::bls12_381::Fr, BitIterator, Field, PrimeField, UniformRand}; + use num_traits::{One, Zero}; use r1cs_core::ConstraintSystem; use rand::SeedableRng; use rand_xorshift::XorShiftRng; @@ -903,8 +902,8 @@ mod test { assert_eq!(c.value.unwrap(), *a_val | *b_val); assert!(cs.is_satisfied()); - assert!(cs.get("a/boolean") == if *a_val { Field::one() } else { Field::zero() }); - assert!(cs.get("b/boolean") == if *b_val { Field::one() } else { Field::zero() }); + assert!(cs.get("a/boolean") == if *a_val { Fr::one() } else { Fr::zero() }); + assert!(cs.get("b/boolean") == if *b_val { Fr::one() } else { Fr::zero() }); } } } @@ -920,14 +919,14 @@ mod test { assert_eq!(c.value.unwrap(), *a_val & *b_val); assert!(cs.is_satisfied()); - assert!(cs.get("a/boolean") == if *a_val { Field::one() } else { Field::zero() }); - assert!(cs.get("b/boolean") == if *b_val { Field::one() } else { Field::zero() }); + assert!(cs.get("a/boolean") == if *a_val { Fr::one() } else { Fr::zero() }); + assert!(cs.get("b/boolean") == if *b_val { Fr::one() } else { Fr::zero() }); assert!( cs.get("and result") == if *a_val & *b_val { - Field::one() + Fr::one() } else { - Field::zero() + Fr::zero() } ); @@ -935,9 +934,9 @@ mod test { cs.set( "and result", if *a_val & *b_val { - Field::zero() + Fr::zero() } else { - Field::one() + Fr::one() }, ); assert!(!cs.is_satisfied()); @@ -956,14 +955,14 @@ mod test { assert_eq!(c.value.unwrap(), *a_val & !*b_val); assert!(cs.is_satisfied()); - assert!(cs.get("a/boolean") == if *a_val { Field::one() } else { Field::zero() }); - assert!(cs.get("b/boolean") == if *b_val { Field::one() } else { Field::zero() }); + assert!(cs.get("a/boolean") == if *a_val { Fr::one() } else { Fr::zero() }); + assert!(cs.get("b/boolean") == if *b_val { Fr::one() } else { Fr::zero() }); assert!( cs.get("and not result") == if *a_val & !*b_val { - Field::one() + Fr::one() } else { - Field::zero() + Fr::zero() } ); @@ -971,9 +970,9 @@ mod test { cs.set( "and not result", if *a_val & !*b_val { - Field::zero() + Fr::zero() } else { - Field::one() + Fr::one() }, ); assert!(!cs.is_satisfied()); @@ -992,14 +991,14 @@ mod test { assert_eq!(c.value.unwrap(), !*a_val & !*b_val); assert!(cs.is_satisfied()); - assert!(cs.get("a/boolean") == if *a_val { Field::one() } else { Field::zero() }); - assert!(cs.get("b/boolean") == if *b_val { Field::one() } else { Field::zero() }); + assert!(cs.get("a/boolean") == if *a_val { Fr::one() } else { Fr::zero() }); + assert!(cs.get("b/boolean") == if *b_val { Fr::one() } else { Fr::zero() }); assert!( cs.get("nor result") == if !*a_val & !*b_val { - Field::one() + Fr::one() } else { - Field::zero() + Fr::zero() } ); @@ -1007,9 +1006,9 @@ mod test { cs.set( "nor result", if !*a_val & !*b_val { - Field::zero() + Fr::zero() } else { - Field::one() + Fr::one() }, ); assert!(!cs.is_satisfied()); @@ -1235,7 +1234,7 @@ mod test { OperandType::AllocatedTrue, Boolean::Is(ref v), ) => { - assert!(cs.get("xor result") == Field::zero()); + assert!(cs.get("xor result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1243,7 +1242,7 @@ mod test { OperandType::AllocatedFalse, Boolean::Is(ref v), ) => { - assert!(cs.get("xor result") == Field::one()); + assert!(cs.get("xor result") == Fr::one()); assert_eq!(v.value, Some(true)); }, ( @@ -1251,7 +1250,7 @@ mod test { OperandType::NegatedAllocatedTrue, Boolean::Not(ref v), ) => { - assert!(cs.get("xor result") == Field::zero()); + assert!(cs.get("xor result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1259,7 +1258,7 @@ mod test { OperandType::NegatedAllocatedFalse, Boolean::Not(ref v), ) => { - assert!(cs.get("xor result") == Field::one()); + assert!(cs.get("xor result") == Fr::one()); assert_eq!(v.value, Some(true)); }, @@ -1270,7 +1269,7 @@ mod test { OperandType::AllocatedTrue, Boolean::Is(ref v), ) => { - assert!(cs.get("xor result") == Field::one()); + assert!(cs.get("xor result") == Fr::one()); assert_eq!(v.value, Some(true)); }, ( @@ -1278,7 +1277,7 @@ mod test { OperandType::AllocatedFalse, Boolean::Is(ref v), ) => { - assert!(cs.get("xor result") == Field::zero()); + assert!(cs.get("xor result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1286,7 +1285,7 @@ mod test { OperandType::NegatedAllocatedTrue, Boolean::Not(ref v), ) => { - assert!(cs.get("xor result") == Field::one()); + assert!(cs.get("xor result") == Fr::one()); assert_eq!(v.value, Some(true)); }, ( @@ -1294,7 +1293,7 @@ mod test { OperandType::NegatedAllocatedFalse, Boolean::Not(ref v), ) => { - assert!(cs.get("xor result") == Field::zero()); + assert!(cs.get("xor result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, @@ -1305,7 +1304,7 @@ mod test { OperandType::AllocatedTrue, Boolean::Not(ref v), ) => { - assert!(cs.get("xor result") == Field::zero()); + assert!(cs.get("xor result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1313,7 +1312,7 @@ mod test { OperandType::AllocatedFalse, Boolean::Not(ref v), ) => { - assert!(cs.get("xor result") == Field::one()); + assert!(cs.get("xor result") == Fr::one()); assert_eq!(v.value, Some(true)); }, ( @@ -1321,7 +1320,7 @@ mod test { OperandType::NegatedAllocatedTrue, Boolean::Is(ref v), ) => { - assert!(cs.get("xor result") == Field::zero()); + assert!(cs.get("xor result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1329,7 +1328,7 @@ mod test { OperandType::NegatedAllocatedFalse, Boolean::Is(ref v), ) => { - assert!(cs.get("xor result") == Field::one()); + assert!(cs.get("xor result") == Fr::one()); assert_eq!(v.value, Some(true)); }, @@ -1340,7 +1339,7 @@ mod test { OperandType::AllocatedTrue, Boolean::Not(ref v), ) => { - assert!(cs.get("xor result") == Field::one()); + assert!(cs.get("xor result") == Fr::one()); assert_eq!(v.value, Some(true)); }, ( @@ -1348,7 +1347,7 @@ mod test { OperandType::AllocatedFalse, Boolean::Not(ref v), ) => { - assert!(cs.get("xor result") == Field::zero()); + assert!(cs.get("xor result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1356,7 +1355,7 @@ mod test { OperandType::NegatedAllocatedTrue, Boolean::Is(ref v), ) => { - assert!(cs.get("xor result") == Field::one()); + assert!(cs.get("xor result") == Fr::one()); assert_eq!(v.value, Some(true)); }, ( @@ -1364,7 +1363,7 @@ mod test { OperandType::NegatedAllocatedFalse, Boolean::Is(ref v), ) => { - assert!(cs.get("xor result") == Field::zero()); + assert!(cs.get("xor result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, @@ -1732,7 +1731,7 @@ mod test { OperandType::AllocatedTrue, Boolean::Is(ref v), ) => { - assert!(cs.get("and result") == Field::one()); + assert!(cs.get("and result") == Fr::one()); assert_eq!(v.value, Some(true)); }, ( @@ -1740,7 +1739,7 @@ mod test { OperandType::AllocatedFalse, Boolean::Is(ref v), ) => { - assert!(cs.get("and result") == Field::zero()); + assert!(cs.get("and result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1748,7 +1747,7 @@ mod test { OperandType::NegatedAllocatedTrue, Boolean::Is(ref v), ) => { - assert!(cs.get("and not result") == Field::zero()); + assert!(cs.get("and not result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1756,7 +1755,7 @@ mod test { OperandType::NegatedAllocatedFalse, Boolean::Is(ref v), ) => { - assert!(cs.get("and not result") == Field::one()); + assert!(cs.get("and not result") == Fr::one()); assert_eq!(v.value, Some(true)); }, @@ -1768,7 +1767,7 @@ mod test { OperandType::AllocatedTrue, Boolean::Is(ref v), ) => { - assert!(cs.get("and result") == Field::zero()); + assert!(cs.get("and result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1776,7 +1775,7 @@ mod test { OperandType::AllocatedFalse, Boolean::Is(ref v), ) => { - assert!(cs.get("and result") == Field::zero()); + assert!(cs.get("and result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1784,7 +1783,7 @@ mod test { OperandType::NegatedAllocatedTrue, Boolean::Is(ref v), ) => { - assert!(cs.get("and not result") == Field::zero()); + assert!(cs.get("and not result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1792,7 +1791,7 @@ mod test { OperandType::NegatedAllocatedFalse, Boolean::Is(ref v), ) => { - assert!(cs.get("and not result") == Field::zero()); + assert!(cs.get("and not result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, @@ -1807,7 +1806,7 @@ mod test { OperandType::AllocatedTrue, Boolean::Is(ref v), ) => { - assert!(cs.get("and not result") == Field::zero()); + assert!(cs.get("and not result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1815,7 +1814,7 @@ mod test { OperandType::AllocatedFalse, Boolean::Is(ref v), ) => { - assert!(cs.get("and not result") == Field::zero()); + assert!(cs.get("and not result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1823,7 +1822,7 @@ mod test { OperandType::NegatedAllocatedTrue, Boolean::Is(ref v), ) => { - assert!(cs.get("nor result") == Field::zero()); + assert!(cs.get("nor result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1831,7 +1830,7 @@ mod test { OperandType::NegatedAllocatedFalse, Boolean::Is(ref v), ) => { - assert!(cs.get("nor result") == Field::zero()); + assert!(cs.get("nor result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, @@ -1846,7 +1845,7 @@ mod test { OperandType::AllocatedTrue, Boolean::Is(ref v), ) => { - assert!(cs.get("and not result") == Field::one()); + assert!(cs.get("and not result") == Fr::one()); assert_eq!(v.value, Some(true)); }, ( @@ -1854,7 +1853,7 @@ mod test { OperandType::AllocatedFalse, Boolean::Is(ref v), ) => { - assert!(cs.get("and not result") == Field::zero()); + assert!(cs.get("and not result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1862,7 +1861,7 @@ mod test { OperandType::NegatedAllocatedTrue, Boolean::Is(ref v), ) => { - assert!(cs.get("nor result") == Field::zero()); + assert!(cs.get("nor result") == Fr::zero()); assert_eq!(v.value, Some(false)); }, ( @@ -1870,7 +1869,7 @@ mod test { OperandType::NegatedAllocatedFalse, Boolean::Is(ref v), ) => { - assert!(cs.get("nor result") == Field::one()); + assert!(cs.get("nor result") == Fr::one()); assert_eq!(v.value, Some(true)); }, diff --git a/r1cs-std/src/bits/uint32.rs b/r1cs-std/src/bits/uint32.rs index 8349df6..eb42a32 100644 --- a/r1cs-std/src/bits/uint32.rs +++ b/r1cs-std/src/bits/uint32.rs @@ -344,7 +344,8 @@ impl ConditionalEqGadget for UInt32 { mod test { use super::UInt32; use crate::{bits::boolean::Boolean, test_constraint_system::TestConstraintSystem}; - use algebra::fields::{bls12_381::Fr, Field}; + use algebra::fields::bls12_381::Fr; + use num_traits::{One, Zero}; use r1cs_core::ConstraintSystem; use rand::{Rng, SeedableRng}; use rand_xorshift::XorShiftRng; @@ -500,9 +501,9 @@ mod test { // Flip a bit_gadget and see if the addition constraint still works if cs.get("addition/result bit_gadget 0/boolean").is_zero() { - cs.set("addition/result bit_gadget 0/boolean", Field::one()); + cs.set("addition/result bit_gadget 0/boolean", Fr::one()); } else { - cs.set("addition/result bit_gadget 0/boolean", Field::zero()); + cs.set("addition/result bit_gadget 0/boolean", Fr::zero()); } assert!(!cs.is_satisfied()); diff --git a/r1cs-std/src/fields/fp12.rs b/r1cs-std/src/fields/fp12.rs index 22f5be8..1ae353a 100644 --- a/r1cs-std/src/fields/fp12.rs +++ b/r1cs-std/src/fields/fp12.rs @@ -8,6 +8,7 @@ use algebra::{ }, BitIterator, Field, PrimeField, }; +use num_traits::One; use std::{borrow::Borrow, marker::PhantomData}; use crate::{prelude::*, Assignment}; diff --git a/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs b/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs index 88f333a..7c5ee8b 100644 --- a/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs +++ b/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs @@ -3,6 +3,7 @@ use algebra::{ fields::Field, BitIterator, ProjectiveCurve, }; +use num_traits::One; use r1cs_core::{ConstraintSystem, SynthesisError}; use crate::{ diff --git a/r1cs-std/src/groups/curves/short_weierstrass/mod.rs b/r1cs-std/src/groups/curves/short_weierstrass/mod.rs index 55ad019..77801b0 100644 --- a/r1cs-std/src/groups/curves/short_weierstrass/mod.rs +++ b/r1cs-std/src/groups/curves/short_weierstrass/mod.rs @@ -5,6 +5,7 @@ use algebra::{ }, AffineCurve, BitIterator, Field, PrimeField, ProjectiveCurve, }; +use num_traits::{One, Zero}; use r1cs_core::{ConstraintSystem, SynthesisError}; use std::{borrow::Borrow, marker::PhantomData, ops::Neg}; diff --git a/r1cs-std/src/groups/curves/twisted_edwards/mod.rs b/r1cs-std/src/groups/curves/twisted_edwards/mod.rs index 4951131..720a8d9 100644 --- a/r1cs-std/src/groups/curves/twisted_edwards/mod.rs +++ b/r1cs-std/src/groups/curves/twisted_edwards/mod.rs @@ -5,6 +5,7 @@ use algebra::{ }, BitIterator, Field, }; +use num_traits::{One, Zero}; use r1cs_core::{ConstraintSystem, SynthesisError}; @@ -38,7 +39,7 @@ pub struct MontgomeryAffineGadget< mod montgomery_affine_impl { use super::*; use crate::Assignment; - use algebra::{twisted_edwards_extended::GroupAffine, AffineCurve, Field}; + use algebra::{twisted_edwards_extended::GroupAffine, Field}; use std::ops::{AddAssign, MulAssign, SubAssign}; impl> diff --git a/r1cs-std/src/pairing/mod.rs b/r1cs-std/src/pairing/mod.rs index e1ad84c..353dab3 100644 --- a/r1cs-std/src/pairing/mod.rs +++ b/r1cs-std/src/pairing/mod.rs @@ -60,6 +60,7 @@ mod test { // use rand; use crate::test_constraint_system::TestConstraintSystem; use algebra::{BitIterator, Field}; + use num_traits::One; use r1cs_core::ConstraintSystem; #[test]