Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/recursion-bench.yml
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 19 additions & 0 deletions crates/lean_compiler/snark_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions crates/lean_compiler/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down
20 changes: 19 additions & 1 deletion crates/lean_compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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}] + <lanes fp[{x}..+2], *(fp[{p}]·g^{w})..+4>")
}
};
writeln!(out, "{:>6} {line}", pretty_integer(pc)).unwrap();
}
Expand Down Expand Up @@ -344,6 +353,7 @@ fn resolve(op: &LOp, entry: &HashMap<String, u32>, sentinel: u32, pc0: u32, fram
c,
in_odd,
out_odd,
swap,
} => Op::Keccak {
m: *m,
tail: *tail,
Expand All @@ -352,6 +362,14 @@ fn resolve(op: &LOp, entry: &HashMap<String, u32>, 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,
},
}
}
1 change: 1 addition & 0 deletions crates/lean_compiler/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
92 changes: 90 additions & 2 deletions crates/lean_compiler/src/lower/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -56,6 +63,7 @@ impl FnLower<'_> {
c: out.state,
in_odd: false,
out_odd: false,
swap: false,
});
self.keccak_finish(out);
return;
Expand All @@ -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,
Expand Down Expand Up @@ -273,6 +335,7 @@ impl FnLower<'_> {
c,
in_odd: k % 2 == 1,
out_odd: !last && k % 2 == 0,
swap: false,
});
state = Some(c);
}
Expand Down Expand Up @@ -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" => {
Expand Down Expand Up @@ -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" => {
Expand Down
2 changes: 2 additions & 0 deletions crates/lean_compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions crates/lean_compiler/tests/suite/determinism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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 {
Expand Down
Loading
Loading