Skip to content

Commit 0ccd1ec

Browse files
committed
debug
1 parent ee54156 commit 0ccd1ec

File tree

10 files changed

+96
-51
lines changed

10 files changed

+96
-51
lines changed

ceno_zkvm/src/instructions.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ where
9797
num_witin: usize,
9898
steps: Vec<StepRecord>,
9999
gkr_layout: &mut Self::Layout,
100-
) -> Result<(RowMajorMatrix<E::BaseField>, LkMultiplicity), ZKVMError> {
100+
) -> Result<
101+
(
102+
RowMajorMatrix<E::BaseField>,
103+
GKRCircuitWitness<E>,
104+
LkMultiplicity,
105+
),
106+
ZKVMError,
107+
> {
101108
let nthreads = max_usable_threads();
102109
let num_instance_per_batch = if steps.len() > 256 {
103110
steps.len().div_ceil(nthreads)
@@ -163,6 +170,6 @@ where
163170
.collect::<Result<(), ZKVMError>>()?;
164171

165172
raw_witin.padding_by_strategy();
166-
Ok((raw_witin, lk_multiplicity))
173+
Ok((raw_witin, gkr_witness, lk_multiplicity))
167174
}
168175
}

ceno_zkvm/src/instructions/riscv/dummy/dummy_ecall.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<E: ExtensionField, S: SyscallSpec> Instruction<E> for LargeEcallDummy<E, S>
7979
.collect::<Result<Vec<_>, _>>()?;
8080

8181
// Temporarily set this to < 24 to avoid cb.num_witin overflow
82-
let active_rounds = 0;
82+
let active_rounds = 12;
8383

8484
let mut lookups = Vec::with_capacity(
8585
active_rounds
@@ -187,7 +187,7 @@ impl<E: ExtensionField> GKRIOPInstruction<E> for LargeEcallDummy<E, KeccakSpec>
187187
) -> Result<(), ZKVMError> {
188188
Self::assign_instance(config, instance, lk_multiplicity, step)?;
189189

190-
let active_rounds = 0;
190+
let active_rounds = 12;
191191
let mut wit_iter = lookups.iter().map(|f| f.to_canonical_u64());
192192
let mut var_iter = config.lookups.iter();
193193

@@ -197,28 +197,29 @@ impl<E: ExtensionField> GKRIOPInstruction<E> for LargeEcallDummy<E, KeccakSpec>
197197
let mut pop_arg = || -> u64 {
198198
let wit = wit_iter.next().unwrap();
199199
let var = var_iter.next().unwrap();
200-
// set_val!(instance, var, wit);
201-
set_val!(instance, var, 0);
200+
set_val!(instance, var, wit);
201+
// set_val!(instance, var, 0);
202202
wit
203203
};
204204

205205
for _round in 0..active_rounds {
206206
for _i in 0..AND_LOOKUPS_PER_ROUND {
207-
// lk_multiplicity.lookup_and_byte(pop_arg(), pop_arg());
208-
lk_multiplicity.lookup_and_byte(0, 0);
209-
pop_arg();
210-
pop_arg();
207+
lk_multiplicity.lookup_and_byte(pop_arg(), pop_arg());
208+
// lk_multiplicity.lookup_and_byte(0, 0);
209+
// pop_arg();
210+
// pop_arg();
211211
pop_arg();
212212
}
213213
for _i in 0..XOR_LOOKUPS_PER_ROUND {
214-
lk_multiplicity.lookup_xor_byte(0, 0);
215-
pop_arg();
216-
pop_arg();
214+
lk_multiplicity.lookup_xor_byte(pop_arg(), pop_arg());
215+
// lk_multiplicity.lookup_xor_byte(0, 0);
216+
// pop_arg();
217+
// pop_arg();
217218
pop_arg();
218219
}
219220
for _i in 0..RANGE_LOOKUPS_PER_ROUND {
220-
lk_multiplicity.assert_ux::<16>(0);
221-
pop_arg();
221+
lk_multiplicity.assert_ux::<16>(pop_arg());
222+
// pop_arg();
222223
}
223224
}
224225

ceno_zkvm/src/instructions/riscv/rv32im.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ pub struct DummyExtraConfig<E: ExtensionField> {
466466
impl<E: ExtensionField> DummyExtraConfig<E> {
467467
pub fn construct_circuits(cs: &mut ZKVMConstraintSystem<E>) -> Self {
468468
let ecall_config = cs.register_opcode_circuit::<EcallDummy<E>>();
469+
// let keccak_config = cs.register_opcode_circuit::<LargeEcallDummy<E, KeccakSpec>>();
469470
let keccak_config = cs.register_keccakf_circuit();
470471
let secp256k1_add_config =
471472
cs.register_opcode_circuit::<LargeEcallDummy<E, Secp256k1AddSpec>>();
@@ -510,9 +511,10 @@ impl<E: ExtensionField> DummyExtraConfig<E> {
510511
) {
511512
fixed.register_opcode_circuit::<EcallDummy<E>>(cs);
512513
fixed.register_keccakf_circuit(cs);
514+
// fixed.register_opcode_circuit::<LargeEcallDummy<E, KeccakSpec>>(cs);
513515
fixed.register_opcode_circuit::<LargeEcallDummy<E, Secp256k1AddSpec>>(cs);
514-
fixed.register_opcode_circuit::<LargeEcallDummy<E, Secp256k1DoubleSpec>>(cs);
515516
fixed.register_opcode_circuit::<LargeEcallDummy<E, Secp256k1DecompressSpec>>(cs);
517+
fixed.register_opcode_circuit::<LargeEcallDummy<E, Secp256k1DoubleSpec>>(cs);
516518
fixed.register_opcode_circuit::<LargeEcallDummy<E, Sha256ExtendSpec>>(cs);
517519
fixed.register_opcode_circuit::<LargeEcallDummy<E, Bn254AddSpec>>(cs);
518520
fixed.register_opcode_circuit::<LargeEcallDummy<E, Bn254DoubleSpec>>(cs);
@@ -564,6 +566,12 @@ impl<E: ExtensionField> DummyExtraConfig<E> {
564566

565567
witness.assign_keccakf_circuit(cs, &self.keccak_config, keccak_steps)?;
566568

569+
// witness.assign_opcode_circuit::<LargeEcallDummy<E, KeccakSpec>>(
570+
// cs,
571+
// &self.keccak_config,
572+
// keccak_steps,
573+
//)?;
574+
567575
witness.assign_opcode_circuit::<LargeEcallDummy<E, Secp256k1AddSpec>>(
568576
cs,
569577
&self.secp256k1_add_config,

ceno_zkvm/src/scheme/prover.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use ceno_emul::KeccakSpec;
22
use ff_ext::ExtensionField;
3+
use gkr_iop::gkr::GKRCircuitWitness;
34
use std::{
45
collections::{BTreeMap, BTreeSet, HashMap},
56
sync::Arc,
@@ -99,6 +100,7 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
99100
let mut commitments = BTreeMap::new();
100101
let mut wits = BTreeMap::new();
101102
let mut structural_wits = BTreeMap::new();
103+
let mut keccak_gkr_wit = Some(witnesses.keccak_gkr_wit.clone());
102104

103105
let commit_to_traces_span = entered_span!("commit_to_traces", profiling_1 = true);
104106
// commit to opcode circuits first and then commit to table circuits, sorted by name
@@ -180,10 +182,7 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
180182
&& cs.r_table_expressions.is_empty()
181183
&& cs.w_table_expressions.is_empty();
182184

183-
if *circuit_name == <LargeEcallDummy<E, KeccakSpec> as Instruction<E>>::name() {
184-
// unimplemented!("keccak impl wip");
185-
()
186-
} else if is_opcode_circuit {
185+
if is_opcode_circuit {
187186
tracing::debug!(
188187
"opcode circuit {} has {} witnesses, {} reads, {} writes, {} lookups",
189188
circuit_name,
@@ -197,7 +196,7 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
197196
let gkr_iop_pk = if *circuit_name
198197
== <LargeEcallDummy<E, KeccakSpec> as Instruction<E>>::name()
199198
{
200-
Some(self.pk.keccak_pk.clone())
199+
Some((self.pk.keccak_pk.clone(), keccak_gkr_wit.take().unwrap()))
201200
} else {
202201
None
203202
};
@@ -266,7 +265,10 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
266265
name: &str,
267266
pp: &PCS::ProverParam,
268267
circuit_pk: &ProvingKey<E, PCS>,
269-
gkr_iop_pk: &Option<GKRIOPProvingKey<E, PCS, KeccakGKRIOP<E>>>,
268+
gkr_iop_pk: &Option<(
269+
GKRIOPProvingKey<E, PCS, KeccakGKRIOP<E>>,
270+
GKRCircuitWitness<E>,
271+
)>,
270272
witnesses: Vec<ArcMultilinearExtension<'_, E>>,
271273
wits_commit: PCS::CommitmentWithWitness,
272274
pi: &[ArcMultilinearExtension<'_, E>],
@@ -279,11 +281,6 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
279281
let log2_num_instances = ceil_log2(next_pow2_instances);
280282
let (chip_record_alpha, _) = (challenges[0], challenges[1]);
281283

282-
// if let Some(gkr_iop_pk) = gkr_iop_pk {
283-
// let mut gkr_iop_pk = gkr_iop_pk.clone();
284-
// unimplemented!("cannot fully handle GKRIOP component yet")
285-
// }
286-
287284
// sanity check
288285
assert_eq!(witnesses.len(), cs.num_witin as usize);
289286
assert!(
@@ -609,6 +606,18 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
609606
}
610607
}
611608

609+
if let Some((gkr_iop_pk, gkr_wit)) = gkr_iop_pk {
610+
let mut gkr_iop_pk = gkr_iop_pk.clone();
611+
let gkr_circuit = gkr_iop_pk.vk.get_state().chip.gkr_circuit();
612+
613+
let mut prover_transcript = transcript::BasicTranscript::<E>::new(b"protocol");
614+
615+
let gkr_iop::gkr::GKRProverOutput { gkr_proof, .. } = gkr_circuit
616+
.prove(wit, &out_evals, &vec![], &mut prover_transcript)
617+
.expect("Failed to prove phase");
618+
// unimplemented!("cannot fully handle GKRIOP component yet")
619+
}
620+
612621
tracing::debug!("main sel sumcheck start");
613622
let (main_sel_sumcheck_proofs, state) = IOPProverState::prove_batch_polys(
614623
num_threads,
@@ -653,6 +662,12 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
653662
distrinct_zerocheck_terms_set.len() + 1
654663
}
655664
);
665+
666+
// if let Some(gkr_iop_pk) = gkr_iop_pk {
667+
// let mut gkr_iop_pk = gkr_iop_pk.clone();
668+
// unimplemented!("cannot fully handle GKRIOP component yet")
669+
// }
670+
656671
let input_open_point = main_sel_sumcheck_proofs.point.clone();
657672
assert!(input_open_point.len() == log2_num_instances);
658673
exit_span!(main_sel_span);

ceno_zkvm/src/structs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use ceno_emul::{CENO_PLATFORM, KeccakSpec, Platform, StepRecord};
1111
use ff_ext::ExtensionField;
1212
use gkr_iop::{
1313
ProtocolWitnessGenerator,
14+
gkr::GKRCircuitWitness,
1415
precompiles::{KeccakLayout, KeccakTrace},
1516
};
1617
use itertools::{Itertools, chain};
@@ -343,7 +344,7 @@ impl<E: ExtensionField> ZKVMFixedTraces<E> {
343344

344345
#[derive(Default, Clone)]
345346
pub struct ZKVMWitnesses<E: ExtensionField> {
346-
keccak_phase1wit: Vec<Vec<E::BaseField>>,
347+
pub keccak_gkr_wit: GKRCircuitWitness<E>,
347348
witnesses_opcodes: BTreeMap<String, RowMajorMatrix<E::BaseField>>,
348349
witnesses_tables: BTreeMap<String, RMMCollections<E::BaseField>>,
349350
lk_mlts: BTreeMap<String, LkMultiplicity>,
@@ -374,13 +375,14 @@ impl<E: ExtensionField> ZKVMWitnesses<E> {
374375
let cs = css
375376
.get_cs(&LargeEcallDummy::<E, KeccakSpec>::name())
376377
.unwrap();
377-
let (witness, logup_multiplicity) =
378+
let (witness, gkr_witness, logup_multiplicity) =
378379
LargeEcallDummy::<E, KeccakSpec>::assign_instances_with_gkr_iop(
379380
config,
380381
cs.num_witin as usize,
381382
records,
382383
&mut css.keccak_gkr_iop.layout,
383384
)?;
385+
self.keccak_gkr_wit = gkr_witness;
384386

385387
// // Intercept row-major matrix, convert into KeccakTrace and obtain phase1_wit
386388
// self.keccak_phase1wit = css

gkr_iop/src/gkr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct GKRCircuit {
2222
pub ext_openings: Vec<(usize, EvalExpression)>,
2323
}
2424

25-
#[derive(Clone, Debug)]
25+
#[derive(Clone, Debug, Default)]
2626
pub struct GKRCircuitWitness<E: ExtensionField> {
2727
pub layers: Vec<LayerWitness<E>>,
2828
}
@@ -85,7 +85,7 @@ impl GKRCircuit {
8585
let mut challenges = challenges.to_vec();
8686
let mut evaluations = out_evals.to_vec();
8787
evaluations.resize(self.n_evaluations, PointAndEval::default());
88-
for (layer, layer_proof) in izip!(self.layers.clone(), sumcheck_proofs) {
88+
for (layer, layer_proof) in izip!(&self.layers, sumcheck_proofs) {
8989
layer.verify(layer_proof, &mut evaluations, &mut challenges, transcript)?;
9090
}
9191

gkr_iop/src/gkr/layer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct Layer {
5353
pub expr_names: Vec<String>,
5454
}
5555

56-
#[derive(Clone, Debug)]
56+
#[derive(Clone, Debug, Default)]
5757
pub struct LayerWitness<E: ExtensionField> {
5858
pub bases: Vec<Vec<E::BaseField>>,
5959
pub exts: Vec<Vec<E>>,

gkr_iop/src/precompiles/faster_keccak.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ pub const AND_LOOKUPS_PER_ROUND: usize = 200;
314314
pub const XOR_LOOKUPS_PER_ROUND: usize = 608;
315315
pub const RANGE_LOOKUPS_PER_ROUND: usize = 290;
316316
pub const LOOKUPS_PER_ROUND: usize =
317+
AND_LOOKUPS_PER_ROUND + XOR_LOOKUPS_PER_ROUND + RANGE_LOOKUPS_PER_ROUND;
318+
pub const LOOKUP_FELTS_PER_ROUND: usize =
317319
3 * AND_LOOKUPS_PER_ROUND + 3 * XOR_LOOKUPS_PER_ROUND + RANGE_LOOKUPS_PER_ROUND;
318320

319321
pub const AND_LOOKUPS: usize = ROUNDS * AND_LOOKUPS_PER_ROUND;
@@ -348,14 +350,15 @@ impl<E: ExtensionField> ProtocolBuilder for KeccakLayout<E> {
348350

349351
fn build_gkr_phase(&mut self, chip: &mut Chip) {
350352
let final_outputs =
351-
chip.allocate_output_evals::<{ KECCAK_OUTPUT_SIZE + KECCAK_INPUT_SIZE + LOOKUPS_PER_ROUND * ROUNDS }>();
353+
chip.allocate_output_evals::<{ KECCAK_OUTPUT_SIZE + KECCAK_INPUT_SIZE + LOOKUP_FELTS_PER_ROUND * ROUNDS }>();
352354

353355
let mut final_outputs_iter = final_outputs.iter();
354356

355357
let [keccak_output32, keccak_input32, lookup_outputs] = [
356358
KECCAK_OUTPUT_SIZE,
357359
KECCAK_INPUT_SIZE,
358-
LOOKUPS_PER_ROUND * ROUNDS,
360+
//LOOKUPS_PER_ROUND
361+
LOOKUP_FELTS_PER_ROUND * ROUNDS,
359362
]
360363
.map(|many| final_outputs_iter.by_ref().take(many).collect_vec());
361364

@@ -656,12 +659,18 @@ impl<E: ExtensionField> ProtocolBuilder for KeccakLayout<E> {
656659
let beta = Constant::Base(0);
657660

658661
// Send all lookups to the final output layer
662+
// for (i, lookup) in chain!(and_lookups, xor_lookups, range_lookups)
663+
// .flatten()
664+
// .enumerate()
665+
// {
666+
// expressions.push(lookup.clone());
659667
for (i, lookup) in chain!(and_lookups, xor_lookups, range_lookups)
660668
.flatten()
661669
.enumerate()
662670
{
663-
expressions.push(lookup.clone());
664-
expr_names.push(format!("{i}th: {:?}", lookup));
671+
expressions.push(lookup);
672+
//expressions.push(lookup.compress(alpha.clone(), beta.clone()));
673+
expr_names.push(format!("{i}th: aha"));
665674
evals.push(lookup_outputs[lookup_index].clone());
666675
lookup_index += 1;
667676
}
@@ -686,7 +695,7 @@ impl<E: ExtensionField> ProtocolBuilder for KeccakLayout<E> {
686695
},
687696
);
688697

689-
assert!(lookup_index == LOOKUPS_PER_ROUND * ROUNDS);
698+
assert!(lookup_index == LOOKUP_FELTS_PER_ROUND * ROUNDS);
690699

691700
let (state8, _) = chip.allocate_wits_in_layer::<200, 0>();
692701

@@ -743,16 +752,6 @@ pub struct KeccakTrace {
743752
pub instances: Vec<[u32; KECCAK_INPUT_SIZE]>,
744753
}
745754

746-
use p3_field::Field;
747-
748-
impl<F: Field> From<RowMajorMatrix<F>> for KeccakTrace {
749-
fn from(rmm: RowMajorMatrix<F>) -> Self {
750-
// clarify rmm encoding for large_ecall_dummy to obtain input
751-
//unimplemented!();
752-
KeccakTrace { instances: vec![] }
753-
}
754-
}
755-
756755
impl<E> ProtocolWitnessGenerator<E> for KeccakLayout<E>
757756
where
758757
E: ExtensionField,
@@ -842,8 +841,9 @@ where
842841
if layer_wits[curr_layer].bases.is_empty() {
843842
layer_wits[curr_layer] = LayerWitness::new(nest::<E>(&felts), vec![]);
844843
} else {
845-
for (i, bases) in layer_wits[curr_layer].bases.iter_mut().enumerate() {
846-
bases.push(felts[i]);
844+
assert_eq!(felts.len(), layer_wits[curr_layer].bases.len());
845+
for (i, base) in layer_wits[curr_layer].bases.iter_mut().enumerate() {
846+
base.push(felts[i]);
847847
}
848848
}
849849
curr_layer += 1;
@@ -1065,6 +1065,8 @@ where
10651065
);
10661066
}
10671067

1068+
dbg!(layer_wits.len());
1069+
10681070
let len = layer_wits.len() - 1;
10691071
layer_wits[..len].reverse();
10701072

@@ -1075,7 +1077,6 @@ where
10751077
pub fn run_faster_keccakf(states: Vec<[u64; 25]>, verify: bool, test: bool) -> () {
10761078
let params = KeccakParams {};
10771079
let (layout, chip) = KeccakLayout::build(params);
1078-
let gkr_circuit = chip.gkr_circuit();
10791080

10801081
let mut instances = vec![];
10811082
for state in states {
@@ -1094,6 +1095,7 @@ pub fn run_faster_keccakf(states: Vec<[u64; 25]>, verify: bool, test: bool) -> (
10941095
);
10951096
}
10961097

1098+
dbg!(instances.len());
10971099
let phase1_witness = layout.phase1_witness(KeccakTrace { instances });
10981100

10991101
let mut prover_transcript = BasicTranscript::<E>::new(b"protocol");
@@ -1139,6 +1141,11 @@ pub fn run_faster_keccakf(states: Vec<[u64; 25]>, verify: bool, test: bool) -> (
11391141
// }
11401142

11411143
let len = final_output1.len();
1144+
assert_eq!(
1145+
len,
1146+
KECCAK_INPUT_SIZE + KECCAK_OUTPUT_SIZE + LOOKUP_FELTS_PER_ROUND * ROUNDS
1147+
);
1148+
assert!(final_output1.len() == final_output2.len());
11421149
let gkr_outputs = chain!(
11431150
zip(final_output1, once(point1).cycle().take(len)),
11441151
zip(final_output2, once(point2).cycle().take(len))
@@ -1152,6 +1159,8 @@ pub fn run_faster_keccakf(states: Vec<[u64; 25]>, verify: bool, test: bool) -> (
11521159
.collect_vec()
11531160
};
11541161

1162+
let gkr_circuit = chip.gkr_circuit();
1163+
dbg!(&gkr_circuit.layers.len());
11551164
let GKRProverOutput { gkr_proof, .. } = gkr_circuit
11561165
.prove(gkr_witness, &out_evals, &vec![], &mut prover_transcript)
11571166
.expect("Failed to prove phase");

0 commit comments

Comments
 (0)