Skip to content

Commit

Permalink
async hint no serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirhemo committed Sep 20, 2023
1 parent 7e41da3 commit 0b4a87d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 14 deletions.
14 changes: 12 additions & 2 deletions plonky2x/src/backend/circuit/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use plonky2::plonk::circuit_data::CircuitData;
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig, GenericHashOut};
use plonky2::plonk::proof::ProofWithPublicInputs;
use plonky2::plonk::prover::prove_with_partition_witness;
use plonky2::util::serialization::{Buffer, GateSerializer, IoResult, WitnessGeneratorSerializer};
use plonky2::util::serialization::{
Buffer, GateSerializer, IoResult, Read, WitnessGeneratorSerializer, Write,
};
use plonky2::util::timing::TimingTree;

use super::config::PlonkParameters;
Expand Down Expand Up @@ -104,6 +106,14 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuild<L, D> {
let io = bincode::serialize(&self.io).unwrap();
buffer.write_bytes(&io)?;

// serialize the async generator map
let map_size = self.async_generators.len();
buffer.write_usize(map_size)?;
for (key, gen_data) in self.async_generators.iter() {
buffer.write_usize(*key)?;
gen_data.0.serialize(&mut buffer, &self.data.common)?;
}

Ok(buffer)
}

Expand All @@ -125,7 +135,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuild<L, D> {
let io = buffer.read_bytes()?;
let io: CircuitIO<D> = bincode::deserialize(&io).unwrap();

//TODO: deseialize
// TODO: deserialize the async generator map
let async_generators = BTreeMap::new();

Ok(CircuitBuild {
Expand Down
11 changes: 11 additions & 0 deletions plonky2x/src/backend/circuit/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ use crate::frontend::eth::beacon::vars::{
};
use crate::frontend::eth::storage::generators::{
EthBlockGenerator, EthLogGenerator, EthStorageKeyGenerator, EthStorageProofGenerator,
EthStorageProofHint,
};
use crate::frontend::generator::asynchronous::hint::AsyncHint;
use crate::frontend::generator::asynchronous::serializer::AsyncHintSerializer;
use crate::frontend::generator::simple::hint::Hint;
use crate::frontend::generator::simple::serializer::SimpleHintSerializer;
use crate::frontend::hash::bit_operations::{XOR3Gate, XOR3Generator};
Expand Down Expand Up @@ -215,6 +218,12 @@ impl<L: PlonkParameters<D>, const D: usize> WitnessGeneratorRegistry<L, D> {
let id = H::id();
self.0.register(id, serializer).unwrap();
}

pub fn register_async_hint<H: AsyncHint<L, D>>(&mut self) {
let serializer = AsyncHintSerializer::<L, H>::new();
let id = H::id();
self.0.register(id, serializer).unwrap();
}
}

impl<L: PlonkParameters<D>, const D: usize> GateRegistry<L, D> {
Expand Down Expand Up @@ -500,6 +509,8 @@ where
D,
>>(simple_stark_witness_generator_id);

r.register_async_hint::<EthStorageProofHint<L, D>>();

register_watch_generator!(
r,
L,
Expand Down
17 changes: 8 additions & 9 deletions plonky2x/src/frontend/eth/storage/builder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use ethers::types::Address;

use super::generators::{
EthBlockGenerator, EthLogGenerator, EthStorageKeyGenerator,
EthStorageProofHint,
EthBlockGenerator, EthLogGenerator, EthStorageKeyGenerator, EthStorageProofHint,
};
use super::vars::{EthAccountVariable, EthHeaderVariable, EthLogVariable};
use crate::backend::circuit::PlonkParameters;
Expand Down Expand Up @@ -139,14 +138,14 @@ mod tests {
bytes32!("0x0000000000000000000000dd4bc51496dc93a0c47008e820e0d80745476f2201"),
);

// // initialize serializers
// let gate_serializer = GateRegistry::<L, D>::new();
// let generator_serializer = WitnessGeneratorRegistry::<L, D>::new();
// initialize serializers
let gate_serializer = GateRegistry::<L, D>::new();
let generator_serializer = WitnessGeneratorRegistry::<L, D>::new();

// // test serialization
// let _ = circuit
// .serialize(&gate_serializer, &generator_serializer)
// .unwrap();
// test serialization
let _ = circuit
.serialize(&gate_serializer, &generator_serializer)
.unwrap();
}

#[test]
Expand Down
12 changes: 9 additions & 3 deletions plonky2x/src/frontend/generator/asynchronous/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anyhow::Result;
use plonky2::iop::generator::{GeneratedValues, WitnessGenerator, WitnessGeneratorRef};
use plonky2::iop::target::Target;
use plonky2::iop::witness::{PartitionWitness, Witness};
use plonky2::util::serialization::IoError;
use tokio::sync::mpsc::UnboundedSender;

use super::channel::{HintChannel, HintInMessage};
Expand All @@ -13,6 +14,7 @@ use crate::backend::circuit::PlonkParameters;
use crate::frontend::generator::HintGenerator;
use crate::frontend::vars::{ValueStream, VariableStream};
use crate::prelude::CircuitVariable;
use crate::utils::serde::BufferWrite;

pub trait AsyncGeneratorData<L: PlonkParameters<D>, const D: usize>: HintGenerator<L, D> {
fn generator(
Expand Down Expand Up @@ -214,10 +216,14 @@ impl<L: PlonkParameters<D>, H: AsyncHint<L, D>, const D: usize> WitnessGenerator

fn serialize(
&self,
_dst: &mut Vec<u8>,
dst: &mut Vec<u8>,
_common_data: &plonky2::plonk::circuit_data::CommonCircuitData<L::Field, D>,
) -> plonky2::util::serialization::IoResult<()> {
unimplemented!("AsyncHintGenerator::serialize")
self.input_stream.serialize_to_writer(dst)?;
self.output_stream.serialize_to_writer(dst)?;

let bytes = bincode::serialize(&self.hint).map_err(|_| IoError)?;
dst.write_bytes(&bytes)
}

fn deserialize(
Expand All @@ -227,7 +233,7 @@ impl<L: PlonkParameters<D>, H: AsyncHint<L, D>, const D: usize> WitnessGenerator
where
Self: Sized,
{
unimplemented!("AsyncHintGenerator::deserialize")
unimplemented!("Hints are not deserializable through the plonky2 crate, only directly through the witness registry")
}

fn run(
Expand Down
54 changes: 54 additions & 0 deletions plonky2x/src/frontend/generator/asynchronous/serializer.rs
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
use core::marker::PhantomData;

use plonky2::iop::generator::WitnessGeneratorRef;
use plonky2::plonk::circuit_data::CommonCircuitData;
use plonky2::util::serialization::{Buffer, IoError, IoResult};

use super::generator::AsyncHintData;
use super::hint::AsyncHint;
use crate::backend::circuit::Serializer;
use crate::frontend::vars::VariableStream;
use crate::prelude::PlonkParameters;
use crate::utils::serde::BufferRead;

#[derive(Debug, Clone)]
pub struct AsyncHintSerializer<L, H>(PhantomData<L>, PhantomData<H>);

impl<L, H> AsyncHintSerializer<L, H> {
pub fn new() -> Self {
Self(PhantomData, PhantomData)
}
}

impl<L, H> Default for AsyncHintSerializer<L, H> {
fn default() -> Self {
Self::new()
}
}

impl<L: PlonkParameters<D>, H: AsyncHint<L, D>, const D: usize>
Serializer<L::Field, WitnessGeneratorRef<L::Field, D>, D> for AsyncHintSerializer<L, H>
{
fn read(
&self,
buf: &mut Buffer,
_common_data: &CommonCircuitData<L::Field, D>,
) -> IoResult<WitnessGeneratorRef<L::Field, D>> {
let input_stream = VariableStream::deserialize_from_reader(buf)?;
let output_stream = VariableStream::deserialize_from_reader(buf)?;

let bytes = buf.read_bytes()?;
let hint: H = bincode::deserialize(&bytes).map_err(|_| IoError)?;
let hint_data = AsyncHintData::<L, H, D>::new(hint, input_stream, output_stream);

Ok(WitnessGeneratorRef::new(hint_data))
}

fn write(
&self,
buf: &mut Vec<u8>,
object: &WitnessGeneratorRef<L::Field, D>,
common_data: &CommonCircuitData<L::Field, D>,
) -> IoResult<()> {
object.0.serialize(buf, common_data)
}
}

0 comments on commit 0b4a87d

Please sign in to comment.