From 6284afdb41a18b2094d346b5fe75553ef975d55c Mon Sep 17 00:00:00 2001 From: Dmitry Vdovin Date: Sun, 21 Jan 2024 02:45:52 +0400 Subject: [PATCH] Implement read/write for ProvingKey and VerifyingKey --- fawkes-crypto/src/backend/plonk/setup.rs | 53 ++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/fawkes-crypto/src/backend/plonk/setup.rs b/fawkes-crypto/src/backend/plonk/setup.rs index c70b742..70d3311 100644 --- a/fawkes-crypto/src/backend/plonk/setup.rs +++ b/fawkes-crypto/src/backend/plonk/setup.rs @@ -1,6 +1,16 @@ use super::{*, engines::Bn256}; -use halo2_curves::pairing::Engine as PairingEngine; -use crate::backend::plonk::engines::Engine; +use halo2_curves::{ + CurveAffine, + ff::FromUniformBytes, + group::prime::PrimeCurveAffine, + pairing::Engine as PairingEngine, +}; +use halo2_proofs::SerdeFormat; +use crate::{ + circuit::cs::BuildCS, + engines::bn256::Fr, + backend::plonk::engines::Engine +}; use halo2_proofs::plonk::{ keygen_pk, keygen_vk, @@ -12,19 +22,54 @@ use std::{ rc::Rc, cell::{RefCell} }; - -use crate::{circuit::cs::BuildCS, engines::bn256::Fr}; +use std::io::{Read, Write}; #[derive(Clone, Debug)] pub struct ProvingKey( pub HaloProvingKey<::G1Affine> ); +impl ProvingKey +where + <::BE as PairingEngine>::G1Affine: SerdeObject, + <<::BE as PairingEngine>::G1Affine as PrimeCurveAffine>::Scalar: SerdeObject + FromUniformBytes<64>, +{ + pub fn write(&self, writer: &mut W) -> std::io::Result<()> { + let mut w = brotli::CompressorWriter::new(writer, 4096, 9, 22); + self.0.write(&mut w, SerdeFormat::Processed) + } + + pub fn read(reader: &mut R) -> std::io::Result + where + R: std::io::Read, + { + let mut r = brotli::Decompressor::new(reader, 4096); + Ok(Self(HaloProvingKey::<::G1Affine>::read::<_, HaloCS>>(&mut r, SerdeFormat::Processed)?)) + } +} + #[derive(Clone, Debug)] pub struct VerifyingKey( pub HaloVerifyingKey<::G1Affine> ); +impl VerifyingKey +where + <::BE as PairingEngine>::G1Affine: SerdeObject, + <<::BE as PairingEngine>::G1Affine as PrimeCurveAffine>::Scalar: SerdeObject + FromUniformBytes<64>, +{ + pub fn write(&self, writer: &mut W) -> std::io::Result<()> { + self.0.write(writer, SerdeFormat::Processed) + } + + pub fn read(reader: &mut R) -> std::io::Result + where + R: std::io::Read, + { + Ok(Self(HaloVerifyingKey::<::G1Affine>::read::<_, HaloCS>>(reader, SerdeFormat::Processed)?)) + } +} + pub fn setup< 'a, Pub: Signal>,