From 34a035450765f0b2c408dcd73bc3d215ae2e5398 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 6 Apr 2023 14:11:19 -0700 Subject: [PATCH] Serialize impls, and use in Fibonacci example --- evm/Cargo.toml | 1 + plonky2/Cargo.toml | 1 + plonky2/examples/fibonacci.rs | 6 ++++++ plonky2/src/fri/mod.rs | 6 ++++-- plonky2/src/fri/reduction_strategies.rs | 3 ++- plonky2/src/gates/constant.rs | 4 +++- plonky2/src/gates/gate.rs | 7 +++++++ plonky2/src/gates/selectors.rs | 4 +++- plonky2/src/plonk/circuit_data.rs | 5 +++-- 9 files changed, 30 insertions(+), 7 deletions(-) diff --git a/evm/Cargo.toml b/evm/Cargo.toml index 63f4ec0c..25251daa 100644 --- a/evm/Cargo.toml +++ b/evm/Cargo.toml @@ -33,6 +33,7 @@ rand_chacha = "0.3.1" rlp = "0.5.1" rlp-derive = "0.1.0" serde = { version = "1.0.144", features = ["derive"] } + static_assertions = "1.1.0" hashbrown = { version = "0.12.3" } tiny-keccak = "2.0.2" diff --git a/plonky2/Cargo.toml b/plonky2/Cargo.toml index 6df46060..39b95f21 100644 --- a/plonky2/Cargo.toml +++ b/plonky2/Cargo.toml @@ -31,6 +31,7 @@ plonky2_util = { version = "0.1.0", default-features = false } rand = { version = "0.8.4", default-features = false } rand_chacha = { version = "0.3.1", optional = true, default-features = false } serde = { version = "1.0", default-features = false, features = ["derive"] } +serde_json = "1.0" static_assertions = { version = "1.1.0", default-features = false } unroll = { version = "0.1.5", default-features = false } diff --git a/plonky2/examples/fibonacci.rs b/plonky2/examples/fibonacci.rs index e3d865b9..8946c2a9 100644 --- a/plonky2/examples/fibonacci.rs +++ b/plonky2/examples/fibonacci.rs @@ -1,5 +1,7 @@ #![allow(clippy::upper_case_acronyms)] +use std::fs; + use anyhow::Result; use plonky2::field::types::Field; use plonky2::iop::witness::{PartialWitness, WitnessWrite}; @@ -40,6 +42,10 @@ fn main() -> Result<()> { pw.set_target(initial_b, F::ONE); let data = builder.build::(); + + let common_circuit_data_serialized = serde_json::to_string(&data.common).unwrap(); + fs::write("common_circuit_data.json", common_circuit_data_serialized).expect("Unable to write file"); + let proof = data.prove(pw)?; println!( diff --git a/plonky2/src/fri/mod.rs b/plonky2/src/fri/mod.rs index ca800b98..5121d755 100644 --- a/plonky2/src/fri/mod.rs +++ b/plonky2/src/fri/mod.rs @@ -1,5 +1,7 @@ use alloc::vec::Vec; +use serde::Serialize; + use crate::fri::reduction_strategies::FriReductionStrategy; mod challenges; @@ -13,7 +15,7 @@ mod validate_shape; pub mod verifier; pub mod witness_util; -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq, Serialize)] pub struct FriConfig { /// `rate = 2^{-rate_bits}`. pub rate_bits: usize, @@ -56,7 +58,7 @@ impl FriConfig { /// FRI parameters, including generated parameters which are specific to an instance size, in /// contrast to `FriConfig` which is user-specified and independent of instance size. -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq, Serialize)] pub struct FriParams { /// User-specified FRI configuration. pub config: FriConfig, diff --git a/plonky2/src/fri/reduction_strategies.rs b/plonky2/src/fri/reduction_strategies.rs index 409a0224..df273eea 100644 --- a/plonky2/src/fri/reduction_strategies.rs +++ b/plonky2/src/fri/reduction_strategies.rs @@ -4,9 +4,10 @@ use alloc::vec::Vec; use std::time::Instant; use log::debug; +use serde::Serialize; /// A method for deciding what arity to use at each reduction layer. -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq, Serialize)] pub enum FriReductionStrategy { /// Specifies the exact sequence of arities (expressed in bits) to use. Fixed(Vec), diff --git a/plonky2/src/gates/constant.rs b/plonky2/src/gates/constant.rs index f794c904..5fac1f00 100644 --- a/plonky2/src/gates/constant.rs +++ b/plonky2/src/gates/constant.rs @@ -2,6 +2,8 @@ use alloc::string::String; use alloc::vec::Vec; use alloc::{format, vec}; +use serde::{Deserialize, Serialize}; + use crate::field::extension::Extendable; use crate::field::packed::PackedField; use crate::gates::gate::Gate; @@ -18,7 +20,7 @@ use crate::plonk::vars::{ use crate::util::serialization::{Buffer, IoResult, Read, Write}; /// A gate which takes a single constant parameter and outputs that value. -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] pub struct ConstantGate { pub(crate) num_consts: usize, } diff --git a/plonky2/src/gates/gate.rs b/plonky2/src/gates/gate.rs index 43c1179a..1dbc4f57 100644 --- a/plonky2/src/gates/gate.rs +++ b/plonky2/src/gates/gate.rs @@ -8,6 +8,7 @@ use core::hash::{Hash, Hasher}; use core::ops::Range; use hashbrown::HashMap; +use serde::{Serialize, Serializer}; use crate::field::batch_util::batch_multiply_inplace; use crate::field::extension::{Extendable, FieldExtension}; @@ -239,6 +240,12 @@ impl, const D: usize> Debug for GateRef { } } +impl, const D: usize> Serialize for GateRef { + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_str(&self.0.id()) + } +} + /// Map between gate parameters and available slots. /// An available slot is of the form `(row, op)`, meaning the current available slot /// is at gate index `row` in the `op`-th operation. diff --git a/plonky2/src/gates/selectors.rs b/plonky2/src/gates/selectors.rs index 7217de2f..0e690c6d 100644 --- a/plonky2/src/gates/selectors.rs +++ b/plonky2/src/gates/selectors.rs @@ -2,6 +2,8 @@ use alloc::vec; use alloc::vec::Vec; use core::ops::Range; +use serde::Serialize; + use crate::field::extension::Extendable; use crate::field::polynomial::PolynomialValues; use crate::gates::gate::{GateInstance, GateRef}; @@ -10,7 +12,7 @@ use crate::hash::hash_types::RichField; /// Placeholder value to indicate that a gate doesn't use a selector polynomial. pub(crate) const UNUSED_SELECTOR: usize = u32::MAX as usize; -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq, Serialize)] pub struct SelectorsInfo { pub(crate) selector_indices: Vec, pub(crate) groups: Vec>, diff --git a/plonky2/src/plonk/circuit_data.rs b/plonky2/src/plonk/circuit_data.rs index fa9af818..70d40e42 100644 --- a/plonky2/src/plonk/circuit_data.rs +++ b/plonky2/src/plonk/circuit_data.rs @@ -4,6 +4,7 @@ use alloc::vec::Vec; use core::ops::{Range, RangeFrom}; use anyhow::Result; +use serde::Serialize; use crate::field::extension::Extendable; use crate::field::fft::FftRootTable; @@ -35,7 +36,7 @@ use crate::util::serialization::{ }; use crate::util::timing::TimingTree; -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] pub struct CircuitConfig { pub num_wires: usize, pub num_routed_wires: usize, @@ -370,7 +371,7 @@ impl, const D: usize> VerifierOnlyCircuitData { } /// Circuit data required by both the prover and the verifier. -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq, Serialize)] pub struct CommonCircuitData, const D: usize> { pub config: CircuitConfig,