-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdata_structures.rs
205 lines (171 loc) · 6.22 KB
/
data_structures.rs
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use ark_ec::AffineCurve;
use ark_ff::{Field, PrimeField};
use ark_poly_commit::trivial_pc::CommitterKey;
use ark_relations::r1cs::Matrix;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError};
use ark_sponge::{collect_sponge_bytes, collect_sponge_field_elements, Absorbable};
use ark_std::io::{Read, Write};
use ark_std::vec::Vec;
/// The public parameters of this NARK.
pub type PublicParameters = ();
/// Information about the index, including the field of definition, the number of
/// variables, the number of constraints, and the maximum number of non-zero
/// entries in any of the constraint matrices.
#[derive(Clone, Copy)]
pub(crate) struct IndexInfo {
/// The total number of variables in the constraint system.
pub(crate) num_variables: usize,
/// The number of constraints.
pub(crate) num_constraints: usize,
/// The number of public input (i.e. instance) variables.
pub(crate) num_instance_variables: usize,
/// Hash of the matrices.
pub(crate) matrices_hash: [u8; 32],
}
/// The index prover key for our NARK.
#[derive(Clone)]
pub struct IndexProverKey<G: AffineCurve> {
/// Information about the index.
pub(crate) index_info: IndexInfo,
/// The `A` matrix of the R1CS instance.
pub(crate) a: Matrix<G::ScalarField>,
/// The `B` matrix of the R1CS instance.
pub(crate) b: Matrix<G::ScalarField>,
/// The `C` matrix of the R1CS instance.
pub(crate) c: Matrix<G::ScalarField>,
/// Group elements required by the Pedersen commitment.
pub(crate) ck: CommitterKey<G>,
}
/// Index verifier key for our NARK.
pub type IndexVerifierKey<G> = IndexProverKey<G>;
/// The sigma protocol's prover commitment randomness.
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct FirstRoundMessageRandomness<G: AffineCurve> {
/// Pedersen commitment to the vector that blinds the witness in `Az`.
pub(crate) comm_r_a: G,
/// Pedersen commitment to the vector that blinds the witness in `Bz`.
pub(crate) comm_r_b: G,
/// Pedersen commitment to the vector that blinds the witness in `Cz`.
pub(crate) comm_r_c: G,
/// Pedersen commitment to the first cross term randomness vector
pub(crate) comm_1: G,
/// Pedersen commitment to the second cross term randomness vector
pub(crate) comm_2: G,
}
impl<CF, G> Absorbable<CF> for FirstRoundMessageRandomness<G>
where
CF: PrimeField,
G: AffineCurve + Absorbable<CF>,
{
fn to_sponge_bytes(&self) -> Vec<u8> {
collect_sponge_bytes!(
CF,
self.comm_r_a,
self.comm_r_b,
self.comm_r_c,
self.comm_1,
self.comm_2
)
}
fn to_sponge_field_elements(&self) -> Vec<CF> {
collect_sponge_field_elements!(
self.comm_r_a,
self.comm_r_b,
self.comm_r_c,
self.comm_1,
self.comm_2
)
}
}
/// The sigma protocol's prover commitment.
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct FirstRoundMessage<G: AffineCurve> {
/// Pedersen commitment to the `Az` vector.
pub(crate) comm_a: G,
/// Pedersen commitment to the `Bz` vector.
pub(crate) comm_b: G,
/// Pedersen commitment to the `Cz` vector.
pub(crate) comm_c: G,
/// The randomness used for the commitment.
pub(crate) randomness: Option<FirstRoundMessageRandomness<G>>,
}
impl<G: AffineCurve> FirstRoundMessage<G> {
pub(crate) fn zero(make_zk: bool) -> Self {
Self {
comm_a: G::zero(),
comm_b: G::zero(),
comm_c: G::zero(),
randomness: if make_zk {
Some(FirstRoundMessageRandomness {
comm_r_a: G::zero(),
comm_r_b: G::zero(),
comm_r_c: G::zero(),
comm_1: G::zero(),
comm_2: G::zero(),
})
} else {
None
},
}
}
}
impl<CF, G> Absorbable<CF> for FirstRoundMessage<G>
where
CF: PrimeField,
G: AffineCurve + Absorbable<CF>,
{
fn to_sponge_bytes(&self) -> Vec<u8> {
collect_sponge_bytes!(CF, self.comm_a, self.comm_b, self.comm_c, self.randomness)
}
fn to_sponge_field_elements(&self) -> Vec<CF> {
collect_sponge_field_elements!(self.comm_a, self.comm_b, self.comm_c, self.randomness)
}
}
/// The sigma protocol's prover response randomness.
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct SecondRoundMessageRandomness<F: Field> {
/// The blinded randomness for the Pedersen commitment to the linear combination with the
/// `A` matrix.
pub(crate) sigma_a: F,
/// The blinded randomness for the Pedersen commitment to the linear combination with the
/// `B` matrix.
pub(crate) sigma_b: F,
/// The blinded randomness for the Pedersen commitment to the linear combination with the
/// `C` matrix.
pub(crate) sigma_c: F,
/// The blinded randomness for the Pedersen commitment to the cross terms
pub(crate) sigma_o: F,
}
/// The sigma protocol's prover response.
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct SecondRoundMessage<F: Field> {
/// The R1CS witness with randomness applied if zero-knowledge is needed.
pub(crate) blinded_witness: Vec<F>,
/// The randomness used for the response.
pub(crate) randomness: Option<SecondRoundMessageRandomness<F>>,
}
impl<F: Field> SecondRoundMessage<F> {
pub(crate) fn zero(witness_len: usize, make_zk: bool) -> Self {
Self {
blinded_witness: vec![F::zero(); witness_len],
randomness: if make_zk {
Some(SecondRoundMessageRandomness {
sigma_a: F::zero(),
sigma_b: F::zero(),
sigma_c: F::zero(),
sigma_o: F::zero(),
})
} else {
None
},
}
}
}
/// The proof for our NARK.
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct Proof<G: AffineCurve> {
/// The sigma protocol's prove commitment.
pub first_msg: FirstRoundMessage<G>,
/// The sigma protocol's prove response.
pub second_msg: SecondRoundMessage<G::ScalarField>,
}