arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/crypto-primitives/src/nizk/mod.rs
crypto-primitives/src/nizk/mod.rsRaw
  1use algebra::bytes::ToBytes;
  2use rand::Rng;
  3
  4#[cfg(feature = "gm17")]
  5pub mod gm17;
  6#[cfg(feature = "gm17")]
  7pub use self::gm17::Gm17;
  8
  9#[cfg(feature = "r1cs")]
 10pub mod constraints;
 11#[cfg(feature = "r1cs")]
 12pub use constraints::*;
 13
 14use crate::Error;
 15
 16pub trait NIZK {
 17    type Circuit;
 18    type AssignedCircuit;
 19    type VerifierInput: ?Sized;
 20    type ProvingParameters: Clone;
 21    type VerificationParameters: Clone + Default + From<Self::PreparedVerificationParameters>;
 22    type PreparedVerificationParameters: Clone + Default + From<Self::VerificationParameters>;
 23    type Proof: ToBytes + Clone + Default;
 24
 25    fn setup<R: Rng>(
 26        circuit: Self::Circuit,
 27        rng: &mut R,
 28    ) -> Result<
 29        (
 30            Self::ProvingParameters,
 31            Self::PreparedVerificationParameters,
 32        ),
 33        Error,
 34    >;
 35
 36    fn prove<R: Rng>(
 37        parameter: &Self::ProvingParameters,
 38        input_and_witness: Self::AssignedCircuit,
 39        rng: &mut R,
 40    ) -> Result<Self::Proof, Error>;
 41
 42    fn verify(
 43        verifier_key: &Self::PreparedVerificationParameters,
 44        input: &Self::VerifierInput,
 45        proof: &Self::Proof,
 46    ) -> Result<bool, Error>;
 47}
 48
 49#[cfg(all(feature = "gm17", test))]
 50mod test {
 51    use rand::thread_rng;
 52    use std::ops::AddAssign;
 53
 54    #[test]
 55    fn test_gm17() {
 56        use crate::nizk::{gm17::Gm17, NIZK};
 57        use algebra::{curves::bls12_381::Bls12_381, fields::bls12_381::Fr, Field};
 58        use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, SynthesisError};
 59
 60        #[derive(Copy, Clone)]
 61        struct R1CSCircuit {
 62            x:   Option<Fr>,
 63            sum: Option<Fr>,
 64            w:   Option<Fr>,
 65        }
 66
 67        impl R1CSCircuit {
 68            pub(super) fn new(x: Fr, sum: Fr, w: Fr) -> Self {
 69                Self {
 70                    x:   Some(x),
 71                    sum: Some(sum),
 72                    w:   Some(w),
 73                }
 74            }
 75        }
 76
 77        impl ConstraintSynthesizer<Fr> for R1CSCircuit {
 78            fn generate_constraints<CS: ConstraintSystem<Fr>>(
 79                self,
 80                cs: &mut CS,
 81            ) -> Result<(), SynthesisError> {
 82                let input = cs.alloc_input(|| "x", || Ok(self.x.unwrap()))?;
 83                let sum = cs.alloc_input(|| "sum", || Ok(self.sum.unwrap()))?;
 84                let witness = cs.alloc(|| "w", || Ok(self.w.unwrap()))?;
 85
 86                cs.enforce(
 87                    || "check_one",
 88                    |lc| lc + sum,
 89                    |lc| lc + CS::one(),
 90                    |lc| lc + input + witness,
 91                );
 92                Ok(())
 93            }
 94        }
 95
 96        let mut sum = Fr::one();
 97        sum.add_assign(&Fr::one());
 98        let circuit = R1CSCircuit::new(Fr::one(), sum, Fr::one());
 99
100        let rng = &mut thread_rng();
101
102        let parameters = Gm17::<Bls12_381, R1CSCircuit, [Fr]>::setup(circuit, rng).unwrap();
103
104        let proof =
105            Gm17::<Bls12_381, R1CSCircuit, [Fr]>::prove(&parameters.0, circuit, rng).unwrap();
106
107        let result =
108            Gm17::<Bls12_381, R1CSCircuit, [Fr]>::verify(&parameters.1, &[Fr::one(), sum], &proof)
109                .unwrap();
110        assert!(result);
111    }
112}