diff --git a/Cargo.toml b/Cargo.toml index 68b79d0..009e968 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,8 +10,8 @@ edition = "2021" anyhow = { version = "1.0.40", default-features = false } itertools = { version = "0.10.0", default-features = false } num = { version = "0.4", default-features = false } -plonky2 = { git = "https://github.com/mir-protocol/plonky2.git" } +plonky2 = "0.1.4" [dev-dependencies] -plonky2 = { git = "https://github.com/mir-protocol/plonky2.git", default-features = false, features = ["gate_testing"] } rand = { version = "0.8.4", default-features = false, features = ["getrandom"] } +plonky2 = "0.1.4" diff --git a/src/gadgets/arithmetic_u32.rs b/src/gadgets/arithmetic_u32.rs index 170352b..8d00a81 100644 --- a/src/gadgets/arithmetic_u32.rs +++ b/src/gadgets/arithmetic_u32.rs @@ -1,8 +1,9 @@ use alloc::string::{String, ToString}; use alloc::vec; use alloc::vec::Vec; -use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use core::marker::PhantomData; +use plonky2::plonk::circuit_data::CommonCircuitData; +use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use plonky2::field::extension::Extendable; use plonky2::hash::hash_types::RichField; @@ -241,24 +242,29 @@ struct SplitToU32Generator, const D: usize> { _phantom: PhantomData, } -impl, const D: usize> SimpleGenerator +impl, const D: usize> SimpleGenerator for SplitToU32Generator { fn id(&self) -> String { "SplitToU32Generator".to_string() } - fn serialize(&self, dst: &mut Vec) -> IoResult<()> { + fn serialize(&self, dst: &mut Vec, _common_data: &CommonCircuitData) -> IoResult<()> { dst.write_target(self.x)?; dst.write_target_u32(self.low)?; dst.write_target_u32(self.high) } - fn deserialize(src: &mut Buffer) -> IoResult { + fn deserialize(src: &mut Buffer, _common_data: &CommonCircuitData) -> IoResult { let x = src.read_target()?; let low = src.read_target_u32()?; let high = src.read_target_u32()?; - Ok(Self { x, low, high, _phantom: PhantomData }) + Ok(Self { + x, + low, + high, + _phantom: PhantomData, + }) } fn dependencies(&self) -> Vec { diff --git a/src/gates/add_many_u32.rs b/src/gates/add_many_u32.rs index 37b9997..d357e11 100644 --- a/src/gates/add_many_u32.rs +++ b/src/gates/add_many_u32.rs @@ -1,8 +1,8 @@ use alloc::format; use alloc::string::{String, ToString}; use alloc::vec::Vec; -use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use core::marker::PhantomData; +use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use itertools::unfold; use plonky2::field::extension::Extendable; @@ -16,7 +16,7 @@ use plonky2::iop::target::Target; use plonky2::iop::wire::Wire; use plonky2::iop::witness::{PartitionWitness, Witness, WitnessWrite}; use plonky2::plonk::circuit_builder::CircuitBuilder; -use plonky2::plonk::circuit_data::CircuitConfig; +use plonky2::plonk::circuit_data::{CircuitConfig, CommonCircuitData}; use plonky2::plonk::vars::{EvaluationTargets, EvaluationVars, EvaluationVarsBase}; use plonky2::util::ceil_div_usize; @@ -91,15 +91,19 @@ impl, const D: usize> Gate for U32AddManyGate format!("{self:?}") } - fn serialize(&self, dst: &mut Vec) -> IoResult<()> { + fn serialize(&self, dst: &mut Vec, _common_data: &CommonCircuitData) -> IoResult<()> { dst.write_usize(self.num_addends)?; dst.write_usize(self.num_ops) } - fn deserialize(src: &mut Buffer) -> IoResult { + fn deserialize(src: &mut Buffer, _common_data: &CommonCircuitData) -> IoResult { let num_addends = src.read_usize()?; let num_ops = src.read_usize()?; - Ok(Self { num_addends, num_ops, _phantom: PhantomData }) + Ok(Self { + num_addends, + num_ops, + _phantom: PhantomData, + }) } fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { @@ -247,7 +251,7 @@ impl, const D: usize> Gate for U32AddManyGate constraints } - fn generators(&self, row: usize, _local_constants: &[F]) -> Vec> { + fn generators(&self, row: usize, _local_constants: &[F]) -> Vec> { (0..self.num_ops) .map(|i| { WitnessGeneratorRef::new( @@ -288,24 +292,29 @@ struct U32AddManyGenerator, const D: usize> { _phantom: PhantomData, } -impl, const D: usize> SimpleGenerator +impl, const D: usize> SimpleGenerator for U32AddManyGenerator { fn id(&self) -> String { "U32AddManyGenerator".to_string() } - fn serialize(&self, dst: &mut Vec) -> IoResult<()> { - self.gate.serialize(dst)?; + fn serialize(&self, dst: &mut Vec, common_data: &CommonCircuitData) -> IoResult<()> { + self.gate.serialize(dst, common_data)?; dst.write_usize(self.row)?; dst.write_usize(self.i) } - fn deserialize(src: &mut Buffer) -> IoResult { - let gate = U32AddManyGate::deserialize(src)?; + fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData) -> IoResult { + let gate = U32AddManyGate::deserialize(src, common_data)?; let row = src.read_usize()?; let i = src.read_usize()?; - Ok(Self { gate, row, i, _phantom: PhantomData }) + Ok(Self { + gate, + row, + i, + _phantom: PhantomData, + }) } fn dependencies(&self) -> Vec { diff --git a/src/gates/arithmetic_u32.rs b/src/gates/arithmetic_u32.rs index 1d2be54..5ca4efb 100644 --- a/src/gates/arithmetic_u32.rs +++ b/src/gates/arithmetic_u32.rs @@ -1,8 +1,8 @@ use alloc::string::{String, ToString}; use alloc::vec::Vec; use alloc::{format, vec}; -use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use core::marker::PhantomData; +use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use itertools::unfold; use plonky2::field::extension::Extendable; @@ -18,7 +18,7 @@ use plonky2::iop::target::Target; use plonky2::iop::wire::Wire; use plonky2::iop::witness::{PartitionWitness, Witness, WitnessWrite}; use plonky2::plonk::circuit_builder::CircuitBuilder; -use plonky2::plonk::circuit_data::CircuitConfig; +use plonky2::plonk::circuit_data::{CircuitConfig, CommonCircuitData}; use plonky2::plonk::vars::{ EvaluationTargets, EvaluationVars, EvaluationVarsBase, EvaluationVarsBaseBatch, EvaluationVarsBasePacked, @@ -93,13 +93,16 @@ impl, const D: usize> Gate for U32ArithmeticG format!("{self:?}") } - fn serialize(&self, dst: &mut Vec) -> IoResult<()> { + fn serialize(&self, dst: &mut Vec, _common_data: &CommonCircuitData) -> IoResult<()> { dst.write_usize(self.num_ops) } - fn deserialize(src: &mut Buffer) -> IoResult { + fn deserialize(src: &mut Buffer, _common_data: &CommonCircuitData) -> IoResult { let num_ops = src.read_usize()?; - Ok(Self { num_ops, _phantom: PhantomData }) + Ok(Self { + num_ops, + _phantom: PhantomData, + }) } fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { @@ -249,10 +252,10 @@ impl, const D: usize> Gate for U32ArithmeticG constraints } - fn generators(&self, row: usize, _local_constants: &[F]) -> Vec> { + fn generators(&self, row: usize, _local_constants: &[F]) -> Vec> { (0..self.num_ops) .map(|i| { - let g: WitnessGeneratorRef = WitnessGeneratorRef::new( + let g: WitnessGeneratorRef = WitnessGeneratorRef::new( U32ArithmeticGenerator { gate: *self, row, @@ -356,7 +359,7 @@ struct U32ArithmeticGenerator, const D: usize> { _phantom: PhantomData, } -impl, const D: usize> SimpleGenerator +impl, const D: usize> SimpleGenerator for U32ArithmeticGenerator { fn id(&self) -> String { @@ -425,17 +428,22 @@ impl, const D: usize> SimpleGenerator } } - fn serialize(&self, dst: &mut Vec) -> IoResult<()> { - self.gate.serialize(dst)?; + fn serialize(&self, dst: &mut Vec, common_data: &CommonCircuitData) -> IoResult<()> { + self.gate.serialize(dst, common_data)?; dst.write_usize(self.row)?; dst.write_usize(self.i) } - fn deserialize(src: &mut Buffer) -> IoResult { - let gate = U32ArithmeticGate::deserialize(src)?; + fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData) -> IoResult { + let gate = U32ArithmeticGate::deserialize(src, common_data)?; let row = src.read_usize()?; let i = src.read_usize()?; - Ok(Self { gate, row, i, _phantom: PhantomData }) + Ok(Self { + gate, + row, + i, + _phantom: PhantomData, + }) } } diff --git a/src/gates/comparison.rs b/src/gates/comparison.rs index e5524e3..5f23cab 100644 --- a/src/gates/comparison.rs +++ b/src/gates/comparison.rs @@ -1,8 +1,9 @@ use alloc::string::{String, ToString}; use alloc::vec::Vec; use alloc::{format, vec}; -use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use core::marker::PhantomData; +use plonky2::plonk::circuit_data::CommonCircuitData; +use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use plonky2::field::extension::Extendable; use plonky2::field::packed::PackedField; @@ -98,16 +99,20 @@ impl, const D: usize> Gate for ComparisonGate format!("{self:?}") } - fn serialize(&self, dst: &mut Vec) -> IoResult<()> { + fn serialize(&self, dst: &mut Vec, _common_data: &CommonCircuitData) -> IoResult<()> { dst.write_usize(self.num_bits)?; dst.write_usize(self.num_chunks)?; Ok(()) } - fn deserialize(src: &mut Buffer) -> IoResult { + fn deserialize(src: &mut Buffer, _common_data: &CommonCircuitData) -> IoResult { let num_bits = src.read_usize()?; let num_chunks = src.read_usize()?; - Ok(Self { num_bits, num_chunks, _phantom: PhantomData }) + Ok(Self { + num_bits, + num_chunks, + _phantom: PhantomData, + }) } fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { @@ -299,7 +304,7 @@ impl, const D: usize> Gate for ComparisonGate constraints } - fn generators(&self, row: usize, _local_constants: &[F]) -> Vec> { + fn generators(&self, row: usize, _local_constants: &[F]) -> Vec> { let gen = ComparisonGenerator:: { row, gate: self.clone(), @@ -413,7 +418,7 @@ struct ComparisonGenerator, const D: usize> { gate: ComparisonGate, } -impl, const D: usize> SimpleGenerator +impl, const D: usize> SimpleGenerator for ComparisonGenerator { fn id(&self) -> String { @@ -529,17 +534,16 @@ impl, const D: usize> SimpleGenerator } } - fn serialize(&self, dst: &mut Vec) -> IoResult<()> { + fn serialize(&self, dst: &mut Vec, common_data: &CommonCircuitData) -> IoResult<()> { dst.write_usize(self.row)?; - self.gate.serialize(dst) + self.gate.serialize(dst, common_data) } - fn deserialize(src: &mut Buffer) -> IoResult { + fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData) -> IoResult { let row = src.read_usize()?; - let gate = ComparisonGate::deserialize(src)?; + let gate = ComparisonGate::deserialize(src, common_data)?; Ok(Self { row, gate }) } - } #[cfg(test)] diff --git a/src/gates/range_check_u32.rs b/src/gates/range_check_u32.rs index 1ca9457..1b0d1e4 100644 --- a/src/gates/range_check_u32.rs +++ b/src/gates/range_check_u32.rs @@ -1,8 +1,9 @@ use alloc::string::{String, ToString}; use alloc::vec::Vec; use alloc::{format, vec}; -use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use core::marker::PhantomData; +use plonky2::plonk::circuit_data::CommonCircuitData; +use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use plonky2::field::extension::Extendable; use plonky2::field::types::Field; @@ -55,13 +56,16 @@ impl, const D: usize> Gate for U32RangeCheckG format!("{self:?}") } - fn serialize(&self, dst: &mut Vec) -> IoResult<()> { + fn serialize(&self, dst: &mut Vec, _common_data: &CommonCircuitData) -> IoResult<()> { dst.write_usize(self.num_input_limbs) } - fn deserialize(src: &mut Buffer) -> IoResult { + fn deserialize(src: &mut Buffer, _common_data: &CommonCircuitData) -> IoResult { let num_input_limbs = src.read_usize()?; - Ok(Self { num_input_limbs, _phantom: PhantomData }) + Ok(Self { + num_input_limbs, + _phantom: PhantomData, + }) } fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { @@ -147,7 +151,7 @@ impl, const D: usize> Gate for U32RangeCheckG constraints } - fn generators(&self, row: usize, _local_constants: &[F]) -> Vec> { + fn generators(&self, row: usize, _local_constants: &[F]) -> Vec> { let gen = U32RangeCheckGenerator { gate: *self, row }; vec![WitnessGeneratorRef::new(gen.adapter())] } @@ -177,7 +181,7 @@ pub struct U32RangeCheckGenerator, const D: usize> row: usize, } -impl, const D: usize> SimpleGenerator +impl, const D: usize> SimpleGenerator for U32RangeCheckGenerator { fn id(&self) -> String { @@ -215,17 +219,16 @@ impl, const D: usize> SimpleGenerator } } - fn serialize(&self, dst: &mut Vec) -> IoResult<()> { - self.gate.serialize(dst)?; + fn serialize(&self, dst: &mut Vec, common_data: &CommonCircuitData) -> IoResult<()> { + self.gate.serialize(dst, common_data)?; dst.write_usize(self.row) } - fn deserialize(src: &mut Buffer) -> IoResult { - let gate = U32RangeCheckGate::deserialize(src)?; + fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData) -> IoResult { + let gate = U32RangeCheckGate::deserialize(src, common_data)?; let row = src.read_usize()?; Ok(Self { row, gate }) } - } #[cfg(test)] diff --git a/src/gates/subtraction_u32.rs b/src/gates/subtraction_u32.rs index d329e22..74ce2ba 100644 --- a/src/gates/subtraction_u32.rs +++ b/src/gates/subtraction_u32.rs @@ -1,8 +1,8 @@ use alloc::string::{String, ToString}; use alloc::vec::Vec; use alloc::{format, vec}; -use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use core::marker::PhantomData; +use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use plonky2::field::extension::Extendable; use plonky2::field::packed::PackedField; @@ -17,7 +17,7 @@ use plonky2::iop::target::Target; use plonky2::iop::wire::Wire; use plonky2::iop::witness::{PartitionWitness, Witness, WitnessWrite}; use plonky2::plonk::circuit_builder::CircuitBuilder; -use plonky2::plonk::circuit_data::CircuitConfig; +use plonky2::plonk::circuit_data::{CircuitConfig, CommonCircuitData}; use plonky2::plonk::vars::{ EvaluationTargets, EvaluationVars, EvaluationVarsBase, EvaluationVarsBaseBatch, EvaluationVarsBasePacked, @@ -87,13 +87,16 @@ impl, const D: usize> Gate for U32Subtraction format!("{self:?}") } - fn serialize(&self, dst: &mut Vec) -> IoResult<()> { + fn serialize(&self, dst: &mut Vec, _common_data: &CommonCircuitData) -> IoResult<()> { dst.write_usize(self.num_ops) } - fn deserialize(src: &mut Buffer) -> IoResult { + fn deserialize(src: &mut Buffer, _common_data: &CommonCircuitData) -> IoResult { let num_ops = src.read_usize()?; - Ok(Self { num_ops, _phantom: PhantomData }) + Ok(Self { + num_ops, + _phantom: PhantomData, + }) } fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { @@ -195,7 +198,7 @@ impl, const D: usize> Gate for U32Subtraction constraints } - fn generators(&self, row: usize, _local_constants: &[F]) -> Vec> { + fn generators(&self, row: usize, _local_constants: &[F]) -> Vec> { (0..self.num_ops) .map(|i| { let g = WitnessGeneratorRef::new( @@ -279,7 +282,7 @@ struct U32SubtractionGenerator, const D: usize> { _phantom: PhantomData, } -impl, const D: usize> SimpleGenerator +impl, const D: usize> SimpleGenerator for U32SubtractionGenerator { fn id(&self) -> String { @@ -343,19 +346,23 @@ impl, const D: usize> SimpleGenerator } } - fn serialize(&self, dst: &mut Vec) -> IoResult<()> { - self.gate.serialize(dst)?; + fn serialize(&self, dst: &mut Vec, common_data: &CommonCircuitData) -> IoResult<()> { + self.gate.serialize(dst, common_data)?; dst.write_usize(self.row)?; dst.write_usize(self.i) } - fn deserialize(src: &mut Buffer) -> IoResult { - let gate = U32SubtractionGate::deserialize(src)?; + fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData) -> IoResult { + let gate = U32SubtractionGate::deserialize(src, common_data)?; let row = src.read_usize()?; let i = src.read_usize()?; - Ok(Self { gate, row, i, _phantom: PhantomData }) + Ok(Self { + gate, + row, + i, + _phantom: PhantomData, + }) } - } #[cfg(test)] diff --git a/src/serialization.rs b/src/serialization.rs index d64c6f8..7beca40 100644 --- a/src/serialization.rs +++ b/src/serialization.rs @@ -18,7 +18,7 @@ pub trait ReadU32 { fn read_target_u32(&mut self) -> IoResult; } -impl ReadU32 for Buffer { +impl ReadU32 for Buffer<'_> { #[inline] fn read_target_u32(&mut self) -> IoResult { Ok(U32Target(self.read_target()?))