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

Fix Lagrange bases caching on the web #313

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 7 additions & 4 deletions crypto/bindings/srs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ function srsPerField(f: 'fp' | 'fq', wasm: Wasm, conversion: RustConversion) {
let setSrs = wasm[`caml_${f}_srs_set`];

let maybeLagrangeCommitment = wasm[`caml_${f}_srs_maybe_lagrange_commitment`];
let lagrangeCommitment = wasm[`caml_${f}_srs_lagrange_commitment`];
let lagrangeCommitment = (srs: WasmFpSrs, domain_size: number, i: number) =>
wasm[`caml_${f}_srs_lagrange_commitment`](srs, domain_size, i);
let setLagrangeBasis = wasm[`caml_${f}_srs_set_lagrange_basis`];
let getLagrangeBasis = (srs: WasmSrs, n: number) =>
wasm[`caml_${f}_srs_get_lagrange_basis`](srs, n);
Expand Down Expand Up @@ -158,14 +159,16 @@ function srsPerField(f: 'fp' | 'fq', wasm: Wasm, conversion: RustConversion) {

if (didRead !== true) {
// not in cache
let wasmComms = getLagrangeBasis(srs, domainSize);

if (cache.canWrite) {
// TODO: this code path will throw on the web since `caml_${f}_srs_get_lagrange_basis` is not properly implemented
// using a writable cache in the browser seems to be fairly uncommon though, so it's at least an 80/20 solution
let wasmComms = getLagrangeBasis(srs, domainSize);
let mlComms = conversion[f].polyCommsFromRust(wasmComms);
let comms = polyCommsToJSON(mlComms);
let bytes = new TextEncoder().encode(JSON.stringify(comms));

writeCache(cache, header, bytes);
} else {
lagrangeCommitment(srs, domainSize, i);
}
}

Expand Down
2 changes: 2 additions & 0 deletions js/web/web-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ function overrideBindings(plonk_wasm, worker) {
let spec = workerSpec(plonk_wasm);
for (let key in spec) {
plonk_wasm[key] = (...args) => {
if (spec[key].disabled)
throw Error(`Wasm method '${key}' is disabled on the web.`);
let u32_ptr = wasm.create_zero_u32_ptr();
worker.postMessage({
type: 'run',
Expand Down
9 changes: 9 additions & 0 deletions js/web/worker-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,20 @@ function workerSpec(wasm) {
res: wasm.WasmFqSrs,
},
caml_fp_srs_get_lagrange_basis: {
disabled: true,
args: [wasm.WasmFpSrs, undefined /* number */],
// TODO: returning a UintXArray does not work:
// the worker wrapper excepts the return value to be a number
// that can be stored in a single u32.
// A UintXArray is coerced into a 0 pointer, which doesn't trigger `wait_until_non_zero()`,
// which means the main worker just keeps spinning waiting for a response.
// A proper solution would be to wrap the return value in a pointer!
res: undefined /* UintXArray */,
},
caml_fq_srs_get_lagrange_basis: {
disabled: true,
args: [wasm.WasmFqSrs, undefined /* number */],
// TODO: returning a UintXArray does not work, see above
res: undefined /* UintXArray */,
},
caml_fp_srs_b_poly_commitment: {
Expand Down
Loading