Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[develop] Add o1js-stub to Mina repo in place of SnarkyJS #14649

Merged
merged 19 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
82fee60
feat(o1js-stubs): add new dune and o1js_stub.ml files for js_of_ocaml…
MartinMinkov Oct 29, 2023
9a9ba0a
feat(kimchi): add kimchi lib from o1js-bindings
MartinMinkov Nov 13, 2023
da15dfe
refactor(nix): remove snarky_js and update paths for kimchi and plonk…
MartinMinkov Nov 13, 2023
288f56e
feat(dune-project): add bindings_js package to support JavaScript bin…
MartinMinkov Nov 14, 2023
de06395
remove(SnarkyJSTest.dhall): delete SnarkyJSTest.dhall file as it's no…
MartinMinkov Nov 30, 2023
36b82f4
feat(buildkite/scripts): remove test-snarkyjs-bindings-minimal.sh and…
MartinMinkov Nov 30, 2023
c935d72
chore: remove redundant SnarkyJS build scripts
MartinMinkov Nov 30, 2023
c8e9d66
refactor(Makefile): remove snarkyjs and snarkyjs_no_types build targe…
MartinMinkov Nov 30, 2023
9d6e14f
chore(CODEOWNERS): remove snarkyjs directory from product-eng-reviewe…
MartinMinkov Nov 30, 2023
9f0e6d1
feat(kimchi): use develop version instead of copied main versions
MartinMinkov Nov 30, 2023
14b1130
Update kimchi library with latest develop changes
MartinMinkov Dec 11, 2023
ba1d03e
Merge branch 'develop' into feat/add-o1js-stubs-develop
MartinMinkov Dec 11, 2023
4b2ffbc
fix dune pathing in cargo kimchi wasm
MartinMinkov Dec 11, 2023
4f7c81b
Merge branch 'develop' into feat/add-o1js-stubs-develop
MartinMinkov Dec 13, 2023
4c9d45c
feat: fixup mina to compile as submodule in o1js
MartinMinkov Dec 13, 2023
cf39617
Merge branch 'develop' into feat/add-o1js-stubs-develop
MartinMinkov Dec 19, 2023
9fb4f06
move /kimchi/{wasm,js} to kimchi_bindings
mitschabaude Dec 19, 2023
a681b13
add back newline
mitschabaude Dec 19, 2023
d22d685
fix(rust.nix): update regex pattern to match kimchi_bindings instead …
MartinMinkov Dec 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 3 additions & 35 deletions src/lib/crypto/kimchi/wasm/src/pasta_fp_plonk_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ pub struct WasmPastaFpLookupTable {
// below.
impl From<WasmPastaFpLookupTable> for LookupTable<Fp> {
fn from(wasm_lt: WasmPastaFpLookupTable) -> LookupTable<Fp> {
LookupTable {
id: wasm_lt.id.into(),
data: wasm_lt.data.0,
}
LookupTable::create(wasm_lt.id.into(), wasm_lt.data.0)
dannywillems marked this conversation as resolved.
Show resolved Hide resolved
.expect("LookupTable -> WasmPastaFpLookupTable conversion must succeed")
}
}

Expand Down Expand Up @@ -190,36 +188,6 @@ pub fn caml_pasta_fp_plonk_index_domain_d8_size(index: &WasmPastaFpPlonkIndex) -
index.0.cs.domain.d8.size() as i32
}

#[wasm_bindgen]
pub fn caml_pasta_fp_plonk_index_decode(
dannywillems marked this conversation as resolved.
Show resolved Hide resolved
bytes: &[u8],
srs: &WasmSrs,
) -> Result<WasmPastaFpPlonkIndex, JsError> {
let mut deserializer = rmp_serde::Deserializer::new(bytes);
let mut index =
ProverIndex::<GAffine, OpeningProof<GAffine>>::deserialize(&mut deserializer)
.map_err(|e| JsError::new(&format!("caml_pasta_fp_plonk_index_decode: {}", e)))?;

index.srs = srs.0.clone();
let (linearization, powers_of_alpha) =
expr_linearization(Some(&index.cs.feature_flags), true, 3);
index.linearization = linearization;
index.powers_of_alpha = powers_of_alpha;

Ok(WasmPastaFpPlonkIndex(Box::new(index)))
}

#[wasm_bindgen]
pub fn caml_pasta_fp_plonk_index_encode(index: &WasmPastaFpPlonkIndex) -> Result<Vec<u8>, JsError> {
let mut buffer = Vec::new();
let mut serializer = rmp_serde::Serializer::new(&mut buffer);
index
.0
.serialize(&mut serializer)
.map_err(|e| JsError::new(&format!("caml_pasta_fp_plonk_index_encode: {}", e)))?;
Ok(buffer)
}

#[wasm_bindgen]
pub fn caml_pasta_fp_plonk_index_read(
offset: Option<i32>,
Expand All @@ -245,7 +213,7 @@ pub fn caml_pasta_fp_plonk_index_read(
)
.map_err(|err| JsValue::from_str(&format!("caml_pasta_fp_plonk_index_read: {err}")))?;
t.srs = srs.0.clone();
let (linearization, powers_of_alpha) = expr_linearization(Some(&t.cs.feature_flags), true, 3);
let (linearization, powers_of_alpha) = expr_linearization(Some(&t.cs.feature_flags), true);
t.linearization = linearization;
t.powers_of_alpha = powers_of_alpha;

Expand Down
38 changes: 3 additions & 35 deletions src/lib/crypto/kimchi/wasm/src/pasta_fq_plonk_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ pub struct WasmPastaFqLookupTable {

impl From<WasmPastaFqLookupTable> for LookupTable<Fq> {
fn from(wasm_lt: WasmPastaFqLookupTable) -> LookupTable<Fq> {
LookupTable {
id: wasm_lt.id.into(),
data: wasm_lt.data.0,
}
LookupTable::create(wasm_lt.id.into(), wasm_lt.data.0)
.expect("LookupTable -> WasmPastaFqLookupTable conversion must succeed")
}
}

Expand Down Expand Up @@ -189,36 +187,6 @@ pub fn caml_pasta_fq_plonk_index_domain_d8_size(index: &WasmPastaFqPlonkIndex) -
index.0.cs.domain.d8.size() as i32
}

#[wasm_bindgen]
pub fn caml_pasta_fq_plonk_index_decode(
bytes: &[u8],
srs: &WasmSrs,
) -> Result<WasmPastaFqPlonkIndex, JsError> {
let mut deserializer = rmp_serde::Deserializer::new(bytes);
let mut index =
ProverIndex::<GAffine, OpeningProof<GAffine>>::deserialize(&mut deserializer)
.map_err(|e| JsError::new(&format!("caml_pasta_fq_plonk_index_decode: {}", e)))?;

index.srs = srs.0.clone();
let (linearization, powers_of_alpha) =
expr_linearization(Some(&index.cs.feature_flags), true, 3);
index.linearization = linearization;
index.powers_of_alpha = powers_of_alpha;

Ok(WasmPastaFqPlonkIndex(Box::new(index)))
}

#[wasm_bindgen]
pub fn caml_pasta_fq_plonk_index_encode(index: &WasmPastaFqPlonkIndex) -> Result<Vec<u8>, JsError> {
let mut buffer = Vec::new();
let mut serializer = rmp_serde::Serializer::new(&mut buffer);
index
.0
.serialize(&mut serializer)
.map_err(|e| JsError::new(&format!("caml_pasta_fq_plonk_index_encode: {}", e)))?;
Ok(buffer)
}

#[wasm_bindgen]
pub fn caml_pasta_fq_plonk_index_read(
offset: Option<i32>,
Expand All @@ -244,7 +212,7 @@ pub fn caml_pasta_fq_plonk_index_read(
)
.map_err(|err| JsValue::from_str(&format!("caml_pasta_fq_plonk_index_read: {err}")))?;
t.srs = srs.0.clone();
let (linearization, powers_of_alpha) = expr_linearization(Some(&t.cs.feature_flags), true, 3);
let (linearization, powers_of_alpha) = expr_linearization(Some(&t.cs.feature_flags), true);
t.linearization = linearization;
t.powers_of_alpha = powers_of_alpha;

Expand Down
111 changes: 53 additions & 58 deletions src/lib/crypto/kimchi/wasm/src/plonk_verifier_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ macro_rules! impl_verification_key {
pub shifts: WasmShifts,
#[wasm_bindgen(skip)]
pub lookup_index: Option<WasmLookupVerifierIndex>,
pub zk_rows: isize,
}
type WasmPlonkVerifierIndex = [<Wasm $field_name:camel PlonkVerifierIndex>];

Expand All @@ -591,6 +592,7 @@ macro_rules! impl_verification_key {
evals: &WasmPlonkVerificationEvals,
shifts: &WasmShifts,
lookup_index: Option<WasmLookupVerifierIndex>,
zk_rows: isize,
) -> Self {
WasmPlonkVerifierIndex {
domain: domain.clone(),
Expand All @@ -601,6 +603,7 @@ macro_rules! impl_verification_key {
evals: evals.clone(),
shifts: shifts.clone(),
lookup_index: lookup_index.clone(),
zk_rows: zk_rows,
}
}

Expand Down Expand Up @@ -675,6 +678,7 @@ macro_rules! impl_verification_key {
s6: vi.shift[6].into(),
},
lookup_index: vi.lookup_index.map(Into::into),
zk_rows: vi.zk_rows as isize,
}
}

Expand Down Expand Up @@ -713,54 +717,17 @@ macro_rules! impl_verification_key {
}
} */

fn compute_feature_flags(index: &WasmPlonkVerifierIndex) -> FeatureFlags {
let xor = index.evals.xor_comm.is_some();
let range_check0 = index.evals.range_check0_comm.is_some();
let range_check1 = index.evals.range_check1_comm.is_some();
let foreign_field_add = index.evals.foreign_field_add_comm.is_some();
let foreign_field_mul = index.evals.foreign_field_mul_comm.is_some();
let rot = index.evals.rot_comm.is_some();

let lookup = index
.lookup_index.as_ref()
.map_or(false, |li| li.lookup_info.features.patterns.lookup);

// TODO
let runtime_tables = false;

let patterns = LookupPatterns {
xor,
lookup,
range_check: range_check0 || range_check1 || rot,
foreign_field_mul: foreign_field_mul,
};

FeatureFlags {
range_check0,
range_check1,
foreign_field_add,
foreign_field_mul,
xor,
rot,
lookup_features: LookupFeatures {
patterns,
joint_lookup_used: patterns.joint_lookups_used(),
uses_runtime_tables: runtime_tables,
},
}
}

pub fn of_wasm(
index: WasmPlonkVerifierIndex,
max_poly_size: i32,
public_: i32,
prev_challenges: i32,
log_size_of_group: i32,
srs: &$WasmSrs,
evals: &WasmPlonkVerificationEvals,
shifts: &WasmShifts,
lookup_index: Option<WasmLookupVerifierIndex>,
zk_rows: isize,
) -> (DlogVerifierIndex<GAffine, OpeningProof<GAffine>>, Arc<SRS<GAffine>>) {
let max_poly_size = index.max_poly_size;
let public_ = index.public_;
let prev_challenges = index.prev_challenges;
let log_size_of_group = index.domain.log_size_of_group;
let srs = &index.srs;
let evals = &index.evals;
let shifts = &index.shifts;

/*
let urs_copy = Rc::clone(&*urs);
let urs_copy_outer = Rc::clone(&*urs);
Expand All @@ -772,8 +739,27 @@ macro_rules! impl_verification_key {
let (endo_q, _endo_r) = poly_commitment::srs::endos::<$GOther>();
let domain = Domain::<$F>::new(1 << log_size_of_group).unwrap();

let feature_flags = compute_feature_flags(&index);
let (linearization, powers_of_alpha) = expr_linearization(Some(&feature_flags), true, 3);
let feature_flags =
FeatureFlags {
range_check0: false,
range_check1: false,
foreign_field_add: false,
foreign_field_mul: false,
rot: false,
xor: false,
lookup_features:
LookupFeatures {
patterns: LookupPatterns {
xor: false,
lookup: false,
range_check: false,
foreign_field_mul: false, },
joint_lookup_used:false,
uses_runtime_tables: false,
},
};

let (linearization, powers_of_alpha) = expr_linearization(Some(&feature_flags), true);

let index =
DlogVerifierIndex {
Expand Down Expand Up @@ -824,18 +810,29 @@ macro_rules! impl_verification_key {
Arc::clone(&srs.0)
},

zk_rows: 3,
zk_rows: zk_rows as u64,

linearization,
powers_of_alpha,
lookup_index: index.lookup_index.map(Into::into),
lookup_index: lookup_index.map(Into::into),
};
(index, srs.0.clone())
}

impl From<WasmPlonkVerifierIndex> for DlogVerifierIndex<$G, OpeningProof<$G>> {
fn from(index: WasmPlonkVerifierIndex) -> Self {
of_wasm(index).0
of_wasm(
index.max_poly_size,
index.public_,
index.prev_challenges,
index.domain.log_size_of_group,
&index.srs,
&index.evals,
&index.shifts,
index.lookup_index,
index.zk_rows
)
.0
}
}

Expand Down Expand Up @@ -910,12 +907,9 @@ macro_rules! impl_verification_key {
pub fn [<$name:snake _deserialize>](
srs: &$WasmSrs,
index: String,
) -> Result<WasmPlonkVerifierIndex, JsError> {
let vi: Result<DlogVerifierIndex<$G, OpeningProof<$G>>, serde_json::Error> = serde_json::from_str(&index);
match vi {
Ok(vi) => Ok(to_wasm(srs, vi)),
Err(e) => Err(JsError::new(&(e.to_string()))),
}
) -> WasmPlonkVerifierIndex {
let vi: DlogVerifierIndex<$G, OpeningProof<$G>> = serde_json::from_str(&index).unwrap();
return to_wasm(srs, vi.into())
}

#[wasm_bindgen]
Expand Down Expand Up @@ -995,7 +989,8 @@ macro_rules! impl_verification_key {
s5: $F::one().into(),
s6: $F::one().into(),
},
lookup_index: None
lookup_index: None,
zk_rows: 3,
}
}

Expand Down
Loading