forked from risc0/risc0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap_poseidon.rs
364 lines (317 loc) · 11.8 KB
/
bootstrap_poseidon.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright 2024 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::{
fs,
process::{Command, Stdio},
};
use clap::{Parser, ValueEnum};
use regex::Regex;
use risc0_core::field::baby_bear::Elem;
use tempfile::tempdir;
// Set the user chosen constants
// The baby bear prime
const FIELD: usize = 15 * (1 << 27) + 1;
// 16 for rate, 8 for capacity
const CELLS: usize = 24;
// The goal security level
const SECURITY: usize = 128;
// # of bits in FIELD
const NUM_BITS: usize = 31;
// Smallest allowed alpha for Baby Bear
const ALPHA: usize = 7;
// A structure to hold all computed constant to eventually be exported
struct ComputedConstants {
rounds_full: usize, // The number of full rounds, always 8 in practice
rounds_partial: usize, // The number of partial rounds, computed based on security
round_constants: Vec<Elem>, // Additive round constants
mds: Vec<Elem>, // mds matrix constants
partial_comp_matrix: Vec<Elem>, // Computed compressed matrix for partial rounds
partial_comp_offset: Vec<Elem>, // Computed compressed offsets for partial rounds
}
impl ComputedConstants {
fn new() -> Self {
ComputedConstants {
rounds_full: 0,
rounds_partial: 0,
round_constants: Vec::<Elem>::new(),
mds: Vec::<Elem>::new(),
partial_comp_matrix: Vec::<Elem>::new(),
partial_comp_offset: Vec::<Elem>::new(),
}
}
}
// A function to turns a string of hex constants into a vector of Elems
fn to_elems(input_string: &str) -> Vec<Elem> {
let mut out = Vec::<Elem>::new();
for part in input_string.split(',') {
let tidy_part: String = part
.chars()
.filter(|c| "0123456789abcdef".contains(*c))
.collect();
let num = u32::from_str_radix(&tidy_part, 16).unwrap();
assert!(num < (FIELD as u32));
out.push(Elem::new(num));
}
out
}
// The code that parses the textual output from sage into the constants
fn extract_from_sage(consts: &mut ComputedConstants, stdout: &str) {
// Split stdout into lines
let out_lines: Vec<&str> = stdout.split('\n').collect();
// Match the first line and extract the computed values
let regex = Regex::new("n=31, t=24, alpha=7, M=128, R_F=([0-9]+), R_P=([0-9]+)").unwrap();
let cap = regex.captures(out_lines[0]).unwrap();
consts.rounds_full = cap[1].parse::<usize>().unwrap();
consts.rounds_partial = cap[2].parse::<usize>().unwrap();
// Grab the two constants we care about
consts.round_constants = to_elems(out_lines[5]);
consts.mds = to_elems(out_lines[17]);
assert!(consts.round_constants.len() == (consts.rounds_full + consts.rounds_partial) * CELLS);
assert!(consts.mds.len() == CELLS * CELLS);
}
// Run the upstream 'official' sage code and gets the output
fn run_sage() -> String {
// Make a temporary directory
let temp_dir = tempdir().unwrap();
// Checkout the 'official' sagemath code
tracing::info!("Checking out hadeshash repo");
let status = Command::new("git")
.current_dir(temp_dir.path())
.arg("clone")
.arg("https://extgit.iaik.tugraz.at/krypto/hadeshash.git")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
assert!(status.success());
// Get the directory the checkout is in
let sage_dir = temp_dir.path().join("hadeshash");
fs::create_dir_all(&sage_dir).unwrap();
// Move the correct commit
tracing::info!("Setting commit");
let status = Command::new("git")
.current_dir(&sage_dir)
.arg("checkout")
.arg("b5434fd2b2785926dd1dd386efbef167da57c064")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
assert!(status.success());
// Get the directory the code is in
let code_dir = sage_dir.join("code");
fs::create_dir_all(&code_dir).unwrap();
// Run sage math with the right parameters
tracing::info!("Running some math, please wait about 2 minutes");
let sage_child = Command::new("sage")
.current_dir(code_dir)
.arg("generate_params_poseidon.sage")
.arg("1") // Flag to use GF(p)
.arg("0") // Flag to use box x^alpha
.arg(NUM_BITS.to_string()) // Field size in bits
.arg(CELLS.to_string()) // Number of cells
.arg(ALPHA.to_string()) // Alpha
.arg(SECURITY.to_string()) // Desired security level
.arg(format!("{:x}", FIELD)) // P in hex i.e. 15*2^27 + 1
.stdout(Stdio::piped()) // Pipe output
.spawn()
.unwrap();
// Convert the output to a bunch of lines
let sage_output = sage_child.wait_with_output().unwrap();
assert!(sage_output.status.success());
std::str::from_utf8(&sage_output.stdout)
.unwrap()
.to_string()
}
// Precompute multipliers and offsets for the partial rounds
fn compute_partial_compression(consts: &mut ComputedConstants) {
// We make a matrix of size (CELLS + rounds_partial) x (CELLS + rounds_partial +
// 1) which allows us to combine the linear operations of round constants +
// mds values. As we go, we track for each 'cell' the linear effect of all
// input cells and all sboxes up until this point.
let row_size = CELLS + consts.rounds_partial;
let rounds_partial = consts.rounds_partial;
let rounds_half_full = consts.rounds_full / 2;
let round_constants = &consts.round_constants;
let mds = &consts.mds;
let mut final_matrix = vec![Elem::new(0); row_size * row_size];
let mut final_offset = vec![Elem::new(0); row_size];
// Initially, each cell is identity
let mut cur_matrix = vec![Elem::new(0); CELLS * row_size];
for i in 0..CELLS {
cur_matrix[i * row_size + i] = Elem::new(1);
}
// Initially, offset is 0
let mut cur_offset = [Elem::new(0); CELLS];
// Now, time to do all the rounds
for round in 0..rounds_partial {
// Add the constants to the current offset
for i in 0..CELLS {
cur_offset[i] += round_constants[(rounds_half_full + round) * CELLS + i];
}
// Write data for this round's sbox based on CELL 0
final_offset[CELLS + round] = cur_offset[0];
for i in 0..row_size {
final_matrix[(CELLS + round) * row_size + i] = cur_matrix[i];
}
// Now, replace the 0th row so it is purely determined by rounds box
for (i, cur) in cur_matrix.iter_mut().enumerate().take(row_size) {
*cur = if i == CELLS + round {
Elem::new(1)
} else {
Elem::new(0)
};
}
cur_offset[0] = Elem::new(0);
// Now, do the mds multiply.
let old_matrix = cur_matrix.clone();
let old_offset = cur_offset;
for i in 0..CELLS {
for j in 0..row_size {
let mut tot = Elem::new(0);
for k in 0..CELLS {
tot += mds[i * CELLS + k] * old_matrix[k * row_size + j];
}
cur_matrix[i * row_size + j] = tot;
}
}
for i in 0..CELLS {
let mut tot = Elem::new(0);
for j in 0..CELLS {
tot += mds[i * CELLS + j] * old_offset[j];
}
cur_offset[i] = tot;
}
}
// Write out final state
for i in 0..CELLS {
final_offset[i] = cur_offset[i];
for j in 0..row_size {
final_matrix[i * row_size + j] = cur_matrix[i * row_size + j];
}
}
consts.partial_comp_matrix = final_matrix;
consts.partial_comp_offset = final_offset;
}
trait LanguageExporter {
// Define overloadable implementations
fn header();
fn export_constant(name: &str, value: usize);
fn export_array(name: &str, value: &[Elem]);
// Define main entry point that calls above overloads
fn export(consts: &ComputedConstants) {
Self::header();
Self::export_constant("CELLS", CELLS);
Self::export_constant("ALPHA", ALPHA);
Self::export_constant("ROUNDS_FULL", consts.rounds_full);
Self::export_constant("ROUNDS_HALF_FULL", consts.rounds_full / 2);
Self::export_constant("ROUNDS_PARTIAL", consts.rounds_partial);
Self::export_array("ROUND_CONSTANTS", &consts.round_constants);
Self::export_array("MDS", &consts.mds);
Self::export_array("PARTIAL_COMP_MATRIX", &consts.partial_comp_matrix);
Self::export_array("PARTIAL_COMP_OFFSET", &consts.partial_comp_offset);
}
}
struct RustLanguageExporter;
impl LanguageExporter for RustLanguageExporter {
fn header() {
const RUST_HEADER: &str = r#"// Copyright 2024 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file is automatically generated by:
// cargo xtask bootstrap-poseidon rust
use risc0_core::field::baby_bear::Elem;
macro_rules! baby_bear_array {
[$($x:literal),* $(,)?] => {
[$(Elem::new($x)),* ]
}
}
"#;
print!("{}", RUST_HEADER);
}
fn export_constant(name: &str, value: usize) {
println!("pub const {}: usize = {};", name, value);
}
fn export_array(name: &str, elems: &[Elem]) {
println!();
println!("pub const {}: &[Elem] = &baby_bear_array![", name);
for line in elems.chunks(8) {
print!(" ");
for elem in line {
print!(" 0x{:08x},", elem.as_u32())
}
println!();
}
println!("];");
}
}
struct CppLanguageExporter;
impl LanguageExporter for CppLanguageExporter {
fn header() {
const CPP_HEADER: &str = r#"// Copyright 2024 RISC Zero, Inc.
//
// All rights reserved.
// This file is automatically generated by:
// cargo xtask bootstrap-poseidon cpp
"#;
print!("{}", CPP_HEADER);
}
fn export_constant(name: &str, value: usize) {
println!("constexpr size_t {} = {};", name, value);
}
fn export_array(name: &str, elems: &[Elem]) {
println!();
println!("constexpr uint64_t {}[{}] = {{", name, elems.len());
for line in elems.chunks(8) {
print!(" ");
for elem in line {
print!(" 0x{:08x},", elem.as_u32())
}
println!();
}
println!("}};");
}
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum Lang {
Cpp,
Rust,
}
#[derive(Parser)]
pub struct BootstrapPoseidon {
/// The language to output to
lang: Lang,
}
impl BootstrapPoseidon {
pub fn run(&self) {
let mut consts = ComputedConstants::new();
let sage_str = run_sage();
extract_from_sage(&mut consts, &sage_str);
compute_partial_compression(&mut consts);
match self.lang {
Lang::Cpp => CppLanguageExporter::export(&consts),
Lang::Rust => RustLanguageExporter::export(&consts),
}
}
}