-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtranscript.rs
More file actions
222 lines (197 loc) · 5.62 KB
/
Copy pathtranscript.rs
File metadata and controls
222 lines (197 loc) · 5.62 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// srctranscript.rs
//! Fiat–Shamir transcript for UltraHonk
use crate::debug::{dbg_fr, dbg_vec};
use crate::trace;
use crate::{
field::Fr,
hash::keccak256,
types::{Proof, RelationParameters, Transcript, CONST_PROOF_SIZE_LOG_N},
utils::fq_to_halves_be,
};
use ark_bn254::G1Affine;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
fn push_point(buf: &mut Vec<u8>, pt: &G1Affine) {
let (x_lo, x_hi) = fq_to_halves_be(&pt.x);
let (y_lo, y_hi) = fq_to_halves_be(&pt.y);
buf.extend_from_slice(&x_lo);
buf.extend_from_slice(&x_hi);
buf.extend_from_slice(&y_lo);
buf.extend_from_slice(&y_hi);
}
fn split(fr: Fr) -> (Fr, Fr) {
let b = fr.to_bytes();
let mut lo = [0u8; 32];
lo[16..].copy_from_slice(&b[16..]);
let mut hi = [0u8; 32];
hi[16..].copy_from_slice(&b[..16]);
(Fr::from_bytes(&lo), Fr::from_bytes(&hi))
}
#[inline(always)]
fn hash_to_fr(bytes: &[u8]) -> Fr {
Fr::from_bytes(&keccak256(bytes))
}
fn u64_to_be32(x: u64) -> [u8; 32] {
let mut out = [0u8; 32];
out[24..].copy_from_slice(&x.to_be_bytes());
out
}
fn gen_eta(
proof: &Proof,
pub_inputs: &[Vec<u8>],
cs: u64,
pis: u64,
offset: u64,
) -> (RelationParameters, Fr) {
let mut data = Vec::new();
data.extend_from_slice(&u64_to_be32(cs));
data.extend_from_slice(&u64_to_be32(pis));
data.extend_from_slice(&u64_to_be32(offset));
for pi in pub_inputs {
data.extend_from_slice(pi);
}
for w in &[&proof.w1, &proof.w2, &proof.w3] {
push_point(&mut data, &w.to_affine());
}
let h = hash_to_fr(&data);
let (eta, eta_two) = split(h);
let h2 = hash_to_fr(&h.to_bytes());
let (eta_three, _) = split(h2);
(
RelationParameters {
eta,
eta_two,
eta_three,
beta: Fr::zero(),
gamma: Fr::zero(),
public_inputs_delta: Fr::zero(),
},
h2,
)
}
fn gen_beta_gamma(prev: Fr, proof: &Proof) -> (Fr, Fr, Fr) {
let mut data = prev.to_bytes().to_vec();
for w in &[
&proof.lookup_read_counts,
&proof.lookup_read_tags,
&proof.w4,
] {
push_point(&mut data, &w.to_affine());
}
let h = hash_to_fr(&data);
let (beta, gamma) = split(h);
(beta, gamma, h)
}
fn gen_alphas(prev: Fr, proof: &Proof) -> (Vec<Fr>, Fr) {
let mut data = prev.to_bytes().to_vec();
for w in &[&proof.lookup_inverses, &proof.z_perm] {
push_point(&mut data, &w.to_affine());
}
let mut cur = hash_to_fr(&data);
let mut alphas = Vec::with_capacity(25);
let (a0, a1) = split(cur);
alphas.push(a0);
alphas.push(a1);
while alphas.len() < 25 {
cur = hash_to_fr(&cur.to_bytes());
let (lo, hi) = split(cur);
alphas.push(lo);
if alphas.len() < 25 {
alphas.push(hi);
}
}
(alphas, cur)
}
fn gen_challenges(mut cur: Fr, rounds: usize) -> (Vec<Fr>, Fr) {
let mut out = Vec::with_capacity(rounds);
for _ in 0..rounds {
cur = hash_to_fr(&cur.to_bytes());
out.push(split(cur).0);
}
(out, cur)
}
pub fn generate_transcript(
proof: &Proof,
pub_inputs: &[Vec<u8>],
cs: u64,
pis: u64,
offset: u64,
) -> Transcript {
// 1) η
let (mut rp, mut cur) = gen_eta(proof, pub_inputs, cs, pis, offset);
// 2) β, γ
let (beta, gamma, tmp) = gen_beta_gamma(cur, proof);
rp.beta = beta;
rp.gamma = gamma;
cur = tmp;
// 3) α’s
let (alphas, tmp) = gen_alphas(cur, proof);
cur = tmp;
// 4) gate challenges
let (gate_chals, tmp) = gen_challenges(cur, CONST_PROOF_SIZE_LOG_N);
cur = tmp;
// 5) sumcheck u challenges
let (u_chals, tmp) = {
let mut t = cur;
let mut vs = Vec::with_capacity(CONST_PROOF_SIZE_LOG_N);
for r in 0..CONST_PROOF_SIZE_LOG_N {
let mut d = t.to_bytes().to_vec();
for &c in &proof.sumcheck_univariates[r] {
d.extend_from_slice(&c.to_bytes());
}
t = hash_to_fr(&d);
vs.push(split(t).0);
}
(vs, t)
};
cur = tmp;
// 6) ρ
let mut data = cur.to_bytes().to_vec();
for &e in &proof.sumcheck_evaluations {
data.extend_from_slice(&e.to_bytes());
}
let rho = split(hash_to_fr(&data)).0;
cur = hash_to_fr(&data);
// 7) gemini_r
let mut data = cur.to_bytes().to_vec();
for pt in &proof.gemini_fold_comms {
push_point(&mut data, &pt.to_affine());
}
let gemini_r = split(hash_to_fr(&data)).0;
cur = hash_to_fr(&data);
// 8) shplonk_nu
let mut data = cur.to_bytes().to_vec();
for &a in &proof.gemini_a_evaluations {
data.extend_from_slice(&a.to_bytes());
}
let shplonk_nu = split(hash_to_fr(&data)).0;
cur = hash_to_fr(&data);
// 9) shplonk_z
let mut data = cur.to_bytes().to_vec();
push_point(&mut data, &proof.shplonk_q.to_affine());
let shplonk_z = split(hash_to_fr(&data)).0;
trace!("===== TRANSCRIPT (Rust) =====");
dbg_fr("eta", &rp.eta);
dbg_fr("eta_two", &rp.eta_two);
dbg_fr("eta_three", &rp.eta_three);
dbg_fr("beta", &rp.beta);
dbg_fr("gamma", &rp.gamma);
dbg_vec("alpha", &alphas);
dbg_vec("gate_ch", &gate_chals);
dbg_vec("u_ch", &u_chals);
dbg_fr("rho", &rho);
dbg_fr("gemini_r", &gemini_r);
dbg_fr("shplonk_nu", &shplonk_nu);
dbg_fr("shplonk_z", &shplonk_z);
trace!("=============================");
Transcript {
rel_params: rp,
alphas,
gate_challenges: gate_chals,
sumcheck_u_challenges: u_chals,
rho,
gemini_r,
shplonk_nu,
shplonk_z,
}
}