ark-r1cs-std/r1cs-std/src/instantiated/bls12_377/mod.rs
r1cs-std/src/instantiated/bls12_377/mod.rsRaw
1//! This module implements the R1CS equivalent of `algebra::bls12_377`.
2//!
3//! It implements field variables for `algebra::bls12_377::{Fq, Fq2, Fq6, Fq12}`,
4//! group variables for `algebra::bls12_377::{G1, G2}`, and implements constraint
5//! generation for computing `Bls12_377::pairing`.
6//!
7//! The field underlying these constraints is `algebra::bls12_377::Fq`.
8//!
9//! # Examples
10//!
11//! One can perform standard algebraic operations on `FqVar`:
12//!
13//! ```
14//! # fn main() -> Result<(), r1cs_core::SynthesisError> {
15//! use algebra::{UniformRand, bls12_377::*};
16//! use r1cs_core::*;
17//! use r1cs_std::prelude::*;
18//! use r1cs_std::bls12_377::*;
19//!
20//! let cs = ConstraintSystem::<Fq>::new_ref();
21//! // This rng is just for test purposes; do not use it
22//! // in real applications.
23//! let mut rng = algebra::test_rng();
24//!
25//! // Generate some random `Fq` elements.
26//! let a_native = Fq::rand(&mut rng);
27//! let b_native = Fq::rand(&mut rng);
28//!
29//! // Allocate `a_native` and `b_native` as witness variables in `cs`.
30//! let a = FqVar::new_witness(r1cs_core::ns!(cs, "generate_a"), || Ok(a_native))?;
31//! let b = FqVar::new_witness(r1cs_core::ns!(cs, "generate_b"), || Ok(b_native))?;
32//!
33//! // Allocate `a_native` and `b_native` as constants in `cs`. This does not add any
34//! // constraints or variables.
35//! let a_const = FqVar::new_constant(r1cs_core::ns!(cs, "a_as_constant"), a_native)?;
36//! let b_const = FqVar::new_constant(r1cs_core::ns!(cs, "b_as_constant"), b_native)?;
37//!
38//! let one = FqVar::one();
39//! let zero = FqVar::zero();
40//!
41//! // Sanity check one + one = two
42//! let two = &one + &one + &zero;
43//! two.enforce_equal(&one.double()?)?;
44//!
45//! assert!(cs.is_satisfied()?);
46//!
47//! // Check that the value of &a + &b is correct.
48//! assert_eq!((&a + &b).value()?, a_native + &b_native);
49//!
50//! // Check that the value of &a * &b is correct.
51//! assert_eq!((&a * &b).value()?, a_native * &b_native);
52//!
53//! // Check that operations on variables and constants are equivalent.
54//! (&a + &b).enforce_equal(&(&a_const + &b_const))?;
55//! assert!(cs.is_satisfied()?);
56//! # Ok(())
57//! # }
58//! ```
59//!
60//! One can also perform standard algebraic operations on `G1Var` and `G2Var`:
61//!
62//! ```
63//! # fn main() -> Result<(), r1cs_core::SynthesisError> {
64//! # use algebra::{UniformRand, bls12_377::*};
65//! # use r1cs_core::*;
66//! # use r1cs_std::prelude::*;
67//! # use r1cs_std::bls12_377::*;
68//!
69//! # let cs = ConstraintSystem::<Fq>::new_ref();
70//! # let mut rng = algebra::test_rng();
71//!
72//! // Generate some random `G1` elements.
73//! let a_native = G1Projective::rand(&mut rng);
74//! let b_native = G1Projective::rand(&mut rng);
75//!
76//! // Allocate `a_native` and `b_native` as witness variables in `cs`.
77//! let a = G1Var::new_witness(r1cs_core::ns!(cs, "a"), || Ok(a_native))?;
78//! let b = G1Var::new_witness(r1cs_core::ns!(cs, "b"), || Ok(b_native))?;
79//!
80//! // Allocate `a_native` and `b_native` as constants in `cs`. This does not add any
81//! // constraints or variables.
82//! let a_const = G1Var::new_constant(r1cs_core::ns!(cs, "a_as_constant"), a_native)?;
83//! let b_const = G1Var::new_constant(r1cs_core::ns!(cs, "b_as_constant"), b_native)?;
84//!
85//! // This returns the identity of `G1`.
86//! let zero = G1Var::zero();
87//!
88//! // Sanity check one + one = two
89//! let two_a = &a + &a + &zero;
90//! two_a.enforce_equal(&a.double()?)?;
91//!
92//! assert!(cs.is_satisfied()?);
93//!
94//! // Check that the value of &a + &b is correct.
95//! assert_eq!((&a + &b).value()?, a_native + &b_native);
96//!
97//! // Check that operations on variables and constants are equivalent.
98//! (&a + &b).enforce_equal(&(&a_const + &b_const))?;
99//! assert!(cs.is_satisfied()?);
100//! # Ok(())
101//! # }
102//! ```
103//!
104//! Finally, one can check pairing computations as well:
105//!
106//! ```
107//! # fn main() -> Result<(), r1cs_core::SynthesisError> {
108//! # use algebra::{UniformRand, PairingEngine, bls12_377::*};
109//! # use r1cs_core::*;
110//! # use r1cs_std::prelude::*;
111//! # use r1cs_std::bls12_377::{self, *};
112//!
113//! # let cs = ConstraintSystem::<Fq>::new_ref();
114//! # let mut rng = algebra::test_rng();
115//!
116//! // Generate random `G1` and `G2` elements.
117//! let a_native = G1Projective::rand(&mut rng);
118//! let b_native = G2Projective::rand(&mut rng);
119//!
120//! // Allocate `a_native` and `b_native` as witness variables in `cs`.
121//! let a = G1Var::new_witness(r1cs_core::ns!(cs, "a"), || Ok(a_native))?;
122//! let b = G2Var::new_witness(r1cs_core::ns!(cs, "b"), || Ok(b_native))?;
123//!
124//! // Allocate `a_native` and `b_native` as constants in `cs`. This does not add any
125//! // constraints or variables.
126//! let a_const = G1Var::new_constant(r1cs_core::ns!(cs, "a_as_constant"), a_native)?;
127//! let b_const = G2Var::new_constant(r1cs_core::ns!(cs, "b_as_constant"), b_native)?;
128//!
129//! let pairing_result_native = Bls12_377::pairing(a_native, b_native);
130//!
131//! // Prepare `a` and `b` for pairing.
132//! let a_prep = bls12_377::PairingVar::prepare_g1(&a)?;
133//! let b_prep = bls12_377::PairingVar::prepare_g2(&b)?;
134//! let pairing_result = bls12_377::PairingVar::pairing(a_prep, b_prep)?;
135//!
136//! // Check that the value of &a + &b is correct.
137//! assert_eq!(pairing_result.value()?, pairing_result_native);
138//!
139//! // Check that operations on variables and constants are equivalent.
140//! let a_prep_const = bls12_377::PairingVar::prepare_g1(&a_const)?;
141//! let b_prep_const = bls12_377::PairingVar::prepare_g2(&b_const)?;
142//! let pairing_result_const = bls12_377::PairingVar::pairing(a_prep_const, b_prep_const)?;
143//! println!("Done here 3");
144//!
145//! pairing_result.enforce_equal(&pairing_result_const)?;
146//! assert!(cs.is_satisfied()?);
147//! # Ok(())
148//! # }
149//! ```
150
151mod curves;
152mod fields;
153mod pairing;
154
155pub use curves::*;
156pub use fields::*;
157pub use pairing::*;