Skip to content

Commit 713fda2

Browse files
authored
feat: Add support for constraint usage within ark_bn254 (#737)
* feat: Add support for constraint usage within ark_bn254 This modifies the `Cargo.toml` with the following changes: - Add `ark-r1cs-std` as an optional dependency. - Add `ark-curve-constraint-tests` an a dev-dependency. - Add `r1cs` feature which activates `ark-r1cs-std` depencendy. (cherry picked from commit 48687352268d94a9dae74b50465273af15b0d132) * feat: Add `constraints` module for Bn245 Resolves: #186 * fix: Fix cherry-picking issues * chore: Conditionally activate `r1cs/std`. activate std for ark-r1cs-std only if r1cs is enabled as suggested by @vlopes11.
1 parent d9527c8 commit 713fda2

File tree

5 files changed

+137
-2
lines changed

5 files changed

+137
-2
lines changed

curves/bn254/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@ edition = "2021"
1616
ark-ff = { version= "0.4.0", default-features = false }
1717
ark-ec = { version= "0.4.0", default-features = false }
1818
ark-std = { version = "0.4.0", default-features = false }
19+
ark-r1cs-std = { version = "0.4.0", default-features = false, optional = true }
1920

2021
[dev-dependencies]
2122
ark-serialize = { version = "0.4.0", default-features = false }
2223
ark-algebra-test-templates = { version = "0.4.0", default-features = false }
2324
ark-algebra-bench-templates = { version = "0.4.0", default-features = false }
25+
ark-curve-constraint-tests = { path = "../curve-constraint-tests", default-features = false }
26+
ark-relations = { version = "0.4.0", default-features = false }
2427

2528
[features]
2629
default = [ "curve" ]
27-
std = [ "ark-std/std", "ark-ff/std", "ark-ec/std" ]
28-
30+
std = [ "ark-std/std", "ark-ff/std", "ark-ec/std", "ark-r1cs-std?/std" ]
31+
r1cs = [ "ark-r1cs-std" ]
2932
curve = [ "scalar_field" ]
3033
scalar_field = []
3134

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use ark_r1cs_std::groups::curves::short_weierstrass::ProjectiveVar;
2+
3+
use crate::{constraints::FBaseVar, g1::Config};
4+
5+
/// A group element in the Bn254 prime-order group.
6+
pub type GVar = ProjectiveVar<Config, FBaseVar>;
7+
8+
#[test]
9+
fn test() {
10+
ark_curve_constraint_tests::curves::sw_test::<Config, GVar>().unwrap();
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use ark_r1cs_std::fields::fp::FpVar;
2+
3+
use crate::fq::Fq;
4+
5+
/// A variable that is the R1CS equivalent of `crate::Fq`.
6+
pub type FBaseVar = FpVar<Fq>;
7+
8+
#[test]
9+
fn test() {
10+
ark_curve_constraint_tests::fields::field_test::<_, _, FBaseVar>().unwrap();
11+
}

curves/bn254/src/constraints/mod.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//! This module implements the R1CS equivalent of `ark_bn254`.
2+
//!
3+
//! It implements field variables for `crate::Fq`,
4+
//! and group variables for `crate::G1Projective`.
5+
//!
6+
//! The field underlying these constraints is `crate::Fq`.
7+
//!
8+
//! # Examples
9+
//!
10+
//! One can perform standard algebraic operations on `FBaseVar`:
11+
//!
12+
//! ```
13+
//! # fn main() -> Result<(), ark_relations::r1cs::SynthesisError> {
14+
//! use ark_std::UniformRand;
15+
//! use ark_relations::r1cs::*;
16+
//! use ark_r1cs_std::prelude::*;
17+
//! use ark_bn254::{*, constraints::*};
18+
//!
19+
//! let cs = ConstraintSystem::<Fq>::new_ref();
20+
//! // This rng is just for test purposes; do not use it
21+
//! // in real applications.
22+
//! let mut rng = ark_std::test_rng();
23+
//!
24+
//! // Generate some random `Fq` elements.
25+
//! let a_native = Fq::rand(&mut rng);
26+
//! let b_native = Fq::rand(&mut rng);
27+
//!
28+
//! // Allocate `a_native` and `b_native` as witness variables in `cs`.
29+
//! let a = FBaseVar::new_witness(ark_relations::ns!(cs, "generate_a"), || Ok(a_native))?;
30+
//! let b = FBaseVar::new_witness(ark_relations::ns!(cs, "generate_b"), || Ok(b_native))?;
31+
//!
32+
//! // Allocate `a_native` and `b_native` as constants in `cs`. This does not add any
33+
//! // constraints or variables.
34+
//! let a_const = FBaseVar::new_constant(ark_relations::ns!(cs, "a_as_constant"), a_native)?;
35+
//! let b_const = FBaseVar::new_constant(ark_relations::ns!(cs, "b_as_constant"), b_native)?;
36+
//!
37+
//! let one = FBaseVar::one();
38+
//! let zero = FBaseVar::zero();
39+
//!
40+
//! // Sanity check one + one = two
41+
//! let two = &one + &one + &zero;
42+
//! two.enforce_equal(&one.double()?)?;
43+
//!
44+
//! assert!(cs.is_satisfied()?);
45+
//!
46+
//! // Check that the value of &a + &b is correct.
47+
//! assert_eq!((&a + &b).value()?, a_native + &b_native);
48+
//!
49+
//! // Check that the value of &a * &b is correct.
50+
//! assert_eq!((&a * &b).value()?, a_native * &b_native);
51+
//!
52+
//! // Check that operations on variables and constants are equivalent.
53+
//! (&a + &b).enforce_equal(&(&a_const + &b_const))?;
54+
//! assert!(cs.is_satisfied()?);
55+
//! # Ok(())
56+
//! # }
57+
//! ```
58+
//!
59+
//! One can also perform standard algebraic operations on `GVar`:
60+
//!
61+
//! ```
62+
//! # fn main() -> Result<(), ark_relations::r1cs::SynthesisError> {
63+
//! # use ark_std::UniformRand;
64+
//! # use ark_relations::r1cs::*;
65+
//! # use ark_r1cs_std::prelude::*;
66+
//! # use ark_bn254::{*, constraints::*};
67+
//!
68+
//! # let cs = ConstraintSystem::<Fq>::new_ref();
69+
//! # let mut rng = ark_std::test_rng();
70+
//!
71+
//! // Generate some random `G1Projective` elements.
72+
//! let a_native = G1Projective::rand(&mut rng);
73+
//! let b_native = G1Projective::rand(&mut rng);
74+
//!
75+
//! // Allocate `a_native` and `b_native` as witness variables in `cs`.
76+
//! let a = GVar::new_witness(ark_relations::ns!(cs, "a"), || Ok(a_native))?;
77+
//! let b = GVar::new_witness(ark_relations::ns!(cs, "b"), || Ok(b_native))?;
78+
//!
79+
//! // Allocate `a_native` and `b_native` as constants in `cs`. This does not add any
80+
//! // constraints or variables.
81+
//! let a_const = GVar::new_constant(ark_relations::ns!(cs, "a_as_constant"), a_native)?;
82+
//! let b_const = GVar::new_constant(ark_relations::ns!(cs, "b_as_constant"), b_native)?;
83+
//!
84+
//! // This returns the identity.
85+
//! let zero = GVar::zero();
86+
//!
87+
//! // Sanity check one + one = two
88+
//! let two_a = &a + &a + &zero;
89+
//! two_a.enforce_equal(&a.double()?)?;
90+
//!
91+
//! assert!(cs.is_satisfied()?);
92+
//!
93+
//! // Check that the value of &a + &b is correct.
94+
//! assert_eq!((&a + &b).value()?, a_native + &b_native);
95+
//!
96+
//! // Check that operations on variables and constants are equivalent.
97+
//! (&a + &b).enforce_equal(&(&a_const + &b_const))?;
98+
//! assert!(cs.is_satisfied()?);
99+
//! # Ok(())
100+
//! # }
101+
//! ```
102+
103+
mod curves;
104+
mod fields;
105+
106+
pub use curves::*;
107+
pub use fields::*;

curves/bn254/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ mod fields;
4141
pub use curves::*;
4242

4343
pub use fields::*;
44+
45+
#[cfg(feature = "r1cs")]
46+
pub mod constraints;

0 commit comments

Comments
 (0)