export field elements (in the witness) as strings, as javascript handles JSON numbers as floats (53 bit precision is not enough for Goldilocks)

This commit is contained in:
Balazs Komuves 2024-12-17 18:28:27 +01:00
parent b4085c3071
commit 1079f0f3ce
No known key found for this signature in database
GPG Key ID: F63B7AEF18435562
5 changed files with 51 additions and 6 deletions

3
plonky2/examples/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.DS_Store
false.rs
lookup_bigger.rs

View File

@ -1,4 +1,5 @@
//! plonky2 prover implementation.
#![allow(non_snake_case)]
#[cfg(not(feature = "std"))]
use alloc::{format, vec, vec::Vec};
@ -138,6 +139,31 @@ struct ThingsToExport<F> {
// circuit_digest: Vec<F>,
}
fn fmap_Vec<A,B>(f: &dyn Fn(&A) -> B, input: &Vec<A>) -> Vec<B> {
let v: Vec<B> = input.iter().map(f).collect();
v
}
fn fmap_VecVec<A,B>(f: &dyn Fn(&A) -> B, input: &Vec<Vec<A>>) -> Vec<Vec<B>> {
input.iter().map( |xs| fmap_Vec(f,xs) ).collect()
}
fn field_to_string<F: RichField>(x : &F) -> String {
format!("{}",x)
}
fn fmap_ThingsToExport<A,B>(f: &dyn Fn(&A) -> B, input: &ThingsToExport<A>) -> ThingsToExport<B> {
let output = ThingsToExport {
gates: input.gates.clone(),
selector_vector: input.selector_vector.clone(),
gate_selector_columns: fmap_VecVec( f , &input.gate_selector_columns ),
lookup_selectors_columns: fmap_VecVec( f , &input.lookup_selectors_columns ),
constants_columns: fmap_VecVec( f , &input.constants_columns ),
matrix: fmap_VecVec( f , &input.matrix ),
};
output
}
// the idea is to export the witness (plus some more information),
// so that third-party tools can for example visualize it
fn collect_things_to_export<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>(
@ -266,8 +292,9 @@ where
match &prover_options.export_witness {
None => (),
Some(fname) => {
let things_to_export = collect_things_to_export::<F, C, D>( &common_data, &prover_data, &witness );
write_json_file(&fname, &things_to_export)?;
let to_export_felt : ThingsToExport<F> = collect_things_to_export::<F, C, D>( &common_data, &prover_data, &witness );
let to_export_str : ThingsToExport<String> = fmap_ThingsToExport( &field_to_string , &to_export_felt );
write_json_file(&fname, &to_export_str)?;
println!("exported witness to `{}`",fname);
},
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -251,6 +251,21 @@ function cell_hover(row,column) {
el_gate.style.background = gate_colors[sel];
}
// parseInt behaves like seriously WTF here...
function myParseInt(what) {
if (typeof what == "string") {
return parseInt(what);
}
else {
return what;
}
}
// from String to BigInt
function convertMatrix(matrix) {
return matrix.map( (vector) => vector.map(BigInt) );
}
function initialize_from_witness(fname,json) {
//console.log(json);
@ -259,8 +274,8 @@ function initialize_from_witness(fname,json) {
// these are global variables!
gates = json.gates;
matrix = json.matrix;
selectors = json.selector_vector;
matrix = convertMatrix(json.matrix);
selectors = json.selector_vector.map(myParseInt);
ngates = gates.length;
nrows = selectors.length;
ncolumns = matrix.length