Skip to content

Commit 0bbc44e

Browse files
committed
improve `hint_decompose_bits_merkle_whir
1 parent 1154ca4 commit 0bbc44e

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

crates/lean_vm/src/isa/hint.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl CustomHint {
130130
pub fn n_args(&self) -> usize {
131131
match self {
132132
Self::DecomposeBitsXMSS => 5,
133-
Self::DecomposeBitsMerkleWhir => 4,
133+
Self::DecomposeBitsMerkleWhir => 3,
134134
Self::DecomposeBits => 4,
135135
Self::LessThan => 3,
136136
Self::Log2Ceil => 2,
@@ -166,8 +166,8 @@ impl CustomHint {
166166
}
167167
Self::DecomposeBitsMerkleWhir => {
168168
let decomposed_ptr = args[0].read_value(ctx.memory, ctx.fp)?.to_usize();
169-
let value = args[2].read_value(ctx.memory, ctx.fp)?.to_usize();
170-
let chunk_size = args[3].read_value(ctx.memory, ctx.fp)?.to_usize();
169+
let value = args[1].read_value(ctx.memory, ctx.fp)?.to_usize();
170+
let chunk_size = args[2].read_value(ctx.memory, ctx.fp)?.to_usize();
171171
assert!(24_usize.is_multiple_of(chunk_size));
172172
let mut memory_index_decomposed = decomposed_ptr;
173173
#[allow(clippy::explicit_counter_loop)]
@@ -176,8 +176,6 @@ impl CustomHint {
176176
ctx.memory.set(memory_index_decomposed, value)?;
177177
memory_index_decomposed += 1;
178178
}
179-
ctx.memory
180-
.set(args[1].memory_address(ctx.fp)?, F::from_usize(value >> 24))?;
181179
}
182180
Self::DecomposeBits => {
183181
let to_decompose = args[0].read_value(ctx.memory, ctx.fp)?.to_usize();

crates/rec_aggregation/utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,19 +516,22 @@ def whir_1_merkle_step_and_pow(v, state_in, path_chunk, state_out, power_shift):
516516
@inline
517517
def decompose_and_verify_merkle_query(a, domain_size, prev_root, num_chunks):
518518
nibbles = Array(6)
519-
top7: Imu
520-
hint_decompose_bits_merkle_whir(nibbles, top7, a, 4)
519+
hint_decompose_bits_merkle_whir(nibbles, a, 4)
521520

522521
for i in unroll(0, 6):
523522
assert nibbles[i] < 16
524-
assert top7 < 2**7
525523

526524
partial_sum: Mut = nibbles[0]
527525
for i in unroll(1, 6):
528526
partial_sum += nibbles[i] * 16**i
527+
528+
# p = 2^31 - 2^24 + 1, so 2^24 * 127 = p - 1 ≡ -1 (mod p), hence inv(2^24) = -127.
529+
# Deduce top7 from the identity partial_sum + top7 * 2^24 == a:
530+
# top7 = (a - partial_sum) * inv(2^24) = (partial_sum - a) * 127
531+
top7 = (partial_sum - a) * 127
532+
assert top7 < 2**7
529533
if top7 == 2**7 - 1:
530534
assert partial_sum == 0
531-
assert partial_sum + top7 * 2**24 == a
532535

533536
leaf_data = Array(num_chunks * DIGEST_LEN)
534537
hint_witness("merkle_leaf", leaf_data)

0 commit comments

Comments
 (0)