[RFC] Convert identity functions in Field, Group, and {Projective,Affine}Curve traits with One/Zero traits from num_traits.
- contributes to #50,
- depends on #53 and builds on it,
- due to coherence & requirements of `num_traits::{Zero, One}` to implement `std::ops::Add<Self, ..>` and (resp.) `std::ops::Mul<Self, ..>`, I've had to replace the afferent `impl<'a, P: ..> (Add|Mul)<&'a Self> for Group(Affine|Projective)<P>` by direct implementations on `Self`,
- I did not have to fight the borrow checker for this conversion => I think this hints arithmetic operations are called in contexts where the operand is owned,
- hence should this end up on a merge track, we may want to open an issue to convert the `impl<'a, P:..> (Neg|Sub|..)<&'a Self> for ..<P>` trait usage to direct `impl<P:..> (Neg|Sub|..)<Self> for ..<P>`
- the `impl AddAssign for GroupAffine<P>` in curves/models/short_weierstrass_jacobian.rs is provided to fit trait bounds, and without any guarantee of suitability for any particular purpose
- and that, even though I don't think it's used.
jump to
@@ -32,6 +32,7 @@
digest = "0.7" blake2 = "0.7" +num-traits = { version = "0.2.11" } rand = { version = "0.7" } derivative = "1" rayon = "1"
@@ -372,6 +372,7 @@ crh::{pedersen::*, *},
merkle_tree::*, }; use algebra::curves::jubjub::JubJubAffine as JubJub; + use num_traits::Zero; use rand::SeedableRng; use rand_xorshift::XorShiftRng;@@ -419,7 +420,6 @@ generate_merkle_tree(&leaves);
} fn bad_merkle_tree_verify<L: ToBytes + Clone + Eq>(leaves: &[L]) -> () { - use algebra::groups::Group; let mut rng = XorShiftRng::seed_from_u64(13423423u64); let crh_parameters = Rc::new(H::setup(&mut rng).unwrap());
@@ -59,7 +59,8 @@
#[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)]
@@ -6,6 +6,7 @@ groups::Group,
to_bytes, ToConstraintField, UniformRand, }; use digest::Digest; +use num_traits::{One, Zero}; use rand::Rng; use std::{ hash::Hash,
@@ -25,6 +25,7 @@ [dependencies]
algebra = { path = "../algebra" } r1cs-core = { path = "../r1cs-core" } derivative = "1" +num-traits = { version = "0.2.11" } radix_trie = "0.1" [dev-dependencies]
@@ -832,13 +832,12 @@ 1
} } - - #[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 @@ let c = AllocatedBit::or(&mut cs, &a, &b).unwrap();
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 @@ let c = AllocatedBit::and(&mut cs, &a, &b).unwrap();
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 @@ // Invert the result and check if the constraint system is still satisfied
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 @@ let c = AllocatedBit::and_not(&mut cs, &a, &b).unwrap();
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 @@ // Invert the result and check if the constraint system is still satisfied
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 @@ let c = AllocatedBit::nor(&mut cs, &a, &b).unwrap();
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 @@ // Invert the result and check if the constraint system is still satisfied
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 @@ OperandType::AllocatedTrue,
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 @@ OperandType::AllocatedTrue,
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 @@ OperandType::AllocatedTrue,
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 @@ OperandType::AllocatedTrue,
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 @@ OperandType::AllocatedFalse,
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 @@ OperandType::AllocatedFalse,
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 @@ OperandType::AllocatedFalse,
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 @@ OperandType::AllocatedFalse,
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 @@ OperandType::NegatedAllocatedTrue,
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 @@ OperandType::NegatedAllocatedTrue,
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 @@ OperandType::NegatedAllocatedTrue,
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 @@ OperandType::NegatedAllocatedTrue,
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 @@ OperandType::NegatedAllocatedFalse,
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 @@ OperandType::NegatedAllocatedFalse,
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 @@ OperandType::NegatedAllocatedFalse,
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 @@ OperandType::NegatedAllocatedFalse,
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 @@ OperandType::AllocatedTrue,
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 @@ OperandType::AllocatedTrue,
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 @@ OperandType::AllocatedTrue,
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 @@ OperandType::AllocatedTrue,
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 @@ OperandType::AllocatedFalse,
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 @@ OperandType::AllocatedFalse,
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 @@ OperandType::AllocatedFalse,
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 @@ OperandType::AllocatedFalse,
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 @@ OperandType::NegatedAllocatedTrue,
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 @@ OperandType::NegatedAllocatedTrue,
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 @@ OperandType::NegatedAllocatedTrue,
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 @@ OperandType::NegatedAllocatedTrue,
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 @@ OperandType::NegatedAllocatedFalse,
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 @@ OperandType::NegatedAllocatedFalse,
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 @@ OperandType::NegatedAllocatedFalse,
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 @@ OperandType::NegatedAllocatedFalse,
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)); },
@@ -344,7 +344,8 @@ #[cfg(test)]
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 @@ }
// 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());
@@ -8,6 +8,7 @@ Fp2Parameters,
}, BitIterator, Field, PrimeField, }; +use num_traits::One; use std::{borrow::Borrow, marker::PhantomData}; use crate::{prelude::*, Assignment};
@@ -3,6 +3,7 @@ curves::bls12::{Bls12Parameters, G1Prepared, TwistType},
fields::Field, BitIterator, ProjectiveCurve, }; +use num_traits::One; use r1cs_core::{ConstraintSystem, SynthesisError}; use crate::{
@@ -5,6 +5,7 @@ SWModelParameters,
}, AffineCurve, BitIterator, Field, PrimeField, ProjectiveCurve, }; +use num_traits::{One, Zero}; use r1cs_core::{ConstraintSystem, SynthesisError}; use std::{borrow::Borrow, marker::PhantomData, ops::Neg};
@@ -5,6 +5,7 @@ TEModelParameters,
}, BitIterator, Field, }; +use num_traits::{One, Zero}; use r1cs_core::{ConstraintSystem, SynthesisError};@@ -38,7 +39,7 @@
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<P: TEModelParameters, ConstraintF: Field, F: FieldGadget<P::BaseField, ConstraintF>>
@@ -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]