Skip to content

Commit

Permalink
Decompress parameters when reading
Browse files Browse the repository at this point in the history
  • Loading branch information
voidxnull committed Dec 26, 2023
1 parent 533274f commit 1148e65
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
14 changes: 12 additions & 2 deletions fawkes-crypto/src/backend/bellman_groth16/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::io::{Read, Write};
use crate::{
circuit::{
cs::{RCS, WitnessCS, CS},
Expand Down Expand Up @@ -149,16 +150,25 @@ impl<E: Engine> Parameters<E> {

pub fn write<W:std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
let bitvec_len = self.3.len() as u32;

let mut gates = Vec::new();
let mut w = brotli::CompressorWriter::new(&mut gates, 4096, 9, 22);
w.write_all(&self.2)?;
drop(w);

BorshSerialize::serialize(&self.1, writer)?;
BorshSerialize::serialize(&self.2, writer)?;
BorshSerialize::serialize(&gates, writer)?;
BorshSerialize::serialize(&bitvec_len, writer)?;
BorshSerialize::serialize(&self.3.to_bytes(), writer)?;
self.0.write(writer)
}

pub fn read(reader: &mut &[u8], disallow_points_at_infinity: bool, checked: bool) -> std::io::Result<Self> {
let e1 = BorshDeserialize::deserialize(reader)?;
let e2 = BorshDeserialize::deserialize(reader)?;
let e2_compressed: Vec<u8> = BorshDeserialize::deserialize(reader)?;
let mut e2 = Vec::new();
brotli::Decompressor::new(e2_compressed.as_slice(), 1024).read_to_end(&mut e2)?;

let e3_len = <u32 as BorshDeserialize>::deserialize(reader)? as usize;
let e3_buf:Vec<u8> = BorshDeserialize::deserialize(reader)?;

Expand Down
7 changes: 3 additions & 4 deletions fawkes-crypto/src/backend/bellman_groth16/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ pub fn setup<E: Engine, Pub: Signal<BuildCS<E::Fr>>, Sec: Signal<BuildCS<E::Fr>>

let num_gates = cs.gates.len();

let mut buf = std::io::Cursor::new(vec![]);
let mut c = brotli::CompressorWriter::new(&mut buf, 4096, 9, 22);
let mut buf = vec![];
let mut c = &mut buf;
for g in cs.gates.iter() {
c.write_all(&g.try_to_vec().unwrap()).unwrap();
}

c.flush().unwrap();
drop(c);

Parameters(bp, num_gates as u32, buf.into_inner(), cs.const_tracker.clone())
Parameters(bp, num_gates as u32, buf, cs.const_tracker.clone())
}
4 changes: 2 additions & 2 deletions fawkes-crypto/src/circuit/r1cs/cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl<Fr:PrimeField, R:std::io::Read> Iterator for GateStreamedIterator<Fr, R> {
impl<'a, Fr: PrimeField> CS for WitnessCS<'a, Fr> {
type Fr = Fr;
type LC = ZeroLC;
type GateIterator = GateStreamedIterator<Fr, brotli::Decompressor<&'a [u8]>>;
type GateIterator = GateStreamedIterator<Fr, &'a [u8]>;

fn num_gates(&self) -> usize {
self.num_gates
Expand All @@ -246,7 +246,7 @@ impl<'a, Fr: PrimeField> CS for WitnessCS<'a, Fr> {
}

fn get_gate_iterator(&self) -> Self::GateIterator {
GateStreamedIterator(brotli::Decompressor::new(self.gates_data, 4096), PhantomData)
GateStreamedIterator(&self.gates_data, PhantomData)
}

fn enforce(_: &CNum<Self>, _: &CNum<Self>, _: &CNum<Self>) {
Expand Down

0 comments on commit 1148e65

Please sign in to comment.