@ -1,8 +0,0 @@ |
|||||
use algebra::fields::bls12_377::{Fq, Fq12Parameters, Fq2Parameters, Fq6Parameters};
|
|
||||
|
|
||||
use super::{fp::FpGadget, fp12::Fp12Gadget, fp2::Fp2Gadget, fp6_3over2::Fp6Gadget};
|
|
||||
|
|
||||
pub type FqGadget = FpGadget<Fq>;
|
|
||||
pub type Fq2Gadget = Fp2Gadget<Fq2Parameters, Fq>;
|
|
||||
pub type Fq6Gadget = Fp6Gadget<Fq6Parameters, Fq>;
|
|
||||
pub type Fq12Gadget = Fp12Gadget<Fq12Parameters, Fq>;
|
|
@ -1,4 +0,0 @@ |
|||||
use crate::fields::fp::FpGadget;
|
|
||||
use algebra::fields::edwards_bls12::fq::Fq;
|
|
||||
|
|
||||
pub type FqGadget = FpGadget<Fq>;
|
|
@ -1,4 +0,0 @@ |
|||||
use crate::fields::fp::FpGadget;
|
|
||||
use algebra::fields::edwards_sw6::fq::Fq;
|
|
||||
|
|
||||
pub type FqGadget = FpGadget<Fq>;
|
|
@ -1,6 +0,0 @@ |
|||||
use algebra::fields::jubjub::fq::Fq;
|
|
||||
|
|
||||
use crate::fields::fp::FpGadget;
|
|
||||
|
|
||||
// JubJub Fq uses BLS12-381 Fr.
|
|
||||
pub type FqGadget = FpGadget<Fq>;
|
|
@ -1,30 +0,0 @@ |
|||||
use crate::groups::curves::twisted_edwards::AffineGadget;
|
|
||||
use algebra::{curves::edwards_bls12::EdwardsParameters, fields::edwards_bls12::fq::Fq};
|
|
||||
|
|
||||
use crate::fields::edwards_bls12::FqGadget;
|
|
||||
|
|
||||
pub type EdwardsBlsGadget = AffineGadget<EdwardsParameters, Fq, FqGadget>;
|
|
||||
|
|
||||
#[cfg(test)]
|
|
||||
mod test {
|
|
||||
use super::EdwardsBlsGadget as EdwardsG;
|
|
||||
use crate::{
|
|
||||
groups::curves::twisted_edwards::test::{edwards_constraint_costs, edwards_test},
|
|
||||
test_constraint_system::TestConstraintSystem,
|
|
||||
};
|
|
||||
use algebra::{curves::edwards_bls12::EdwardsParameters, fields::edwards_bls12::fq::Fq};
|
|
||||
|
|
||||
#[test]
|
|
||||
fn edwards_constraint_costs_test() {
|
|
||||
let mut cs = TestConstraintSystem::<Fq>::new();
|
|
||||
edwards_constraint_costs::<_, EdwardsParameters, EdwardsG, _>(&mut cs);
|
|
||||
assert!(cs.is_satisfied());
|
|
||||
}
|
|
||||
|
|
||||
#[test]
|
|
||||
fn edwards_bls12_gadget_test() {
|
|
||||
let mut cs = TestConstraintSystem::<Fq>::new();
|
|
||||
edwards_test::<_, EdwardsParameters, EdwardsG, _>(&mut cs);
|
|
||||
assert!(cs.is_satisfied());
|
|
||||
}
|
|
||||
}
|
|
@ -1,30 +0,0 @@ |
|||||
use crate::groups::curves::twisted_edwards::AffineGadget;
|
|
||||
use algebra::{curves::edwards_sw6::EdwardsParameters, fields::edwards_sw6::fq::Fq};
|
|
||||
|
|
||||
use crate::fields::edwards_sw6::FqGadget;
|
|
||||
|
|
||||
pub type EdwardsSWGadget = AffineGadget<EdwardsParameters, Fq, FqGadget>;
|
|
||||
|
|
||||
#[cfg(test)]
|
|
||||
mod test {
|
|
||||
use super::EdwardsSWGadget as EdwardsG;
|
|
||||
use crate::{
|
|
||||
groups::curves::twisted_edwards::test::{edwards_constraint_costs, edwards_test},
|
|
||||
test_constraint_system::TestConstraintSystem,
|
|
||||
};
|
|
||||
use algebra::{curves::edwards_sw6::EdwardsParameters, fields::edwards_sw6::fq::Fq};
|
|
||||
|
|
||||
#[test]
|
|
||||
fn edwards_constraint_costs_test() {
|
|
||||
let mut cs = TestConstraintSystem::<Fq>::new();
|
|
||||
edwards_constraint_costs::<_, EdwardsParameters, EdwardsG, _>(&mut cs);
|
|
||||
assert!(cs.is_satisfied());
|
|
||||
}
|
|
||||
|
|
||||
#[test]
|
|
||||
fn edwards_sw6_gadget_test() {
|
|
||||
let mut cs = TestConstraintSystem::<Fq>::new();
|
|
||||
edwards_test::<_, EdwardsParameters, EdwardsG, _>(&mut cs);
|
|
||||
assert!(cs.is_satisfied());
|
|
||||
}
|
|
||||
}
|
|
@ -1,30 +0,0 @@ |
|||||
use crate::groups::curves::twisted_edwards::AffineGadget;
|
|
||||
use algebra::{curves::jubjub::JubJubParameters, fields::jubjub::fq::Fq};
|
|
||||
|
|
||||
use crate::fields::jubjub::FqGadget;
|
|
||||
|
|
||||
pub type JubJubGadget = AffineGadget<JubJubParameters, Fq, FqGadget>;
|
|
||||
|
|
||||
#[cfg(test)]
|
|
||||
mod test {
|
|
||||
use super::JubJubGadget as EdwardsG;
|
|
||||
use crate::{
|
|
||||
groups::curves::twisted_edwards::test::{edwards_constraint_costs, edwards_test},
|
|
||||
test_constraint_system::TestConstraintSystem,
|
|
||||
};
|
|
||||
use algebra::{curves::jubjub::JubJubParameters as EdwardsParameters, fields::jubjub::fq::Fq};
|
|
||||
|
|
||||
#[test]
|
|
||||
fn edwards_constraint_costs_test() {
|
|
||||
let mut cs = TestConstraintSystem::<Fq>::new();
|
|
||||
edwards_constraint_costs::<_, EdwardsParameters, EdwardsG, _>(&mut cs);
|
|
||||
assert!(cs.is_satisfied());
|
|
||||
}
|
|
||||
|
|
||||
#[test]
|
|
||||
fn jubjub_gadget_test() {
|
|
||||
let mut cs = TestConstraintSystem::<Fq>::new();
|
|
||||
edwards_test::<_, EdwardsParameters, EdwardsG, _>(&mut cs);
|
|
||||
assert!(cs.is_satisfied());
|
|
||||
}
|
|
||||
}
|
|
@ -1,72 +0,0 @@ |
|||||
use crate::{groups::test::group_test, prelude::*, Vec};
|
|
||||
use algebra::{
|
|
||||
curves::{models::TEModelParameters, twisted_edwards_extended::GroupAffine as TEAffine},
|
|
||||
test_rng, BitIterator, Field, Group, PrimeField, UniformRand,
|
|
||||
};
|
|
||||
use rand::Rng;
|
|
||||
|
|
||||
use r1cs_core::ConstraintSystem;
|
|
||||
|
|
||||
pub(crate) fn edwards_test<ConstraintF, P, GG, CS>(cs: &mut CS)
|
|
||||
where
|
|
||||
ConstraintF: Field,
|
|
||||
P: TEModelParameters,
|
|
||||
GG: GroupGadget<TEAffine<P>, ConstraintF, Value = TEAffine<P>>,
|
|
||||
CS: ConstraintSystem<ConstraintF>,
|
|
||||
{
|
|
||||
let a: TEAffine<P> = UniformRand::rand(&mut test_rng());
|
|
||||
let b: TEAffine<P> = UniformRand::rand(&mut test_rng());
|
|
||||
let gadget_a = GG::alloc(&mut cs.ns(|| "a"), || Ok(a)).unwrap();
|
|
||||
let gadget_b = GG::alloc(&mut cs.ns(|| "b"), || Ok(b)).unwrap();
|
|
||||
assert_eq!(gadget_a.get_value().unwrap(), a);
|
|
||||
assert_eq!(gadget_b.get_value().unwrap(), b);
|
|
||||
group_test::<ConstraintF, TEAffine<P>, GG, _>(
|
|
||||
&mut cs.ns(|| "GroupTest(a, b)"),
|
|
||||
gadget_a.clone(),
|
|
||||
gadget_b,
|
|
||||
);
|
|
||||
|
|
||||
// Check mul_bits
|
|
||||
let scalar: <TEAffine<P> as Group>::ScalarField = UniformRand::rand(&mut test_rng());
|
|
||||
let native_result = a.mul(&scalar);
|
|
||||
|
|
||||
let mut scalar: Vec<bool> = BitIterator::new(scalar.into_repr()).collect();
|
|
||||
// Get the scalar bits into little-endian form.
|
|
||||
scalar.reverse();
|
|
||||
let input = Vec::<Boolean>::alloc(cs.ns(|| "Input"), || Ok(scalar)).unwrap();
|
|
||||
let zero = GG::zero(cs.ns(|| "zero")).unwrap();
|
|
||||
let result = gadget_a
|
|
||||
.mul_bits(cs.ns(|| "mul_bits"), &zero, input.iter())
|
|
||||
.unwrap();
|
|
||||
let gadget_value = result.get_value().expect("Gadget_result failed");
|
|
||||
assert_eq!(native_result, gadget_value);
|
|
||||
}
|
|
||||
|
|
||||
pub(crate) fn edwards_constraint_costs<ConstraintF, P, GG, CS>(cs: &mut CS)
|
|
||||
where
|
|
||||
ConstraintF: Field,
|
|
||||
P: TEModelParameters,
|
|
||||
GG: GroupGadget<TEAffine<P>, ConstraintF, Value = TEAffine<P>>,
|
|
||||
CS: ConstraintSystem<ConstraintF>,
|
|
||||
{
|
|
||||
use crate::boolean::AllocatedBit;
|
|
||||
|
|
||||
let bit = AllocatedBit::alloc(&mut cs.ns(|| "bool"), || Ok(true))
|
|
||||
.unwrap()
|
|
||||
.into();
|
|
||||
|
|
||||
let mut rng = test_rng();
|
|
||||
let a: TEAffine<P> = rng.gen();
|
|
||||
let b: TEAffine<P> = rng.gen();
|
|
||||
let gadget_a = GG::alloc(&mut cs.ns(|| "a"), || Ok(a)).unwrap();
|
|
||||
let gadget_b = GG::alloc(&mut cs.ns(|| "b"), || Ok(b)).unwrap();
|
|
||||
let alloc_cost = cs.num_constraints();
|
|
||||
let _ =
|
|
||||
GG::conditionally_select(&mut cs.ns(|| "cond_select"), &bit, &gadget_a, &gadget_b).unwrap();
|
|
||||
let cond_select_cost = cs.num_constraints() - alloc_cost;
|
|
||||
|
|
||||
let _ = gadget_a.add(&mut cs.ns(|| "ab"), &gadget_b).unwrap();
|
|
||||
let add_cost = cs.num_constraints() - cond_select_cost - alloc_cost;
|
|
||||
assert_eq!(cond_select_cost, <GG as CondSelectGadget<_>>::cost());
|
|
||||
assert_eq!(add_cost, GG::cost_of_add());
|
|
||||
}
|
|
@ -0,0 +1,27 @@ |
|||||
|
use algebra::bls12_377::{Fq, Fq12Parameters, Fq2Parameters, Fq6Parameters};
|
||||
|
|
||||
|
use crate::fields::{fp::FpGadget, fp12::Fp12Gadget, fp2::Fp2Gadget, fp6_3over2::Fp6Gadget};
|
||||
|
|
||||
|
pub type FqGadget = FpGadget<Fq>;
|
||||
|
pub type Fq2Gadget = Fp2Gadget<Fq2Parameters, Fq>;
|
||||
|
pub type Fq6Gadget = Fp6Gadget<Fq6Parameters, Fq>;
|
||||
|
pub type Fq12Gadget = Fp12Gadget<Fq12Parameters, Fq>;
|
||||
|
|
||||
|
#[test]
|
||||
|
fn bls12_377_field_gadgets_test() {
|
||||
|
use super::*;
|
||||
|
use crate::fields::tests::*;
|
||||
|
use algebra::bls12_377::{Fq, Fq12, Fq2, Fq6};
|
||||
|
|
||||
|
field_test::<_, Fq, FqGadget>();
|
||||
|
frobenius_tests::<Fq, Fq, FqGadget>(13);
|
||||
|
|
||||
|
field_test::<_, Fq, Fq2Gadget>();
|
||||
|
frobenius_tests::<Fq2, Fq, Fq2Gadget>(13);
|
||||
|
|
||||
|
field_test::<_, Fq, Fq6Gadget>();
|
||||
|
frobenius_tests::<Fq6, Fq, Fq6Gadget>(13);
|
||||
|
|
||||
|
field_test::<_, Fq, Fq12Gadget>();
|
||||
|
frobenius_tests::<Fq12, Fq, Fq12Gadget>(13);
|
||||
|
}
|
@ -0,0 +1,7 @@ |
|||||
|
mod curves;
|
||||
|
mod fields;
|
||||
|
mod pairing;
|
||||
|
|
||||
|
pub use curves::*;
|
||||
|
pub use fields::*;
|
||||
|
pub use pairing::*;
|
@ -0,0 +1,8 @@ |
|||||
|
use algebra::bls12_377::Parameters;
|
||||
|
|
||||
|
pub type PairingGadget = crate::pairing::bls12::PairingGadget<Parameters>;
|
||||
|
|
||||
|
#[test]
|
||||
|
fn test() {
|
||||
|
crate::pairing::tests::bilinearity_test::<algebra::Bls12_377, _, PairingGadget>()
|
||||
|
}
|
@ -0,0 +1,11 @@ |
|||||
|
use crate::groups::curves::twisted_edwards::AffineGadget;
|
||||
|
use algebra::edwards_bls12::*;
|
||||
|
|
||||
|
use crate::edwards_bls12::FqGadget;
|
||||
|
|
||||
|
pub type EdwardsBlsGadget = AffineGadget<EdwardsParameters, Fq, FqGadget>;
|
||||
|
|
||||
|
#[test]
|
||||
|
fn test() {
|
||||
|
crate::groups::curves::twisted_edwards::test::<_, EdwardsParameters, EdwardsBlsGadget>();
|
||||
|
}
|
@ -0,0 +1,9 @@ |
|||||
|
use crate::fields::fp::FpGadget;
|
||||
|
use algebra::edwards_bls12::fq::Fq;
|
||||
|
|
||||
|
pub type FqGadget = FpGadget<Fq>;
|
||||
|
|
||||
|
#[test]
|
||||
|
fn test() {
|
||||
|
crate::fields::tests::field_test::<_, Fq, FqGadget>();
|
||||
|
}
|
@ -0,0 +1,5 @@ |
|||||
|
mod curves;
|
||||
|
mod fields;
|
||||
|
|
||||
|
pub use curves::*;
|
||||
|
pub use fields::*;
|
@ -0,0 +1,11 @@ |
|||||
|
use crate::groups::curves::twisted_edwards::AffineGadget;
|
||||
|
use algebra::edwards_sw6::*;
|
||||
|
|
||||
|
use crate::edwards_sw6::FqGadget;
|
||||
|
|
||||
|
pub type EdwardsSWGadget = AffineGadget<EdwardsParameters, Fq, FqGadget>;
|
||||
|
|
||||
|
#[test]
|
||||
|
fn test() {
|
||||
|
crate::groups::curves::twisted_edwards::test::<_, EdwardsParameters, EdwardsSWGadget>();
|
||||
|
}
|
@ -0,0 +1,9 @@ |
|||||
|
use crate::fields::fp::FpGadget;
|
||||
|
use algebra::edwards_sw6::fq::Fq;
|
||||
|
|
||||
|
pub type FqGadget = FpGadget<Fq>;
|
||||
|
|
||||
|
#[test]
|
||||
|
fn test() {
|
||||
|
crate::fields::tests::field_test::<_, Fq, FqGadget>();
|
||||
|
}
|
@ -0,0 +1,5 @@ |
|||||
|
mod curves;
|
||||
|
mod fields;
|
||||
|
|
||||
|
pub use curves::*;
|
||||
|
pub use fields::*;
|
@ -0,0 +1,11 @@ |
|||||
|
use crate::groups::curves::twisted_edwards::AffineGadget;
|
||||
|
use algebra::jubjub::*;
|
||||
|
|
||||
|
use crate::jubjub::FqGadget;
|
||||
|
|
||||
|
pub type JubJubGadget = AffineGadget<JubJubParameters, Fq, FqGadget>;
|
||||
|
|
||||
|
#[test]
|
||||
|
fn test() {
|
||||
|
crate::groups::curves::twisted_edwards::test::<Fq, _, JubJubGadget>();
|
||||
|
}
|
@ -0,0 +1,8 @@ |
|||||
|
use crate::fields::fp::FpGadget;
|
||||
|
|
||||
|
pub type FqGadget = FpGadget<algebra::jubjub::Fq>;
|
||||
|
|
||||
|
#[test]
|
||||
|
fn test() {
|
||||
|
crate::fields::tests::field_test::<_, algebra::jubjub::Fq, FqGadget>();
|
||||
|
}
|
@ -0,0 +1,5 @@ |
|||||
|
mod curves;
|
||||
|
mod fields;
|
||||
|
|
||||
|
pub use curves::*;
|
||||
|
pub use fields::*;
|
@ -0,0 +1,11 @@ |
|||||
|
#[cfg(feature = "bls12_377")]
|
||||
|
pub mod bls12_377;
|
||||
|
|
||||
|
#[cfg(feature = "edwards_bls12")]
|
||||
|
pub mod edwards_bls12;
|
||||
|
|
||||
|
#[cfg(feature = "edwards_sw6")]
|
||||
|
pub mod edwards_sw6;
|
||||
|
|
||||
|
#[cfg(feature = "jubjub")]
|
||||
|
pub mod jubjub;
|
@ -1,4 +0,0 @@ |
|||||
use crate::pairing::bls12::PairingGadget as Bls12PG;
|
|
||||
use algebra::curves::bls12_377::Bls12_377Parameters;
|
|
||||
|
|
||||
pub type PairingGadget = Bls12PG<Bls12_377Parameters>;
|
|