Skip to content

Commit

Permalink
add verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Jul 29, 2024
1 parent 5eb9787 commit 7676067
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 20 deletions.
18 changes: 14 additions & 4 deletions ceno_zkvm/src/instructions/riscv/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ mod test {
use ark_std::test_rng;
use ff::Field;
use ff_ext::ExtensionField;
use gkr::util::ceil_log2;
use gkr::{structs::PointAndEval, util::ceil_log2};
use goldilocks::{Goldilocks, GoldilocksExt2};
use multilinear_extensions::mle::DenseMultilinearExtension;
use simple_frontend::structs::WitnessId;
use singer_utils::{structs_v2::CircuitBuilderV2, util_v2::InstructionV2};
use transcript::Transcript;

use crate::scheme::prover::ZKVMProver;
use crate::scheme::{prover::ZKVMProver, verifier::ZKVMVerifier};

use super::AddInstruction;

Expand Down Expand Up @@ -154,14 +154,24 @@ mod test {
});

// get proof
let prover = ZKVMProver::new(circuit);
let prover = ZKVMProver::new(circuit.clone()); // circuit clone due to verifier alos need circuit reference
let mut transcript = Transcript::new(b"riscv");
let challenges = vec![1.into(), 2.into()];

let _ = prover
let proof = prover
.create_proof(wits_in, num_instances, &mut transcript, &challenges)
.expect("create_proof failed");

let verifier = ZKVMVerifier::new(circuit);
let mut v_transcript = Transcript::new(b"riscv");
verifier
.verify(
&proof,
&mut v_transcript,
&PointAndEval::default(),
&challenges,
)
.expect("verifier failed");
// println!("circuit_builder {:?}", circuit_builder);
}

Expand Down
9 changes: 8 additions & 1 deletion ceno_zkvm/src/scheme.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
use ff_ext::ExtensionField;
use gkr::structs::PointAndEval;

pub mod prover;
pub mod verifier;

pub struct ZKVMProof {}
#[derive(Clone)]
pub struct ZKVMProof<E: ExtensionField> {
pub input_point_and_evals: Vec<PointAndEval<E>>,
}
58 changes: 43 additions & 15 deletions ceno_zkvm/src/scheme/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::error::ZKVMError;

use super::ZKVMProof;

const MIN_PAR_SIZE: usize = 64;
pub struct ZKVMProver<E: ExtensionField> {
circuit: Circuit<E>,
}
Expand All @@ -34,7 +35,7 @@ impl<E: ExtensionField> ZKVMProver<E> {
num_instances: usize,
_transcript: &mut Transcript<E>,
challenges: &[E],
) -> Result<ZKVMProof, ZKVMError> {
) -> Result<ZKVMProof<E>, ZKVMError> {
let circuit = &self.circuit;
let log2_instances = ceil_log2(num_instances);
let next_pow2_instances = 1 << log2_instances;
Expand All @@ -45,8 +46,8 @@ impl<E: ExtensionField> ZKVMProver<E> {
v.num_vars() == log2_instances && v.evaluations().len() == next_pow2_instances
});

// main constraint: read record witness inference
let span = entered_span!("inference::read_record_evaluation");
// main constraint: read/write record witness inference
let span = entered_span!("wit_inference::record");
let records_wit: Vec<ArcMultilinearExtension<'_, E>> = circuit
.r_expressions
.iter()
Expand All @@ -70,7 +71,7 @@ impl<E: ExtensionField> ZKVMProver<E> {
scalar
},
&|challenge_id, pow, scalar, offset| {
// TODO cache challenge power to be aquire once
// TODO cache challenge power to be aquire once for each power
let challenge = challenges[challenge_id as usize];
let challenge: ArcMultilinearExtension<E> =
Arc::new(DenseMultilinearExtension::from_evaluations_ext_vec(
Expand All @@ -91,21 +92,27 @@ impl<E: ExtensionField> ZKVMProver<E> {
(1, _) => {
Arc::new(DenseMultilinearExtension::from_evaluation_vec_smart(
ceil_log2(b.len()),
b.par_iter().with_min_len(64).map(|b| a[0] + *b).collect(),
b.par_iter()
.with_min_len(MIN_PAR_SIZE)
.map(|b| a[0] + *b)
.collect(),
))
}
(_, 1) => {
Arc::new(DenseMultilinearExtension::from_evaluation_vec_smart(
ceil_log2(a.len()),
a.par_iter().with_min_len(64).map(|a| *a + b[0]).collect(),
a.par_iter()
.with_min_len(MIN_PAR_SIZE)
.map(|a| *a + b[0])
.collect(),
))
}
(_, _) => {
Arc::new(DenseMultilinearExtension::from_evaluation_vec_smart(
ceil_log2(a.len()),
a.par_iter()
.zip(b.par_iter())
.with_min_len(64)
.with_min_len(MIN_PAR_SIZE)
.map(|(a, b)| *a + b)
.collect(),
))
Expand All @@ -125,13 +132,19 @@ impl<E: ExtensionField> ZKVMProver<E> {
(1, _) => {
Arc::new(DenseMultilinearExtension::from_evaluation_vec_smart(
ceil_log2(b.len()),
b.par_iter().with_min_len(64).map(|b| a[0] * *b).collect(),
b.par_iter()
.with_min_len(MIN_PAR_SIZE)
.map(|b| a[0] * *b)
.collect(),
))
}
(_, 1) => {
Arc::new(DenseMultilinearExtension::from_evaluation_vec_smart(
ceil_log2(a.len()),
a.par_iter().with_min_len(64).map(|a| *a * b[0]).collect(),
a.par_iter()
.with_min_len(MIN_PAR_SIZE)
.map(|a| *a * b[0])
.collect(),
))
}
(_, _) => {
Expand All @@ -144,7 +157,10 @@ impl<E: ExtensionField> ZKVMProver<E> {
op_mle!(|a| {
Arc::new(DenseMultilinearExtension::from_evaluation_vec_smart(
ceil_log2(a.len()),
a.par_iter().with_min_len(64).map(|a| scalar * a).collect(),
a.par_iter()
.with_min_len(MIN_PAR_SIZE)
.map(|a| scalar * a)
.collect(),
))
})
},
Expand All @@ -153,15 +169,27 @@ impl<E: ExtensionField> ZKVMProver<E> {
.collect();
let (r_records_wit, w_records_wit) = records_wit.split_at(circuit.r_expressions.len());
println!("r_records_wit {:?}", r_records_wit,);

println!("w_records_wit {:?}", w_records_wit);
exit_span!(span);

// construct main constraint sumcheck virtual polynomial
// product constraint: tower witness inference
let span = entered_span!("wit_inference::tower_witness");
// TODO
// we dont make the last layer as new vector to save memory
exit_span!(span);

// build first part of sumcheck: selector
// circuit.
// product constraint tower sumcheck
let span = entered_span!("sumcheck::tower");
// TODO
exit_span!(span);

// main constraints degree > 1 + selector sumcheck
let span = entered_span!("sumcheck::main_sel");
// TODO
exit_span!(span);

Ok(ZKVMProof {})
Ok(ZKVMProof {
input_point_and_evals: vec![],
})
}
}
34 changes: 34 additions & 0 deletions ceno_zkvm/src/scheme/verifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use ff_ext::ExtensionField;
use gkr::structs::PointAndEval;
use singer_utils::structs_v2::Circuit;
use transcript::Transcript;

use crate::error::ZKVMError;

use super::ZKVMProof;

pub struct ZKVMVerifier<E: ExtensionField> {
circuit: Circuit<E>,
}

impl<E: ExtensionField> ZKVMVerifier<E> {
pub fn new(circuit: Circuit<E>) -> Self {
ZKVMVerifier { circuit }
}
pub fn verify(
&self,
proof: &ZKVMProof<E>,
_transcript: &mut Transcript<E>,
out_evals: &PointAndEval<E>,
challenges: &[E], // derive challenge from PCS
) -> Result<(), ZKVMError> {
// verify and reduce product tower sumcheck

// verify main + sel sumcheck

// verify record (degree = 1) statement, thus no sumcheck

// verify zero expression (degree = 1) statement, thus no sumcheck
Ok(())
}
}
1 change: 1 addition & 0 deletions singer-utils/src/structs_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct CircuitBuilderV2<E: ExtensionField> {
pub(crate) phantom: PhantomData<E>,
}

#[derive(Clone, Debug)]
pub struct Circuit<E: ExtensionField> {
pub num_witin: WitnessId,
pub r_expressions: Vec<ExpressionV2<E>>,
Expand Down

0 comments on commit 7676067

Please sign in to comment.