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,14 +16,18 @@ 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 }
|
||||
ark-bls12-377 = { path = "../bls12_377", default-features = false, features = [ "scalar_field" ] }
|
||||
|
||||
[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"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
std = [ "ark-std/std", "ark-ff/std", "ark-ec/std", "ark-bls12-377/std" ]
|
||||
r1cs = [ "ark-r1cs-std" ]
|
||||
|
||||
12
ed_on_bls12_377/src/constraints/curves.rs
Normal file
12
ed_on_bls12_377/src/constraints/curves.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use crate::*;
|
||||
use ark_r1cs_std::groups::curves::twisted_edwards::AffineVar;
|
||||
|
||||
use crate::constraints::FqVar;
|
||||
|
||||
/// A variable that is the R1CS equivalent of `crate::EdwardsAffine`.
|
||||
pub type EdwardsVar = AffineVar<EdwardsParameters, FqVar>;
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
ark_curve_constraint_tests::curves::te_test::<EdwardsParameters, EdwardsVar>().unwrap();
|
||||
}
|
||||
10
ed_on_bls12_377/src/constraints/fields.rs
Normal file
10
ed_on_bls12_377/src/constraints/fields.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use crate::fq::Fq;
|
||||
use ark_r1cs_std::fields::fp::FpVar;
|
||||
|
||||
/// A variable that is the R1CS equivalent of `crate::Fq`.
|
||||
pub type FqVar = FpVar<Fq>;
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
ark_curve_constraint_tests::fields::field_test::<_, _, FqVar>().unwrap();
|
||||
}
|
||||
107
ed_on_bls12_377/src/constraints/mod.rs
Normal file
107
ed_on_bls12_377/src/constraints/mod.rs
Normal file
@@ -0,0 +1,107 @@
|
||||
//! This module implements the R1CS equivalent of `ark_ed_on_bls12_377`.
|
||||
//!
|
||||
//! It implements field variables for `crate::Fq`,
|
||||
//! and group variables for `crate::GroupProjective`.
|
||||
//!
|
||||
//! 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_ed_on_bls12_377::{*, 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 `EdwardsVar`:
|
||||
//!
|
||||
//! ```
|
||||
//! # fn main() -> Result<(), ark_relations::r1cs::SynthesisError> {
|
||||
//! # use ark_ff::UniformRand;
|
||||
//! # use ark_relations::r1cs::*;
|
||||
//! # use ark_r1cs_std::prelude::*;
|
||||
//! # use ark_ed_on_bls12_377::{*, constraints::*};
|
||||
//!
|
||||
//! # let cs = ConstraintSystem::<Fq>::new_ref();
|
||||
//! # let mut rng = ark_ff::test_rng();
|
||||
//!
|
||||
//! // Generate some random `Edwards` elements.
|
||||
//! let a_native = EdwardsProjective::rand(&mut rng);
|
||||
//! let b_native = EdwardsProjective::rand(&mut rng);
|
||||
//!
|
||||
//! // Allocate `a_native` and `b_native` as witness variables in `cs`.
|
||||
//! let a = EdwardsVar::new_witness(ark_relations::ns!(cs, "a"), || Ok(a_native))?;
|
||||
//! let b = EdwardsVar::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 = EdwardsVar::new_constant(ark_relations::ns!(cs, "a_as_constant"), a_native)?;
|
||||
//! let b_const = EdwardsVar::new_constant(ark_relations::ns!(cs, "b_as_constant"), b_native)?;
|
||||
//!
|
||||
//! // This returns the identity.
|
||||
//! let zero = EdwardsVar::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(())
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
mod curves;
|
||||
mod fields;
|
||||
|
||||
pub use curves::*;
|
||||
pub use fields::*;
|
||||
@@ -22,6 +22,8 @@
|
||||
//! * a = -1
|
||||
//! * d = 3021
|
||||
|
||||
#[cfg(feature = "r1cs")]
|
||||
pub mod constraints;
|
||||
mod curves;
|
||||
mod fields;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user