From 24fd98f28f5ea72d05e6a597be44f6b8218d593c Mon Sep 17 00:00:00 2001 From: "sm.wu" Date: Thu, 11 Jul 2024 16:35:43 +0800 Subject: [PATCH] compiling pass --- gkr/src/structs.rs | 4 ++- multilinear_extensions/src/mle.rs | 27 +++++++++++++++++++ singer/benches/add.rs | 12 ++++----- singer/examples/add.rs | 13 +++++---- singer/examples/push_and_pop.rs | 7 ++--- singer/src/instructions/add.rs | 10 +++---- singer/src/instructions/calldataload.rs | 10 +++---- singer/src/instructions/dup.rs | 9 ++++--- singer/src/instructions/gt.rs | 10 +++---- singer/src/instructions/jump.rs | 10 +++---- singer/src/instructions/jumpdest.rs | 10 +++---- singer/src/instructions/mstore.rs | 36 ++++++++++++------------- singer/src/instructions/pop.rs | 9 ++++--- singer/src/instructions/push.rs | 9 ++++--- singer/src/instructions/ret.rs | 2 +- singer/src/instructions/swap.rs | 10 +++---- singer/src/lib.rs | 12 ++++----- singer/src/scheme.rs | 3 --- singer/src/scheme/prover.rs | 25 ++++++++--------- singer/src/scheme/verifier.rs | 25 ++++++++++------- 20 files changed, 143 insertions(+), 110 deletions(-) diff --git a/gkr/src/structs.rs b/gkr/src/structs.rs index 3cb866bae..8a793725e 100644 --- a/gkr/src/structs.rs +++ b/gkr/src/structs.rs @@ -6,7 +6,9 @@ use std::{ use ff_ext::ExtensionField; use goldilocks::SmallField; use multilinear_extensions::{ - mle::ArcDenseMultilinearExtension, virtual_poly_v2::ArcMultilinearExtension, + mle::{ArcDenseMultilinearExtension, DenseMultilinearExtension}, + util::ceil_log2, + virtual_poly_v2::ArcMultilinearExtension, }; use serde::{Deserialize, Serialize, Serializer}; use simple_frontend::structs::{CellId, ChallengeConst, ConstantType, LayerId}; diff --git a/multilinear_extensions/src/mle.rs b/multilinear_extensions/src/mle.rs index aa1bb7d2a..66505390e 100644 --- a/multilinear_extensions/src/mle.rs +++ b/multilinear_extensions/src/mle.rs @@ -48,6 +48,33 @@ impl Debug for dyn MultilinearExtension Into> for Vec> { + fn into(self) -> DenseMultilinearExtension { + let per_instance_size = self[0].len(); + let next_pow2_per_instance_size = ceil_log2(per_instance_size); + let evaluations = self + .into_iter() + .enumerate() + .map(|(i, mut instance)| { + assert_eq!( + instance.len(), + per_instance_size, + "{}th instance with length {} != {} ", + i, + instance.len(), + per_instance_size + ); + instance.resize(1 << next_pow2_per_instance_size, E::BaseField::ZERO); + instance + }) + .flatten() + .collect::>(); + assert!(evaluations.len().is_power_of_two()); + let num_vars = ceil_log2(evaluations.len()); + DenseMultilinearExtension::from_evaluations_vec(num_vars, evaluations) + } +} + #[derive(Clone, PartialEq, Eq, Hash, Default, Debug, Serialize, Deserialize)] #[serde(untagged)] /// Differentiate inner vector on base/extension field. diff --git a/singer/benches/add.rs b/singer/benches/add.rs index d674f9795..edc8c5e76 100644 --- a/singer/benches/add.rs +++ b/singer/benches/add.rs @@ -8,7 +8,6 @@ use const_env::from_env; use criterion::*; use ff_ext::{ff::Field, ExtensionField}; -use gkr::structs::LayerWitness; use goldilocks::GoldilocksExt2; use itertools::Itertools; @@ -51,7 +50,9 @@ fn bench_add(c: &mut Criterion) { if !is_power_of_2(RAYON_NUM_THREADS) { #[cfg(not(feature = "non_pow2_rayon_thread"))] { - panic!("add --features non_pow2_rayon_thread to enable unsafe feature which support non pow of 2 rayon thread pool"); + panic!( + "add --features non_pow2_rayon_thread to enable unsafe feature which support non pow of 2 rayon thread pool" + ); } #[cfg(feature = "non_pow2_rayon_thread")] @@ -87,8 +88,7 @@ fn bench_add(c: &mut Criterion) { }, |(mut rng,mut singer_builder, real_challenges)| { let size = AddInstruction::phase0_size(); - let phase0: CircuitWiresIn<::BaseField> = vec![LayerWitness { - instances: (0..(1 << instance_num_vars)) + let phase0: CircuitWiresIn = vec![(0..(1 << instance_num_vars)) .map(|_| { (0..size) .map(|_| { @@ -98,8 +98,8 @@ fn bench_add(c: &mut Criterion) { }) .collect_vec() }) - .collect_vec(), - }]; + .collect_vec().into(), + ]; let timer = Instant::now(); diff --git a/singer/examples/add.rs b/singer/examples/add.rs index 4d32f63c2..962e7181f 100644 --- a/singer/examples/add.rs +++ b/singer/examples/add.rs @@ -2,7 +2,6 @@ use std::{collections::BTreeMap, time::Instant}; use ark_std::test_rng; use ff_ext::{ff::Field, ExtensionField}; -use gkr::structs::LayerWitness; use gkr_graph::structs::CircuitGraphAuxInfo; use goldilocks::{Goldilocks, GoldilocksExt2}; use itertools::Itertools; @@ -143,12 +142,12 @@ fn main() { } } - let phase0: CircuitWiresIn<::BaseField> = - vec![LayerWitness { - instances: (0..(1 << instance_num_vars)) - .map(|_| single_witness_in.clone()) - .collect_vec(), - }]; + let phase0: CircuitWiresIn = vec![ + (0..(1 << instance_num_vars)) + .map(|_| single_witness_in.clone()) + .collect_vec() + .into(), + ]; let real_challenges = vec![E::random(&mut rng), E::random(&mut rng)]; diff --git a/singer/examples/push_and_pop.rs b/singer/examples/push_and_pop.rs index 9e1c7963f..0e3dffd88 100644 --- a/singer/examples/push_and_pop.rs +++ b/singer/examples/push_and_pop.rs @@ -23,7 +23,7 @@ fn main() { let real_challenges = vec![]; let singer_params = SingerParams::default(); - let (proof, singer_aux_info) = { + let (proof, singer_aux_info, singer_wire_out_values) = { let real_n_instances = singer_wires_in .instructions .iter() @@ -40,7 +40,7 @@ fn main() { ) .expect("construct failed"); - let (proof, graph_aux_info) = + let (proof, graph_aux_info, singer_wire_out_values) = prove(&circuit, &witness, &wires_out_id, &mut prover_transcript).expect("prove failed"); let aux_info = SingerAuxInfo { graph_aux_info, @@ -49,7 +49,7 @@ fn main() { bytecode_len: bytecode.len(), ..Default::default() }; - (proof, aux_info) + (proof, aux_info, singer_wire_out_values) }; // 4. Verify. @@ -61,6 +61,7 @@ fn main() { verify( &circuit, proof, + singer_wire_out_values, &singer_aux_info, &real_challenges, &mut verifier_transcript, diff --git a/singer/src/instructions/add.rs b/singer/src/instructions/add.rs index 0302f8cd0..3d4f3f368 100644 --- a/singer/src/instructions/add.rs +++ b/singer/src/instructions/add.rs @@ -185,7 +185,6 @@ mod test { use ark_std::test_rng; use ff::Field; use ff_ext::ExtensionField; - use gkr::structs::LayerWitness; use goldilocks::{Goldilocks, GoldilocksExt2}; use itertools::Itertools; use singer_utils::{ @@ -336,15 +335,16 @@ mod test { let mut rng = test_rng(); let size = AddInstruction::phase0_size(); - let phase0: CircuitWiresIn = vec![LayerWitness { - instances: (0..(1 << instance_num_vars)) + let phase0: CircuitWiresIn = vec![ + (0..(1 << instance_num_vars)) .map(|_| { (0..size) .map(|_| E::BaseField::random(&mut rng)) .collect_vec() }) - .collect_vec(), - }]; + .collect_vec() + .into(), + ]; let real_challenges = vec![E::random(&mut rng), E::random(&mut rng)]; diff --git a/singer/src/instructions/calldataload.rs b/singer/src/instructions/calldataload.rs index 5a7a176d9..8dd04654a 100644 --- a/singer/src/instructions/calldataload.rs +++ b/singer/src/instructions/calldataload.rs @@ -153,7 +153,6 @@ mod test { use ark_std::test_rng; use ff::Field; use ff_ext::ExtensionField; - use gkr::structs::LayerWitness; use goldilocks::{Goldilocks, GoldilocksExt2}; use itertools::Itertools; use singer_utils::{constants::RANGE_CHIP_BIT_WIDTH, structs::TSUInt}; @@ -269,15 +268,16 @@ mod test { let mut rng = test_rng(); let size = CalldataloadInstruction::phase0_size(); - let phase0: CircuitWiresIn = vec![LayerWitness { - instances: (0..(1 << instance_num_vars)) + let phase0: CircuitWiresIn = vec![ + (0..(1 << instance_num_vars)) .map(|_| { (0..size) .map(|_| E::BaseField::random(&mut rng)) .collect_vec() }) - .collect_vec(), - }]; + .collect_vec() + .into(), + ]; let real_challenges = vec![E::random(&mut rng), E::random(&mut rng)]; diff --git a/singer/src/instructions/dup.rs b/singer/src/instructions/dup.rs index 700e31557..233bb7928 100644 --- a/singer/src/instructions/dup.rs +++ b/singer/src/instructions/dup.rs @@ -276,15 +276,16 @@ mod test { let mut rng = test_rng(); let size = DupInstruction::::phase0_size(); - let phase0: CircuitWiresIn = vec![LayerWitness { - instances: (0..(1 << instance_num_vars)) + let phase0: CircuitWiresIn = vec![ + (0..(1 << instance_num_vars)) .map(|_| { (0..size) .map(|_| E::BaseField::random(&mut rng)) .collect_vec() }) - .collect_vec(), - }]; + .collect_vec() + .into(), + ]; let real_challenges = vec![E::random(&mut rng), E::random(&mut rng)]; diff --git a/singer/src/instructions/gt.rs b/singer/src/instructions/gt.rs index 857181378..d59403862 100644 --- a/singer/src/instructions/gt.rs +++ b/singer/src/instructions/gt.rs @@ -178,7 +178,6 @@ mod test { use ark_std::test_rng; use ff::Field; use ff_ext::ExtensionField; - use gkr::structs::LayerWitness; use goldilocks::{Goldilocks, GoldilocksExt2}; use itertools::Itertools; use singer_utils::{constants::RANGE_CHIP_BIT_WIDTH, structs::TSUInt}; @@ -304,15 +303,16 @@ mod test { let mut rng = test_rng(); let size = GtInstruction::phase0_size(); - let phase0: CircuitWiresIn = vec![LayerWitness { - instances: (0..(1 << instance_num_vars)) + let phase0: CircuitWiresIn = vec![ + (0..(1 << instance_num_vars)) .map(|_| { (0..size) .map(|_| E::BaseField::random(&mut rng)) .collect_vec() }) - .collect_vec(), - }]; + .collect_vec() + .into(), + ]; let real_challenges = vec![E::random(&mut rng), E::random(&mut rng)]; diff --git a/singer/src/instructions/jump.rs b/singer/src/instructions/jump.rs index dc44bf631..f9d106bd1 100644 --- a/singer/src/instructions/jump.rs +++ b/singer/src/instructions/jump.rs @@ -134,7 +134,6 @@ mod test { use ark_std::test_rng; use ff::Field; use ff_ext::ExtensionField; - use gkr::structs::LayerWitness; use goldilocks::{Goldilocks, GoldilocksExt2}; use itertools::Itertools; use singer_utils::{constants::RANGE_CHIP_BIT_WIDTH, structs::TSUInt}; @@ -219,15 +218,16 @@ mod test { let mut rng = test_rng(); let size = JumpInstruction::phase0_size(); - let phase0: CircuitWiresIn = vec![LayerWitness { - instances: (0..(1 << instance_num_vars)) + let phase0: CircuitWiresIn = vec![ + (0..(1 << instance_num_vars)) .map(|_| { (0..size) .map(|_| E::BaseField::random(&mut rng)) .collect_vec() }) - .collect_vec(), - }]; + .collect_vec() + .into(), + ]; let real_challenges = vec![E::random(&mut rng), E::random(&mut rng)]; diff --git a/singer/src/instructions/jumpdest.rs b/singer/src/instructions/jumpdest.rs index ede7b3e05..9bf5255d3 100644 --- a/singer/src/instructions/jumpdest.rs +++ b/singer/src/instructions/jumpdest.rs @@ -102,7 +102,6 @@ mod test { use ark_std::test_rng; use ff::Field; use ff_ext::ExtensionField; - use gkr::structs::LayerWitness; use goldilocks::{Goldilocks, GoldilocksExt2}; use itertools::Itertools; use std::{collections::BTreeMap, time::Instant}; @@ -174,15 +173,16 @@ mod test { let mut rng = test_rng(); let size = JumpdestInstruction::phase0_size(); - let phase0: CircuitWiresIn = vec![LayerWitness { - instances: (0..(1 << instance_num_vars)) + let phase0: CircuitWiresIn = vec![ + (0..(1 << instance_num_vars)) .map(|_| { (0..size) .map(|_| E::BaseField::random(&mut rng)) .collect_vec() }) - .collect_vec(), - }]; + .collect_vec() + .into(), + ]; let real_challenges = vec![E::random(&mut rng), E::random(&mut rng)]; diff --git a/singer/src/instructions/mstore.rs b/singer/src/instructions/mstore.rs index e8d7fcfe1..890723cca 100644 --- a/singer/src/instructions/mstore.rs +++ b/singer/src/instructions/mstore.rs @@ -38,7 +38,7 @@ impl InstructionGraph for MstoreInstruction { graph_builder: &mut CircuitGraphBuilder, chip_builder: &mut SingerChipBuilder, inst_circuits: &[InstCircuit], - mut sources: Vec>, + mut sources: Vec>, real_challenges: &[E], real_n_instances: usize, _: &SingerParams, @@ -384,9 +384,9 @@ mod test { use ark_std::test_rng; use ff::Field; use ff_ext::ExtensionField; - use gkr::structs::LayerWitness; use goldilocks::GoldilocksExt2; use itertools::Itertools; + use multilinear_extensions::mle::DenseMultilinearExtension; use singer_utils::structs::ChipChallenges; use std::time::Instant; use transcript::Transcript; @@ -508,28 +508,28 @@ mod test { let mut rng = test_rng(); let inst_phase0_size = MstoreInstruction::phase0_size(); - let inst_wit: CircuitWiresIn = vec![LayerWitness { - instances: (0..(1 << instance_num_vars)) + let inst_wit: CircuitWiresIn = vec![ + (0..(1 << instance_num_vars)) .map(|_| { (0..inst_phase0_size) .map(|_| E::BaseField::random(&mut rng)) .collect_vec() }) - .collect_vec(), - }]; + .collect_vec() + .into(), + ]; let acc_phase0_size = MstoreAccessory::phase0_size(); - let acc_wit: CircuitWiresIn = vec![ - LayerWitness { instances: vec![] }, - LayerWitness { instances: vec![] }, - LayerWitness { - instances: (0..(1 << instance_num_vars) * 32) - .map(|_| { - (0..acc_phase0_size) - .map(|_| E::BaseField::random(&mut rng)) - .collect_vec() - }) - .collect_vec(), - }, + let acc_wit: CircuitWiresIn = vec![ + DenseMultilinearExtension::default(), + DenseMultilinearExtension::default(), + (0..(1 << instance_num_vars) * 32) + .map(|_| { + (0..acc_phase0_size) + .map(|_| E::BaseField::random(&mut rng)) + .collect_vec() + }) + .collect_vec() + .into(), ]; let real_challenges = vec![E::random(&mut rng), E::random(&mut rng)]; diff --git a/singer/src/instructions/pop.rs b/singer/src/instructions/pop.rs index da34c2a99..0652e0ef9 100644 --- a/singer/src/instructions/pop.rs +++ b/singer/src/instructions/pop.rs @@ -233,15 +233,16 @@ mod test { let mut rng = test_rng(); let size = PopInstruction::phase0_size(); - let phase0: CircuitWiresIn = vec![LayerWitness { - instances: (0..(1 << instance_num_vars)) + let phase0: CircuitWiresIn = vec![ + (0..(1 << instance_num_vars)) .map(|_| { (0..size) .map(|_| E::BaseField::random(&mut rng)) .collect_vec() }) - .collect_vec(), - }]; + .collect_vec() + .into(), + ]; let real_challenges = vec![E::random(&mut rng), E::random(&mut rng)]; diff --git a/singer/src/instructions/push.rs b/singer/src/instructions/push.rs index 3eb2fad05..54531faa0 100644 --- a/singer/src/instructions/push.rs +++ b/singer/src/instructions/push.rs @@ -242,15 +242,16 @@ mod test { let mut rng = test_rng(); let size = PushInstruction::::phase0_size(); - let phase0: CircuitWiresIn = vec![LayerWitness { - instances: (0..(1 << instance_num_vars)) + let phase0: CircuitWiresIn = vec![ + (0..(1 << instance_num_vars)) .map(|_| { (0..size) .map(|_| E::BaseField::random(&mut rng)) .collect_vec() }) - .collect_vec(), - }]; + .collect_vec() + .into(), + ]; let real_challenges = vec![E::random(&mut rng), E::random(&mut rng)]; diff --git a/singer/src/instructions/ret.rs b/singer/src/instructions/ret.rs index 4215564fe..a7fd9b9c7 100644 --- a/singer/src/instructions/ret.rs +++ b/singer/src/instructions/ret.rs @@ -52,7 +52,7 @@ impl InstructionGraph for ReturnInstruction { graph_builder: &mut CircuitGraphBuilder, chip_builder: &mut SingerChipBuilder, inst_circuits: &[InstCircuit], - mut sources: Vec>, + mut sources: Vec>, real_challenges: &[E], _: usize, params: &SingerParams, diff --git a/singer/src/instructions/swap.rs b/singer/src/instructions/swap.rs index c1f00cf49..8854fcb94 100644 --- a/singer/src/instructions/swap.rs +++ b/singer/src/instructions/swap.rs @@ -183,7 +183,6 @@ mod test { use ark_std::test_rng; use ff::Field; use ff_ext::ExtensionField; - use gkr::structs::LayerWitness; use goldilocks::{Goldilocks, GoldilocksExt2}; use itertools::Itertools; use singer_utils::{constants::RANGE_CHIP_BIT_WIDTH, structs::TSUInt}; @@ -325,15 +324,16 @@ mod test { let mut rng = test_rng(); let size = SwapInstruction::::phase0_size(); - let phase0: CircuitWiresIn = vec![LayerWitness { - instances: (0..(1 << instance_num_vars)) + let phase0: CircuitWiresIn = vec![ + (0..(1 << instance_num_vars)) .map(|_| { (0..size) .map(|_| E::BaseField::random(&mut rng)) .collect_vec() }) - .collect_vec(), - }]; + .collect_vec() + .into(), + ]; let real_challenges = vec![E::random(&mut rng), E::random(&mut rng)]; diff --git a/singer/src/lib.rs b/singer/src/lib.rs index f3e9200bf..b25f2b91f 100644 --- a/singer/src/lib.rs +++ b/singer/src/lib.rs @@ -198,13 +198,13 @@ pub struct SingerWiresOutID { } #[derive(Clone)] -pub struct SingerWiresOutValues { - ram_load: Vec>, - ram_store: Vec>, - rom_input: Vec>, - rom_table: Vec>, +pub struct SingerWiresOutValues<'a, E: ExtensionField> { + ram_load: Vec>, + ram_store: Vec>, + rom_input: Vec>, + rom_table: Vec>, - public_output_size: Option>, + public_output_size: Option>, } impl SingerWiresOutID { diff --git a/singer/src/scheme.rs b/singer/src/scheme.rs index 18c776e59..1b26cdd71 100644 --- a/singer/src/scheme.rs +++ b/singer/src/scheme.rs @@ -1,7 +1,5 @@ use ff_ext::ExtensionField; -use crate::SingerWiresOutValues; - // TODO: to be changed to a real PCS scheme. type BatchedPCSProof = Vec>; type Commitment = Vec; @@ -25,5 +23,4 @@ pub struct SingerProof { // commitment_phase_proof: CommitPhaseProof, gkr_phase_proof: GKRGraphProof, // open_phase_proof: OpenPhaseProof, - singer_out_evals: SingerWiresOutValues, } diff --git a/singer/src/scheme/prover.rs b/singer/src/scheme/prover.rs index c121ac5dc..3d2ccdef9 100644 --- a/singer/src/scheme/prover.rs +++ b/singer/src/scheme/prover.rs @@ -1,11 +1,7 @@ -use std::mem; - use ff_ext::ExtensionField; use gkr_graph::structs::{CircuitGraphAuxInfo, NodeOutputType}; use itertools::Itertools; -use multilinear_extensions::{ - mle::DenseMultilinearExtension, virtual_poly_v2::ArcMultilinearExtension, -}; +use multilinear_extensions::virtual_poly_v2::ArcMultilinearExtension; use transcript::Transcript; use crate::{ @@ -19,7 +15,14 @@ pub fn prove<'a, E: ExtensionField>( vm_witness: &SingerWitness<'a, E>, vm_out_id: &SingerWiresOutID, transcript: &mut Transcript, -) -> Result<(SingerProof, CircuitGraphAuxInfo), ZKVMError> { +) -> Result< + ( + SingerProof, + CircuitGraphAuxInfo, + SingerWiresOutValues<'a, E>, + ), + ZKVMError, +> { // TODO: Add PCS. let point = (0..2 * ::DEGREE) .map(|_| { @@ -56,7 +59,7 @@ pub fn prove<'a, E: ExtensionField>( rom_table, public_output_size: vm_out_id .public_output_size - .map(|node| mem::take(&mut target_wits(&[node])[0])), + .map(|node| target_wits(&[node])[0].clone()), } }; @@ -72,11 +75,5 @@ pub fn prove<'a, E: ExtensionField>( let target_evals = vm_circuit.0.target_evals(&vm_witness.0, &point); let gkr_phase_proof = GKRGraphProverState::prove(&vm_circuit.0, &vm_witness.0, &target_evals, transcript, 1)?; - Ok(( - SingerProof { - gkr_phase_proof, - singer_out_evals, - }, - aux_info, - )) + Ok((SingerProof { gkr_phase_proof }, aux_info, singer_out_evals)) } diff --git a/singer/src/scheme/verifier.rs b/singer/src/scheme/verifier.rs index a5b5967e8..024affc64 100644 --- a/singer/src/scheme/verifier.rs +++ b/singer/src/scheme/verifier.rs @@ -9,9 +9,10 @@ use crate::{error::ZKVMError, SingerAuxInfo, SingerCircuit, SingerWiresOutValues use super::{GKRGraphVerifierState, SingerProof}; -pub fn verify( +pub fn verify<'a, E: ExtensionField>( vm_circuit: &SingerCircuit, vm_proof: SingerProof, + singer_out_evals: SingerWiresOutValues<'a, E>, aux_info: &SingerAuxInfo, challenges: &[E], transcript: &mut Transcript, @@ -31,10 +32,16 @@ pub fn verify( rom_input, rom_table, public_output_size, - } = vm_proof.singer_out_evals; + } = singer_out_evals; - let ram_load_product: E = ram_load.iter().map(|x| E::from_limbs(&x)).product(); - let ram_store_product = ram_store.iter().map(|x| E::from_limbs(&x)).product(); + let ram_load_product: E = ram_load + .iter() + .map(|x| E::from_limbs(x.get_base_field_vec())) + .product(); + let ram_store_product = ram_store + .iter() + .map(|x| E::from_limbs(x.get_base_field_vec())) + .product(); if ram_load_product != ram_store_product { return Err(ZKVMError::VerifyError); } @@ -74,14 +81,14 @@ pub fn verify( .collect_vec(), ); - if let Some(output) = public_output_size { - let f = vec![output.to_vec()].as_slice().original_mle(); + if let Some(output) = &public_output_size { + let f = output; target_evals.0.push(PointAndEval::new( - point[..f.num_vars].to_vec(), - f.evaluate(&point[..f.num_vars]), + point[..f.num_vars()].to_vec(), + f.evaluate(&point[..f.num_vars()]), )); assert_eq!( - output[0], + output.get_base_field_vec()[0], E::BaseField::from(aux_info.program_output_len as u64) ) }