o1-labs / o1js

TypeScript framework for zk-SNARKs and zkApps
https://docs.minaprotocol.com/en/zkapps/how-to-write-a-zkapp
Apache License 2.0
497 stars 109 forks source link

investigate chunking bug #1806

Open Trivo25 opened 3 weeks ago

Trivo25 commented 3 weeks ago

Problem: Chunking is implemented in Kimchi and Pickles. However, some specific circuit configuration seem to cause a bug in Pickles. A prior RFC talks about these specific issues, which seem to be part of the wrap circuit (which needs to implement chunking as well)

Goal: Figure out what would need to be done in order to fully support chunking in o1js. Is it a relatively small engineering task or does it require further research?

querolita commented 1 day ago

Observations after preliminary exploration of chunking bug in o1js with potential relations with Pickles code.

TODO's throughout Pickles code

Domain size of wrap circuit

Verifier

ZK rows

Exceptions

/workspace_root/src/bindings/ocaml/overrides.js:45
    if (err instanceof Error) throw err;

RuntimeError: unreachable
    at __rg_oom (wasm://wasm/011a91aa:wasm-function[4848]:0x3e27c2)
    at __rust_alloc_error_handler (wasm://wasm/011a91aa:wasm-function[4998]:0x3e3251)
    at alloc::alloc::handle_alloc_error::rt_error::h6c30faefac1fefed (wasm://wasm/011a91aa:wasm-function[5056]:0x3e355f)
    at alloc::alloc::handle_alloc_error::h6289c616c1280e38 (wasm://wasm/011a91aa:wasm-function[5055]:0x3e3554)
    at alloc::raw_vec::RawVec<T,A>::reserve::do_reserve_and_handle::h41bc9aef765cae77 (wasm://wasm/011a91aa:wasm-function[3755]:0x3c27a7)
    at <o1_utils::serialization::SerdeAs as serde_with::ser::SerializeAs<T>>::serialize_as::h1da92fe6ae712792 (wasm://wasm/011a91aa:wasm-function[2483]:0x366fca)
    at serde_with::ser::const_arrays::<impl serde_with::ser::SerializeAs<[T; N]> for [As; N]>::serialize_as::h1233a3b3efbeac4b (wasm://wasm/011a91aa:wasm-function[1484]:0x2f0683)
    at kimchi::circuits::constraints::_::<impl serde::ser::Serialize for kimchi::circuits::constraints::ColumnEvaluations<F>>::serialize::h2627bc77795152ac (wasm://wasm/011a91aa:wasm-function[1126]:0x2ad0c6)
    at kimchi::prover_index::_::<impl serde::ser::Serialize for kimchi::prover_index::ProverIndex<G,OpeningProof>>::serialize::hf0dd2bc7ae7d95f7 (wasm://wasm/011a91aa:wasm-function[1589]:0x300c01)
    at caml_pasta_fp_plonk_index_encode (wasm://wasm/011a91aa:wasm-function[1909]:0x32b28c)
    at module.exports.caml_pasta_fp_plonk_index_encode (o1js/dist/node/bindings/compiled/_node_bindings/plonk_wasm.cjs:1475:14)
    at encodeProverKey (o1js/src/lib/proof-system/prover-keys.ts:105:26)
    at write_ (o1js/src/lib/proof-system/zkprogram.ts:1017:48)
    at write$0 (src/bindings/ocaml/lib/pickles_bindings.ml:470:7)
    at spec (src/bindings/ocaml/lib/pickles_bindings.ml:421:40)
    at filter_map$1 (ocaml/base/list.ml:812:14)
    at write$1 (src/bindings/ocaml/lib/pickles_bindings.ml:417:9)
    at write$0 (src/mina/src/lib/key_cache/key_cache.ml:140:5)
    at _iCZ_ (src/mina/src/lib/pickles/cache.ml:114:18)
    at <anonymous> (src/mina/src/lib/promise/js/promise.js:25:37)

Hardcoded

The codebase is full of places where a hardcoded 3 is used to refer to the number of rows for zero knowledge and 1 for the number of chunks:

Proposals

PR16057

PR16092

mitschabaude commented 1 day ago

Nice investigation @querolita!!

When running a program with chunking on o1js (4 chunks, N1 wrap domain), it fails with an uncaught error

Judging from the trace, this is an out of memory error ("__rg_oom") that happens when writing the prover key to a file. You could compile with zkprogram.compile({ cache: Cache.None }) to skip writing the prover key (but probably the OOM will be somewhere later then.)

We might have to investigate and improve memory usage in Kimchi/Wasm!