Skip to content

Commit

Permalink
Implement read/write for ProvingKey and VerifyingKey
Browse files Browse the repository at this point in the history
  • Loading branch information
voidxnull committed Jan 20, 2024
1 parent 48d3df1 commit 6284afd
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions fawkes-crypto/src/backend/plonk/setup.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<E: Engine>(
pub HaloProvingKey<<E::BE as PairingEngine>::G1Affine>
);

impl<E: Engine> ProvingKey<E>
where
<<E as Engine>::BE as PairingEngine>::G1Affine: SerdeObject,
<<<E as Engine>::BE as PairingEngine>::G1Affine as PrimeCurveAffine>::Scalar: SerdeObject + FromUniformBytes<64>,
{
pub fn write<W: std::io::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<R>(reader: &mut R) -> std::io::Result<Self>
where
R: std::io::Read,
{
let mut r = brotli::Decompressor::new(reader, 4096);
Ok(Self(HaloProvingKey::<<E::BE as PairingEngine>::G1Affine>::read::<_, HaloCS<BuildCS<E::Fr>>>(&mut r, SerdeFormat::Processed)?))
}
}

#[derive(Clone, Debug)]
pub struct VerifyingKey<E: Engine>(
pub HaloVerifyingKey<<E::BE as PairingEngine>::G1Affine>
);

impl<E: Engine> VerifyingKey<E>
where
<<E as Engine>::BE as PairingEngine>::G1Affine: SerdeObject,
<<<E as Engine>::BE as PairingEngine>::G1Affine as PrimeCurveAffine>::Scalar: SerdeObject + FromUniformBytes<64>,
{
pub fn write<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
self.0.write(writer, SerdeFormat::Processed)
}

pub fn read<R>(reader: &mut R) -> std::io::Result<Self>
where
R: std::io::Read,
{
Ok(Self(HaloVerifyingKey::<<E::BE as PairingEngine>::G1Affine>::read::<_, HaloCS<BuildCS<E::Fr>>>(reader, SerdeFormat::Processed)?))
}
}

pub fn setup<
'a,
Pub: Signal<BuildCS<Fr>>,
Expand Down

0 comments on commit 6284afd

Please sign in to comment.