diff --git a/src/plonk.rs b/src/plonk.rs index bc81918..d2bf6c7 100644 --- a/src/plonk.rs +++ b/src/plonk.rs @@ -6,7 +6,9 @@ //! [plonk]: https://eprint.iacr.org/2019/953 use crate::arithmetic::CurveAffine; -use crate::poly::{Coeff, EvaluationDomain, ExtendedLagrangeCoeff, LagrangeCoeff, Polynomial}; +use crate::poly::{ + commitment::Params, Coeff, EvaluationDomain, ExtendedLagrangeCoeff, LagrangeCoeff, Polynomial, +}; use crate::transcript::ChallengeScalar; mod circuit; @@ -23,6 +25,8 @@ pub use keygen::*; pub use prover::*; pub use verifier::*; +use std::io; + /// This is a verifying key which allows for the verification of proofs for a /// particular circuit. #[derive(Debug)] @@ -33,6 +37,45 @@ pub struct VerifyingKey { cs: ConstraintSystem, } +impl VerifyingKey { + /// Writes a verifying key to a buffer. + pub fn write(&self, writer: &mut W) -> io::Result<()> { + for commitment in &self.fixed_commitments { + writer.write_all(&commitment.to_bytes())?; + } + for permutation in &self.permutations { + permutation.write(writer)?; + } + + Ok(()) + } + + /// Reads a verification key from a buffer. + pub fn read>( + reader: &mut R, + params: &Params, + ) -> io::Result { + let (domain, cs, _) = keygen::create_domain::(params); + + let mut fixed_commitments = Vec::with_capacity(cs.num_fixed_columns); + for _ in 0..cs.num_fixed_columns { + fixed_commitments.push(C::read(reader)?); + } + + let mut permutations = Vec::with_capacity(cs.permutations.len()); + for argument in &cs.permutations { + permutations.push(permutation::VerifyingKey::read(reader, argument)?); + } + + Ok(VerifyingKey { + domain, + fixed_commitments, + permutations, + cs, + }) + } +} + /// This is a proving key which allows for the creation of proofs for a /// particular circuit. #[derive(Debug)] @@ -487,8 +530,11 @@ fn test_proving() { let msm = guard.clone().use_challenges(); assert!(msm.clone().eval()); let mut transcript = DummyHashRead::init(&proof[..], Fq::one()); - let guard = - verify_proof(¶ms, pk.get_vk(), msm, pubinput_slice, &mut transcript).unwrap(); + let mut vk_buffer = vec![]; + pk.get_vk().write(&mut vk_buffer).unwrap(); + let vk = VerifyingKey::::read::<_, MyCircuit>(&mut &vk_buffer[..], ¶ms) + .unwrap(); + let guard = verify_proof(¶ms, &vk, msm, pubinput_slice, &mut transcript).unwrap(); { let msm = guard.clone().use_challenges(); assert!(msm.eval()); diff --git a/src/plonk/permutation.rs b/src/plonk/permutation.rs index 8073f2f..affb0bf 100644 --- a/src/plonk/permutation.rs +++ b/src/plonk/permutation.rs @@ -10,6 +10,8 @@ pub(crate) mod keygen; mod prover; mod verifier; +use std::io; + /// A permutation argument. #[derive(Debug, Clone)] pub(crate) struct Argument { @@ -49,6 +51,25 @@ pub(crate) struct VerifyingKey { commitments: Vec, } +impl VerifyingKey { + pub(crate) fn write(&self, writer: &mut W) -> io::Result<()> { + for commitment in &self.commitments { + commitment.write(writer)?; + } + + Ok(()) + } + + pub(crate) fn read(reader: &mut R, argument: &Argument) -> io::Result { + let mut commitments = Vec::with_capacity(argument.columns.len()); + for _ in 0..argument.columns.len() { + commitments.push(C::read(reader)?); + } + + Ok(VerifyingKey { commitments }) + } +} + /// The proving key for a single permutation argument. #[derive(Debug)] pub(crate) struct ProvingKey {