|
1 | | -use ark_ff::PrimeField; |
2 | | -use ark_r1cs_std::{ |
3 | | - prelude::{AllocVar, EqGadget}, |
4 | | - uint32::UInt32, |
5 | | -}; |
6 | | -use ark_relations::r1cs::{ |
7 | | - ConstraintSynthesizer, ConstraintSystemRef, SynthesisError, SynthesisError::AssignmentMissing, |
8 | | -}; |
9 | | -use ark_std::{marker::PhantomData, vec::Vec}; |
10 | | - |
11 | | -use crate::{ |
12 | | - relation::{ |
13 | | - state::{FullInput, NoInput, State}, |
14 | | - GetPublicInput, |
15 | | - }, |
16 | | - CircuitField, |
17 | | -}; |
| 1 | +use snark_relation_proc_macro::snark_relation; |
18 | 2 |
|
19 | 3 | /// Linear equation relation: a*x + b = y |
20 | 4 | /// |
21 | 5 | /// Relation with: |
22 | 6 | /// - 1 private witness (x) |
23 | | -/// - 3 constants (a, b, y) |
24 | | -#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] |
25 | | -pub struct LinearEquationRelation<S: State> { |
26 | | - /// constant (a slope) |
27 | | - pub a: u32, |
28 | | - /// private witness |
29 | | - pub x: Option<u32>, |
30 | | - /// constant(an intercept) |
31 | | - pub b: u32, |
32 | | - /// constant |
33 | | - pub y: u32, |
34 | | - |
35 | | - _phantom: PhantomData<S>, |
36 | | -} |
| 7 | +/// - 3 constants (a, b, y) |
| 8 | +#[snark_relation] |
| 9 | +mod relation { |
| 10 | + use ark_r1cs_std::{alloc::AllocVar, eq::EqGadget, uint32::UInt32}; |
37 | 11 |
|
38 | | -impl LinearEquationRelation<NoInput> { |
39 | | - pub fn without_input(a: u32, b: u32, y: u32) -> Self { |
40 | | - Self { |
41 | | - a, |
42 | | - x: None, |
43 | | - b, |
44 | | - y, |
45 | | - _phantom: PhantomData, |
46 | | - } |
| 12 | + #[relation_object_definition] |
| 13 | + struct LinearEquationRelation { |
| 14 | + /// slope |
| 15 | + #[constant] |
| 16 | + pub a: u32, |
| 17 | + /// private witness |
| 18 | + #[private_input] |
| 19 | + pub x: u32, |
| 20 | + /// an intercept |
| 21 | + #[constant] |
| 22 | + pub b: u32, |
| 23 | + /// constant |
| 24 | + #[constant] |
| 25 | + pub y: u32, |
47 | 26 | } |
48 | | -} |
49 | 27 |
|
50 | | -impl LinearEquationRelation<FullInput> { |
51 | | - pub fn with_full_input(a: u32, x: u32, b: u32, y: u32) -> Self { |
52 | | - Self { |
53 | | - a, |
54 | | - x: Some(x), |
55 | | - b, |
56 | | - y, |
57 | | - _phantom: PhantomData, |
58 | | - } |
59 | | - } |
60 | | -} |
61 | | - |
62 | | -impl<Field: PrimeField, S: State> ConstraintSynthesizer<Field> for LinearEquationRelation<S> { |
63 | | - fn generate_constraints(self, cs: ConstraintSystemRef<Field>) -> Result<(), SynthesisError> { |
| 28 | + #[circuit_definition] |
| 29 | + fn generate_constraints() { |
64 | 30 | // TODO: migrate from real values to values in the finite field (see FpVar) |
65 | 31 | // Watch out for overflows!!! |
66 | | - let x = UInt32::new_witness(ark_relations::ns!(cs, "x"), || { |
67 | | - self.x.ok_or(AssignmentMissing) |
68 | | - })?; |
69 | | - let b = UInt32::new_constant(ark_relations::ns!(cs, "b"), self.b)?; |
70 | | - let y = UInt32::new_constant(ark_relations::ns!(cs, "y"), self.y)?; |
| 32 | + let x = UInt32::new_witness(ark_relations::ns!(cs, "x"), || self.x())?; |
| 33 | + let b = UInt32::new_constant(ark_relations::ns!(cs, "b"), self.b())?; |
| 34 | + let y = UInt32::new_constant(ark_relations::ns!(cs, "y"), self.y())?; |
71 | 35 |
|
72 | 36 | let mut left = ark_std::iter::repeat(x) |
73 | | - .take(self.a as usize) |
74 | | - .collect::<Vec<UInt32<Field>>>(); |
| 37 | + .take(*self.a() as usize) |
| 38 | + .collect::<Vec<UInt32<_>>>(); |
75 | 39 |
|
76 | 40 | left.push(b); |
77 | 41 |
|
78 | 42 | UInt32::addmany(&left)?.enforce_equal(&y) |
79 | 43 | } |
80 | 44 | } |
81 | 45 |
|
82 | | -impl<S: State> GetPublicInput<CircuitField> for LinearEquationRelation<S> {} |
| 46 | +#[cfg(test)] |
| 47 | +mod tests { |
| 48 | + use ark_bls12_381::Bls12_381; |
| 49 | + use ark_groth16::Groth16; |
| 50 | + use ark_relations::r1cs::{ConstraintSynthesizer, ConstraintSystem, ConstraintSystemRef}; |
| 51 | + use ark_snark::SNARK; |
| 52 | + |
| 53 | + use super::*; |
| 54 | + use crate::CircuitField; |
| 55 | + |
| 56 | + const A: u32 = 2; |
| 57 | + const X: u32 = 1; |
| 58 | + const B: u32 = 1; |
| 59 | + const Y: u32 = 3; |
| 60 | + |
| 61 | + #[test] |
| 62 | + fn linear_constraints_correctness() { |
| 63 | + let circuit = LinearEquationRelationWithFullInput::new(A, B, Y, X); |
| 64 | + |
| 65 | + let cs: ConstraintSystemRef<CircuitField> = ConstraintSystem::new_ref(); |
| 66 | + circuit.generate_constraints(cs.clone()).unwrap(); |
| 67 | + |
| 68 | + let is_satisfied = cs.is_satisfied().unwrap(); |
| 69 | + if !is_satisfied { |
| 70 | + println!("{:?}", cs.which_is_unsatisfied()); |
| 71 | + } |
| 72 | + |
| 73 | + assert!(is_satisfied); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn linear_proving_procedure() { |
| 78 | + let circuit_wo_input = LinearEquationRelationWithoutInput::new(A, B, Y); |
| 79 | + |
| 80 | + let mut rng = ark_std::test_rng(); |
| 81 | + let (pk, vk) = |
| 82 | + Groth16::<Bls12_381>::circuit_specific_setup(circuit_wo_input, &mut rng).unwrap(); |
| 83 | + |
| 84 | + let circuit_with_public_input = LinearEquationRelationWithPublicInput::new(A, B, Y); |
| 85 | + let input = circuit_with_public_input.serialize_public_input(); |
| 86 | + |
| 87 | + let circuit_with_full_input = LinearEquationRelationWithFullInput::new(A, B, Y, X); |
| 88 | + |
| 89 | + let proof = Groth16::prove(&pk, circuit_with_full_input, &mut rng).unwrap(); |
| 90 | + let valid_proof = Groth16::verify(&vk, &input, &proof).unwrap(); |
| 91 | + assert!(valid_proof); |
| 92 | + } |
| 93 | +} |
0 commit comments