Browse Source

[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.
master
François Garillot 5 years ago
committed by Pratyush Mishra
parent
commit
722a901ae7
12 changed files with 69 additions and 60 deletions
  1. +1
    -0
      crypto-primitives/Cargo.toml
  2. +1
    -1
      crypto-primitives/src/merkle_tree/mod.rs
  3. +2
    -1
      crypto-primitives/src/nizk/mod.rs
  4. +1
    -0
      crypto-primitives/src/signature/schnorr/mod.rs
  5. +1
    -0
      r1cs-std/Cargo.toml
  6. +53
    -54
      r1cs-std/src/bits/boolean.rs
  7. +4
    -3
      r1cs-std/src/bits/uint32.rs
  8. +1
    -0
      r1cs-std/src/fields/fp12.rs
  9. +1
    -0
      r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs
  10. +1
    -0
      r1cs-std/src/groups/curves/short_weierstrass/mod.rs
  11. +2
    -1
      r1cs-std/src/groups/curves/twisted_edwards/mod.rs
  12. +1
    -0
      r1cs-std/src/pairing/mod.rs

+ 1
- 0
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"

+ 1
- 1
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<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());

+ 2
- 1
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)]

+ 1
- 0
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,

+ 1
- 0
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]

+ 53
- 54
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));
},

+ 4
- 3
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());

+ 1
- 0
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};

+ 1
- 0
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::{

+ 1
- 0
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};

+ 2
- 1
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<P: TEModelParameters, ConstraintF: Field, F: FieldGadget<P::BaseField, ConstraintF>>

+ 1
- 0
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]

Loading…
Cancel
Save