Skip to content

Commit 2d0a486

Browse files
committed
generalize ram tag to custom
1 parent 00b3871 commit 2d0a486

6 files changed

Lines changed: 34 additions & 10 deletions

File tree

ceno_zkvm/src/instructions/riscv/ecall/keccak.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<E: ExtensionField> Instruction<E> for KeccakEcallInstruction<E> {
135135

136136
cb.write_record(
137137
|| "keccak_state_in",
138-
RAMType::Undefined,
138+
RAMType::Custom,
139139
keccak_state_record(
140140
vm_state.ts.expr(),
141141
state_ptr_value.expr_unaligned(),
@@ -145,7 +145,7 @@ impl<E: ExtensionField> Instruction<E> for KeccakEcallInstruction<E> {
145145
)?;
146146
cb.read_record(
147147
|| "keccak_state_out",
148-
RAMType::Undefined,
148+
RAMType::Custom,
149149
keccak_state_record(
150150
vm_state.ts.expr(),
151151
state_ptr_value.expr_unaligned(),

ceno_zkvm/src/precompiles/lookup_keccakf.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::{
4646
utils::{Mask, MaskRepresentation, not8_expr, set_slice_felts_from_u64 as push_instance},
4747
},
4848
scheme::utils::gkr_witness,
49-
structs::RAMType,
49+
structs::{CustomRWTag, RAMType},
5050
};
5151

5252
pub const ROUNDS: usize = 24;
@@ -103,7 +103,6 @@ pub const AND_LOOKUPS: usize = AND_LOOKUPS_PER_ROUND;
103103
pub const XOR_LOOKUPS: usize = XOR_LOOKUPS_PER_ROUND;
104104
pub const RANGE_LOOKUPS: usize = RANGE_LOOKUPS_PER_ROUND;
105105
pub const STRUCTURAL_WITIN: usize = 6;
106-
pub const KECCAK_STATE_TAG: u64 = 0x4b4543;
107106
pub const KECCAK_STATE_PHASE_INPUT: u64 = 0;
108107
pub const KECCAK_STATE_PHASE_OUTPUT: u64 = 1;
109108

@@ -173,7 +172,7 @@ pub fn keccak_state_record<E: ExtensionField>(
173172
state: impl IntoIterator<Item = Expression<E>>,
174173
) -> Vec<Expression<E>> {
175174
[
176-
E::BaseField::from_canonical_u64(KECCAK_STATE_TAG).expr(),
175+
CustomRWTag::KeccakState.expr::<E>(),
177176
cycle,
178177
state_ptr,
179178
E::BaseField::from_canonical_u64(phase).expr(),
@@ -557,7 +556,7 @@ impl<E: ExtensionField> ProtocolBuilder<E> for KeccakLayout<E> {
557556

558557
system.read_record(
559558
|| "keccak_state_in",
560-
RAMType::Undefined,
559+
RAMType::Custom,
561560
keccak_state_record(
562561
layout.cycle.expr(),
563562
layout.state_ptr.expr(),
@@ -570,7 +569,7 @@ impl<E: ExtensionField> ProtocolBuilder<E> for KeccakLayout<E> {
570569
)?;
571570
system.write_record(
572571
|| "keccak_state_out",
573-
RAMType::Undefined,
572+
RAMType::Custom,
574573
keccak_state_record(
575574
layout.cycle.expr(),
576575
layout.state_ptr.expr(),

ceno_zkvm/src/precompiles/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ mod weierstrass;
99

1010
pub use lookup_keccakf::{
1111
AND_LOOKUPS, KECCAK_INPUT32_SIZE, KECCAK_OUT_EVAL_SIZE, KECCAK_STATE_PHASE_INPUT,
12-
KECCAK_STATE_PHASE_OUTPUT, KECCAK_STATE_TAG, KeccakInstance, KeccakLayout, KeccakParams,
13-
KeccakStateInstance, KeccakTrace, KeccakWitInstance, RANGE_LOOKUPS, ROUNDS as KECCAK_ROUNDS,
12+
KECCAK_STATE_PHASE_OUTPUT, KeccakInstance, KeccakLayout, KeccakParams, KeccakStateInstance,
13+
KeccakTrace, KeccakWitInstance, RANGE_LOOKUPS, ROUNDS as KECCAK_ROUNDS,
1414
ROUNDS_CEIL_LOG2 as KECCAK_ROUNDS_CEIL_LOG2, XOR_LOOKUPS, keccak_state_record,
1515
run_lookup_keccakf, setup_gkr_circuit as setup_lookup_keccak_gkr_circuit,
1616
};

ceno_zkvm/src/scheme/mock_prover.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,18 @@ Hints:
14981498
gs
14991499
);
15001500

1501+
// part4 custom local buses
1502+
let (custom_rs, rs_grp_by_anno, custom_ws, ws_grp_by_anno, _) =
1503+
derive_ram_rws!(RAMType::Custom);
1504+
find_rw_mismatch!(
1505+
custom_rs,
1506+
rs_grp_by_anno,
1507+
custom_ws,
1508+
ws_grp_by_anno,
1509+
RAMType::Custom,
1510+
gs
1511+
);
1512+
15011513
if num_rw_mismatch_errors > 0 {
15021514
panic!("found {} r/w mismatch errors", num_rw_mismatch_errors);
15031515
}

ceno_zkvm/src/structs.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use gkr_iop::{
1818
};
1919
use itertools::Itertools;
2020
use mpcs::{Point, PolynomialCommitmentScheme};
21-
use multilinear_extensions::Instance;
21+
use multilinear_extensions::{Expression, Instance, ToExpr};
2222
use p3::field::FieldAlgebra;
2323
use poseidon::challenger::{CanObserve, DefaultChallenger, FieldChallenger};
2424
use rayon::{
@@ -110,6 +110,18 @@ pub type ROMType = LookupTable;
110110

111111
pub type RAMType = gkr_iop::RAMType;
112112

113+
#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize)]
114+
#[repr(u16)]
115+
pub enum CustomRWTag {
116+
KeccakState = 0,
117+
}
118+
119+
impl CustomRWTag {
120+
pub fn expr<E: ExtensionField>(self) -> Expression<E> {
121+
E::BaseField::from_canonical_u16(self as u16).expr()
122+
}
123+
}
124+
113125
pub type PointAndEval<F> = multilinear_extensions::mle::PointAndEval<F>;
114126

115127
#[derive(Clone)]

gkr_iop/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ pub enum RAMType {
106106
GlobalState = 0,
107107
Register,
108108
Memory,
109+
Custom,
109110
Undefined,
110111
}
111112

0 commit comments

Comments
 (0)