Skip to content

Commit

Permalink
Merge pull request #4 from jorgeantonio21/main
Browse files Browse the repository at this point in the history
fix: resolve compile errors
  • Loading branch information
dlubarov authored Sep 19, 2023
2 parents 945ff60 + e80513d commit fa4f324
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 68 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
16 changes: 11 additions & 5 deletions src/gadgets/arithmetic_u32.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -241,24 +242,29 @@ struct SplitToU32Generator<F: RichField + Extendable<D>, const D: usize> {
_phantom: PhantomData<F>,
}

impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>
for SplitToU32Generator<F, D>
{
fn id(&self) -> String {
"SplitToU32Generator".to_string()
}

fn serialize(&self, dst: &mut Vec<u8>) -> IoResult<()> {
fn serialize(&self, dst: &mut Vec<u8>, _common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
dst.write_target(self.x)?;
dst.write_target_u32(self.low)?;
dst.write_target_u32(self.high)
}

fn deserialize(src: &mut Buffer) -> IoResult<Self> {
fn deserialize(src: &mut Buffer, _common_data: &CommonCircuitData<F, D>) -> IoResult<Self> {
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<Target> {
Expand Down
33 changes: 21 additions & 12 deletions src/gates/add_many_u32.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -91,15 +91,19 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for U32AddManyGate
format!("{self:?}")
}

fn serialize(&self, dst: &mut Vec<u8>) -> IoResult<()> {
fn serialize(&self, dst: &mut Vec<u8>, _common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
dst.write_usize(self.num_addends)?;
dst.write_usize(self.num_ops)
}

fn deserialize(src: &mut Buffer) -> IoResult<Self> {
fn deserialize(src: &mut Buffer, _common_data: &CommonCircuitData<F, D>) -> IoResult<Self> {
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<F, D>) -> Vec<F::Extension> {
Expand Down Expand Up @@ -247,7 +251,7 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for U32AddManyGate
constraints
}

fn generators(&self, row: usize, _local_constants: &[F]) -> Vec<WitnessGeneratorRef<F>> {
fn generators(&self, row: usize, _local_constants: &[F]) -> Vec<WitnessGeneratorRef<F, D>> {
(0..self.num_ops)
.map(|i| {
WitnessGeneratorRef::new(
Expand Down Expand Up @@ -288,24 +292,29 @@ struct U32AddManyGenerator<F: RichField + Extendable<D>, const D: usize> {
_phantom: PhantomData<F>,
}

impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>
for U32AddManyGenerator<F, D>
{
fn id(&self) -> String {
"U32AddManyGenerator".to_string()
}

fn serialize(&self, dst: &mut Vec<u8>) -> IoResult<()> {
self.gate.serialize(dst)?;
fn serialize(&self, dst: &mut Vec<u8>, common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
self.gate.serialize(dst, common_data)?;
dst.write_usize(self.row)?;
dst.write_usize(self.i)
}

fn deserialize(src: &mut Buffer) -> IoResult<Self> {
let gate = U32AddManyGate::deserialize(src)?;
fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData<F, D>) -> IoResult<Self> {
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<Target> {
Expand Down
34 changes: 21 additions & 13 deletions src/gates/arithmetic_u32.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -93,13 +93,16 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for U32ArithmeticG
format!("{self:?}")
}

fn serialize(&self, dst: &mut Vec<u8>) -> IoResult<()> {
fn serialize(&self, dst: &mut Vec<u8>, _common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
dst.write_usize(self.num_ops)
}

fn deserialize(src: &mut Buffer) -> IoResult<Self> {
fn deserialize(src: &mut Buffer, _common_data: &CommonCircuitData<F, D>) -> IoResult<Self> {
let num_ops = src.read_usize()?;
Ok(Self { num_ops, _phantom: PhantomData })
Ok(Self {
num_ops,
_phantom: PhantomData,
})
}

fn eval_unfiltered(&self, vars: EvaluationVars<F, D>) -> Vec<F::Extension> {
Expand Down Expand Up @@ -249,10 +252,10 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for U32ArithmeticG
constraints
}

fn generators(&self, row: usize, _local_constants: &[F]) -> Vec<WitnessGeneratorRef<F>> {
fn generators(&self, row: usize, _local_constants: &[F]) -> Vec<WitnessGeneratorRef<F, D>> {
(0..self.num_ops)
.map(|i| {
let g: WitnessGeneratorRef<F> = WitnessGeneratorRef::new(
let g: WitnessGeneratorRef<F, D> = WitnessGeneratorRef::new(
U32ArithmeticGenerator {
gate: *self,
row,
Expand Down Expand Up @@ -356,7 +359,7 @@ struct U32ArithmeticGenerator<F: RichField + Extendable<D>, const D: usize> {
_phantom: PhantomData<F>,
}

impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>
for U32ArithmeticGenerator<F, D>
{
fn id(&self) -> String {
Expand Down Expand Up @@ -425,17 +428,22 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
}
}

fn serialize(&self, dst: &mut Vec<u8>) -> IoResult<()> {
self.gate.serialize(dst)?;
fn serialize(&self, dst: &mut Vec<u8>, common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
self.gate.serialize(dst, common_data)?;
dst.write_usize(self.row)?;
dst.write_usize(self.i)
}

fn deserialize(src: &mut Buffer) -> IoResult<Self> {
let gate = U32ArithmeticGate::deserialize(src)?;
fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData<F, D>) -> IoResult<Self> {
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,
})
}
}

Expand Down
26 changes: 15 additions & 11 deletions src/gates/comparison.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -98,16 +99,20 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for ComparisonGate
format!("{self:?}<D={D}>")
}

fn serialize(&self, dst: &mut Vec<u8>) -> IoResult<()> {
fn serialize(&self, dst: &mut Vec<u8>, _common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
dst.write_usize(self.num_bits)?;
dst.write_usize(self.num_chunks)?;
Ok(())
}

fn deserialize(src: &mut Buffer) -> IoResult<Self> {
fn deserialize(src: &mut Buffer, _common_data: &CommonCircuitData<F, D>) -> IoResult<Self> {
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<F, D>) -> Vec<F::Extension> {
Expand Down Expand Up @@ -299,7 +304,7 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for ComparisonGate
constraints
}

fn generators(&self, row: usize, _local_constants: &[F]) -> Vec<WitnessGeneratorRef<F>> {
fn generators(&self, row: usize, _local_constants: &[F]) -> Vec<WitnessGeneratorRef<F, D>> {
let gen = ComparisonGenerator::<F, D> {
row,
gate: self.clone(),
Expand Down Expand Up @@ -413,7 +418,7 @@ struct ComparisonGenerator<F: RichField + Extendable<D>, const D: usize> {
gate: ComparisonGate<F, D>,
}

impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>
for ComparisonGenerator<F, D>
{
fn id(&self) -> String {
Expand Down Expand Up @@ -529,17 +534,16 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
}
}

fn serialize(&self, dst: &mut Vec<u8>) -> IoResult<()> {
fn serialize(&self, dst: &mut Vec<u8>, common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
dst.write_usize(self.row)?;
self.gate.serialize(dst)
self.gate.serialize(dst, common_data)
}

fn deserialize(src: &mut Buffer) -> IoResult<Self> {
fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData<F, D>) -> IoResult<Self> {
let row = src.read_usize()?;
let gate = ComparisonGate::deserialize(src)?;
let gate = ComparisonGate::deserialize(src, common_data)?;
Ok(Self { row, gate })
}

}

#[cfg(test)]
Expand Down
25 changes: 14 additions & 11 deletions src/gates/range_check_u32.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -55,13 +56,16 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for U32RangeCheckG
format!("{self:?}")
}

fn serialize(&self, dst: &mut Vec<u8>) -> IoResult<()> {
fn serialize(&self, dst: &mut Vec<u8>, _common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
dst.write_usize(self.num_input_limbs)
}

fn deserialize(src: &mut Buffer) -> IoResult<Self> {
fn deserialize(src: &mut Buffer, _common_data: &CommonCircuitData<F, D>) -> IoResult<Self> {
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<F, D>) -> Vec<F::Extension> {
Expand Down Expand Up @@ -147,7 +151,7 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for U32RangeCheckG
constraints
}

fn generators(&self, row: usize, _local_constants: &[F]) -> Vec<WitnessGeneratorRef<F>> {
fn generators(&self, row: usize, _local_constants: &[F]) -> Vec<WitnessGeneratorRef<F, D>> {
let gen = U32RangeCheckGenerator { gate: *self, row };
vec![WitnessGeneratorRef::new(gen.adapter())]
}
Expand Down Expand Up @@ -177,7 +181,7 @@ pub struct U32RangeCheckGenerator<F: RichField + Extendable<D>, const D: usize>
row: usize,
}

impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>
for U32RangeCheckGenerator<F, D>
{
fn id(&self) -> String {
Expand Down Expand Up @@ -215,17 +219,16 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F>
}
}

fn serialize(&self, dst: &mut Vec<u8>) -> IoResult<()> {
self.gate.serialize(dst)?;
fn serialize(&self, dst: &mut Vec<u8>, common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
self.gate.serialize(dst, common_data)?;
dst.write_usize(self.row)
}

fn deserialize(src: &mut Buffer) -> IoResult<Self> {
let gate = U32RangeCheckGate::deserialize(src)?;
fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData<F, D>) -> IoResult<Self> {
let gate = U32RangeCheckGate::deserialize(src, common_data)?;
let row = src.read_usize()?;
Ok(Self { row, gate })
}

}

#[cfg(test)]
Expand Down
Loading

0 comments on commit fa4f324

Please sign in to comment.