|
| 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::*; |
0 commit comments