mirror of
https://github.com/arnaucube/ark-curves-cherry-picked.git
synced 2026-01-08 23:11:29 +01:00
Add constraints for relevant curves (#3)
This commit is contained in:
@@ -16,10 +16,13 @@ edition = "2018"
|
||||
ark-ff = { git = "https://github.com/arkworks-rs/algebra", default-features = false }
|
||||
ark-ec = { git = "https://github.com/arkworks-rs/algebra", default-features = false }
|
||||
ark-std = { git = "https://github.com/arkworks-rs/utils", default-features = false }
|
||||
ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std", default-features = false, optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
ark-relations = { git = "https://github.com/arkworks-rs/snark", default-features = false }
|
||||
ark-serialize = { git = "https://github.com/arkworks-rs/algebra", default-features = false }
|
||||
ark-curve-tests = { path = "../curve-tests", default-features = false }
|
||||
ark-curve-constraint-tests = { path = "../curve-constraint-tests", default-features = false }
|
||||
rand = { version = "0.7", default-features = false }
|
||||
rand_xorshift = "0.2"
|
||||
|
||||
@@ -30,3 +33,4 @@ std = [ "ark-std/std", "ark-ff/std", "ark-ec/std" ]
|
||||
curve = [ "scalar_field", "base_field" ]
|
||||
scalar_field = []
|
||||
base_field = []
|
||||
r1cs = [ "base_field", "ark-r1cs-std" ]
|
||||
29
mnt4_298/src/constraints/curves.rs
Normal file
29
mnt4_298/src/constraints/curves.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use crate::Parameters;
|
||||
use ark_r1cs_std::groups::mnt4;
|
||||
|
||||
/// An element of G1 in the MNT4-298 bilinear group.
|
||||
pub type G1Var = mnt4::G1Var<Parameters>;
|
||||
/// An element of G2 in the MNT4-298 bilinear group.
|
||||
pub type G2Var = mnt4::G2Var<Parameters>;
|
||||
|
||||
/// Represents the cached precomputation that can be performed on a G1 element
|
||||
/// which enables speeding up pairing computation.
|
||||
pub type G1PreparedVar = mnt4::G1PreparedVar<Parameters>;
|
||||
/// Represents the cached precomputation that can be performed on a G2 element
|
||||
/// which enables speeding up pairing computation.
|
||||
pub type G2PreparedVar = mnt4::G2PreparedVar<Parameters>;
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
use ark_ec::models::mnt4::MNT4Parameters;
|
||||
ark_curve_constraint_tests::curves::sw_test::<
|
||||
<Parameters as MNT4Parameters>::G1Parameters,
|
||||
G1Var,
|
||||
>()
|
||||
.unwrap();
|
||||
ark_curve_constraint_tests::curves::sw_test::<
|
||||
<Parameters as MNT4Parameters>::G2Parameters,
|
||||
G2Var,
|
||||
>()
|
||||
.unwrap();
|
||||
}
|
||||
26
mnt4_298/src/constraints/fields.rs
Normal file
26
mnt4_298/src/constraints/fields.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use crate::{Fq, Fq2Parameters, Fq4Parameters};
|
||||
|
||||
use ark_r1cs_std::fields::{fp::FpVar, fp2::Fp2Var, fp4::Fp4Var};
|
||||
|
||||
/// A variable that is the R1CS equivalent of `crate::Fq`.
|
||||
pub type FqVar = FpVar<Fq>;
|
||||
/// A variable that is the R1CS equivalent of `crate::Fq2`.
|
||||
pub type Fq2Var = Fp2Var<Fq2Parameters>;
|
||||
/// A variable that is the R1CS equivalent of `crate::Fq4`.
|
||||
pub type Fq4Var = Fp4Var<Fq4Parameters>;
|
||||
|
||||
#[test]
|
||||
fn mnt4_298_field_gadgets_test() {
|
||||
use super::*;
|
||||
use crate::{Fq, Fq2, Fq4};
|
||||
use ark_curve_constraint_tests::fields::*;
|
||||
|
||||
field_test::<_, _, FqVar>().unwrap();
|
||||
frobenius_tests::<Fq, _, FqVar>(13).unwrap();
|
||||
|
||||
field_test::<_, _, Fq2Var>().unwrap();
|
||||
frobenius_tests::<Fq2, _, Fq2Var>(13).unwrap();
|
||||
|
||||
field_test::<_, _, Fq4Var>().unwrap();
|
||||
frobenius_tests::<Fq4, _, Fq4Var>(13).unwrap();
|
||||
}
|
||||
163
mnt4_298/src/constraints/mod.rs
Normal file
163
mnt4_298/src/constraints/mod.rs
Normal file
@@ -0,0 +1,163 @@
|
||||
//! This module implements the R1CS equivalent of `ark_mnt4_298`.
|
||||
//!
|
||||
//! It implements field variables for `crate::{Fq, Fq2, Fq4}`,
|
||||
//! group variables for `crate::{G1, G2}`, and implements constraint
|
||||
//! generation for computing `MNT4_298::pairing`.
|
||||
//!
|
||||
//! The field underlying these constraints is `crate::Fq`.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! One can perform standard algebraic operations on `FqVar`:
|
||||
//!
|
||||
//! ```
|
||||
//! # fn main() -> Result<(), ark_relations::r1cs::SynthesisError> {
|
||||
//! use ark_ff::UniformRand;
|
||||
//! use ark_relations::r1cs::*;
|
||||
//! use ark_r1cs_std::prelude::*;
|
||||
//! use ark_mnt4_298::{*, constraints::*};
|
||||
//!
|
||||
//! let cs = ConstraintSystem::<Fq>::new_ref();
|
||||
//! // This rng is just for test purposes; do not use it
|
||||
//! // in real applications.
|
||||
//! let mut rng = ark_ff::test_rng();
|
||||
//!
|
||||
//! // Generate some random `Fq` elements.
|
||||
//! let a_native = Fq::rand(&mut rng);
|
||||
//! let b_native = Fq::rand(&mut rng);
|
||||
//!
|
||||
//! // Allocate `a_native` and `b_native` as witness variables in `cs`.
|
||||
//! let a = FqVar::new_witness(ark_relations::ns!(cs, "generate_a"), || Ok(a_native))?;
|
||||
//! let b = FqVar::new_witness(ark_relations::ns!(cs, "generate_b"), || Ok(b_native))?;
|
||||
//!
|
||||
//! // Allocate `a_native` and `b_native` as constants in `cs`. This does not add any
|
||||
//! // constraints or variables.
|
||||
//! let a_const = FqVar::new_constant(ark_relations::ns!(cs, "a_as_constant"), a_native)?;
|
||||
//! let b_const = FqVar::new_constant(ark_relations::ns!(cs, "b_as_constant"), b_native)?;
|
||||
//!
|
||||
//! let one = FqVar::one();
|
||||
//! let zero = FqVar::zero();
|
||||
//!
|
||||
//! // Sanity check one + one = two
|
||||
//! let two = &one + &one + &zero;
|
||||
//! two.enforce_equal(&one.double()?)?;
|
||||
//!
|
||||
//! assert!(cs.is_satisfied()?);
|
||||
//!
|
||||
//! // Check that the value of &a + &b is correct.
|
||||
//! assert_eq!((&a + &b).value()?, a_native + &b_native);
|
||||
//!
|
||||
//! // Check that the value of &a * &b is correct.
|
||||
//! assert_eq!((&a * &b).value()?, a_native * &b_native);
|
||||
//!
|
||||
//! // Check that operations on variables and constants are equivalent.
|
||||
//! (&a + &b).enforce_equal(&(&a_const + &b_const))?;
|
||||
//! assert!(cs.is_satisfied()?);
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! One can also perform standard algebraic operations on `G1Var` and `G2Var`:
|
||||
//!
|
||||
//! ```
|
||||
//! # fn main() -> Result<(), ark_relations::r1cs::SynthesisError> {
|
||||
//! # use ark_ff::UniformRand;
|
||||
//! # use ark_relations::r1cs::*;
|
||||
//! # use ark_r1cs_std::prelude::*;
|
||||
//! # use ark_mnt4_298::{*, constraints::*};
|
||||
//!
|
||||
//! # let cs = ConstraintSystem::<Fq>::new_ref();
|
||||
//! # let mut rng = ark_ff::test_rng();
|
||||
//!
|
||||
//! // Generate some random `G1` elements.
|
||||
//! let a_native = G1Projective::rand(&mut rng);
|
||||
//! let b_native = G1Projective::rand(&mut rng);
|
||||
//!
|
||||
//! // Allocate `a_native` and `b_native` as witness variables in `cs`.
|
||||
//! let a = G1Var::new_witness(ark_relations::ns!(cs, "a"), || Ok(a_native))?;
|
||||
//! let b = G1Var::new_witness(ark_relations::ns!(cs, "b"), || Ok(b_native))?;
|
||||
//!
|
||||
//! // Allocate `a_native` and `b_native` as constants in `cs`. This does not add any
|
||||
//! // constraints or variables.
|
||||
//! let a_const = G1Var::new_constant(ark_relations::ns!(cs, "a_as_constant"), a_native)?;
|
||||
//! let b_const = G1Var::new_constant(ark_relations::ns!(cs, "b_as_constant"), b_native)?;
|
||||
//!
|
||||
//! // This returns the identity of `G1`.
|
||||
//! let zero = G1Var::zero();
|
||||
//!
|
||||
//! // Sanity check one + one = two
|
||||
//! let two_a = &a + &a + &zero;
|
||||
//! two_a.enforce_equal(&a.double()?)?;
|
||||
//!
|
||||
//! assert!(cs.is_satisfied()?);
|
||||
//!
|
||||
//! // Check that the value of &a + &b is correct.
|
||||
//! assert_eq!((&a + &b).value()?, a_native + &b_native);
|
||||
//!
|
||||
//! // Check that operations on variables and constants are equivalent.
|
||||
//! (&a + &b).enforce_equal(&(&a_const + &b_const))?;
|
||||
//! assert!(cs.is_satisfied()?);
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! Finally, one can check pairing computations as well:
|
||||
//!
|
||||
//! ```
|
||||
//! # fn main() -> Result<(), ark_relations::r1cs::SynthesisError> {
|
||||
//! # use ark_ff::UniformRand;
|
||||
//! # use ark_ec::PairingEngine;
|
||||
//! # use ark_relations::r1cs::*;
|
||||
//! # use ark_r1cs_std::prelude::*;
|
||||
//! # use ark_mnt4_298::{*, constraints::*};
|
||||
//!
|
||||
//! # let cs = ConstraintSystem::<Fq>::new_ref();
|
||||
//! # let mut rng = ark_ff::test_rng();
|
||||
//!
|
||||
//! // Generate random `G1` and `G2` elements.
|
||||
//! let a_native = G1Projective::rand(&mut rng);
|
||||
//! let b_native = G2Projective::rand(&mut rng);
|
||||
//!
|
||||
//! // Allocate `a_native` and `b_native` as witness variables in `cs`.
|
||||
//! let a = G1Var::new_witness(ark_relations::ns!(cs, "a"), || Ok(a_native))?;
|
||||
//! let b = G2Var::new_witness(ark_relations::ns!(cs, "b"), || Ok(b_native))?;
|
||||
//!
|
||||
//! // Allocate `a_native` and `b_native` as constants in `cs`. This does not add any
|
||||
//! // constraints or variables.
|
||||
//! let a_const = G1Var::new_constant(ark_relations::ns!(cs, "a_as_constant"), a_native)?;
|
||||
//! let b_const = G2Var::new_constant(ark_relations::ns!(cs, "b_as_constant"), b_native)?;
|
||||
//!
|
||||
//! let pairing_result_native = MNT4_298::pairing(a_native, b_native);
|
||||
//!
|
||||
//! // Prepare `a` and `b` for pairing.
|
||||
//! let a_prep = constraints::PairingVar::prepare_g1(&a)?;
|
||||
//! let b_prep = constraints::PairingVar::prepare_g2(&b)?;
|
||||
//! let pairing_result = constraints::PairingVar::pairing(a_prep, b_prep)?;
|
||||
//!
|
||||
//! // Check that the value of &a + &b is correct.
|
||||
//! assert_eq!(pairing_result.value()?, pairing_result_native);
|
||||
//!
|
||||
//! // Check that operations on variables and constants are equivalent.
|
||||
//! let a_prep_const = constraints::PairingVar::prepare_g1(&a_const)?;
|
||||
//! let b_prep_const = constraints::PairingVar::prepare_g2(&b_const)?;
|
||||
//! let pairing_result_const = constraints::PairingVar::pairing(a_prep_const, b_prep_const)?;
|
||||
//! println!("Done here 3");
|
||||
//!
|
||||
//! pairing_result.enforce_equal(&pairing_result_const)?;
|
||||
//! assert!(cs.is_satisfied()?);
|
||||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
mod fields;
|
||||
pub use fields::*;
|
||||
|
||||
#[cfg(feature = "curve")]
|
||||
mod curves;
|
||||
#[cfg(feature = "curve")]
|
||||
mod pairing;
|
||||
|
||||
#[cfg(feature = "curve")]
|
||||
pub use curves::*;
|
||||
#[cfg(feature = "curve")]
|
||||
pub use pairing::*;
|
||||
10
mnt4_298/src/constraints/pairing.rs
Normal file
10
mnt4_298/src/constraints/pairing.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use crate::Parameters;
|
||||
|
||||
/// Specifies the constraints for computing a pairing in the MNT4-298 bilinear group.
|
||||
pub type PairingVar = ark_r1cs_std::pairing::mnt4::PairingVar<Parameters>;
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
use crate::MNT4_298;
|
||||
ark_curve_constraint_tests::pairing::bilinearity_test::<MNT4_298, PairingVar>().unwrap()
|
||||
}
|
||||
@@ -33,6 +33,9 @@ mod curves;
|
||||
#[cfg(any(feature = "scalar_field", feature = "base_field"))]
|
||||
mod fields;
|
||||
|
||||
#[cfg(feature = "r1cs")]
|
||||
pub mod constraints;
|
||||
|
||||
#[cfg(feature = "curve")]
|
||||
pub use curves::*;
|
||||
#[cfg(any(feature = "scalar_field", feature = "base_field"))]
|
||||
|
||||
Reference in New Issue
Block a user