Skip to content

Commit 8779f2f

Browse files
zkMaguseigmax
andauthored
fix out of index in keccak sponge (#88)
* fix out of index in keccak sponge * remove code hash * chore: remove hardcode --------- Co-authored-by: eigmax <[email protected]>
1 parent e5ac805 commit 8779f2f

File tree

3 files changed

+5
-36
lines changed

3 files changed

+5
-36
lines changed

examples/zkmips.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn prove_single_seg() {
7979

8080
let allstark: AllStark<F, D> = AllStark::default();
8181
let config = StarkConfig::standard_fast_config();
82-
let mut timing = TimingTree::new("prove", log::Level::Debug);
82+
let mut timing = TimingTree::new("prove", log::Level::Info);
8383
let allproof: proof::AllProof<GoldilocksField, C, D> =
8484
prove(&allstark, &kernel, &config, &mut timing).unwrap();
8585
let mut count_bytes = 0;
@@ -88,6 +88,7 @@ fn prove_single_seg() {
8888
log::info!("row:{} proof bytes:{}", row, proof_str.len());
8989
count_bytes += proof_str.len();
9090
}
91+
timing.filter(Duration::from_millis(100)).print();
9192
log::info!("total proof bytes:{}KB", count_bytes / 1024);
9293
verify_proof(&allstark, allproof, &config).unwrap();
9394
log::info!("Prove done");

src/cpu/bootstrap_kernel.rs

-33
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,6 @@ pub(crate) fn generate_bootstrap_kernel<F: Field>(state: &mut GenerationState<F>
5757

5858
check_image_id(state, kernel);
5959

60-
let mut final_cpu_row = CpuColumnsView::default();
61-
final_cpu_row.clock = F::from_canonical_usize(state.traces.clock());
62-
final_cpu_row.is_bootstrap_kernel = F::ONE;
63-
final_cpu_row.is_keccak_sponge = F::ONE;
64-
65-
let mut image_addr_value_byte_be = vec![0u8; image_addr_value.len() * 4];
66-
for (i, v) in image_addr_value.iter().enumerate() {
67-
image_addr_value_byte_be[i * 4..(i * 4 + 4)].copy_from_slice(&v.to_be_bytes());
68-
}
69-
70-
// The Keccak sponge CTL uses memory value columns for its inputs and outputs.
71-
final_cpu_row.mem_channels[0].value[0] = F::ZERO; // context
72-
final_cpu_row.mem_channels[1].value[0] = F::from_canonical_usize(Segment::Code as usize);
73-
// align with the `already_absorbed_bytes/4` to avoid that the padding block bytes are not present in
74-
// memory
75-
let final_idx = image_addr_value_byte_be.len() / KECCAK_RATE_BYTES * KECCAK_RATE_U32S;
76-
final_cpu_row.mem_channels[2].value[0] = F::from_canonical_usize(image_addr[final_idx].virt);
77-
final_cpu_row.mem_channels[3].value[0] =
78-
F::from_canonical_usize(image_addr_value_byte_be.len()); // len
79-
80-
let code_hash_bytes = keccak(&image_addr_value_byte_be).0;
81-
let code_hash_be = core::array::from_fn(|i| {
82-
u32::from_le_bytes(core::array::from_fn(|j| code_hash_bytes[i * 4 + j]))
83-
});
84-
let code_hash = code_hash_be.map(u32::from_be);
85-
log::info!("code_hash: {:?}", code_hash);
86-
87-
final_cpu_row.mem_channels[4].value = code_hash.map(F::from_canonical_u32);
88-
final_cpu_row.mem_channels[4].value.reverse();
89-
90-
keccak_sponge_log(state, image_addr, image_addr_value_byte_be);
91-
state.traces.push_cpu(final_cpu_row);
92-
9360
state.memory.apply_ops(&state.traces.memory_ops);
9461
log::info!("Bootstrapping took {} cycles", state.traces.clock());
9562
}

src/keccak_sponge/keccak_sponge_stark.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::cpu::kernel::keccak_util::keccakf_u32s;
2020
use crate::cross_table_lookup::Column;
2121
use crate::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
2222
use crate::keccak_sponge::columns::*;
23+
use crate::memory::segments::Segment;
2324
use crate::stark::Stark;
2425
use crate::util::trace_rows_to_poly_values;
2526
use crate::witness::memory::MemoryAddress;
@@ -366,8 +367,8 @@ impl<F: RichField + Extendable<D>, const D: usize> KeccakSpongeStark<F, D> {
366367
virt.resize(KECCAK_RATE_U32S, 0);
367368
let virt: [usize; KECCAK_RATE_U32S] = virt.try_into().unwrap();
368369

369-
row.context = F::from_canonical_usize(op.base_address[idx].context);
370-
row.segment = F::from_canonical_usize(op.base_address[idx].segment);
370+
row.context = F::from_canonical_usize(op.base_address[0].context);
371+
row.segment = F::from_canonical_usize(op.base_address[Segment::Code as usize].segment);
371372
row.virt = virt.map(F::from_canonical_usize);
372373
row.timestamp = F::from_canonical_usize(op.timestamp);
373374
row.len = F::from_canonical_usize(op.input.len());

0 commit comments

Comments
 (0)