diff --git a/.github/workflows/recursion-bench.yml b/.github/workflows/recursion-bench.yml new file mode 100644 index 0000000000..d9c0f862f4 --- /dev/null +++ b/.github/workflows/recursion-bench.yml @@ -0,0 +1,74 @@ +name: Recursion bench + +# Runs the 2→1 recursion benchmark of the README at a PR's merge commit and at its base (the merge commit's first +# parent), on one runner, and prints both reports: the cycle and row counts, the committed size and the proof size are +# deterministic, the times are this runner's. It gates nothing. + +on: + pull_request: + types: [ opened, reopened, synchronize ] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: recursion-bench-${{ github.ref }} + cancel-in-progress: true + +env: + CARGO_TERM_COLOR: always + RUST_BACKTRACE: 1 + # As in rust.yml: pin the ISA, or a cached build dies of SIGILL on an older runner. + RUSTFLAGS: -C target-cpu=haswell + +jobs: + recursion: + name: 2→1 recursion, PR and base + runs-on: ubuntu-latest + timeout-minutes: 150 + steps: + - uses: actions/checkout@v4 + with: + # The merge commit and its first parent, the base. + fetch-depth: 2 + - uses: dtolnay/rust-toolchain@stable + - uses: Swatinem/rust-cache@v2 + with: + key: recursion-bench + # The benchmark's signers, generated once and cached under target/ by `rec_aggregation::signers_cache`. + - uses: actions/cache@v4 + with: + path: target/signers-cache + key: signers-${{ hashFiles('crates/signatures/**', 'crates/rec_aggregation/src/signers_cache.rs') }} + restore-keys: signers- + - name: Build the PR and its base + run: | + mkdir -p ab + cargo build --release -p leanvm-cli + cp target/release/leanvm ab/head + git checkout --quiet --detach HEAD^1 + cargo build --release -p leanvm-cli + cp target/release/leanvm ab/base + git checkout --quiet --detach "$GITHUB_SHA" + - name: Prove the recursion step, base then PR + run: | + for side in base head; do + echo "::group::$side" + "ab/$side" recursion --n 2 --xmss-per-leaf 900 --log-inv-rate 2 --repeat 3 | tee "ab/$side.txt" + echo "::endgroup::" + done + - name: Report + run: | + field() { grep -m1 "$2" "ab/$1.txt" | sed 's/^[^:]*: *//'; } + { + echo "### 2→1 recursion over leaves of 900 leanXMSS, \`--log-inv-rate 2\`" + echo + echo "| | base | PR |" + echo "|---|---|---|" + for row in "cycles (VM steps)" "details" "proof size" "proving time" "verifying" "aggregation bytecode"; do + echo "| $row | $(field base "$row") | $(field head "$row") |" + done + echo + echo "Runner: $(lscpu | sed -n 's/^Model name: *//p' | head -n1), $(nproc) vCPUs." + } | tee -a "$GITHUB_STEP_SUMMARY" diff --git a/crates/lean_compiler/snark_lib.py b/crates/lean_compiler/snark_lib.py index 1a59eb4ece..7d752f866e 100644 --- a/crates/lean_compiler/snark_lib.py +++ b/crates/lean_compiler/snark_lib.py @@ -72,6 +72,14 @@ def hint_decompose_bits_exponent(bits, x, nbits: int) -> None: _ = bits, x, nbits +def hint_decompose_nibbles_exponent(digits, x, shift, ndigits: int, rev: int) -> None: + """Computed advice: with x = g^n and shift = g^s, the prover writes the + `ndigits` base-16 digits d_j of n >> s, low digit first, into `digits` as + the g-powers g^(d_j), each digit's four bits reversed when `rev` is 1. + UNCONSTRAINED: the caller range-checks every g^(d_j) and rebuilds x.""" + _ = digits, x, shift, ndigits, rev + + def hint_log2_ceil(bits, nbits: int, floor: int) -> _Elt: """Computed advice: returns `g^max(log2_ceil(v), floor)`, where `v` is the integer the `nbits`-cell `bits` buffer decodes to. The prover fills it at @@ -198,6 +206,17 @@ def hint_f192_limbs(dest, value) -> None: _ = dest, value +def dot(x, w, acc) -> _Elt: + """`acc + sum_j w[j] * x_j`, one `DOT` instruction: an affine form of four + GF(2^64) lanes with GF(2^192) weights. `x` is a run of two frame cells (a + StackBuf or its slice), read as four lanes, lane `2k + h` limb `h` of cell + `k`; every cell must be canonical (top limb zero), which the instruction + proves along with its lanes being in GF(2^64). `w` is a four-cell HeapBuf + slice `buf[lo:lo + 4]`, read through its pointer.""" + _ = x, w, acc + return _Elt() + + def keccak( a, b, diff --git a/crates/lean_compiler/src/ir.rs b/crates/lean_compiler/src/ir.rs index 2464520ef9..a30d731eac 100644 --- a/crates/lean_compiler/src/ir.rs +++ b/crates/lean_compiler/src/ir.rs @@ -91,6 +91,17 @@ pub(crate) enum LOp { c: Off, in_odd: bool, out_odd: bool, + /// `KECCAK_M`: `b8` holds the direction bit that orders the `m` pairs. + swap: bool, + }, + /// `DOT`: `out = acc + Σ_j w_j·x_j` over the lanes of the frame cells from `x`, the weights + /// the heap run `m[p]·g^(w+j)`. + Dot { + x: Off, + p: Off, + w: u32, + acc: Off, + out: Off, }, } diff --git a/crates/lean_compiler/src/lib.rs b/crates/lean_compiler/src/lib.rs index c835898d59..c6a915f2ae 100644 --- a/crates/lean_compiler/src/lib.rs +++ b/crates/lean_compiler/src/lib.rs @@ -255,10 +255,16 @@ pub fn disassemble(prog: &[Op]) -> String { out, in_odd, out_odd, + swap, } => { let layout = |odd: bool| if odd { "odd" } else { "even" }; + let (name, b8) = if *swap { + ("KECCAK_M", format!("bit=fp[{b8}]")) + } else { + ("KECCAK", format!("b8=fp[{b8}]")) + }; format!( - "KECCAK fp[{out}..+13] {} = keccak_f(fp[{}],fp[{}],fp[{}],fp[{}], tail=fp[{tail}..+4], b8=fp[{b8}], cap=fp[{cap}..+4]) {}", + "{name:<6} fp[{out}..+13] {} = keccak_f(fp[{}],fp[{}],fp[{}],fp[{}], tail=fp[{tail}..+4], {b8}, cap=fp[{cap}..+4]) {}", layout(*out_odd), m[0], m[1], @@ -267,6 +273,9 @@ pub fn disassemble(prog: &[Op]) -> String { layout(*in_odd) ) } + Op::Dot { x, p, w, acc, out } => { + format!("DOT fp[{out}] = fp[{acc}] + ") + } }; writeln!(out, "{:>6} {line}", pretty_integer(pc)).unwrap(); } @@ -344,6 +353,7 @@ fn resolve(op: &LOp, entry: &HashMap, sentinel: u32, pc0: u32, fram c, in_odd, out_odd, + swap, } => Op::Keccak { m: *m, tail: *tail, @@ -352,6 +362,14 @@ fn resolve(op: &LOp, entry: &HashMap, sentinel: u32, pc0: u32, fram out: *c, in_odd: *in_odd, out_odd: *out_odd, + swap: *swap, + }, + LOp::Dot { x, p, w, acc, out } => Op::Dot { + x: *x, + p: *p, + w: *w, + acc: *acc, + out: *out, }, } } diff --git a/crates/lean_compiler/src/lower.rs b/crates/lean_compiler/src/lower.rs index ec1dac66d3..66038978c7 100644 --- a/crates/lean_compiler/src/lower.rs +++ b/crates/lean_compiler/src/lower.rs @@ -914,6 +914,7 @@ impl FnLower<'_> { let ga = self.stack_addr(args); self.materialize(ga) } + Expr::Call(f, args) if f == "dot" => self.lower_dot(args), Expr::Call(f, args) if f == "hint_log2_ceil" => { // Computed advice: the prover fills g^log2_ceil (base-2 ceil-log) of the value in // `bits` (a `nbits`-bit buffer), floored at `floor`. Returned diff --git a/crates/lean_compiler/src/lower/builtins.rs b/crates/lean_compiler/src/lower/builtins.rs index a9d9e34c8a..b8c47c17d7 100644 --- a/crates/lean_compiler/src/lower/builtins.rs +++ b/crates/lean_compiler/src/lower/builtins.rs @@ -30,7 +30,14 @@ impl FnLower<'_> { /// output or those five cells). Lane 16 is in Keccak-256's 17-lane rate, but the /// transcript keeps it. fn lower_keccak(&mut self, args: &[Expr]) { - let kwargs = self.keccak_kwargs(args, "keccak", &["a", "b", "out"], &["tail", "len", "cap"]); + let kwargs = self.keccak_kwargs(args, "keccak", &["a", "b", "out"], &["tail", "len", "cap", "swap"]); + if let Some(bit) = kwargs.get("swap") { + if kwargs.len() != 1 { + self.fail("keccak swap= is a Merkle step: it takes no tail=, len= or cap=") + } + self.lower_merkle_step(args, bit); + return; + } let mut cells = self.keccak_operand(&args[0], 2); cells.extend(self.keccak_operand(&args[1], 2)); if let Some(tail) = kwargs.get("tail") { @@ -56,6 +63,7 @@ impl FnLower<'_> { c: out.state, in_odd: false, out_odd: false, + swap: false, }); self.keccak_finish(out); return; @@ -66,6 +74,60 @@ impl FnLower<'_> { self.keccak_finish(out); } + /// `keccak(a, b, out, swap=bit)`: one `KECCAK_M`, the Keccak-256 of the 64 bytes `a ‖ b` + /// when the cell `bit` holds 0 and of `b ‖ a` when it holds 1 (any other value faults). `a` + /// and `b` are two frame cells each, read where they are; the padding's first byte rides a + /// constant tail and its last byte is the instruction's own, `bit` taking its place in `b8`. + fn lower_merkle_step(&mut self, args: &[Expr], bit: &Expr) { + let mut cells = self.keccak_operand(&args[0], 2); + cells.extend(self.keccak_operand(&args[1], 2)); + cells.resize(BLOCK_CELLS as usize, SpongeCell::Zero); + self.xor_byte(&mut cells, 64, primitives::hash::PAD_FIRST); + let out = self.keccak_out(&args[2]); + let (m, tail) = self.keccak_rate(&cells, None); + let b8 = self.expr(bit); + let cap = self.keccak_pad_run() + 1; + self.emit(LOp::Keccak { + m, + tail, + b8, + cap, + c: out.state, + in_odd: false, + out_odd: false, + swap: true, + }); + self.keccak_finish(out); + } + + /// `dot(x, w, acc)`: `acc + Σ_j w[j]·x_j`, one `DOT` instruction over `n = DOT_LANES` lanes. + /// `x` is a run of `n/2` frame cells, read as `K`-lanes (lane `2k + h` is limb `h` of cell + /// `k`), each cell required canonical; `w` is an `n`-cell heap slice of weights, read through + /// its pointer. + pub(super) fn lower_dot(&mut self, args: &[Expr]) -> Off { + if args.len() != 3 { + self.fail(format!("dot(x, w, acc) takes three arguments, got {}", args.len())) + }; + const LANES: u32 = leanvm_core::tables::DOT_LANES as u32; + let x = match self.cell_run(&args[0]) { + CellRun::Stack { base, len } if len == LANES / 2 => base, + _ => self.fail(format!( + "dot's lanes must be a run of {} frame cells (a StackBuf or its slice)", + LANES / 2 + )), + }; + let (p, w) = match self.cell_run(&args[1]) { + CellRun::Heap { ptr, lo, len } if len == LANES => (ptr, lo), + _ => self.fail(format!( + "dot's weights must be a {LANES}-cell HeapBuf slice `buf[lo:lo + {LANES}]`" + )), + }; + let acc = self.expr(&args[2]); + let out = self.fresh(); + self.emit(LOp::Dot { x, p, w, acc, out }); + out + } + /// `keccak_cells(cells, out, state=, len=, final=)`: the Keccak-256 of the message the /// cells hold, 136 bytes a `KECCAK` instruction, into `out` as for [`Self::lower_keccak`]. /// `cells` is a run (a `StackBuf`, or a slice with compile-time bounds) or a list of words, @@ -273,6 +335,7 @@ impl FnLower<'_> { c, in_odd: k % 2 == 1, out_odd: !last && k % 2 == 0, + swap: false, }); state = Some(c); } @@ -378,7 +441,9 @@ impl FnLower<'_> { /// advice, re-checked in-circuit by their caller: `hint_decompose_bits` /// writes a value's bits into a buffer, `hint_decompose_bits_exponent` the /// bits of `n` where the value is `g^n` (a bounded dlog at witness - /// generation), `hint_f192_limbs` a value's coordinate limbs. + /// generation), `hint_decompose_nibbles_exponent` the base-16 digits of + /// such an `n` shifted right by another exponent, each as a g-power, + /// `hint_f192_limbs` a value's coordinate limbs. pub(super) fn lower_builtin(&mut self, f: &str, args: &[Expr]) -> bool { match f { "hint_decompose_bits" | "hint_decompose_bits_exponent" => { @@ -413,6 +478,29 @@ impl FnLower<'_> { RHint::BitDecomposeExp { value, bits, nbits } })); } + "hint_decompose_nibbles_exponent" => { + if args.len() != 5 { + self.fail(format!( + "{f} takes `(digits, value, shift, ndigits, rev)`, got {} arguments", + args.len() + )) + }; + let ndigits = self.const_index(&args[3]); + let rev = self.const_index(&args[4]); + if ndigits > 16 || rev > 1 { + self.fail(format!("{f} writes at most 16 digits, and `rev` is 0 or 1")) + } + let digits = self.bits_dest(&args[0], ndigits, f); + let value = self.expr(&args[1]); + let shift = self.expr(&args[2]); + self.pending.push(Hint::Resolved(RHint::NibbleDecomposeExp { + value, + shift, + digits, + ndigits, + rev: rev == 1, + })); + } "keccak" => self.lower_keccak(args), "keccak_cells" => self.lower_keccak_cells(args), "assert_in_k" => { diff --git a/crates/lean_compiler/src/parser.rs b/crates/lean_compiler/src/parser.rs index 513e3c4027..e260affa9e 100644 --- a/crates/lean_compiler/src/parser.rs +++ b/crates/lean_compiler/src/parser.rs @@ -252,10 +252,12 @@ const BUILTINS: &[&str] = &[ "assert_in_k", "keccak", "keccak_cells", + "dot", "const", "f192", "hint_decompose_bits", "hint_decompose_bits_exponent", + "hint_decompose_nibbles_exponent", "hint_f192_limbs", "hint_log2_ceil", "hint_witness", diff --git a/crates/lean_compiler/tests/suite/determinism.rs b/crates/lean_compiler/tests/suite/determinism.rs index c24928c01e..b1e2697de2 100644 --- a/crates/lean_compiler/tests/suite/determinism.rs +++ b/crates/lean_compiler/tests/suite/determinism.rs @@ -32,10 +32,10 @@ use leanvm_core::cpu::Program; #[rustfmt::skip] const GOLDEN: &[(&str, &str)] = &[ ("conditionals", "4f88f144b73dfb5e6453c12fb3f1b20bbc99835d2b7e8b30c0b80a8c4d558c40"), - ("const_params", "f28aa76ef2b1415821a2573704d2590e8ac85e87d47c795b270a6fa7ff6625bb"), + ("const_params", "fb4295e108630d4da87b4436a96d1c11c2531f440791b67d4309cd2dea2132d2"), ("fibonacci", "edb68f2529e0a1cb12169b8f629eae2e196a26151917af0d8d8572ded22b3a64"), - ("hash_heap_chain", "309365f5b90a6cf1fadde436e9df7d6022398b243cdd17f46c1b672044a63bdf"), - ("hash_slices", "b8f40cefc0466e7be412e27be0fa513ff02bab648dd1c55fcad8b15a8983b5d6"), + ("hash_heap_chain", "b9673017783105e88aeadc9cd95d041f67e1ee22d974a0f506a5f185f56eb077"), + ("hash_slices", "6f726881f378d0b39c4bae684a050186607f04a7b6b4939cd8d8304d7a674da2"), ("heapbuf_dyn", "95e1a69f54106145fc4f845e9076117ba3e891305dc30e20de6e2aa989ecb8e7"), ("hint", "ac78fc8dfcef7129fd40c591da640ed26c1798b4ca435fabe9994571c8fb0bcc"), ("identities", "6b5cdc55c458e318c8ea5b87f7cd212135e44698a0fd397bae5d835fa84b815a"), @@ -44,8 +44,8 @@ const GOLDEN: &[(&str, &str)] = &[ ("nested", "fa19f657b534a8bdaf894854617f65d46f35cfd49f0d19e93d2dda7a1f2b1b01"), ("runtime_loop", "187513c18352d40beae1bd224cac7140b9a9c0bf2847a0b4fe86f5982c741757"), ("scoping", "c680d0661bd5b8417ff6615c3b53dde83b15bb6554790429f68e8ed823699587"), - ("unroll", "26d5455233a7e92a04dcee57798b2e8091dfd08bfcab9667952431ec20cf5252"), - ("wots_walk", "f497f7276970efe06306bb21a324626d615e91ec78fc1d8fd7f9e74c383d4d12"), + ("unroll", "55ae8f3c7579012786a727fe1dda753883bbd17df8d8ba74ba8d662257935843"), + ("wots_walk", "5bfe31bff4046481c5d6a9386c2f624bbbcc92ed81de7a303f62b9168e951dc5"), ]; fn digest(p: &Program) -> String { diff --git a/crates/lean_compiler/tests/suite/dot.rs b/crates/lean_compiler/tests/suite/dot.rs new file mode 100644 index 0000000000..10ecbca721 --- /dev/null +++ b/crates/lean_compiler/tests/suite/dot.rs @@ -0,0 +1,47 @@ +//! `dot(x, w, acc)`: one `DOT` instruction, `acc + Σ_j w[j]·x_j` over the lanes of a run of +//! frame cells, two lanes a cell, the weights a heap slice read through its pointer. + +use lean_compiler::{compile, parse}; +use leanvm_core::cpu::{Op, prove, verify}; +use primitives::field::{F192, g_pow}; + +/// The lanes `g^j`, two to a cell, against weights `y·g^j`: the result is `Σ_j y·g^2j`, which +/// the program publishes, so the proof verifies only if `DOT` computed it. +#[test] +fn dot_end_to_end() { + let src = "\ +def main(): + weights = HeapBuf(8) + for j in unroll(0, 8): + weights[GEN ** j] = f192(0, 1, 0) * GEN ** j + lanes = StackBuf(4) + for k in unroll(0, 4): + lanes[k] = GEN ** (2 * k) + f192(0, 1, 0) * GEN ** (2 * k + 1) + p = 1 + p[1] = dot(lanes[2:4], weights[4:8], dot(lanes[0:2], weights[0:4], 0)) + p[GEN] = 7 + return +"; + let program = compile(&parse(src).expect("parse")); + let dots = program.prog.iter().filter(|op| matches!(op, Op::Dot { .. })).count(); + assert_eq!(dots, 2, "one DOT instruction a call"); + let y = F192::new(0, 1, 0); + let sum = (0..8).fold(F192::ZERO, |s, j| s + y * F192::from(g_pow(2 * j))); + let want = [sum, F192::new(7, 0, 0)]; + let (proof, _) = prove(&program, want, leanvm_core::pcs::TEST_LOG_INV_RATE).unwrap(); + verify(&program, &want, &proof).expect("the DOT program verifies"); +} + +/// `DOT` addresses its lanes in the frame, so a heap run is refused rather than bridged. +#[test] +#[should_panic(expected = "frame cells")] +fn dot_lanes_must_be_frame_cells() { + let src = "\ +def main(): + weights = HeapBuf(8) + lanes = HeapBuf(4) + y = dot(lanes[0:2], weights[0:4], 0) + return +"; + let _ = compile(&parse(src).expect("parse")); +} diff --git a/crates/lean_compiler/tests/suite/hint_nibbles.rs b/crates/lean_compiler/tests/suite/hint_nibbles.rs new file mode 100644 index 0000000000..6e59a94721 --- /dev/null +++ b/crates/lean_compiler/tests/suite/hint_nibbles.rs @@ -0,0 +1,92 @@ +//! `hint_decompose_nibbles_exponent(digits, x, shift, ndigits, rev)`: computed advice writing the +//! base-16 digits of `n >> s` (`x = g^n`, `shift = g^s`) as g-powers. The recursion guest's +//! selectors read a nibble's eq factor and its share of `g^offset` through such a digit, so the +//! advice is unconstrained and the caller's checks are what make it sound: each digit +//! range-checked below its table size, and the shares' product pinned to `x`. + +use lean_compiler::{compile, parse}; +use leanvm_core::cpu::{prove, verify}; +use primitives::field::{F64, F192, g_pow}; + +/// The digits the advice writes, low first, each published as `g^d`: plain and bit-reversed, +/// with and without a shift. +#[test] +fn nibble_advice_writes_the_digits() { + let src = "\ +def main(): + x = hint_witness(\"x\") + s = hint_witness(\"s\") + digits = StackBuf(3) + hint_decompose_nibbles_exponent(digits, x, s, 3, REV) + p = 1 + p[1] = digits[0] + p[GEN] = digits[1] * digits[2] ** 17 + return +"; + let rev4 = |d: usize| (0..4).fold(0, |r, t| r | ((d >> t) & 1) << (3 - t)); + for (n, s, rev) in [ + (0x2b7usize, 0usize, 0usize), + (0x2b7, 0, 1), + (0x5c3 << 3, 3, 0), + (0x5c3 << 3, 3, 1), + (0, 0, 0), + ] { + let mut program = compile(&parse(&src.replace("REV", &rev.to_string())).expect("parse")); + program.set_witness("x", vec![vec![F192::from(g_pow(n))]]); + program.set_witness("s", vec![vec![F192::from(g_pow(s))]]); + let d: Vec = (0..3) + .map(|j| ((n >> s) >> (4 * j)) & 15) + .map(|d| if rev == 1 { rev4(d) } else { d }) + .collect(); + // digits[1] · digits[2]^17 + let want = [F192::from(g_pow(d[0])), F192::from(g_pow(d[1] + 17 * d[2]))]; + let (proof, _) = prove(&program, want, leanvm_core::pcs::TEST_LOG_INV_RATE).unwrap(); + verify(&program, &want, &proof).unwrap_or_else(|_| panic!("n={n:#x} s={s} rev={rev}")); + } +} + +/// The checks a selector runs on the digits, here on digits a dishonest prover supplies through +/// a witness stream: every digit range-checked below 16, and `Π g^(d_j·16^j) = x`. +const CHECKED: &str = "\ +def main(): + x = hint_witness(\"x\") + digits = StackBuf(2) + hint_witness(digits[0:2], \"digits\") + RANGE + hi = digits[1] ** 16 + assert digits[0] * hi == x + p = 1 + p[1] = x + p[GEN] = 1 + return +"; + +fn run(range: bool, digits: [usize; 2]) -> bool { + let range = if range { + "assert log(digits[0]) < 16\n assert log(digits[1]) < 16" + } else { + "" + }; + let mut program = compile(&parse(&CHECKED.replace("RANGE", range)).expect("parse")); + let x = F192::from(g_pow(0x21)); + program.set_witness("x", vec![vec![x]]); + program.set_witness("digits", vec![digits.iter().map(|&d| F192::from(g_pow(d))).collect()]); + let want = [x, F192::from(F64::ONE)]; + match prove(&program, want, leanvm_core::pcs::TEST_LOG_INV_RATE) { + Ok((proof, _)) => verify(&program, &want, &proof).is_ok(), + Err(_) => false, + } +} + +/// `0x21` is `1 + 16·2`, and also `17 + 16·1`: the product alone accepts the second split, whose +/// low digit would read past its nibble's table; the range check refuses it. +#[test] +fn an_out_of_range_digit_is_refused() { + assert!(run(true, [1, 2]), "the honest digits pass"); + assert!( + run(false, [17, 1]), + "without the range check the forged split passes the product" + ); + assert!(!run(true, [17, 1]), "the range check refuses the forged split"); + assert!(!run(true, [2, 2]), "a wrong digit fails the product"); +} diff --git a/crates/lean_compiler/tests/suite/main.rs b/crates/lean_compiler/tests/suite/main.rs index b9ee5888c9..aced691088 100644 --- a/crates/lean_compiler/tests/suite/main.rs +++ b/crates/lean_compiler/tests/suite/main.rs @@ -6,9 +6,11 @@ mod assert_ne; mod const_placeholder; mod determinism; mod disassemble; +mod dot; mod field_div; mod field_towers; mod hint_log2_ceil; +mod hint_nibbles; mod inline_expr; mod loop_frames; mod miscompiles; diff --git a/crates/lean_compiler/zkDSL.md b/crates/lean_compiler/zkDSL.md index 5a54fdd104..7e669205de 100644 --- a/crates/lean_compiler/zkDSL.md +++ b/crates/lean_compiler/zkDSL.md @@ -1,8 +1,8 @@ # zkDSL Language Reference (leanVM) -The zkDSL is a Python-syntax language that compiles to the leanVM ISA: six instructions (`XOR`, `MUL`, `SET`, `DEREF`, `JUMP`, `KECCAK`) over the binary field GF(2^192), with write-once memory and all indices carried "in the exponent" as powers of a fixed generator. For the underlying VM and proving system, see [`doc/leanvm/main.tex`](../../doc/leanvm/main.tex). +The zkDSL is a Python-syntax language that compiles to the leanVM ISA: seven instructions (`XOR`, `MUL`, `SET`, `DEREF`, `JUMP`, `KECCAK`, `DOT`) over the binary field GF(2^192), with write-once memory and all indices carried "in the exponent" as powers of a fixed generator. For the underlying VM and proving system, see [`doc/leanvm/main.tex`](../../doc/leanvm/main.tex). -Source files use the `.py` extension and are **Python-shaped**: they import the [`snark_lib`](snark_lib.py) stub, which defines `GEN`, `log`, `mul_range`, `HeapBuf`, `StackBuf`, `assert_in_k`, `keccak` and `keccak_cells`, so editors and linters resolve the intrinsic names. The compiler skips the import. Ordinary helpers such as `pack64x2` are defined in the single-file guest. A program that uses placeholders is not a runnable Python file: its `*_PLACEHOLDER` identifiers are undefined until the host fills them in, so importing it raises `NameError`. +Source files use the `.py` extension and are **Python-shaped**: they import the [`snark_lib`](snark_lib.py) stub, which defines `GEN`, `log`, `mul_range`, `HeapBuf`, `StackBuf`, `assert_in_k`, `dot`, `keccak` and `keccak_cells`, so editors and linters resolve the intrinsic names. The compiler skips the import. Ordinary helpers such as `pack64x2` are defined in the single-file guest. A program that uses placeholders is not a runnable Python file: its `*_PLACEHOLDER` identifiers are undefined until the host fills them in, so importing it raises `NameError`. Entry points: `lean_compiler::parse` / `parse_with_replacements` → `lean_compiler::compile` → `leanvm_core::cpu::prove` / `verify`. @@ -419,6 +419,14 @@ challenge = state[0] + d2[0] * f192(0, 0, 1) Both state words are hash outputs, so their top limbs are already zero. Only `d2` must be exposed separately; deriving `d3 = (s1+d2)/Y` and proving both values lie in GF(2^64) binds the one hinted limb by uniqueness of the tower representation. The challenge is `s0+d2·Y² = d0+d1·Y+d2·Y²`, while `d3` is checked but deliberately discarded. `challenge_from_state` is not a compiler intrinsic: this is the complete `@inline` helper used by the recursion guest. +## DOT + +```python +y = dot(cells[0:2], weights[4 * k:4 * k + 4], acc) # acc + sum_j weights[4k + j] * lane_j +``` + +`dot(x, w, acc)` is one `DOT` instruction: `acc + Σ_{j<4} w[j]·x_j`, an affine form of four GF(2^64) lanes with GF(2^192) weights. `x` is a run of two frame cells (a `StackBuf` or a slice of one with compile-time bounds), read as four lanes: lane `2k + h` is limb `h` of cell `k`, the layout `pack64x2` builds and the one `KECCAK` hashes. Every cell must be canonical (top limb zero), and the instruction proves it, with both its lanes in GF(2^64), as `KECCAK` does for the cells it reads: a run that `dot` and `keccak_cells` both read needs no `assert_in_k`. `w` is a four-cell `HeapBuf` slice `buf[lo:lo + 4]` (or a pointer to frame cells from `addr`), read through its pointer; `acc` is any value. The result is a fresh value. A chain of `dot`s weighs a row of opened data against weights a verifier derived from its transcript, one row per four terms where a `MUL` and an `XOR` per term would do it. + ## KECCAK ```python @@ -443,6 +451,7 @@ keccak_cells([18, 19], out, state=st) # as given, lanes 16..25 (the transcript's capacity) the last five cells of the previous # output `st`. keccak(rate[0:2], rate[2:4], nxt, tail=rate[4:8], cap=st) +keccak(node[0:2], sib[0:2], h, swap=bit) # a Merkle step: H(node ‖ sib) if bit is 0, H(sib ‖ node) if 1 ``` The hash is Keccak-256 (`primitives::hash::hash`), Ethereum's. A block is 136 bytes, 8.5 cells: block `k` of a message in consecutive cells is the nine cells from `17k / 2`, starting halfway into the first when `k` is odd. The compiler XORs a block's nine cells into the previous output and emits one `KECCAK`, which reads the state in the cell layout of the block's parity and writes it in the next block's, so every XOR is of whole cells. The compiler also places the padding `0x01 ‖ 0^* ‖ 0x80`. @@ -451,6 +460,8 @@ Both builtins are **statements**: they write the 13-cell sponge state into `out` `keccak(a, b, out, tail=, len=)` is one instruction, the hash of at most 128 bytes: the cells `a ‖ b ‖ tail` (two, two and four; `tail` omitted is none), and the message their first `len` bytes (a compile-time integer, by default all of them: 64 bytes without `tail` and 128 with). The form every Merkle node and chain step uses. `cap=` is the Fiat-Shamir sponge's step instead: no padding, the eight message cells overwriting lanes 0..16, and lanes 16..25 the five cells a previous output ends with (`cap` that 13-cell output or those five cells). Keccak-256's rate is 17 lanes, 136 bytes, so lane 16 is rate to the hash; the transcript keeps it with the capacity, lanes 17..25, and calls lanes 16..25 its capacity. +`keccak(a, b, out, swap=bit)` is one `KECCAK_M`, a Merkle step: the hash of `a ‖ b` when the cell `bit` holds 0 and of `b ‖ a` when it holds 1 (anything else faults, and no proof has such a row). `a` and `b` are read where they are, so a path level is this one instruction plus the read of its bit into the frame, with no copy of the node into a selected slot. It takes no other keyword. + `keccak_cells(cells, out, state=, len=, final=)` hashes a message of any compile-time length: `cells` is a run (a `StackBuf`, or a slice with compile-time bounds), a list of words, or a list comprehension `[e for k in unroll(lo, hi)]` (its bounds compile-time integers once `Const` parameters are substituted, expanded into the list of `e` at each `k`), and the message is their first `len` bytes (all of them by default). Words named in a list are read where they lie. `state=` continues a hash from a previous 13-cell output whose message so far is a whole number of block pairs, 272 bytes or 17 cells each, so that the cells start a block at a cell boundary: what a runtime loop absorbing a block pair a frame needs. `final=0` absorbs the cells zero-extended to whole blocks, with no padding: the start of a hash continued later, or a bare permutation. The padding's first byte goes where the message ends and its last byte at the end of the last block: into a constant cell it folds, into a cell the program wrote it costs one `XOR`. A cell wholly past `len` must be a literal `0`, or the compiler rejects it; the bytes past `len` in the cell where the message ends must be zero, which the circuit does not enforce. @@ -501,6 +512,7 @@ Three builtins have the prover compute the values at witness generation instead - `hint_decompose_bits(bits, value, nbits[, start])`: writes `nbits` bits of `value`, from bit `start` on (default `0`), into the buffer `bits`, one field element (`0`/`1`) per bit. - `hint_decompose_bits_exponent(bits, x, nbits)`: writes the `nbits` bits of the exponent `n` where `x = GEN ** n` into `bits` (a bounded dlog at witness generation). +- `hint_decompose_nibbles_exponent(digits, x, shift, ndigits, rev)`: writes the `ndigits` base-16 digits `d_j` of `n >> s`, where `x = GEN ** n` and `shift = GEN ** s`, low digit first, into `digits` as `GEN ** d_j`; with `rev = 1` each digit's four bits are reversed. A caller range-checks each digit (`assert log(d) < 16`) and rebuilds `x` from them, which costs a few cycles a digit where bits cost four a bit. - `g = hint_log2_ceil(bits, nbits, floor)`: returns `GEN ** log2_ceil(v)` for the value `v` held bitwise in the `nbits`-bit buffer `bits`, floored at `floor`. `bits` is a `HeapBuf` or a `StackBuf` (of at least `nbits` cells). Prefer the `StackBuf`: a frame cell is addressed directly, so `bits[i]` at a compile-time index is free where a heap read is a `DEREF`, and the booleanity pin `bits[i] = b * b` is then one `MUL` rather than a `MUL` and a `DEREF`. Use `addr` below where the run must also be indexed at runtime or reached from elsewhere. @@ -525,6 +537,7 @@ Three builtins have the prover compute the values at witness generation instead | `unroll` iteration | body only (compile-time replication) | | `keccak(a, b, out, ...)` | 1; plus six `SET`s once per frame for the padding run (a fresh block), one `XOR` per nonzero message cell of a later block, one for a padding byte in a program cell, +1 `DEREF` per heap cell, +2 copies into a 2-cell `out`, +1 `MUL` per runtime slice start | | `hint_witness(dest, "name")` | 0 (+1 `MUL` for a runtime slice start) | +| `dot(x, w, acc)` | 1, for four weighed lanes (+1 `SET` amortized for a literal `acc`) | Every cost above is the FIRST occurrence. Two identical pure operations in one function share one cell and the second is free, so `hb[i]` twice, or `row[i]` where `row = hb * GEN ** 2`, costs one pointer `MUL` between them. The sharing stops at a branch: a cell whose instruction sits inside an `if` is not reused after the join, because the other path leaves it unwritten and therefore prover-chosen. diff --git a/crates/leanvm_core/src/cpu/execute.rs b/crates/leanvm_core/src/cpu/execute.rs index d25adb898d..6eca700722 100644 --- a/crates/leanvm_core/src/cpu/execute.rs +++ b/crates/leanvm_core/src/cpu/execute.rs @@ -97,6 +97,12 @@ impl Memory { self.page(i >> PAGE_LOG).map_or(F192::ZERO, |p| p[i & (PAGE - 1)].word) } + /// Overwrite cell `i`, as a dishonest prover would. + #[cfg(test)] + pub(crate) fn set_word(&mut self, i: usize, value: F192) { + self.pages[i >> PAGE_LOG][i & (PAGE - 1)].word = value; + } + /// Page `p`'s cells, `None` past the last one. pub(crate) fn page(&self, p: usize) -> Option<&[Cell]> { self.pages.get(p).map(|p| &p[..]) @@ -139,6 +145,8 @@ pub enum Fault { NotAGPower { what: &'static str, value: F64 }, /// A `KECCAK` input cell outside the 128-bit embedding. NotCanonical { cell: usize, value: F192 }, + /// A `KECCAK_M` whose `b8` cell's low limb is not a bit, or that reads the odd layout. + BadMerkleStep { value: F192 }, /// A `MUL` asked to solve `a·x = c` for `x` with `a = 0`. BackSolveThroughZero, /// The advice does not fit the program: a witness stream is missing or @@ -207,6 +215,11 @@ impl std::fmt::Display for Fault { "KECCAK input cell {cell} is not a canonical 128-bit embedding ({})", word(*value) ), + Self::BadMerkleStep { value } => write!( + f, + "KECCAK_M needs the even layout and a direction bit in b8's low limb ({})", + word(*value) + ), Self::BackSolveThroughZero => write!(f, "cannot back-solve MUL through a zero operand"), Self::Witness(problem) => write!(f, "{problem}"), Self::OutOfMemory => write!(f, "the program allocates past 2^{} cells", MAX_CELLS.ilog2()), @@ -356,6 +369,7 @@ impl Program { let mut deref: Rows = Rows::new(); let mut jump: Rows = Rows::new(); let mut keccak: Rows = Rows::new(); + let mut dot: Rows = Rows::new(); // The memory's pages, grown as the run reaches them. Every method on the hot // path is `#[inline(always)]`: they sit in the interpreter's opcode loop. @@ -583,6 +597,7 @@ impl Program { RHint::Log2Ceil { .. } => "Log2Ceil", RHint::BitDecompose { .. } => "BitDecompose", RHint::BitDecomposeExp { .. } => "BitDecomposeExp", + RHint::NibbleDecomposeExp { .. } => "NibbleDecomposeExp", RHint::FieldLimbs { .. } => "FieldLimbs", RHint::Inverse { .. } => "Inverse", RHint::Print { .. } => "Print", @@ -734,6 +749,38 @@ impl Program { m.put(bb + j, F192::new(bit, 0, 0))?; } } + RHint::NibbleDecomposeExp { + value, + shift, + digits, + ndigits, + rev, + } => { + const WHAT: &str = "hint_decompose_nibbles_exponent value"; + const SHIFT: &str = "hint_decompose_nibbles_exponent shift"; + let x = in_k(WHAT, m.get(fp + value)).map_err(fail)?; + let n = bounded_dlog(&mut dlog_cache, x, (4 * ndigits).min(64)) + .ok_or_else(|| fail(Fault::NotAGPower { what: WHAT, value: x }))?; + let y = in_k(SHIFT, m.get(fp + shift)).map_err(fail)?; + let s = bounded_dlog(&mut dlog_cache, y, 7) + .ok_or_else(|| fail(Fault::NotAGPower { what: SHIFT, value: y }))?; + let n = if s >= 128 { 0 } else { n >> s }; + let bb = bits_base(&m, &mut g, fp, *digits, "hint_decompose_nibbles_exponent pointer") + .map_err(fail)?; + for j in 0..*ndigits { + let d = if 4 * j >= 128 { + 0 + } else { + ((n >> (4 * j)) & 15) as usize + }; + let d = if *rev { + (d as u8).reverse_bits() as usize >> 4 + } else { + d + }; + m.put(bb + j, F192::new(g.pow(d).0, 0, 0))?; + } + } RHint::FieldLimbs { value, base, len } => { assert!((1..=3).contains(len), "an F192 value has three K limbs"); let v = m.get(fp + value); @@ -921,7 +968,22 @@ impl Program { Op::Keccak { .. } => { use crate::keccak::{ROW_CELLS, STATE_CELLS}; let k = crate::tables::KeccakOp::of(&op); - let cells: [u32; ROW_CELLS] = std::array::from_fn(|c| k.cell(fp, c)); + // KECCAK_M: the low limb of b8 is the direction bit, which picks the order the + // two m pairs are read in. + let bit = if k.swap { + if k.in_odd { + return Err(fail(Fault::BadMerkleStep { value: F192::ZERO })); + } + let b8 = m.read(k.cell(fp, 8, false)); + match b8.c0 { + 0 => false, + 1 => true, + _ => return Err(fail(Fault::BadMerkleStep { value: b8 })), + } + } else { + false + }; + let cells: [u32; ROW_CELLS] = std::array::from_fn(|c| k.cell(fp, c, bit)); let input: [F192; STATE_CELLS] = std::array::from_fn(|c| m.read(cells[c])); // The table reads every cell with a literal zero top limb, so anything // else could not balance the bus. @@ -934,6 +996,9 @@ impl Program { // by the Keccak reduction, §keccak); the interpreter still computes it so // the output cells are consistent for any later read. let mut lanes = crate::keccak::state_of_cells(&input, k.in_odd); + if k.swap { + lanes[16] = primitives::hash::END_BIT; + } keccak_step(&lanes); primitives::hash::keccak_f(&mut lanes); let output = crate::keccak::cells_of_state(&lanes, k.out_odd); @@ -951,6 +1016,47 @@ impl Program { }); pc += 1; } + Op::Dot { x, p, w, acc, out } => { + use crate::tables::{DOT_CELLS, DOT_LANES}; + let pointer = m.read(fp + p); + let p_addr = as_addr(pointer).ok_or_else(|| fail(Fault::WildPointer { value: pointer }))?; + let base = g + .log(p_addr) + .ok_or_else(|| fail(Fault::WildPointer { value: pointer }))? + + w; + let mut cells = [0u32; DOT_CELLS]; + for k in 0..DOT_LANES / 2 { + cells[k] = fp + x + k as u32; + } + cells[DOT_LANES / 2] = fp + p; + for j in 0..DOT_LANES { + cells[DOT_LANES / 2 + 1 + j] = base + j as u32; + } + cells[DOT_CELLS - 2] = fp + acc; + cells[DOT_CELLS - 1] = fp + out; + let mut value = m.read(fp + acc); + for k in 0..DOT_LANES / 2 { + // The table reads a lane cell as `(lo, hi, 0)`, so anything else could + // not balance the bus. + let cell = m.read(cells[k]); + if cell.c2 != 0 { + return Err(fail(Fault::NotCanonical { cell: k, value: cell })); + } + for (h, lane) in [cell.c0, cell.c1].into_iter().enumerate() { + value += m.read(cells[DOT_LANES / 2 + 1 + 2 * k + h]) * F192::new(lane, 0, 0); + } + } + m.put(fp + out, value)?; + let r: [u32; DOT_CELLS] = std::array::from_fn(|c| m.bump_access_count(cells[c])); + dot.push(Dotrow { + pc, + fp, + w: base, + r, + bytecode_read, + }); + pc += 1; + } } steps += 1; } @@ -1005,6 +1111,7 @@ impl Program { deref, jump, keccak, + dot, bytecode_count, counts, }; diff --git a/crates/leanvm_core/src/cpu/hints.rs b/crates/leanvm_core/src/cpu/hints.rs index a60f51722d..19b5e96ec8 100644 --- a/crates/leanvm_core/src/cpu/hints.rs +++ b/crates/leanvm_core/src/cpu/hints.rs @@ -221,6 +221,16 @@ pub enum RHint { /// Write the `nbits` bits of `n`, where `m[fp+value] = g^n` (a bounded /// discrete log at witness generation), into `bits`. BitDecomposeExp { value: Off, bits: BitsDest, nbits: u32 }, + /// Write the `ndigits` base-16 digits of `n >> s`, where `m[fp+value] = g^n` and + /// `m[fp+shift] = g^s` (bounded discrete logs at witness generation), into `digits` as the + /// g-powers `g^d`, the low digit first; with `rev`, each digit's four bits reversed. + NibbleDecomposeExp { + value: Off, + shift: Off, + digits: BitsDest, + ndigits: u32, + rev: bool, + }, /// Write the first `len` K-coordinate limbs of `m[fp+value]` to /// `m[fp+base..]`. Computed advice; callers constrain the result. FieldLimbs { value: Off, base: Off, len: u32 }, diff --git a/crates/leanvm_core/src/cpu/isa.rs b/crates/leanvm_core/src/cpu/isa.rs index b33633ec81..7059b0ec7a 100644 --- a/crates/leanvm_core/src/cpu/isa.rs +++ b/crates/leanvm_core/src/cpu/isa.rs @@ -43,6 +43,13 @@ pub enum Op { /// output: block `k` of a message laid out in cells starts at cell `17k / 2`, halfway /// through it when `k` is odd, and the odd layout pairs the lanes as such a block's cells /// do. The relation is proven by the Keccak reduction ([`crate::keccak`]). + /// + /// With `swap` (opcode `KECCAK_M`, a Merkle step; even input layout only), the cell `b8` + /// holds a direction bit `s` in its low limb instead of lane 16, which is Keccak-256's last + /// padding byte for a one-block message (`hash::PAD_LAST << 56`), and the two `m` pairs are + /// read in the order `s` selects: `m[0], m[1]` then `m[2], m[3]` when `s = 0`, the other + /// way round when `s = 1`. A Merkle path's level is then this one row: the running node + /// and the hinted sibling hashed in the order the query bit says. Keccak { m: [u32; 4], tail: u32, @@ -51,6 +58,20 @@ pub enum Op { out: u32, in_odd: bool, out_odd: bool, + swap: bool, + }, + /// `DOT`: an affine form of `n = tables::DOT_LANES` `K`-lanes, + /// `mem[fp·out] = mem[fp·acc] + Σ_{j [Vec; 1 + BYTECODE_OPERANDS] { out, in_odd, out_odd, + swap, } => { let [m0, m1, m2, m3] = m.map(g_at); put(&[ @@ -338,7 +339,11 @@ pub fn bytecode_columns(prog: &[Op]) -> [Vec; 1 + BYTECODE_OPERANDS] { flag(in_odd), flag(out_odd), ]); - OP_KECCAK + if swap { OP_KECCAK_M } else { OP_KECCAK } + } + Op::Dot { x, p, w, acc, out } => { + put(&[g_at(x), g_at(p), g_at(w), g_at(acc), g_at(out)]); + OP_DOT } }; row diff --git a/crates/leanvm_core/src/cpu/mod.rs b/crates/leanvm_core/src/cpu/mod.rs index a60e189084..35096599e9 100644 --- a/crates/leanvm_core/src/cpu/mod.rs +++ b/crates/leanvm_core/src/cpu/mod.rs @@ -14,7 +14,8 @@ use crate::constraints; use crate::leaf::{self, Block, ColumnClaim, Coord}; use crate::pcs; use crate::tables::{ - self, FillCtx, FlushBuilder, OP_DEREF, OP_JUMP, OP_KECCAK, OP_MUL, OP_SET, OP_XOR, SEP_BYTECODE, SEP_MEM, SEP_STATE, + self, FillCtx, FlushBuilder, OP_DEREF, OP_DOT, OP_JUMP, OP_KECCAK, OP_KECCAK_M, OP_MUL, OP_SET, OP_XOR, + SEP_BYTECODE, SEP_MEM, SEP_STATE, }; use crate::transcript::ProverState; use crate::witness; @@ -29,7 +30,7 @@ pub(crate) use execute::Cell; pub use execute::{ExecError, Execution, Fault, Memory}; pub use isa::{DerefMode, Op}; pub use layout::*; -pub(crate) use trace::{Drow, Jrow, Krow, Rows, Srow, Trace, Xrow}; +pub(crate) use trace::{Dotrow, Drow, Jrow, Krow, Rows, Srow, Trace, Xrow}; /// Data-memory size bounds (doc §Memory): memory is `2^h` cells with /// `MIN_LOG_MEM ≤ h ≤ MAX_LOG_MEM`. The prover pads up to the minimum; the @@ -380,7 +381,7 @@ pub struct Stats { impl Stats { /// Table names in `rows` order. - pub const TABLES: [&'static str; tables::N_TABLES] = ["XOR", "MUL", "SET", "DEREF", "JUMP", "KECCAK"]; + pub const TABLES: [&'static str; tables::N_TABLES] = ["XOR", "MUL", "SET", "DEREF", "JUMP", "KECCAK", "DOT"]; /// One line of per-table instruction counts and shares, largest first, followed by memory, bytecode and committed-witness sizes. /// @@ -421,13 +422,24 @@ impl Stats { /// the commitment. #[tracing::instrument(name = "Prove", skip_all, fields(log_inv_rate))] pub fn prove(program: &Program, public_input: [F192; 2], log_inv_rate: usize) -> Result<(Proof, Stats), ProveError> { + prove_with(program, public_input, log_inv_rate, |_| ()) +} + +/// [`prove`], with `tamper` editing the run before its witness is built: how a test plays a prover that lies. +fn prove_with( + program: &Program, + public_input: [F192; 2], + log_inv_rate: usize, + tamper: impl FnOnce(&mut Execution), +) -> Result<(Proof, Stats), ProveError> { if ::pcs::whir::validate_log_inv_rate(log_inv_rate).is_err() { return Err(ProveError::InvalidRate { log_inv_rate }); } let (exec, keccak_blocks) = crate::stage!("Execute program", || { crate::keccak::while_executing(|step| program.execute_with(public_input, step)) }); - let exec = exec.map_err(ProveError::Execution)?; + let mut exec = exec.map_err(ProveError::Execution)?; + tamper(&mut exec); // A live value that came from outside the constraint system would make the proof // about a weaker statement than the program text, so it is refused here, on the one // path every proof takes, rather than left to whichever test happens to look. @@ -696,6 +708,7 @@ mod tests { out: 13, in_odd: false, out_odd: true, + swap: false, }); prog.push(Op::Keccak { m: [13, 14, 15, 16], @@ -705,6 +718,7 @@ mod tests { out: 26, in_odd: true, out_odd: false, + swap: false, }); for k in 0..2u32 { prog.push(Op::Set { @@ -793,7 +807,11 @@ mod tests { let jumping = program(Op::Jump { oc: 1, od: 2, of: 1 }); let exec = jumping.execute(pi).expect("the run halts in the final state"); assert_eq!(exec.cycles, 5, "three SETs and both JUMPs, the last slot's included"); - assert_eq!(exec.rows(), [0, 0, 3, 0, 2, 0], "XOR, MUL, SET, DEREF, JUMP, KECCAK"); + assert_eq!( + exec.rows(), + [0, 0, 3, 0, 2, 0, 0], + "XOR, MUL, SET, DEREF, JUMP, KECCAK, DOT" + ); let (proof, _) = prove(&jumping, pi, crate::pcs::TEST_LOG_INV_RATE).unwrap(); verify(&jumping, &pi, &proof).expect("the verifier accepts the run"); @@ -806,6 +824,194 @@ mod tests { assert!(matches!(err.fault, Fault::HaltOutsideMain { fp: 1 }), "{err}"); } + const DOT_CELLS: usize = crate::tables::DOT_LANES / 2; + + /// A `DOT` program: the lane cells from 2, the weight pointer at 6, the accumulator at 7, the + /// result at 8, and the weights from 16, which the pointer `g^16` names from main's frame. + fn dot_program(cells: [F192; DOT_CELLS], weights: [F192; crate::tables::DOT_LANES], acc: F192) -> Program { + let mut prog: Vec = (2..).zip(cells).map(|(o, k)| Op::Set { o, k }).collect(); + prog.push(Op::Set { + o: 6, + k: F192::from(g_pow(16)), + }); + prog.push(Op::Set { o: 7, k: acc }); + prog.extend((16..).zip(weights).map(|(o, k)| Op::Set { o, k })); + prog.push(Op::Dot { + x: 2, + p: 6, + w: 0, + acc: 7, + out: 8, + }); + prog.resize(15, Op::Set { o: 30, k: F192::ZERO }); + prog.push(Op::Xor { a: 0, b: 0, c: 0 }); // sentinel + Program::from_bytecode(prog, 32) + } + + fn dot_inputs() -> ([F192; DOT_CELLS], [F192; crate::tables::DOT_LANES], F192) { + let cells = std::array::from_fn(|k| F192::new(0x9e37_79b9_7f4a_7c15 ^ k as u64, 0x0123_4567 << k, 0)); + let weights = std::array::from_fn(|j| { + F192::new(0xfeed_face_dead_beef, 0x1111_2222_3333_4444, 0x5555_6666_7777_8888) * F192::from(g_pow(j)) + }); + (cells, weights, F192::new(3, 5, 7)) + } + + /// `DOT` adds the accumulator and its weighed lanes, lane `2k + h` being limb `h` of cell `k`, + /// and the run it is in proves and verifies. + #[test] + fn dot_weighs_its_lanes() { + let (cells, weights, acc) = dot_inputs(); + let program = dot_program(cells, weights, acc); + let pi = [w(7), w(11)]; + let exec = program.execute(pi).unwrap(); + let lanes = cells.iter().flat_map(|c| [c.c0, c.c1]); + let expected = weights + .iter() + .zip(lanes) + .fold(acc, |sum, (&wj, lane)| sum + wj * F192::new(lane, 0, 0)); + assert_eq!(exec.mem.word(8), expected); + assert_eq!(exec.rows()[crate::tables::N_TABLES - 1], 1, "one DOT row"); + let (proof, _) = prove(&program, pi, crate::pcs::TEST_LOG_INV_RATE).unwrap(); + verify(&program, &pi, &proof).expect("a DOT run verifies"); + } + + /// A lane cell carries `(lo, hi, 0)`, as a `KECCAK` state cell does. + #[test] + fn dot_requires_canonical_cells() { + let (mut cells, weights, acc) = dot_inputs(); + cells[1].c2 = 1; + let err = dot_program(cells, weights, acc) + .execute([w(7), w(11)]) + .err() + .expect("the run must fail"); + assert!(matches!(err.fault, Fault::NotCanonical { cell: 1, .. }), "{err}"); + } + + /// The result is bound by the bus: with another value in the result cell than the row's + /// `acc + Σ w·x`, the bus does not balance, so the prover's GKR has no proof to make (it + /// panics on the two products), and a proof it did make would not verify. + #[test] + fn dot_result_is_bound() { + let (cells, weights, acc) = dot_inputs(); + let program = dot_program(cells, weights, acc); + let pi = [w(7), w(11)]; + let forged = std::panic::catch_unwind(|| { + prove_with(&program, pi, crate::pcs::TEST_LOG_INV_RATE, |exec| { + let forged = exec.mem.word(8) + F192::ONE; + exec.mem.set_word(8, forged); + }) + }); + if let Ok(Ok((proof, _))) = forged { + assert!(verify(&program, &pi, &proof).is_err(), "a forged DOT result verifies"); + } + } + + /// Two `KECCAK_M` rows over the same node `2..4` and sibling `4..6`, their direction bits at + /// 6 and 7, the padding tail `8..12` (`PAD_FIRST` in lane 8) and the zero capacity `12..16`, + /// writing their states at 16 and 29. + fn merkle_program(bits: [u64; 2]) -> Program { + let pairs = [ + F192::new(0x0123_4567_89ab_cdef, 0x1111, 0), + F192::new(0x2222, 0x3333, 0), + F192::new(0xfeed_face_dead_beef, 0x4444, 0), + F192::new(0x5555, 0x6666_7777, 0), + ]; + let mut prog: Vec = (2..).zip(pairs).map(|(o, k)| Op::Set { o, k }).collect(); + prog.push(Op::Set { o: 6, k: w(bits[0]) }); + prog.push(Op::Set { o: 7, k: w(bits[1]) }); + prog.push(Op::Set { + o: 8, + k: w(u64::from(primitives::hash::PAD_FIRST)), + }); + prog.extend((9..16).map(|o| Op::Set { o, k: F192::ZERO })); + for (b8, out) in [(6, 16), (7, 29)] { + prog.push(Op::Keccak { + m: [2, 3, 4, 5], + tail: 8, + b8, + cap: 12, + out, + in_odd: false, + out_odd: false, + swap: true, + }); + } + prog.resize(31, Op::Set { o: 47, k: F192::ZERO }); + prog.push(Op::Xor { a: 0, b: 0, c: 0 }); // sentinel + Program::from_bytecode(prog, 48) + } + + /// The state a `KECCAK_M` over `first ‖ second` writes: Keccak-f of the 64-byte block, padded. + fn merkle_state(first: [F192; 2], second: [F192; 2]) -> [F192; crate::keccak::STATE_CELLS] { + let mut lanes = [0u64; primitives::hash::STATE_LANES]; + for (k, c) in first.iter().chain(&second).enumerate() { + lanes[2 * k] = c.c0; + lanes[2 * k + 1] = c.c1; + } + lanes[8] = u64::from(primitives::hash::PAD_FIRST); + lanes[16] = primitives::hash::END_BIT; + primitives::hash::keccak_f(&mut lanes); + crate::keccak::cells_of_state(&lanes, false) + } + + /// `KECCAK_M` hashes its pairs in the order its bit says, `m[0..2] ‖ m[2..4]` for 0 and the + /// other way round for 1, and the run proves and verifies. + #[test] + fn keccak_m_hashes_in_the_bit_order() { + let program = merkle_program([0, 1]); + let pi = [w(7), w(11)]; + let exec = program.execute(pi).unwrap(); + let (node, sibling) = ( + [exec.mem.word(2), exec.mem.word(3)], + [exec.mem.word(4), exec.mem.word(5)], + ); + for (out, want) in [(16, merkle_state(node, sibling)), (29, merkle_state(sibling, node))] { + for (c, cell) in want.iter().enumerate() { + assert_eq!(exec.mem.word(out + c), *cell, "state cell {c} at {out}"); + } + } + assert_eq!(exec.rows()[crate::tables::KECCAK_TABLE], 2, "two KECCAK_M rows"); + let (proof, _) = prove(&program, pi, crate::pcs::TEST_LOG_INV_RATE).unwrap(); + verify(&program, &pi, &proof).expect("a KECCAK_M run verifies"); + } + + /// The direction is a bit: anything else in `b8`'s low limb is no Merkle step. + #[test] + fn keccak_m_needs_a_bit() { + let err = merkle_program([0, 2]) + .execute([w(7), w(11)]) + .err() + .expect("the run must fail"); + assert!(matches!(err.fault, Fault::BadMerkleStep { .. }), "{err}"); + } + + /// The order is bound: a prover that writes, for the row whose bit is 0, the state of the + /// other order (sibling first) has the table read the pairs at `A0 = o_m0`, `A1 = o_m1` while + /// the output lanes hold the swapped block's permutation, so the Keccak reduction has no + /// proof of it; the prover panics or errs, and a proof it did make would not verify. + #[test] + fn keccak_m_order_is_bound() { + let program = merkle_program([0, 1]); + let pi = [w(7), w(11)]; + let forged = std::panic::catch_unwind(|| { + prove_with(&program, pi, crate::pcs::TEST_LOG_INV_RATE, |exec| { + let (node, sibling) = ( + [exec.mem.word(2), exec.mem.word(3)], + [exec.mem.word(4), exec.mem.word(5)], + ); + for (c, cell) in merkle_state(sibling, node).into_iter().enumerate() { + exec.mem.set_word(16 + c, cell); + } + }) + }); + if let Ok(Ok((proof, _))) = forged { + assert!( + verify(&program, &pi, &proof).is_err(), + "a Merkle step in the wrong order verifies" + ); + } + } + /// A 192-bit-word MUL: the E-product of two full machine words. Full-limb /// constants are why this one is hand-written bytecode: a source literal fills /// only the low two limbs. diff --git a/crates/leanvm_core/src/cpu/trace.rs b/crates/leanvm_core/src/cpu/trace.rs index aa48ba1be0..9a4e92f432 100644 --- a/crates/leanvm_core/src/cpu/trace.rs +++ b/crates/leanvm_core/src/cpu/trace.rs @@ -55,6 +55,17 @@ pub(crate) struct Krow { pub(crate) bytecode_read: u32, } +/// `DOT` row: the access counts of its cells, in the order the table flushes them +/// (`tables::DOT_CELLS`): the lane cells, the weight pointer, the weights, the accumulator, then +/// the result. `w` is the first weight's cell, the pointer resolved. +pub(crate) struct Dotrow { + pub(crate) pc: u32, + pub(crate) fp: u32, + pub(crate) w: u32, + pub(crate) r: [u32; crate::tables::DOT_CELLS], + pub(crate) bytecode_read: u32, +} + /// Rows in fixed-size chunks, so the trace grows without ever moving what it holds: /// a `Vec` would copy every row written so far each time it doubled. pub(crate) struct Rows { @@ -104,6 +115,7 @@ pub(crate) struct Trace { pub(crate) deref: Rows, pub(crate) jump: Rows, pub(crate) keccak: Rows, + pub(crate) dot: Rows, pub(crate) bytecode_count: Vec, // per-pc execution count; the column holds g^{A[pc]} /// One past the highest access count, of a cell or a pc: the `g`-powers a fill looks up. pub(crate) counts: usize, @@ -119,6 +131,7 @@ impl Trace { self.deref.len(), self.jump.len(), self.keccak.len(), + self.dot.len(), ] } } diff --git a/crates/leanvm_core/src/tables.rs b/crates/leanvm_core/src/tables.rs index e41c0f4527..c695f94580 100644 --- a/crates/leanvm_core/src/tables.rs +++ b/crates/leanvm_core/src/tables.rs @@ -14,7 +14,7 @@ //! so a relation on machine words is written out lane by lane. A table hands its //! identities to the table sumcheck as a degree-2 form ([`Table::constraint`]). -use crate::cpu::{Drow, Jrow, Krow, Memory, Op, Rows, Srow, Trace}; +use crate::cpu::{Dotrow, Drow, Jrow, Krow, Memory, Op, Rows, Srow, Trace}; use crate::leaf::BusForm; use crate::leaf::Coord::{self, Col, Const, GCol, Prod}; use primitives::field::{F64, F192, mul_by_g}; @@ -73,6 +73,9 @@ pub(crate) const OP_SET: F64 = g_pow(2); pub(crate) const OP_DEREF: F64 = g_pow(3); pub(crate) const OP_JUMP: F64 = g_pow(4); pub(crate) const OP_KECCAK: F64 = g_pow(5); +pub(crate) const OP_DOT: F64 = g_pow(6); +/// `KECCAK_M`, the Merkle step, proven by the `KECCAK` table (its flag `keccakt::MK`). +pub(crate) const OP_KECCAK_M: F64 = g_pow(7); // ---- flush builder ----------------------------------------------------------- @@ -118,8 +121,14 @@ impl FlushBuilder { /// Bytecode read at `pc`: the program tuple (opcode + ten operand slots), /// with the per-pc execution count advanced by ×g on the push side. pub(crate) fn bytecode(&mut self, pc: usize, count: usize, opcode: F64, operands: &[Coord]) { - let mut push = vec![Const(SEP_BYTECODE), Col(pc), GCol(count, 1), Const(opcode)]; - let mut pull = vec![Const(SEP_BYTECODE), Col(pc), Col(count), Const(opcode)]; + self.bytecode_coord(pc, count, Const(opcode), operands); + } + + /// [`Self::bytecode`] with the opcode a coordinate: a table proving two opcodes reads the + /// one its flag column selects. + pub(crate) fn bytecode_coord(&mut self, pc: usize, count: usize, opcode: Coord, operands: &[Coord]) { + let mut push = vec![Const(SEP_BYTECODE), Col(pc), GCol(count, 1), opcode.clone()]; + let mut pull = vec![Const(SEP_BYTECODE), Col(pc), Col(count), opcode]; push.extend_from_slice(operands); pull.extend_from_slice(operands); self.pair(push, pull); @@ -344,9 +353,9 @@ pub trait Table: Sync { fn fill(&self, ctx: &FillCtx, out: &mut [ColumnOut]); } -/// The tables in fixed order `[XOR, MUL, SET, DEREF, JUMP, KECCAK]`, the +/// The tables in fixed order `[XOR, MUL, SET, DEREF, JUMP, KECCAK, DOT]`, the /// order of `row_counts` / `taus` throughout `cpu`. -pub const N_TABLES: usize = 6; +pub const N_TABLES: usize = 7; pub fn tables() -> [&'static dyn Table; N_TABLES] { [ @@ -356,14 +365,23 @@ pub fn tables() -> [&'static dyn Table; N_TABLES] { &DerefTable, &JumpTable, &KeccakTable, + &DotTable, ] } /// Index of the KECCAK table in [`tables`]. pub(crate) const KECCAK_TABLE: usize = 5; -/// Each table's least log height: 0, but the Keccak reduction's block of steps for `KECCAK`. -pub const TABLE_LOG_FLOORS: [usize; N_TABLES] = [0, 0, 0, 0, 0, crate::keccak::MIN_STEPS.ilog2() as usize]; +/// `DOT`'s least log height. A recursive verifier pays, for each of the table's columns and bus +/// blocks, a selector over the child's position bits above the table's height, so a child with no +/// `DOT` rows (a leaf) would cost it the most; the floor caps that, at the price of committing that +/// many padding rows. +pub const DOT_LOG_FLOOR: usize = 13; + +/// Each table's least log height: 0, but the Keccak reduction's block of steps for `KECCAK`, and +/// [`DOT_LOG_FLOOR`] for `DOT`. +pub const TABLE_LOG_FLOORS: [usize; N_TABLES] = + [0, 0, 0, 0, 0, crate::keccak::MIN_STEPS.ilog2() as usize, DOT_LOG_FLOOR]; /// What each cell a `KECCAK` row touches is addressed by, in flush order (the four `m` cells, the /// four `tail` cells, `b8`, the four `cap` cells, then the thirteen output cells): the offset @@ -391,6 +409,7 @@ pub(crate) struct KeccakOp { offsets: [u32; 8], pub(crate) in_odd: bool, pub(crate) out_odd: bool, + pub(crate) swap: bool, } impl KeccakOp { @@ -404,17 +423,21 @@ impl KeccakOp { out, in_odd, out_odd, + swap, } => Self { offsets: [m[0], m[1], m[2], m[3], tail, b8, cap, out], in_odd, out_odd, + swap, }, op => unreachable!("not a KECCAK: {op:?}"), } } - /// Cell `c` (in flush order) of a row at frame `fp`. - pub(crate) fn cell(&self, fp: u32, c: usize) -> u32 { + /// Cell `c` (in flush order) of a row at frame `fp`, `bit` the row's direction bit (zero but + /// for a `KECCAK_M` row whose `b8` cell holds one): it swaps the two `m` pairs. + pub(crate) fn cell(&self, fp: u32, c: usize, bit: bool) -> u32 { + let c = if bit && c < 4 { c ^ 2 } else { c }; let (operand, step) = KECCAK_CELLS[c]; fp + self.offsets[operand] + step } @@ -856,6 +879,13 @@ impl Table for JumpTable { /// `cpu` treats them as VIRTUAL ([`crate::cpu::keccak_value`]): the input lanes are `q_keccak`'s /// slots, which hold those words, so their claims route there and the fill leaves them alone, /// and the output lanes' claims are the Keccak reduction's claim on the output. +/// +/// The table also proves `KECCAK_M`, the Merkle step (§sec:tab-keccak): the flag `MK` selects the +/// opcode the bytecode read carries. On such a row the low limb of the cell `b8`, the column `B8`, +/// is a direction bit `S` rather than lane 16, which the row pins to Keccak-256's last padding +/// byte, and the first two `m` cells are read at `fp·A0`, `fp·A1`, the offsets `o_m0`, `o_m1` +/// or, when `S = 1`, `o_m2`, `o_m3`; the other pair at the offsets left over, `A0 + o_m0 + o_m2` +/// and `A1 + o_m1 + o_m3`. Eight identities tie these columns (`constraint`). struct KeccakTable; pub(crate) mod keccakt { @@ -872,7 +902,12 @@ pub(crate) mod keccakt { pub const V_OUT: usize = V_IN + STATE_LANES; // … and the 25 output lanes pub const R0: usize = V_OUT + STATE_LANES; // one read count per cell, in flush order pub const RBC: usize = R0 + ROW_CELLS; - pub const N: usize = RBC + 1; + pub const MK: usize = RBC + 1; // 1 on a KECCAK_M row: the bytecode read is KECCAK_M's + pub const B8: usize = MK + 1; // the low limb of the cell b8, as read + pub const S: usize = B8 + 1; // MK·B8: a KECCAK_M row's direction bit, zero on a KECCAK row + pub const A0: usize = S + 1; // the g-powers of the offsets the first two m cells are read at: + pub const A1: usize = A0 + 1; // o_m0 and o_m1, or o_m2 and o_m3 when S = 1 + pub const N: usize = A1 + 1; } /// A cell limb holding lane `even` in the even layout and `odd` in the odd one, as `flag` @@ -908,6 +943,45 @@ impl Table for KeccakTable { }; &COUNTS } + fn n_constraints(&self) -> usize { + 8 + } + /// `KECCAK_M`'s identities, weighted `pows[0..8]`, in characteristic 2: + /// - `MK·(MK + 1) = 0`: the flag is a bit, so the opcode read is `KECCAK`'s or `KECCAK_M`'s; + /// - `MK·in_odd = 0`: a Merkle step reads the even layout; + /// - `B8 + l + MK·(B8 + v_16) = 0`, `l = v_16 + in_odd·(v_16 + v_15)` the lane the even or + /// odd layout puts in `b8`'s low limb: on a `KECCAK` row `B8` is that lane; + /// - `MK·(v_16 + END_BIT) = 0`: on a `KECCAK_M` row lane 16 is the padding's last byte; + /// - `S + MK·B8 = 0` and `S·(S + 1) = 0`: the direction bit, zero on a `KECCAK` row; + /// - `A0 + o_m0 + S·(o_m0 + o_m2) = 0` and `A1 + o_m1 + S·(o_m1 + o_m3) = 0`. + fn constraint(&self, pows: &[F192]) -> BusForm { + use keccakt::*; + let (v15, v16) = (V_IN + 15, V_IN + 16); + let mut form = BusForm::new(N); + form.coeffs[MK] = pows[0] + pows[3] * F192::from(F64(primitives::hash::END_BIT)); + form.coeffs[B8] = pows[2]; + form.coeffs[v16] = pows[2]; + form.coeffs[S] = pows[4] + pows[5]; + form.coeffs[A0] = pows[6]; + form.coeffs[O_M0] = pows[6]; + form.coeffs[A1] = pows[7]; + form.coeffs[O_M0 + 1] = pows[7]; + form.prods = vec![ + (MK, MK, pows[0]), + (MK, IN_ODD, pows[1]), + (IN_ODD, v16, pows[2]), + (IN_ODD, v15, pows[2]), + (MK, B8, pows[2]), + (MK, v16, pows[2] + pows[3]), + (MK, B8, pows[4]), + (S, S, pows[5]), + (S, O_M0, pows[6]), + (S, O_M0 + 2, pows[6]), + (S, O_M0 + 1, pows[7]), + (S, O_M0 + 3, pows[7]), + ]; + form + } /// Zeros, but for the output lanes: a padding row, like the Keccak reduction's padding /// steps, permutes zeros, in the even layout. fn padding(&self) -> Vec { @@ -924,7 +998,9 @@ impl Table for KeccakTable { use keccakt::*; f.state_step(PC, FP); let operands: Vec = (O_M0..=OUT_ODD).map(Col).collect(); - f.bytecode(PC, RBC, OP_KECCAK, &operands); + // KECCAK's opcode, or KECCAK_M's when MK = 1. + let opcode = Coord::Sum(vec![Const(OP_KECCAK), GCol(MK, 5), GCol(MK, 7)]); + f.bytecode_coord(PC, RBC, opcode, &operands); // Thirteen cells read, then thirteen written. A consecutive cell is a free ×g on the // product's g-power. let (even, odd) = (cell_lanes(false), cell_lanes(true)); @@ -934,9 +1010,23 @@ impl Table for KeccakTable { } else { (c - STATE_CELLS, V_OUT, OUT_ODD, None) }; - let lo = layout_limb(even[s].0, odd[s].0, values, flag, free); + let lo = if c == 8 { + Col(B8) + } else { + layout_limb(even[s].0, odd[s].0, values, flag, free) + }; let hi = layout_limb(even[s].1, odd[s].1, values, flag, free); - f.memory_coords(Prod(FP, O_M0 + operand, step), R0 + c, [lo, hi, Const(F64::ZERO)]); + // The m cells at the offsets the direction bit picks: A0, A1, and the pair left over. + let addr = match c { + 0 => Prod(FP, A0, 0), + 1 => Prod(FP, A1, 0), + 2 | 3 => { + let a = if c == 2 { A0 } else { A1 }; + Coord::Sum(vec![Prod(FP, a, 0), Prod(FP, O_M0 + c - 2, 0), Prod(FP, O_M0 + c, 0)]) + } + _ => Prod(FP, O_M0 + operand, step), + }; + f.memory_coords(addr, R0 + c, [lo, hi, Const(F64::ZERO)]); } } /// Every column but the input lanes, which are `q_keccak`'s and have no window here. @@ -951,19 +1041,156 @@ impl Table for KeccakTable { ctx.cols(out, rows, O_M0, |r| { let k = op(r); let [m0, m1, m2, m3, tail, b8, cap, o] = k.offsets.map(|o| ctx.g_at(o)); + // A KECCAK_M row reads the even layout, whose free limb is b8's high one. let (cell, high) = FREE_LIMB[usize::from(k.in_odd)]; - let free = ctx.mem.word(k.cell(r.fp, cell) as usize); + let free = ctx.mem.word(k.cell(r.fp, cell, false) as usize); let free = F64(if high { free.c1 } else { free.c0 }); [m0, m1, m2, m3, tail, b8, cap, o, flag(k.in_odd), flag(k.out_odd), free] }); ctx.cols(out, rows, V_OUT, |r| { let k = op(r); let cells: [F192; STATE_CELLS] = - std::array::from_fn(|c| ctx.mem.word(k.cell(r.fp, STATE_CELLS + c) as usize)); + std::array::from_fn(|c| ctx.mem.word(k.cell(r.fp, STATE_CELLS + c, false) as usize)); state_of_cells(&cells, k.out_odd).map(F64) }); ctx.cols(out, rows, R0, |r| r.r.map(|k| ctx.g_at(k))); ctx.col(out, rows, RBC, |r| ctx.g_at(r.bytecode_read)); + ctx.cols(out, rows, MK, |r| { + let k = op(r); + let b8 = F64(ctx.mem.word(k.cell(r.fp, 8, false) as usize).c0); + let s = k.swap && b8 == F64::ONE; + let [m0, m1, m2, m3] = [0, 1, 2, 3].map(|i| ctx.g_at(k.offsets[i])); + let (a0, a1) = if s { (m2, m3) } else { (m0, m1) }; + [flag(k.swap), b8, flag(s), a0, a1] + }); + } +} + +// ---- DOT --------------------------------------------------------------------- + +/// The lanes one `DOT` row weighs, two per canonical cell. Every column and bus block of the table +/// costs a recursive verifier a selector per child, so the width is where that fixed cost and the +/// rows a row saves balance. +pub const DOT_LANES: usize = 4; +/// The cells a `DOT` row touches, in flush order: the lane cells, the weight pointer, the +/// weights, the accumulator and the result. +pub const DOT_CELLS: usize = DOT_LANES / 2 + 1 + DOT_LANES + 2; + +/// `DOT` (§sec:tab-dot): `out = acc + Σ_j w_j·x_j` over [`DOT_LANES`] `K`-lanes `x_j` with `E` +/// weights. +/// +/// This is the affine part of a verifier, done in one row per [`DOT_LANES`] terms. Once a child proof's +/// transcript is fixed, the weights a verifier puts on opened data (the eq weights of a WHIR +/// query's row) are known words, and the check is linear in the data. A row commits the lanes as +/// `K` columns and reads each cell as `(lo, hi, 0)`, so the cells it reads are proved canonical +/// and their lanes in `K` by the bus alone, the work `pack64x2` does per lane pair in the +/// guest. The weights are read through a pointer, `p·o_w·g^j`, as `DEREF` reads its target. The +/// result rides the bus as a degree-2 coordinate, `acc_l + Σ_j w_{j,l}·x_j` per limb `l` (a `K` +/// lane times an `E` word needs no tower reduction), so the table has no identity of its own. +struct DotTable; + +pub(crate) mod dot { + use super::DOT_LANES; + pub const PC: usize = 0; + pub const FP: usize = 1; + pub const OX: usize = 2; + pub const OP: usize = 3; + pub const OW: usize = 4; + pub const OACC: usize = 5; + pub const OOUT: usize = 6; + pub const X: usize = 7; // the lanes, lane 2k + h being limb h of cell k + pub const P: usize = X + DOT_LANES; // the weight pointer, a single K lane + pub const W: usize = P + 1; // weight j's limb l at W + 3j + l + pub const ACC: usize = W + 3 * DOT_LANES; + pub const R0: usize = ACC + 3; // one read count per cell, in flush order + pub const RBC: usize = R0 + super::DOT_CELLS; + pub const N: usize = RBC + 1; +} + +/// The result's three limbs: `acc_l + Σ_j w_{j,l}·x_j`. +fn dot_result() -> [Coord; 3] { + use dot::*; + std::array::from_fn(|l| { + let mut terms = vec![Col(ACC + l)]; + terms.extend((0..DOT_LANES).map(|j| Prod(W + 3 * j + l, X + j, 0))); + Coord::Sum(terms) + }) +} + +/// A `DOT` instruction's five operands, in bytecode-slot order. +fn dot_operands(op: &Op) -> [u32; 5] { + match *op { + Op::Dot { x, p, w, acc, out } => [x, p, w, acc, out], + op => unreachable!("not a DOT: {op:?}"), + } +} + +impl Table for DotTable { + fn n_committed_columns(&self) -> usize { + dot::N + } + fn count_columns(&self) -> &'static [usize] { + const COUNTS: [usize; DOT_CELLS + 1] = { + let mut c = [0usize; DOT_CELLS + 1]; + let mut i = 0; + while i < DOT_CELLS { + c[i] = dot::R0 + i; + i += 1; + } + c[DOT_CELLS] = dot::RBC; + c + }; + &COUNTS + } + fn flushes(&self, f: &mut FlushBuilder) { + use dot::*; + f.state_step(PC, FP); + f.bytecode(PC, RBC, OP_DOT, &[Col(OX), Col(OP), Col(OW), Col(OACC), Col(OOUT)]); + for k in 0..DOT_LANES / 2 { + f.memory_coords( + Prod(FP, OX, k as u32), + R0 + k, + [Col(X + 2 * k), Col(X + 2 * k + 1), Const(F64::ZERO)], + ); + } + let r = R0 + DOT_LANES / 2; + f.memory_k(Prod(FP, OP, 0), r, P); + for j in 0..DOT_LANES { + let w = W + 3 * j; + f.memory(Prod(P, OW, j as u32), r + 1 + j, w, w + 1, w + 2); + } + f.memory(Prod(FP, OACC, 0), r + 1 + DOT_LANES, ACC, ACC + 1, ACC + 2); + f.memory_coords(Prod(FP, OOUT, 0), r + 2 + DOT_LANES, dot_result()); + } + fn fill(&self, ctx: &FillCtx, out: &mut [ColumnOut]) { + use dot::*; + let rows = &ctx.trace.dot; + ctx.col(out, rows, PC, |r| ctx.g_at(r.pc)); + ctx.col(out, rows, FP, |r| ctx.g_at(r.fp)); + // The operands, the lanes and the pointer from one decode. + ctx.cols(out, rows, OX, |r: &Dotrow| { + let [x, p, w, acc, o] = dot_operands(&ctx.prog[r.pc as usize]); + let mut v = [F64::ZERO; 5 + DOT_LANES + 1]; + v[..5].copy_from_slice(&[x, p, w, acc, o].map(|o| ctx.g_at(o))); + for k in 0..DOT_LANES / 2 { + let lanes = ctx.limbs(r.fp + x + k as u32); + v[5 + 2 * k] = lanes[0]; + v[5 + 2 * k + 1] = lanes[1]; + } + v[5 + DOT_LANES] = ctx.limbs(r.fp + p)[0]; + v + }); + ctx.cols(out, rows, W, |r: &Dotrow| { + let acc = dot_operands(&ctx.prog[r.pc as usize])[3]; + let mut v = [F64::ZERO; 3 * DOT_LANES + 3]; + for j in 0..DOT_LANES { + v[3 * j..3 * j + 3].copy_from_slice(&ctx.limbs(r.w + j as u32)); + } + v[3 * DOT_LANES..].copy_from_slice(&ctx.limbs(r.fp + acc)); + v + }); + ctx.cols(out, rows, R0, |r| r.r.map(|k| ctx.g_at(k))); + ctx.col(out, rows, RBC, |r| ctx.g_at(r.bytecode_read)); } } @@ -974,6 +1201,15 @@ mod tests { const C: F192 = F192::new(0x0123_4567_89ab_cdef, 0xfeed_face_dead_beef, 0x1111_2222_3333_4444); + /// The recursion guest evaluates `KECCAK_M`'s identities at these local columns + /// (`lean_ethereum.py`, the table sumcheck's `TABLE_KECCAK` arm), as it does `JUMP`'s. + #[test] + fn keccak_m_columns_where_the_guest_reads_them() { + use keccakt::*; + assert_eq!((O_M0, IN_ODD, V_IN + 15, V_IN + 16), (2, 10, 28, 29)); + assert_eq!((MK, B8, S, A0, A1, N), (90, 91, 92, 93, 94, 95)); + } + /// The hand-unrolled tower product IS `E`'s multiplication, lane by lane. Both /// `MUL`'s result coordinate and `JUMP`'s inverse identity are written out from /// [`TOWER_LANES`], and neither can be checked against the field at run time (one diff --git a/crates/leanvm_core/tests/verifiers/python_verifier.rs b/crates/leanvm_core/tests/verifiers/python_verifier.rs index 879f0e73b2..2b5fafe8eb 100644 --- a/crates/leanvm_core/tests/verifiers/python_verifier.rs +++ b/crates/leanvm_core/tests/verifiers/python_verifier.rs @@ -30,6 +30,28 @@ def main(): digest = StackBuf(2) keccak_cells([5, 7, 5, 7, 5, 7, 5, 7, 5, 7], digest) + # Eight lanes g^j, two to a cell, weighed by two DOTs and by MUL and XOR. + weights = HeapBuf(8) + for j in unroll(0, 8): + weights[GEN ** j] = digest[0] * GEN ** j + lanes = StackBuf(4) + for k in unroll(0, 4): + lanes[k] = GEN ** (2 * k) + f192(0, 1, 0) * GEN ** (2 * k + 1) + weighed = digest[1] + for j in unroll(0, 8): + weighed = weighed + weights[GEN ** j] * GEN ** j + assert dot(lanes[2:4], weights[4:8], dot(lanes[0:2], weights[0:4], digest[1])) == weighed + + # Two Merkle steps (KECCAK_M), one per direction bit, each written over the state a plain + # KECCAK of the order the bit names leaves, which asserts they agree. + for bit in unroll(0, 2): + node = StackBuf(2) + keccak(digest[0:2], lanes[0:2], node) + if bit == 0: + keccak(digest[0:2], lanes[0:2], node, swap=0) + else: + keccak(lanes[0:2], digest[0:2], node, swap=1) + chain = HeapBuf(LOOP_STEPS + 1) chain[1] = digest[0] for index in mul_range(1, GEN ** LOOP_STEPS): @@ -213,6 +235,10 @@ fn tampers_fail_the_same_check() { ), (keccak.end - input_opening - 1, VerifierError::KeccakOutputOpening), (keccak.end - 1, VerifierError::KeccakInputOpening), + // The last table's last column evaluation, DOT's bytecode count, sent just before the reduction. + (keccak.start - 1, VerifierError::TableSumcheckTerminal), + // KECCAK's last column evaluation, KECCAK_M's offset A1, sent before DOT's 37: its identity no longer holds. + (keccak.start - 1 - 37, VerifierError::TableSumcheckTerminal), ]; let bytecode = Bytecode::new(&fixture.bytecode_table()); diff --git a/crates/rec_aggregation/guests/lean_ethereum.py b/crates/rec_aggregation/guests/lean_ethereum.py index 2d1e5d45ef..24628213ba 100644 --- a/crates/rec_aggregation/guests/lean_ethereum.py +++ b/crates/rec_aggregation/guests/lean_ethereum.py @@ -31,6 +31,7 @@ Y_TOWER = Y_TOWER_PLACEHOLDER Y_INV = Y_INV_PLACEHOLDER COORD_BASIS = COORD_BASIS_PLACEHOLDER +BIT_WEIGHTS = 32 # the longest run of bits a query position sums: above every tree depth # Six challenges compose the F2-linear map that batches the 192 transposed # ring-switch coordinates. RING_MAP_SHIFTS = [32, 16, 8, 4, 2, 1] @@ -651,7 +652,13 @@ def exponent_tables(): for j in unroll(0, SIZE_BITS): g_squares[GEN ** j] = sq_run sq_run *= sq_run - return g_logs_pow2, g_squares + # The weights DOT reads to sum a run of bit cells: a bit cell is the lane pair + # (b, 0), so bit b of a run weighs x^b and the high lanes nothing. + bit_weights = HeapBuf(2 * BIT_WEIGHTS) + for b in unroll(0, BIT_WEIGHTS): + bit_weights[GEN ** (2 * b)] = COORD_BASIS[b] + bit_weights[GEN ** (2 * b + 1)] = 0 + return g_logs_pow2, g_squares, bit_weights def g_power_of_word(value): @@ -718,7 +725,18 @@ def log2_ceil_in_the_exponent(g_N, g_logs_pow2, g_squares, floor: Const, nbits: return g_log -def decode_query_bits(squeezed_word, positions_out, bit_ptrs_out, depth: Const): +@inline +def bits_value(bits, at: Const, n: Const, bit_weights): + # sum_{b> κ, hinted + # a nibble at a time as g-power indices g^u (u the nibble, low bit first), each + # range-checked below the nibble's table size, so that it reads its eq factor and + # its share of g^offset; the caller pins the product of the shares to g^offset, + # which makes the digits offset >> κ's and the offset κ-aligned. + digits = StackBuf(SEL_TAB_NIBBLES) + hint_decompose_nibbles_exponent(digits, off_g, kappa_g, SEL_TAB_NIBBLES, 0) eq = GEN ** 0 rebuilt = GEN ** 0 for j in unroll(0, (n + 3) // 4): width = 4 if const(j == n // 4): width = n - 4 * j - idx = GEN ** 0 - for t in unroll(0, width): - sbit = sel_bits[GEN ** (4 * j + t)] - sel_bits[GEN ** (4 * j + t)] = sbit * sbit # booleanity as a write-once pin - idx *= 1 + sbit * (1 + GEN ** (2 ** t)) + idx = digits[j] + assert_digit(idx, width) eq *= eq_tab[GEN ** (SEL_TAB_EQ_STRIDE * j + 2 ** width - 2) * idx] rebuilt *= g_tab[GEN ** (16 * j) * idx] return eq, rebuilt @@ -2041,20 +2078,21 @@ def verify_tables(fs0, fs1, fs2, fs3, fs4, cursor, pi_0, pi_1, zeta, g_bus_mu, d # g^offset from those high bits alone (weights g^(2^(κ+k))) and # asserting it equals block_off_g pins the bits AND the κ-alignment # in one shot; the low κ bit cells are written but never read. - offset_bits = HeapBuf(GEN ** SIZE_BITS) - hint_decompose_bits_exponent(offset_bits, block_off_g[GEN ** b], SIZE_BITS) shared = 0 if const(BLOCK_KAPPA_SRC[b] // 2 != 0): if const(BLOCK_KAPPA_ADJ[b] == 0): shared = 1 if const(shared == 1): t = BLOCK_KAPPA_SRC[b] - 2 - sel_bits = offset_bits * dims_g[GEN ** (t + 1)] + off_g = block_off_g[GEN ** b] + tau_g = dims_g[GEN ** (t + 1)] len_g = shared_len[t] tab_eq = shared_eq * GEN ** (t * SEL_TAB_NIBBLES * SEL_TAB_EQ_STRIDE) tab_g = shared_g * GEN ** (t * SEL_TAB_NIBBLES * 16) - eq_hi, goff = match(log(len_g), range(0, SIZE_BITS), lambda n: block_selector_tab(sel_bits, tab_eq, tab_g, n)) + eq_hi, goff = match(log(len_g), range(0, SIZE_BITS), lambda n: block_selector_tab(off_g, tau_g, tab_eq, tab_g, n)) else: + offset_bits = HeapBuf(GEN ** SIZE_BITS) + hint_decompose_bits_exponent(offset_bits, block_off_g[GEN ** b], SIZE_BITS) sel_len_g = g_bus_mu / kappa_g # g^(mu - κ) assert log(sel_len_g) < SIZE_BITS sel_bits = offset_bits * kappa_g @@ -2235,7 +2273,7 @@ def verify_tables(fs0, fs1, fs2, fs3, fs4, cursor, pi_0, pi_1, zeta, g_bus_mu, d # The table's AIR constraint at the final point (col_evals is indexed by # local column index; the formulas mirror tables.rs Table::constraint). Every # value relation now rides the bus as a degree-2 coordinate, so only JUMP's - # is-nonzero indicator is left with an identity of its own. + # is-nonzero indicator and KECCAK_M's selection are left with identities. constraint_eval = 0 if t == TABLE_JUMP: # `b = c*w` and `c*(b+1) = 0`. The condition is K-valued, its memory read @@ -2246,6 +2284,24 @@ def verify_tables(fs0, fs1, fs2, fs3, fs4, cursor, pi_0, pi_1, zeta, g_bus_mu, d b = col_evals[13] constraint_eval = zc_xi_pows[ETA_OFFSET[t] + 0] * (b + c * col_evals[12]) constraint_eval += zc_xi_pows[ETA_OFFSET[t] + 1] * (c * (b + 1)) + if t == TABLE_KECCAK: + # KECCAK_M's eight identities (tables.rs KeccakTable::constraint). Local columns: + # o_m0..o_m3 at 2..5, odd_in at 10, input lanes 15 and 16 at 28 and 29, and + # after the read counts mk, b8, s, a0, a1 at 90..94. GEN ** 63 is lane 16's + # padding byte 0x80. + mk = col_evals[90] + b8 = col_evals[91] + sb = col_evals[92] + odd_in = col_evals[10] + v16 = col_evals[29] + constraint_eval = zc_xi_pows[ETA_OFFSET[t] + 0] * (mk * (mk + 1)) + constraint_eval += zc_xi_pows[ETA_OFFSET[t] + 1] * (mk * odd_in) + constraint_eval += zc_xi_pows[ETA_OFFSET[t] + 2] * (b8 + v16 + odd_in * (v16 + col_evals[28]) + mk * (b8 + v16)) + constraint_eval += zc_xi_pows[ETA_OFFSET[t] + 3] * (mk * (v16 + GEN ** 63)) + constraint_eval += zc_xi_pows[ETA_OFFSET[t] + 4] * (sb + mk * b8) + constraint_eval += zc_xi_pows[ETA_OFFSET[t] + 5] * (sb * (sb + 1)) + constraint_eval += zc_xi_pows[ETA_OFFSET[t] + 6] * (col_evals[93] + col_evals[2] + sb * (col_evals[2] + col_evals[4])) + constraint_eval += zc_xi_pows[ETA_OFFSET[t] + 7] * (col_evals[94] + col_evals[3] + sb * (col_evals[3] + col_evals[5])) # The table's three bus forms, evaluated at the SAME column evaluations: # Σ_b eq_hi(b) · (γ + Σ_i α^i · coord_i), the coords read off col_evals at # their local index. This is what replaces opening those columns at ζ. @@ -3013,22 +3069,21 @@ def column_selector(offset, sel_eq, sel_g, kappa: Const): # Rebuild the offset using only bits above the column's kappa, certifying its # alignment. The same bits select the column at the complete opening point. # Both offset and point are zero above m, so extending to 4 * SEL_NIBBLES adds - # only factors eq(0, 0) = 1. The bits go a nibble at a time, top bit first: - # their g-power index u (bit 4j+3-t weighing 2^t) reads the nibble's eq over - # its coordinates and its g^offset share off the tables of selector_tables. - bits = StackBuf(4 * SEL_NIBBLES) - hint_decompose_bits_exponent(bits, offset, 4 * SEL_NIBBLES) + # only factors eq(0, 0) = 1. The bits go a nibble at a time, hinted as the + # nibble's g-power index g^u (bit 4j+3-t weighing 2^t), range-checked below the + # nibble's table size: it reads the nibble's eq over its coordinates and its + # g^offset share off the tables of selector_tables, and the shares' product + # pinned to the offset makes every digit the offset's. + digits = StackBuf(SEL_NIBBLES) + hint_decompose_nibbles_exponent(digits, offset, GEN ** 0, SEL_NIBBLES, 1) rebuilt = GEN ** 0 selector = GEN ** 0 for j in unroll(kappa // 4, SEL_NIBBLES): width = 4 if const(j == kappa // 4): width = 4 * j + 4 - kappa - idx = GEN ** 0 - for t in unroll(0, width): - bit = bits[4 * j + 3 - t] - bits[4 * j + 3 - t] = bit * bit - idx *= 1 + bit * (1 + GEN ** (2 ** t)) + idx = digits[j] + assert_digit(idx, width) selector *= sel_eq[GEN ** (SEL_EQ_STRIDE * j + 2 ** width - 2) * idx] rebuilt *= sel_g[GEN ** (16 * j) * idx] assert rebuilt == offset @@ -3131,7 +3186,7 @@ def check_opening_terminal(zeta, chi, rm, g_bus_mu, g_zc_n, g_log_mem, tau_kecca assert inner_sum * yr_at_tail == sumcheck_target -def verify_sub(pi_0, pi_1, seed_0, seed_1, g_logs_pow2, g_squares, defer_out): +def verify_sub(pi_0, pi_1, seed_0, seed_1, g_logs_pow2, g_squares, bit_weights, defer_out): # In-circuit verification of ONE inner proof for the statement (pi_0, pi_1), # mirroring `crates/verifier` step for step; the `# ---- ... ----` headers below run # in that order. All proof data is hinted HERE, so each call pops the next @@ -3306,7 +3361,7 @@ def verify_sub(pi_0, pi_1, seed_0, seed_1, g_logs_pow2, g_squares, defer_out): # dispatch independently for every inner proof in a mixed-rate batch. config_sel = size_sel * rate_sel ** LIG_N_LOG_SIZES assert log(config_sel) < LIG_N_CANDIDATES - sumcheck_target, point, point1, inner_total, yr_at_tail = match(log(config_sel), range(0, LIG_N_CANDIDATES), lambda m_idx: open_stacked(m_idx, sp[8], sp[9], sp[10], sp[11], sp[12], target, commit_root_0, commit_root_1, cursor)) + sumcheck_target, point, point1, inner_total, yr_at_tail = match(log(config_sel), range(0, LIG_N_CANDIDATES), lambda m_idx: open_stacked(m_idx, sp[8], sp[9], sp[10], sp[11], sp[12], target, commit_root_0, commit_root_1, cursor, bit_weights)) # `stream` is a fixed-capacity witness transport. The shape fixes the exact # consumed prefix, whose every word is transcript-bound; the unused suffix # is outside the recursively verified proof and intentionally unconstrained. @@ -4131,7 +4186,7 @@ def main(): seed_1 = fs_seed[1] # ---- the claim lists ---- - g_logs_pow2, g_squares = exponent_tables() + g_logs_pow2, g_squares, bit_weights = exponent_tables() # One table per scheme: its declared list (strictly sorted, checked by the outer # verifier, which holds it) followed by its own duplicate slots. The coverage @@ -4219,7 +4274,7 @@ def main(): pi = xc * xc child_pi[pi] = pi_0 child_pi[pi * GEN] = pi_1 - verify_sub(pi_0, pi_1, seed_0, seed_1, g_logs_pow2, g_squares, child_fresh * xc ** DEFER_SIZE) + verify_sub(pi_0, pi_1, seed_0, seed_1, g_logs_pow2, g_squares, bit_weights, child_fresh * xc ** DEFER_SIZE) written[xc * GEN] = base * nsub_g assert written[n_children_g] == n_total_g diff --git a/crates/rec_aggregation/src/aggregation.rs b/crates/rec_aggregation/src/aggregation.rs index 600848d553..225708cc8a 100644 --- a/crates/rec_aggregation/src/aggregation.rs +++ b/crates/rec_aggregation/src/aggregation.rs @@ -1207,11 +1207,12 @@ fn gen_verify( let mut nodes = vec![[0u8; 32]; 2 * n]; let mut active = vec![F192::ZERO; n]; for opening in openings.by_ref().take(queries) { + // The leaf's canonical cells, two lanes each, as the guest hashes and weighs them. query_hints.push(( "merkle_leaf_rows", - opening.leaf.iter().map(|x| F192::new(x.0, 0, 0)).collect(), + opening.leaf.chunks(2).map(|x| F192::new(x[0].0, x[1].0, 0)).collect(), )); - let mut path_children = Vec::with_capacity(4 * path_depth); + let mut path_siblings = Vec::with_capacity(2 * path_depth); let bytes: Vec<_> = opening.leaf.iter().flat_map(|x| x.to_bytes()).collect(); let mut node = pcs::merkle::hash_leaf(&bytes); let mut index = (1 << depth) + opening.leaf_index as usize; @@ -1227,14 +1228,13 @@ fn gen_verify( (sibling, &node) }; if height < path_depth { - path_children.extend(pack_hash_state(left)); - path_children.extend(pack_hash_state(right)); + path_siblings.extend(pack_hash_state(sibling)); } node = pcs::merkle::hash_pair(left, right); index >>= 1; } nodes[1] = node; - query_hints.push(("merkle_children", path_children)); + query_hints.push(("merkle_siblings", path_siblings)); } caps.extend(nodes.iter().flat_map(pack_hash_state)); cap_active.extend(active); @@ -2352,6 +2352,14 @@ fn placeholder_map(kbc: usize) -> BTreeMap { }) .max() .unwrap(); + // A query's row is weighed by `DOT`, `DOT_LANES` lanes an instruction. + assert!( + cands + .iter() + .flat_map(|c| &c.interleaving) + .all(|&n| n % leanvm_core::tables::DOT_LANES == 0), + "a leaf row must be whole DOT instructions" + ); ps("LIG_ROW_CAP", row_cap.to_string()); ps("LIG_PACKED_ROW_CAP", (row_cap / 2).to_string()); ps( @@ -2386,6 +2394,11 @@ fn placeholder_map(kbc: usize) -> BTreeMap { .collect() }), ); + // The guest sums a query position's bits against a table of `BIT_WEIGHTS` powers of `x`. + assert!( + cands.iter().flat_map(|c| &c.tree_depths).all(|&d| d <= 32), + "a query position has more bits than the guest's BIT_WEIGHTS" + ); ps("LIG_TREE_DEPTH", flat(&|c| c.tree_depths.clone())); ps("LIG_CAP_DEPTH", flat(&|c| c.cap_depths.clone())); ps("LIG_CAP_OFF", flat(&|c| c.cap_offsets.clone())); @@ -2997,35 +3010,52 @@ def main(): for i in (1..8).rev() { tree[i] = pcs::merkle::hash_pair(&tree[2 * i], &tree[2 * i + 1]); } - let run = |index: usize, leaf: [u8; 32], pairs: &[[[u8; 32]; 2]], root: [u8; 32]| { + let run = |bits: [u64; 3], leaf: [u8; 32], siblings: &[[u8; 32]], root: [u8; 32]| { let mut hints = Hints::default(); - hints.push( - "bits", - (0..3).map(|k| F192::from(F64(((index >> k) & 1) as u64))).collect(), - ); + hints.push("bits", bits.map(|b| F192::from(F64(b))).to_vec()); hints.push("leaf", pack_hash_state(&leaf).to_vec()); - hints.push( - "merkle_children", - pairs.iter().flatten().flat_map(pack_hash_state).collect(), - ); + hints.push("merkle_siblings", siblings.iter().flat_map(pack_hash_state).collect()); let mut program = guest.clone(); hints.install(&mut program); program.execute(pack_hash_state(&root)) }; + let bits_of = |index: usize| std::array::from_fn(|k| ((index >> k) & 1) as u64); + let siblings_of = + |index: usize| -> Vec<[u8; 32]> { (0..3).map(|level| tree[((8 + index) >> level) ^ 1]).collect() }; for index in 0..8 { - let pairs: Vec<_> = (0..3) - .map(|level| { - let left = ((8 + index) >> level) & !1; - [tree[left], tree[left + 1]] - }) - .collect(); assert!( - run(index, tree[8 + index], &pairs, tree[1]) + run(bits_of(index), tree[8 + index], &siblings_of(index), tree[1]) .unwrap() .unconstrained_reads .is_empty() ); } + // A Merkle step hashes in the order its bit says, so a wrong sibling, a flipped bit or + // a leaf from elsewhere misses the root; a bit that is not 0 or 1 is no KECCAK_M at all + // (its row would break the table's `S·(S + 1) = 0`), and the guest pins the bits anyway. + let index = 5; + let mut wrong = siblings_of(index); + wrong[1] = tree[2]; + assert!( + run(bits_of(index), tree[8 + index], &wrong, tree[1]).is_err(), + "a wrong sibling" + ); + let mut flipped = bits_of(index); + flipped[1] ^= 1; + assert!( + run(flipped, tree[8 + index], &siblings_of(index), tree[1]).is_err(), + "a flipped bit" + ); + assert!( + run(bits_of(index), tree[9], &siblings_of(index), tree[1]).is_err(), + "another leaf" + ); + let mut two = bits_of(index); + two[0] = 2; + assert!( + run(two, tree[8 + index], &siblings_of(index), tree[1]).is_err(), + "a bit of 2" + ); } /// The right leaf holds more claims than one loop frame of its list hashes @@ -3236,7 +3266,7 @@ def main(): let source = include_str!("../guests/lean_ethereum.py"); let (helpers, _) = source.split_once("\ndef main():").unwrap(); let source = format!( - "{helpers}\ndef main():\n _, squares = exponent_tables()\n a, b, v0, v1 = da_verify(squares)\n digest = StackBuf(2)\n keccak([a, b], [v0, v1], digest)\n public = GEN ** 0\n assert public[1] == digest[0]\n assert public[GEN] == digest[1]\n return\n" + "{helpers}\ndef main():\n _, squares, _ = exponent_tables()\n a, b, v0, v1 = da_verify(squares)\n digest = StackBuf(2)\n keccak([a, b], [v0, v1], digest)\n public = GEN ** 0\n assert public[1] == digest[0]\n assert public[GEN] == digest[1]\n return\n" ); let guest = compile(&parse_with_replacements(&source, &placeholder_map(20)).unwrap()); let n_rows = 3usize; diff --git a/crates/verifier/src/layout.rs b/crates/verifier/src/layout.rs index de21ada04f..c4932a453c 100644 --- a/crates/verifier/src/layout.rs +++ b/crates/verifier/src/layout.rs @@ -161,7 +161,12 @@ impl Flushes { } fn bytecode(&mut self, pc: usize, count: usize, opcode: usize, operands: Vec
) { - let suffix = [vec![constant(gpow(opcode as u64))], operands].concat(); + self.bytecode_form(pc, count, constant(gpow(opcode as u64)), operands); + } + + /// [`Self::bytecode`] with the opcode a form: a table proving two opcodes reads the one its flag selects. + fn bytecode_form(&mut self, pc: usize, count: usize, opcode: Form, operands: Vec) { + let suffix = [vec![opcode], operands].concat(); self.counted([constant(SEP_BYTECODE), col(pc, 0)], count, suffix); } @@ -284,6 +289,26 @@ fn flushes_jump() -> Flushes { flushes } +/// `KECCAK_M`'s identities (`tables.rs` `KeccakTable::constraint`): the flag `mk` a bit and the even layout under +/// it; `b8` the lane its layout puts in b8's low limb on a KECCAK row; lane 16 the padding's last byte on a KECCAK_M +/// row; the direction bit `s = mk·b8` a bit; and the offsets `a0`, `a1` the first two m cells are read at. +fn keccak_constraints(columns: &[E]) -> Vec { + let [o_m0, o_m1, o_m2, o_m3, in_odd] = + cols(KECCAK_HEAD, ["o_m0", "o_m1", "o_m2", "o_m3", "odd_in"]).map(|i| columns[i]); + let [mk, b8, s, a0, a1] = [KECCAK_MK, KECCAK_B8, KECCAK_S, KECCAK_A0, KECCAK_A1].map(|i| columns[i]); + let (v15, v16) = (columns[KECCAK_IN + 15], columns[KECCAK_IN + 16]); + vec![ + mk * (mk + ONE), + mk * in_odd, + b8 + v16 + in_odd * (v16 + v15) + mk * (b8 + v16), + mk * (v16 + E::new(1 << 63, 0, 0)), // Keccak-256's last padding byte, 0x80, atop lane 16 + s + mk * b8, + s * (s + ONE), + a0 + o_m0 + s * (o_m0 + o_m2), + a1 + o_m1 + s * (o_m1 + o_m3), + ] +} + fn jump_constraints(columns: &[E]) -> Vec { let [condition, inverse, flag] = cols(JUMP_COLUMNS, ["v_cond", "w", "b"]).map(|index| columns[index]); vec![flag + condition * inverse, condition * (flag + ONE)] @@ -324,16 +349,31 @@ fn flushes_keccak() -> Flushes { let o_m = cols(KECCAK_HEAD, ["o_m0", "o_m1", "o_m2", "o_m3"]); let [o_tail, o_b8, o_cap, o_out] = cols(KECCAK_HEAD, ["o_tail", "o_b8", "o_cap", "o_out"]); let [in_odd, out_odd] = cols(KECCAK_HEAD, ["odd_in", "odd_out"]); + let [mk, b8, a0, a1] = [KECCAK_MK, KECCAK_B8, KECCAK_A0, KECCAK_A1]; let mut flushes = Flushes::default(); flushes.state_step(pc, fp); let operands = [ o_m[0], o_m[1], o_m[2], o_m[3], o_tail, o_b8, o_cap, o_out, in_odd, out_odd, ]; - flushes.bytecode(pc, KECCAK_CNT_BC, OP_KECCAK, operands.map(|i| col(i, 0)).to_vec()); + // KECCAK's opcode, or KECCAK_M's (the Merkle step) when the flag `mk` is 1. + let opcode = Form::sum([ + constant(gpow(OP_KECCAK as u64)), + col(mk, OP_KECCAK as u64), + col(mk, OP_KECCAK_M as u64), + ]); + flushes.bytecode_form(pc, KECCAK_CNT_BC, opcode, operands.map(|i| col(i, 0)).to_vec()); // The thirteen cells read (four addressed ones, the tail run, b8, the cap run), then the thirteen written, each // holding its lanes in the layout its flag names: a read cell's free lane is the column `free`, a written one's zero. + // The first two m cells are read at the offsets `a0`, `a1` the direction bit picks, the other two at the offsets + // left over; b8's low limb is the column `b8`. let address = |cell: usize| match cell { - 0..4 => prod(fp, o_m[cell], 0), + 0 => prod(fp, a0, 0), + 1 => prod(fp, a1, 0), + 2 | 3 => Form::sum([ + prod(fp, [a0, a1][cell - 2], 0), + prod(fp, o_m[cell - 2], 0), + prod(fp, o_m[cell], 0), + ]), 4..8 => prod(fp, o_tail, cell as u64 - 4), 8 => prod(fp, o_b8, 0), _ => prod(fp, o_cap, cell as u64 - 9), @@ -343,11 +383,12 @@ fn flushes_keccak() -> Flushes { let lane = |lane: Option| lane.map(|lane| lanes + lane); for cell in 0..KECCAK_CELLS { let limb = |e: Option, o: Option| layout_limb(lane(e), lane(o), flag, missing); - let values = [ - limb(even[cell].0, odd[cell].0), - limb(even[cell].1, odd[cell].1), - constant(ZERO), - ]; + let low = if side == 0 && cell == 8 { + col(b8, 0) + } else { + limb(even[cell].0, odd[cell].0) + }; + let values = [low, limb(even[cell].1, odd[cell].1), constant(ZERO)]; let at = if side == 0 { address(cell) } else { @@ -359,13 +400,77 @@ fn flushes_keccak() -> Flushes { flushes } +/// `DOT`: `out = acc + Σ_j w_j·x_j`, `DOT_LANES` `K`-lanes `x_j` (the low limbs of canonical cells, two each) against +/// as many weights read through a pointer, the result a degree-2 form (a `K` lane times an `E` word needs no reduction). +fn flushes_dot() -> Flushes { + let columns = dot_columns(); + let at = |name: String| columns.iter().position(|c| *c == name).expect("unknown column"); + let [pc, fp, o_x, o_p, o_w, o_acc, o_out, ptr] = + ["pc", "fp", "o_x", "o_p", "o_w", "o_acc", "o_out", "ptr"].map(|name| at(name.to_string())); + let x: [usize; DOT_LANES] = std::array::from_fn(|j| at(format!("x_{j}"))); + let w: [[usize; 3]; DOT_LANES] = std::array::from_fn(|j| std::array::from_fn(|l| at(format!("w{j}_{l}")))); + let acc: [usize; 3] = std::array::from_fn(|l| at(format!("acc_{l}"))); + let cnt = |name: &str| at(format!("cnt_{name}")); + let mut flushes = Flushes::default(); + flushes.state_step(pc, fp); + let operands = vec![col(o_x, 0), col(o_p, 0), col(o_w, 0), col(o_acc, 0), col(o_out, 0)]; + flushes.bytecode(pc, cnt("bc"), OP_DOT, operands); + for k in 0..DOT_LANES / 2 { + flushes.memory_cols( + prod(fp, o_x, k as u64), + cnt(&format!("x{k}")), + &[x[2 * k], x[2 * k + 1]], + ); + } + flushes.memory_cols(prod(fp, o_p, 0), cnt("ptr"), &[ptr]); + for (j, weight) in w.iter().enumerate() { + flushes.memory_cols(prod(ptr, o_w, j as u64), cnt(&format!("w{j}")), weight); + } + flushes.memory_cols(prod(fp, o_acc, 0), cnt("acc"), &acc); + let result = std::array::from_fn(|l| col(acc[l], 0) + Form::sum((0..DOT_LANES).map(|j| prod(w[j][l], x[j], 0)))); + flushes.memory(prod(fp, o_out, 0), cnt("out"), result); + flushes +} + +const DOT_LANES: usize = 4; +/// DOT's least log height, which caps what a recursive verifier pays for a child with few DOT rows. +const DOT_LOG_FLOOR: usize = 13; + +/// DOT's columns: the operands, the lanes `x_j`, the weight pointer, weight `j`'s limbs `wj_l`, the accumulator's, then +/// a read count per cell in flush order (the lane cells, the pointer, the weights, the accumulator, the result) and +/// `cnt_bc`. +fn dot_columns() -> Vec { + let head = ["pc", "fp", "o_x", "o_p", "o_w", "o_acc", "o_out"].map(String::from); + let lanes = (0..DOT_LANES).map(|j| format!("x_{j}")); + let weights = (0..DOT_LANES).flat_map(|j| (0..3).map(move |l| format!("w{j}_{l}"))); + let accumulator = (0..3).map(|l| format!("acc_{l}")); + let counts = (0..DOT_LANES / 2) + .map(|k| format!("x{k}")) + .chain(["ptr".to_string()]) + .chain((0..DOT_LANES).map(|j| format!("w{j}"))) + .chain(["acc", "out", "bc"].map(String::from)) + .map(|cell| format!("cnt_{cell}")); + head.into_iter() + .chain(lanes) + .chain(["ptr".to_string()]) + .chain(weights) + .chain(accumulator) + .chain(counts) + .collect() +} + +const DOT_WIDTH: usize = 7 + DOT_LANES + 1 + 3 * DOT_LANES + 3 + DOT_LANES / 2 + 1 + DOT_LANES + 3; + pub const OP_XOR: usize = 0; pub const OP_MUL: usize = 1; pub const OP_SET: usize = 2; pub const OP_DEREF: usize = 3; pub const OP_JUMP: usize = 4; pub const OP_KECCAK: usize = 5; -pub const N_TABLES: usize = 6; +pub const OP_DOT: usize = 6; +pub const N_TABLES: usize = 7; +/// `KECCAK_M`, the Merkle step: its own opcode, proven by the KECCAK table. +pub const OP_KECCAK_M: usize = 7; #[rustfmt::skip] const ARITH_COLUMNS: &[&str] = &["pc", "fp", "o_a", "o_b", "o_c", "va_0", "va_1", "va_2", "vb_0", "vb_1", "vb_2", "cnt_a", "cnt_b", "cnt_c", "cnt_bc"]; @@ -384,6 +489,14 @@ pub const KECCAK_IN: usize = KECCAK_HEAD.len(); pub const KECCAK_OUT: usize = KECCAK_IN + STATE_LANES; const KECCAK_CNT: usize = KECCAK_OUT + STATE_LANES; const KECCAK_CNT_BC: usize = KECCAK_CNT + 2 * KECCAK_CELLS; +// After the counts, KECCAK_M's: its flag, b8's low limb as read, the direction bit, and the offsets of the first two +// m cells. +const KECCAK_MK: usize = KECCAK_CNT_BC + 1; +const KECCAK_B8: usize = KECCAK_MK + 1; +const KECCAK_S: usize = KECCAK_B8 + 1; +const KECCAK_A0: usize = KECCAK_S + 1; +const KECCAK_A1: usize = KECCAK_A0 + 1; +const KECCAK_WIDTH: usize = KECCAK_A1 + 1; /// The local KECCAK column riding spare slot `k` (`cpu::keccak_value` in the prover): its /// committed columns in local order, the ones before the value lanes then the read counts @@ -427,7 +540,7 @@ fn keccak_columns() -> Vec { head.chain(lanes("in")) .chain(lanes("out")) .chain(counts) - .chain(["cnt_bc".to_string()]) + .chain(["cnt_bc", "mk", "b8", "s", "a0", "a1"].map(String::from)) .collect() } @@ -440,7 +553,7 @@ pub fn tables() -> Vec { constraints, }; let named = |columns: &[&str]| columns.iter().map(|name| name.to_string()).collect(); - let mut keccak = table(OP_KECCAK, keccak_columns(), flushes_keccak(), |_| vec![]); + let mut keccak = table(OP_KECCAK, keccak_columns(), flushes_keccak(), keccak_constraints); // A padding row is zero, but for KECCAK's output lanes: a padding row permutes zeros. let mut output = [0; STATE_LANES]; keccak_f(&mut output); @@ -454,6 +567,7 @@ pub fn tables() -> Vec
{ table(OP_DEREF, named(DEREF_COLUMNS), flushes_deref(), |_| vec![]), table(OP_JUMP, named(JUMP_COLUMNS), flushes_jump(), jump_constraints), keccak, + table(OP_DOT, dot_columns(), flushes_dot(), |_| vec![]), ] } @@ -463,7 +577,8 @@ const TABLE_WIDTHS: [usize; N_TABLES] = [ SET_COLUMNS.len(), DEREF_COLUMNS.len(), JUMP_COLUMNS.len(), - KECCAK_CNT_BC + 1, + KECCAK_WIDTH, + DOT_WIDTH, ]; pub const GLOBAL_COLUMN_BASES: [usize; N_TABLES] = { let mut bases = [NUM_GLOBAL_COLUMNS; N_TABLES]; @@ -476,7 +591,7 @@ pub const GLOBAL_COLUMN_BASES: [usize; N_TABLES] = { }; /// Each table's least log height: 0, but the Keccak reduction's block of steps for KECCAK. -const TABLE_LOG_FLOORS: [usize; N_TABLES] = [0, 0, 0, 0, 0, KECCAK_MIN_STEPS_LOG]; +const TABLE_LOG_FLOORS: [usize; N_TABLES] = [0, 0, 0, 0, 0, KECCAK_MIN_STEPS_LOG, DOT_LOG_FLOOR]; pub fn build_layout<'a>( bytecode: &'a Bytecode, diff --git a/doc/leanvm/body/02-vm-specification.tex b/doc/leanvm/body/02-vm-specification.tex index b7b4466788..e348e0df60 100644 --- a/doc/leanvm/body/02-vm-specification.tex +++ b/doc/leanvm/body/02-vm-specification.tex @@ -53,7 +53,7 @@ \section{VM specification}\label{sec:vm} \mem[\fp\cdot o],\qquad \fp\cdot o\;=\;\gen^{\mathrm{base}}\cdot\gen^{\,j}\;=\;\gen^{\,\mathrm{base}+j}. \] -\paragraph{Instruction set.} The machine implements six instructions. +\paragraph{Instruction set.} The machine implements seven instructions. \begin{center} \begin{tabularx}{\textwidth}{@{}llX@{}} \hline @@ -65,6 +65,8 @@ \section{VM specification}\label{sec:vm} \texttt{DEREF} & $[o_1,o_2,o_3;\,\dmode]$ & $\mem[\loc{o_1}\cdot o_2]=\srcsel{\dmode}$ (see below)\\ \texttt{JUMP} & $[o_c,o_d,o_f]$ & conditional jump (see below)\\ \texttt{KECCAK} & $[o_{m_0},o_{m_1},o_{m_2},o_{m_3},o_{\mathit{tail}},o_{\mathit{b8}},o_{\mathit{cap}},o_{\mathit{out}},\mathit{odd}_{\mathit{in}},\mathit{odd}_{\mathit{out}}]$ & one Keccak permutation (see below)\\ +\texttt{KECCAK\_M} & as \texttt{KECCAK} & a Merkle step: \texttt{KECCAK} with a direction bit in $\loc{o_{\mathit{b8}}}$ (see below)\\ +\texttt{DOT} & $[o_x,o_p,o_w,o_{\mathit{acc}},o_{\mathit{out}}]$ & an affine form of four $\K$-lanes (see below)\\ \hline \end{tabularx} \end{center} @@ -97,4 +99,13 @@ \section{VM specification}\label{sec:vm} \end{itemize} The 64-bit of the cell 8 (even) or 0 (odd) (denoted $\cdot$ above) is ignored for the input and set to zero for the output. +\vspace{5mm} + +\texttt{KECCAK\_M} (opcode $\gen^{7}$, the Merkle step) is \texttt{KECCAK} with $\mathit{odd}_{\mathit{in}}=0$ and two changes. The low limb of $\loc{o_{\mathit{b8}}}$ is a bit $s\in\{0,1\}$ rather than lane $16$, which is the last byte of Keccak-256's padding of a one-block message, $\texttt{0x80}\cdot2^{56}$. And the first four cells are read in the order $s$ selects: $\loc{o_{m_0}},\loc{o_{m_1}},\loc{o_{m_2}},\loc{o_{m_3}}$ when $s=0$, $\loc{o_{m_2}},\loc{o_{m_3}},\loc{o_{m_0}},\loc{o_{m_1}}$ when $s=1$. A level of a Merkle path is one \texttt{KECCAK\_M} over the running node and the sibling, $s$ the query's bit at that level. + +\vspace{5mm} +\texttt{DOT} reads the two cells $\loc{\gen^k\cdot o_x}$ ($k<2$), each of which must be canonical (top limb zero), as four $\K$-lanes $x_{2k+h}$, limb $h\in\{0,1\}$ of cell $k$; a pointer $p=\loc{o_p}$, which must be in $\K$; four weights $w_j=\mem[p\cdot o_w\cdot\gen^{j}]\in\E$ ($j<4$); and $\loc{o_{\mathit{acc}}}$. It asserts +\[ + \loc{o_{\mathit{out}}}=\loc{o_{\mathit{acc}}}+\sum_{j<4}w_j\,x_j . +\] diff --git a/doc/leanvm/body/06-bus-interactions.tex b/doc/leanvm/body/06-bus-interactions.tex index a1dd045329..0b9fc586c1 100644 --- a/doc/leanvm/body/06-bus-interactions.tex +++ b/doc/leanvm/body/06-bus-interactions.tex @@ -73,7 +73,7 @@ \subsection{The lookup protocol}\label{sec:lookup} \paragraph{The count product.} All counts are nonzero exactly when their product is. The prover therefore sends that product, one $\E$-element, and the verifier checks it is nonzero. It is proven like any other grand product, by GKR over the stacking of every count column (\S\ref{sec:leafstack}), batched with the two other GKRs of the bus (push / pull) (\S\ref{sec:gkr}). -\paragraph{Instance caps.} The verifier checks the announced sizes against public caps (\S\ref{sec:e2e-unrolled}): at most $2^{32}$ rows per table, at most $2^{32}$ memory cells (\S\ref{sec:memchan}) and at most $2^{32}$ bytecode instructions (\S\ref{sec:bytecode}). A row reads at most twenty-seven times (\texttt{KECCAK}: twenty-six cells and its bytecode entry), so the six tables flush $\nread\le27\cdot6\cdot2^{32}<2^{40}$ reads, far under the $2^{64}-1$ that Lemma~\ref{lem:invcnt} requires. +\paragraph{Instance caps.} The verifier checks the announced sizes against public caps (\S\ref{sec:e2e-unrolled}): at most $2^{32}$ rows per table, at most $2^{32}$ memory cells (\S\ref{sec:memchan}) and at most $2^{32}$ bytecode instructions (\S\ref{sec:bytecode}). A row reads at most twenty-seven times (\texttt{KECCAK}: twenty-six cells and its bytecode entry), so the seven tables flush $\nread\le27\cdot7\cdot2^{32}<2^{40}$ reads, far under the $2^{64}-1$ that Lemma~\ref{lem:invcnt} requires. \paragraph{Relaxing the hypotheses.} A bus entry may be any polynomial of degree at most $d = 2$ in the row's columns (\S\ref{sec:m3}), so none of the three (address, count, value) need be committed. Example 1: we don't need to commit memory addresses of the form $\fp\cdot o$ (\S\ref{sec:memchan}) when $\fp$ and $o$ are already committed. Example 2: once the pull counter $\cnt$ is committed, no need to commit to the corresponding push counter $\gen\cdot\cnt$. @@ -88,7 +88,7 @@ \subsection{Bytecode}\label{sec:bytecode} The bytecode is an array of $\nprog = 2^{\kbc}$ instructions, $\kbc \le 32$ (\S\ref{sec:e2e-bc}). Bus interaction uses domain separation $\sep=\dsep{BC} = \gen^{2}$. An instruction is an opcode plus ten operands, so $\ncomp=11$: with the separator, the address and the count ahead of them, this is the widest entry on the bus at $14$ coordinates, which is what forces the four index bits, hence the $m=16$ slots of \S\ref{sec:m3}. Coordinate $3$ is the opcode, naming the instruction: \[ - \opc{XOR}=\gen^{0},\quad \opc{MUL}=\gen^{1},\quad \opc{SET}=\gen^{2},\quad \opc{DRF}=\gen^{3},\quad \opc{JMP}=\gen^{4},\quad \opc{KEC}=\gen^{5}. + \opc{XOR}=\gen^{0},\quad \opc{MUL}=\gen^{1},\quad \opc{SET}=\gen^{2},\quad \opc{DRF}=\gen^{3},\quad \opc{JMP}=\gen^{4},\quad \opc{KEC}=\gen^{5},\quad \opc{DOT}=\gen^{6}. \] The program is public, i.e. not committed, the verifier can evaluate it directly. \subsection{The index column}\label{sec:idxcol} diff --git a/doc/leanvm/body/07-instruction-tables.tex b/doc/leanvm/body/07-instruction-tables.tex index eab561dcfd..1bf04f2188 100644 --- a/doc/leanvm/body/07-instruction-tables.tex +++ b/doc/leanvm/body/07-instruction-tables.tex @@ -113,9 +113,10 @@ \subsection{\texttt{JUMP}}\label{sec:tab-jump} \subsection{\texttt{KECCAK}}\label{sec:tab-keccak} -One row is one permutation (\S\ref{sec:vm}), proved by the GKR of Annex~\ref{annex:keccak} rather than by constraints. A row has $115$ columns: +One row is one permutation (\S\ref{sec:vm}), proved by the GKR of Annex~\ref{annex:keccak}, of a \texttt{KECCAK} or a \texttt{KECCAK\_M}. A row has $120$ columns: \begin{itemize} \item $\pc$, $\fp$, the eight operand offsets, the two layout flags and $\mathit{free}$, and $27$ read counts, one per cell read or written and one for the bytecode. +\item $\mathit{mk}$, $1$ on a \texttt{KECCAK\_M} row; $b_8$, the low limb of the cell $\loc{o_{\mathit{b8}}}$ as read; the direction bit $s$; and $A_0$, $A_1$, the offsets the first two cells are read at. \item $\kin{\ell}$, the $25$ lanes of the state the row reads from memory. \item $\kmid{\ell}$, the $25$ lanes of the state after $12$ rounds. Committing it is an optimization and nothing more: it halves the depth of the reduction. No flush reads it. \item $\kout{\ell}$, the $25$ lanes the row writes back. These are \emph{virtual}: the flushes read them, but nothing commits them, and the claims the table sumcheck leaves on them are deferred to the GKR, which reduces them to claims on $\kin{\ell}$ and $\kmid{\ell}$. @@ -123,15 +124,21 @@ \subsection{\texttt{KECCAK}}\label{sec:tab-keccak} A limb holding lane $a$ in the even layout and $b$ in the odd one is the degree-$2$ form $a+\mathit{odd}\,(a+b)$, a limb holding no lane being the column $\mathit{free}$ when read and $0$ when written. The top limb is a literal $0$. \begin{itemize} -\item \textbf{Constraints:} none. The permutation is proved by the GKR of Annex~\ref{annex:keccak}. +\item \textbf{Constraints:} the permutation is proved by the GKR of Annex~\ref{annex:keccak}; eight identities of degree $2$ select the Merkle step: + \begin{align*} + &\mathit{mk}(\mathit{mk}+1)=0, \qquad \mathit{mk}\cdot\mathit{odd}_{\mathit{in}}=0, \qquad b_8+u_8+\mathit{mk}\,(b_8+\kin{16})=0, \qquad \mathit{mk}\,(\kin{16}+\texttt{0x80}\cdot2^{56})=0,\\ + &s+\mathit{mk}\cdot b_8=0, \qquad s(s+1)=0, \qquad A_0+o_{m_0}+s\,(o_{m_0}+o_{m_2})=0, \qquad A_1+o_{m_1}+s\,(o_{m_1}+o_{m_3})=0, + \end{align*} + $u_8=\kin{16}+\mathit{odd}_{\mathit{in}}(\kin{16}+\kin{15})$ being the form below. So $b_8$ is that lane on a \texttt{KECCAK} row and $s=0$; on a \texttt{KECCAK\_M} row the layout is even, lane $16$ is the padding byte, and $s=b_8$ is a bit. \item \textbf{Flushes:} \begin{itemize} \item \textbf{State:} \pull\ $\tup{\dsep{ST},\pc,\fp}$, \push\ $\tup{\dsep{ST},\gen\cdot\pc,\fp}$. - \item \textbf{Bytecode:} $\rdf{\dsep{BC},\pc,r_{\mathrm{bc}},\opc{KEC},o_{m_0},o_{m_1},o_{m_2},o_{m_3},o_{\mathit{tail}},o_{\mathit{b8}},o_{\mathit{cap}},o_{\mathit{out}},\mathit{odd}_{\mathit{in}},\mathit{odd}_{\mathit{out}}}$. + \item \textbf{Bytecode:} $\rdf{\dsep{BC},\pc,r_{\mathrm{bc}},\opc{KEC}+\mathit{mk}\,(\opc{KEC}+\gen^{7}),o_{m_0},o_{m_1},o_{m_2},o_{m_3},o_{\mathit{tail}},o_{\mathit{b8}},o_{\mathit{cap}},o_{\mathit{out}},\mathit{odd}_{\mathit{in}},\mathit{odd}_{\mathit{out}}}$. \item \textbf{Memory:} cell $c<13$ is read at $a_c$ under $\mathit{odd}_{\mathit{in}}$ and written at $\gen^{c}\fp\cdot o_{\mathit{out}}$ under $\mathit{odd}_{\mathit{out}}$, where \[ a_c=\begin{cases} - \fp\cdot o_{m_c} & c<4,\\ + \fp\cdot A_c & c<2,\\ + \fp\cdot(A_{c-2}+o_{m_{c-2}}+o_{m_c}) & 2\le c<4,\\ \gen^{c-4}\fp\cdot o_{\mathit{tail}} & 4\le c<8,\\ \fp\cdot o_{\mathit{b8}} & c=8,\\ \gen^{c-9}\fp\cdot o_{\mathit{cap}} & 9\le c<13, @@ -141,6 +148,31 @@ \subsection{\texttt{KECCAK}}\label{sec:tab-keccak} &\rdf{\dsep{MEM},a_c,r_c,u_c,v_c,0} &&(c<9),\\ &\rdf{\dsep{MEM},a_c,r_c,\kin{2c-1},\kin{2c},0} &&(9\le c<13), \end{align*} - the writes the same with $\kout{\ell}$ and $r_{13+c}$. The rate cells take the forms $u_c,v_c$ above; the four capacity cells hold lanes $17$ to $24$ in either layout. + the writes the same with $\kout{\ell}$ and $r_{13+c}$. The rate cells take the forms $u_c,v_c$ above, but for the read cell $8$, whose low limb is $b_8$; the four capacity cells hold lanes $17$ to $24$ in either layout. With $s=0$ the four $m$ cells are read at $\fp\cdot o_{m_c}$, with $s=1$ the two pairs swap. \end{itemize} \end{itemize} + +\subsection{\texttt{DOT}}\label{sec:tab-dot} + +\texttt{DOT}\ $[o_x,o_p,o_w,o_{\mathit{acc}},o_{\mathit{out}}]$ asserts $\loc{o_{\mathit{out}}}=\loc{o_{\mathit{acc}}}+\sum_{j<4}w_j\,x_j$, an affine form of four $\K$-lanes with $\E$ weights (\S\ref{sec:vm}). It is the part of a verifier that is linear in the data it opens: once a child proof's transcript is fixed, the weights a recursive verifier puts on an opened row are words it has computed, and the check is affine in the row (\S\ref{sec:recursion}). +\begin{itemize} +\item \textbf{Columns:} $\pc,\fp$; operands $o_x,o_p,o_w,o_{\mathit{acc}},o_{\mathit{out}}$; the four lanes $x_0,\dots,x_3$; the pointer $p$ (a single $\K$-limb); the weights $(w_{j,i})_{j<4,\,i\in\{0,1,2\}}$; the accumulator $(a_i)_{i\in\{0,1,2\}}$; memory counts $r_{x,0},r_{x,1}$, $r_p$, $r_{w,0},\dots,r_{w,3}$, $r_{\mathit{acc}}$, $r_{\mathit{out}}$; bytecode count $r_{\mathrm{bc}}$. +\item \textbf{Constraints:} none. +\item \textbf{Flushes:} + \begin{itemize} + \item \textbf{State:} \pull\ $\tup{\dsep{ST},\pc,\fp}$, \push\ $\tup{\dsep{ST},\gen\cdot\pc,\fp}$. + \item \textbf{Bytecode:} $\rdf{\dsep{BC},\pc,r_{\mathrm{bc}},\opc{DOT},o_x,o_p,o_w,o_{\mathit{acc}},o_{\mathit{out}}}$. + \item \textbf{Memory}, the result carrying, limb by limb, the degree-two form $a_i+\sum_{j<4}w_{j,i}\,x_j$ (a $\K$-lane times an $\E$ word needs no tower reduction): + \begin{align*} + &\rdf{\dsep{MEM},\gen^{k}\fp\cdot o_x,r_{x,k},x_{2k},x_{2k+1},0}\quad(k<2),\\ + &\rdf{\dsep{MEM},\fp\cdot o_p,r_p,p,0,0},\\ + &\rdf{\dsep{MEM},\gen^{j}p\cdot o_w,r_{w,j},w_{j,0},w_{j,1},w_{j,2}}\quad(j<4),\\ + &\rdf{\dsep{MEM},\fp\cdot o_{\mathit{acc}},r_{\mathit{acc}},a_0,a_1,a_2},\\ + &\rdf{\dsep{MEM},\fp\cdot o_{\mathit{out}},r_{\mathit{out}},\textstyle a_0+\sum_j w_{j,0}x_j,\ a_1+\sum_j w_{j,1}x_j,\ a_2+\sum_j w_{j,2}x_j}. + \end{align*} + \end{itemize} +\end{itemize} +The lane cells' top limbs are literal zeros and the lanes are $\K$ columns, so the bus alone proves each cell canonical and both its lanes in $\K$: a run that \texttt{DOT} weighs and \texttt{KECCAK} hashes needs no packing instructions (\S\ref{sec:prog-subfield-checks}). The weights are read through a pointer, as \texttt{DEREF} reads its target, and the bus forces $p\cdot o_w\cdot\gen^{j}$ to be an address (Corollary~\ref{cor:addrrange}). + +The table's least log height is $13$, as \texttt{KECCAK}'s is that of a block of steps. A recursive verifier opens each column of a child proof through its stacking selector, over the child's position bits above the column's height, and decomposes each bus block's offset the same way, so an empty table would cost it the most; the floor caps that cost at the price of $2^{13}$ padding rows. The width, four lanes, balances the same fixed cost per column and per block against the rows a row saves. + diff --git a/doc/leanvm/body/08-end-to-end-protocol.tex b/doc/leanvm/body/08-end-to-end-protocol.tex index 20bdbbe475..7657374a77 100644 --- a/doc/leanvm/body/08-end-to-end-protocol.tex +++ b/doc/leanvm/body/08-end-to-end-protocol.tex @@ -19,6 +19,7 @@ \subsection{Bytecode encoding}\label{sec:e2e-bc} \texttt{DEREF} & $\opc{DRF}$ & $o_1$ & $o_2$ & $o_3$ & $f_{\pc}$ & $f_{\fp}$ & $0$ & $0$ & $0$ & $0$ & $0$\\ \texttt{JUMP} & $\opc{JMP}$ & $o_c$ & $o_d$ & $o_f$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$ & $0$\\ \texttt{KECCAK} & $\opc{KEC}$ & $o_{m_0}$ & $o_{m_1}$ & $o_{m_2}$ & $o_{m_3}$ & $o_{\mathit{tail}}$ & $o_{\mathit{b8}}$ & $o_{\mathit{cap}}$ & $o_{\mathit{out}}$ & $\mathit{odd}_{\mathit{in}}$ & $\mathit{odd}_{\mathit{out}}$\\ +\texttt{DOT} & $\opc{DOT}$ & $o_x$ & $o_p$ & $o_w$ & $o_{\mathit{acc}}$ & $o_{\mathit{out}}$ & $0$ & $0$ & $0$ & $0$ & $0$\\ \hline \end{tabular} \end{center} @@ -81,7 +82,7 @@ \subsection{The unrolled protocol}\label{sec:e2e-unrolled} \paragraph{Setup.} -The Fiat-Shamir initial state (\S\ref{sec:e2e-fiat-shamir}) is seeded by the public input and by the IV, the hash of the version tag and the bytecode. The prover sends the memory log-size $\kmem$, the six row counts $\nexec{j}$ and the rate exponent $\rexp_0\in\{1,\dots,4\}$ of the commitment (Annex~\ref{annex:pcs}); the verifier checks them against the caps (\S\ref{sec:lookup}) and derives the layout of every stack (PCS and GKR leaves). +The Fiat-Shamir initial state (\S\ref{sec:e2e-fiat-shamir}) is seeded by the public input and by the IV, the hash of the version tag and the bytecode. The prover sends the memory log-size $\kmem$, the seven row counts $\nexec{j}$ and the rate exponent $\rexp_0\in\{1,\dots,4\}$ of the commitment (Annex~\ref{annex:pcs}); the verifier checks them against the caps (\S\ref{sec:lookup}) and derives the layout of every stack (PCS and GKR leaves). \paragraph{Commitment.} @@ -99,7 +100,7 @@ \subsection{The unrolled protocol}\label{sec:e2e-unrolled} \item \textbf{Prover \& Verifier.} Decompose each leaf claim (\S\ref{sec:leafstack}). The verifier forms every public part itself: the block selectors, the constant entries, the index column (\S\ref{sec:idxcol}), the padding term of \eqref{eq:pad_leaf}, and the program's whole share of the two bytecode blocks, one evaluation at $(\zeta,\alpha)$ (\S\ref{sec:e2e-bc}). Three blocks per side belong to no table: the state boundary, plus each array's (memory / bytecode) seed (push side) or finalization (pull side). For their committed columns the prover sends one evaluation each, entering the claim pool. Every other block belongs to a table, and is discharged by the table sumcheck (\S\ref{sec:air}). \end{enumerate} -\paragraph{Table sumcheck.} One run proves the six tables' constraints together with their share of the bus (\S\ref{sec:air}). +\paragraph{Table sumcheck.} One run proves the seven tables' constraints together with their share of the bus (\S\ref{sec:air}). \begin{enumerate} \item \textbf{Verifier.} Samples and sends the batch challenge $\xi\in\E$. Its first $\nconstot=\sum_j\ncons{j}$ powers weight the constraints, a disjoint range per table; the last $\nside$ weight the bus forms, one per side and shared by every table. It draws no point: the batch runs at the bus point, $T_j$ tested at $\zeta_{<\tau_j}$. \item \textbf{Prover \& Verifier.} Run the sumcheck, $\taumax=\max_j\tau_j$ rounds with $T_j$ joining at round $\taumax-\tau_j$, against the target the verifier derived from the three leaf claims. Its summand has individual degree three and the round check pins one of the four coefficients (\S\ref{sec:prelim}), so a round costs three $\E$-elements. diff --git a/doc/leanvm/body/09-isa-programming.tex b/doc/leanvm/body/09-isa-programming.tex index 1c92161607..dd8fed96e7 100644 --- a/doc/leanvm/body/09-isa-programming.tex +++ b/doc/leanvm/body/09-isa-programming.tex @@ -1,7 +1,7 @@ % !TeX root = ../drafts/09-isa-programming.tex \section{ISA programming}\label{sec:isa-programming} -How the zkDSL compiler (\path{crates/lean_compiler}; surface language in \path{crates/lean_compiler/zkDSL.md}) programs the six-instruction ISA. Two themes run through every pattern: \emph{write-once memory is an assertion mechanism} (a second write of a cell is an equality check, an unwritten cell is prover-chosen), and \emph{indices live in the exponent} (a logical index $i$ is the element $\gen^{i}$, so incrementing, offsetting, and address formation are single field multiplications). +How the zkDSL compiler (\path{crates/lean_compiler}; surface language in \path{crates/lean_compiler/zkDSL.md}) programs the seven-instruction ISA. Two themes run through every pattern: \emph{write-once memory is an assertion mechanism} (a second write of a cell is an equality check, an unwritten cell is prover-chosen), and \emph{indices live in the exponent} (a logical index $i$ is the element $\gen^{i}$, so incrementing, offsetting, and address formation are single field multiplications). \subsection{Hints}\label{sec:prog-hints} diff --git a/python-verifier/verifier.py b/python-verifier/verifier.py index 4f0472945a..e51b980101 100644 --- a/python-verifier/verifier.py +++ b/python-verifier/verifier.py @@ -760,8 +760,10 @@ def state_step(self, pc: int, fp: int) -> None: def _counted(self, prefix: Sequence[Form], count: int, suffix: Sequence[Form]) -> None: self.pair((*prefix, _col(count, 1), *suffix), (*prefix, _col(count), *suffix)) - def bytecode(self, pc: int, count: int, opcode: int, operands: Sequence[Form]) -> None: - self._counted((_const(SEP_BYTECODE), _col(pc)), count, (_const(_gpow(opcode)), *operands)) + def bytecode(self, pc: int, count: int, opcode: int | Form, operands: Sequence[Form]) -> None: + """The program tuple at pc; `opcode` a form when a table proves two opcodes, its flag selecting one.""" + tag = _const(_gpow(opcode)) if isinstance(opcode, int) else opcode + self._counted((_const(SEP_BYTECODE), _col(pc)), count, (tag, *operands)) def memory(self, address: Form, count: int, values: Sequence[Form]) -> None: self._counted((_const(SEP_MEM), address), count, values) @@ -864,6 +866,25 @@ def _flushes_jump() -> Flushes: return flushes +def _keccak_constraints(columns: Sequence[E]) -> tuple[E, ...]: + """KECCAK_M's identities: the flag a bit and the even layout under it; b8 the lane its layout puts in b8's low limb on a + KECCAK row; lane 16 the padding's last byte on a KECCAK_M row; the direction bit s = mk*b8 a bit; the offsets a0, a1.""" + o_m0, o_m1, o_m2, o_m3, odd_in, mk, b8, s, a0, a1 = ( + columns[i] for i in _cols(KECCAK_COLUMNS, "o_m0", "o_m1", "o_m2", "o_m3", "odd_in", "mk", "b8", "s", "a0", "a1") + ) + v15, v16 = columns[KECCAK_IN + 15], columns[KECCAK_IN + 16] + return ( + mk * (mk + ONE), + mk * odd_in, + b8 + v16 + odd_in * (v16 + v15) + mk * (b8 + v16), + mk * (v16 + E(1 << 63)), # Keccak-256's last padding byte, 0x80, atop lane 16 + s + mk * b8, + s * (s + ONE), + a0 + o_m0 + s * (o_m0 + o_m2), + a1 + o_m1 + s * (o_m1 + o_m3), + ) + + def _jump_constraints(columns: Sequence[E]) -> tuple[E, ...]: condition, inverse, flag = (columns[index] for index in _cols(JUMP_COLUMNS, "v_cond", "w", "b")) return (flag + condition * inverse, condition * (flag + ONE)) @@ -897,16 +918,23 @@ def _flushes_keccak() -> Flushes: pc, fp, cnt_bc, free = _cols(KECCAK_COLUMNS, "pc", "fp", "cnt_bc", "free") o_m = _cols(KECCAK_COLUMNS, "o_m0", "o_m1", "o_m2", "o_m3") o_tail, o_b8, o_cap, o_out, odd_in, odd_out = _cols(KECCAK_COLUMNS, "o_tail", "o_b8", "o_cap", "o_out", "odd_in", "odd_out") + mk, b8, a0, a1 = _cols(KECCAK_COLUMNS, "mk", "b8", "a0", "a1") flushes = Flushes() flushes.state_step(pc, fp) - flushes.bytecode(pc, cnt_bc, OP_KECCAK, tuple(_col(i) for i in (*o_m, o_tail, o_b8, o_cap, o_out, odd_in, odd_out))) + # KECCAK's opcode, or KECCAK_M's (the Merkle step) when the flag mk is 1. + opcode = Form.sum([_const(_gpow(OP_KECCAK)), _col(mk, OP_KECCAK), _col(mk, OP_KECCAK_M)]) + flushes.bytecode(pc, cnt_bc, opcode, tuple(_col(i) for i in (*o_m, o_tail, o_b8, o_cap, o_out, odd_in, odd_out))) # The thirteen cells read (four addressed ones, the tail run, b8, the cap run), then the # thirteen written, each holding its lanes in the layout its flag names: a read cell's - # free lane is the column `free`, a written one's zero. + # free lane is the column `free`, a written one's zero. The first two m cells are read at + # the offsets a0, a1 the direction bit picks, the other two at the offsets left over, and + # b8's low limb is the column b8. def address(cell: int) -> Form: + if cell < 2: + return _prod(fp, (a0, a1)[cell]) if cell < 4: - return _prod(fp, o_m[cell]) + return Form.sum([_prod(fp, (a0, a1)[cell - 2]), _prod(fp, o_m[cell - 2]), _prod(fp, o_m[cell])]) if cell < 8: return _prod(fp, o_tail, cell - 4) return _prod(fp, o_b8) if cell == 8 else _prod(fp, o_cap, cell - 9) @@ -919,11 +947,49 @@ def lane(lane: int | None, lanes: int = lanes) -> int | None: for cell in range(KECCAK_CELLS): values = [_layout_limb(lane(even[cell][h]), lane(odd[cell][h]), flag, missing) for h in (0, 1)] + [_const(ZERO)] + if side == 0 and cell == 8: + values[0] = _col(b8) flushes.memory(address(cell) if side == 0 else _prod(fp, o_out, cell), KECCAK_CNT + KECCAK_CELLS * side + cell, values) return flushes -OP_XOR, OP_MUL, OP_SET, OP_DEREF, OP_JUMP, OP_KECCAK = range(6) +DOT_LANES = 4 +DOT_LOG_FLOOR = 13 # DOT's least log height, which caps what a recursive verifier pays for a child with few DOT rows +# DOT's columns: the operands, the lanes x_j, the weight pointer, weight j's limbs wj_l, the accumulator's, then a read +# count per cell in flush order (the lane cells, the pointer, the weights, the accumulator, the result) and cnt_bc. +DOT_COLUMNS = ( + "pc", "fp", "o_x", "o_p", "o_w", "o_acc", "o_out", + *(f"x_{j}" for j in range(DOT_LANES)), "ptr", + *(f"w{j}_{limb}" for j in range(DOT_LANES) for limb in range(3)), + *(f"acc_{limb}" for limb in range(3)), + *(f"cnt_x{k}" for k in range(DOT_LANES // 2)), "cnt_ptr", *(f"cnt_w{j}" for j in range(DOT_LANES)), "cnt_acc", "cnt_out", "cnt_bc", +) # fmt: skip + + +def _flushes_dot() -> Flushes: + """out = acc + sum_j w_j * x_j: DOT_LANES K-lanes x_j (the low limbs of canonical cells, two each) against as many weights read through a pointer.""" + pc, fp, o_x, o_p, o_w, o_acc, o_out, ptr, cnt_ptr, cnt_acc, cnt_out, cnt_bc = _cols( + DOT_COLUMNS, "pc", "fp", "o_x", "o_p", "o_w", "o_acc", "o_out", "ptr", "cnt_ptr", "cnt_acc", "cnt_out", "cnt_bc" + ) + x = _cols(DOT_COLUMNS, *(f"x_{j}" for j in range(DOT_LANES))) + w = [_cols(DOT_COLUMNS, *(f"w{j}_{limb}" for limb in range(3))) for j in range(DOT_LANES)] + acc = _cols(DOT_COLUMNS, "acc_0", "acc_1", "acc_2") + flushes = Flushes() + flushes.state_step(pc, fp) + flushes.bytecode(pc, cnt_bc, OP_DOT, (_col(o_x), _col(o_p), _col(o_w), _col(o_acc), _col(o_out))) + for k in range(DOT_LANES // 2): + flushes.memory_cols(_prod(fp, o_x, k), DOT_COLUMNS.index(f"cnt_x{k}"), x[2 * k], x[2 * k + 1]) + flushes.memory_cols(_prod(fp, o_p), cnt_ptr, ptr) + for j in range(DOT_LANES): + flushes.memory_cols(_prod(ptr, o_w, j), DOT_COLUMNS.index(f"cnt_w{j}"), *w[j]) + flushes.memory_cols(_prod(fp, o_acc), cnt_acc, *acc) + result = [_col(acc[limb]) + Form.sum([_prod(w[j][limb], x[j]) for j in range(DOT_LANES)]) for limb in range(3)] + flushes.memory(_prod(fp, o_out), cnt_out, result) + return flushes + + +OP_XOR, OP_MUL, OP_SET, OP_DEREF, OP_JUMP, OP_KECCAK, OP_DOT = range(7) +OP_KECCAK_M = 7 # the Merkle step, its own opcode, proven by the KECCAK table ARITH_COLUMNS = ("pc", "fp", "o_a", "o_b", "o_c", "va_0", "va_1", "va_2", "vb_0", "vb_1", "vb_2", "cnt_a", "cnt_b", "cnt_c", "cnt_bc",) # fmt: skip SET_COLUMNS = ("pc", "fp", "o", "k_0", "k_1", "k_2", "cnt", "cnt_bc") @@ -938,6 +1004,8 @@ def lane(lane: int | None, lanes: int = lanes) -> int | None: *(f"in_{lane}" for lane in range(STATE_LANES)), *(f"out_{lane}" for lane in range(STATE_LANES)), # ...and the read counts, each cell read then each written, committed here like every other column. *(f"cnt_{cell}" for cell in range(2 * KECCAK_CELLS)), "cnt_bc", + # KECCAK_M's: its flag, b8's low limb as read, the direction bit, the offsets of the first two m cells. + "mk", "b8", "s", "a0", "a1", ) # fmt: skip KECCAK_IN, KECCAK_OUT, KECCAK_CNT = (KECCAK_COLUMNS.index(name) for name in ("in_0", "out_0", "cnt_0")) # Which slot of q_keccak each local KECCAK column is. The states take the first QKECCAK_SLOTS, the input @@ -960,7 +1028,8 @@ def lane(lane: int | None, lanes: int = lanes) -> int | None: Table("set", OP_SET, SET_COLUMNS, _flushes_set()), Table("deref", OP_DEREF, DEREF_COLUMNS, _flushes_deref()), Table("jump", OP_JUMP, JUMP_COLUMNS, _flushes_jump(), _jump_constraints), - Table("keccak", OP_KECCAK, KECCAK_COLUMNS, _flushes_keccak()), + Table("keccak", OP_KECCAK, KECCAK_COLUMNS, _flushes_keccak(), _keccak_constraints), + Table("dot", OP_DOT, DOT_COLUMNS, _flushes_dot()), ) @@ -968,7 +1037,8 @@ def lane(lane: int | None, lanes: int = lanes) -> int | None: GLOBAL_COLUMN_BASES = tuple(NUM_GLOBAL_COLUMNS + sum(TABLE_WIDTHS[:table]) for table in range(len(TABLES))) -TABLE_LOG_FLOORS = (0, 0, 0, 0, 0, KECCAK_MIN_STEPS_LOG) # each table's least log height: 0, but the Keccak reduction's block of steps for KECCAK +# Each table's least log height: 0, but the Keccak reduction's block of steps for KECCAK, and DOT's floor. +TABLE_LOG_FLOORS = (0, 0, 0, 0, 0, KECCAK_MIN_STEPS_LOG, DOT_LOG_FLOOR) def build_layout(bytecode: Sequence[K], log_memory: int, table_rows: Sequence[int]) -> Layout: