forked from wyattbenno777/SuperNova
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathr1cs.rs
More file actions
139 lines (118 loc) · 3.77 KB
/
Copy pathr1cs.rs
File metadata and controls
139 lines (118 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! Support for generating R1CS using bellpepper.
#![allow(non_snake_case)]
use super::{shape_cs::ShapeCS, solver::SatisfyingAssignment, test_shape_cs::TestShapeCS};
use crate::{
errors::NovaError,
r1cs::{R1CSInstance, R1CSShape, R1CSWitness, R1CS},
traits::Group,
CommitmentKey,
};
use bellpepper_core::{Index, LinearCombination};
use ff::PrimeField;
/// `NovaWitness` provide a method for acquiring an `R1CSInstance` and `R1CSWitness` from implementers.
pub trait NovaWitness<G: Group> {
/// Return an instance and witness, given a shape and ck.
fn r1cs_instance_and_witness(
&self,
shape: &R1CSShape<G>,
ck: &CommitmentKey<G>,
) -> Result<(R1CSInstance<G>, R1CSWitness<G>), NovaError>;
}
/// `NovaShape` provides methods for acquiring `R1CSShape` and `CommitmentKey` from implementers.
pub trait NovaShape<G: Group> {
/// Return an appropriate `R1CSShape` and `CommitmentKey` structs.
fn r1cs_shape_with_commitmentkey(&self) -> (R1CSShape<G>, CommitmentKey<G>) {
let S = self.r1cs_shape();
let ck = R1CS::<G>::commitment_key(&S);
(S, ck)
}
/// Return an appropriate `R1CSShape`.
fn r1cs_shape(&self) -> R1CSShape<G>;
}
impl<G: Group> NovaWitness<G> for SatisfyingAssignment<G> {
fn r1cs_instance_and_witness(
&self,
shape: &R1CSShape<G>,
ck: &CommitmentKey<G>,
) -> Result<(R1CSInstance<G>, R1CSWitness<G>), NovaError> {
let W = R1CSWitness::<G>::new(shape, &self.aux_assignment)?;
let X = &self.input_assignment[1..];
let comm_W = W.commit(ck);
let instance = R1CSInstance::<G>::new(shape, &comm_W, X)?;
Ok((instance, W))
}
}
macro_rules! impl_nova_shape {
( $name:ident) => {
impl<G: Group> NovaShape<G> for $name<G>
where
G::Scalar: PrimeField,
{
fn r1cs_shape(&self) -> R1CSShape<G> {
let mut A: Vec<(usize, usize, G::Scalar)> = Vec::new();
let mut B: Vec<(usize, usize, G::Scalar)> = Vec::new();
let mut C: Vec<(usize, usize, G::Scalar)> = Vec::new();
let mut num_cons_added = 0;
let mut X = (&mut A, &mut B, &mut C, &mut num_cons_added);
let num_inputs = self.num_inputs();
let num_constraints = self.num_constraints();
let num_vars = self.num_aux();
for constraint in self.constraints.iter() {
add_constraint(
&mut X,
num_vars,
&constraint.0,
&constraint.1,
&constraint.2,
);
}
assert_eq!(num_cons_added, num_constraints);
let S: R1CSShape<G> = {
// Don't count One as an input for shape's purposes.
let res = R1CSShape::new(num_constraints, num_vars, num_inputs - 1, &A, &B, &C);
res.unwrap()
};
S
}
}
};
}
impl_nova_shape!(ShapeCS);
impl_nova_shape!(TestShapeCS);
fn add_constraint<S: PrimeField>(
X: &mut (
&mut Vec<(usize, usize, S)>,
&mut Vec<(usize, usize, S)>,
&mut Vec<(usize, usize, S)>,
&mut usize,
),
num_vars: usize,
a_lc: &LinearCombination<S>,
b_lc: &LinearCombination<S>,
c_lc: &LinearCombination<S>,
) {
let (A, B, C, nn) = X;
let n = **nn;
let one = S::ONE;
let add_constraint_component = |index: Index, coeff, V: &mut Vec<_>| {
match index {
Index::Input(idx) => {
// Inputs come last, with input 0, reprsenting 'one',
// at position num_vars within the witness vector.
let i = idx + num_vars;
V.push((n, i, one * coeff))
}
Index::Aux(idx) => V.push((n, idx, one * coeff)),
}
};
for (index, coeff) in a_lc.iter() {
add_constraint_component(index.0, coeff, A);
}
for (index, coeff) in b_lc.iter() {
add_constraint_component(index.0, coeff, B)
}
for (index, coeff) in c_lc.iter() {
add_constraint_component(index.0, coeff, C)
}
**nn += 1;
}