Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Implement Default for DefaultGateSerializer and `DefaultGenerator…
Browse files Browse the repository at this point in the history
…Serializer` (0xPolygonZero#1531)

* Implement Default for DefaultGateSerializer and DefaultGeneratorSerializer

* Apply comments

* Update doc

* Clippy
  • Loading branch information
Nashtare authored Feb 18, 2024
1 parent 3e579b6 commit cfe4448
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
5 changes: 2 additions & 3 deletions plonky2/examples/square_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>
}
}

#[derive(Default)]
pub struct CustomGeneratorSerializer<C: GenericConfig<D>, const D: usize> {
pub _phantom: PhantomData<C>,
}
Expand Down Expand Up @@ -131,9 +132,7 @@ fn main() -> Result<()> {
// Test serialization
{
let gate_serializer = DefaultGateSerializer;
let generator_serializer = CustomGeneratorSerializer {
_phantom: PhantomData::<C>,
};
let generator_serializer = CustomGeneratorSerializer::<C, D>::default();

let data_bytes = data
.to_bytes(&gate_serializer, &generator_serializer)
Expand Down
4 changes: 2 additions & 2 deletions plonky2/src/plonk/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub trait GenericConfig<const D: usize>:
}

/// Configuration using Poseidon over the Goldilocks field.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize)]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Serialize)]
pub struct PoseidonGoldilocksConfig;
impl GenericConfig<2> for PoseidonGoldilocksConfig {
type F = GoldilocksField;
Expand All @@ -116,7 +116,7 @@ impl GenericConfig<2> for PoseidonGoldilocksConfig {
}

/// Configuration using truncated Keccak over the Goldilocks field.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
pub struct KeccakGoldilocksConfig;
impl GenericConfig<2> for KeccakGoldilocksConfig {
type F = GoldilocksField;
Expand Down
9 changes: 9 additions & 0 deletions plonky2/src/util/serialization/gate_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ pub mod default {
use crate::hash::hash_types::RichField;
use crate::util::serialization::GateSerializer;

/// A gate serializer that can be used to serialize all default gates supported
/// by the `plonky2` library.
/// Being a unit struct, it can be simply called as
/// ```rust
/// use plonky2::util::serialization::DefaultGateSerializer;
/// let gate_serializer = DefaultGateSerializer;
/// ```
/// Applications using custom gates should define their own serializer implementing
/// the `GateSerializer` trait. This can be easily done through the `impl_gate_serializer` macro.
pub struct DefaultGateSerializer;
impl<F: RichField + Extendable<D>, const D: usize> GateSerializer<F, D> for DefaultGateSerializer {
impl_gate_serializer! {
Expand Down
14 changes: 14 additions & 0 deletions plonky2/src/util/serialization/generator_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ pub mod default {
use crate::recursion::dummy_circuit::DummyProofGenerator;
use crate::util::serialization::WitnessGeneratorSerializer;

/// A generator serializer that can be used to serialize all default generators supported
/// by the `plonky2` library. It can simply be called as
/// ```rust
/// use plonky2::util::serialization::DefaultGeneratorSerializer;
/// use plonky2::plonk::config::PoseidonGoldilocksConfig;
///
/// const D: usize = 2;
/// type C = PoseidonGoldilocksConfig;
/// let generator_serializer = DefaultGeneratorSerializer::<C, D>::default();
/// ```
/// Applications using custom generators should define their own serializer implementing
/// the `WitnessGeneratorSerializer` trait. This can be easily done through the
/// `impl_generator_serializer` macro.
#[derive(Default)]
pub struct DefaultGeneratorSerializer<C: GenericConfig<D>, const D: usize> {
pub _phantom: PhantomData<C>,
}
Expand Down

0 comments on commit cfe4448

Please sign in to comment.