Skip to content

Commit

Permalink
Derive Default and Eq where possible
Browse files Browse the repository at this point in the history
We can't use Rust's default deriving mechanism, until
rust-lang/rust#26925 and
rust-lang/rust#7671 are fixed.

But fortunately, there's a crate for that.
  • Loading branch information
matthiasgoergens committed Apr 5, 2024
1 parent 53c5bc3 commit 8acdee3
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 43 deletions.
1 change: 1 addition & 0 deletions plonky2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ web-time = { version = "1.0.0", optional = true }
plonky2_field = { version = "0.2.1", path = "../field", default-features = false }
plonky2_maybe_rayon = { version = "0.2.0", path = "../maybe_rayon", default-features = false }
plonky2_util = { version = "0.2.0", path = "../util", default-features = false }
derivative = { version = "2.2", features = ["use_core"] }


[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
Expand Down
17 changes: 2 additions & 15 deletions plonky2/src/fri/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use crate::util::{log2_strict, reverse_bits, reverse_index_bits_in_place, transp
pub const SALT_SIZE: usize = 4;

/// Represents a FRI oracle, i.e. a batch of polynomials which have been Merklized.
#[derive(Eq, PartialEq, Debug)]
#[derive(Eq, PartialEq, Debug, derivative::Derivative)]
#[derivative(Default)]
pub struct PolynomialBatch<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
{
pub polynomials: Vec<PolynomialCoeffs<F>>,
Expand All @@ -36,20 +37,6 @@ pub struct PolynomialBatch<F: RichField + Extendable<D>, C: GenericConfig<D, F =
pub blinding: bool,
}

impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize> Default
for PolynomialBatch<F, C, D>
{
fn default() -> Self {
PolynomialBatch {
polynomials: Vec::new(),
merkle_tree: MerkleTree::default(),
degree_log: 0,
rate_bits: 0,
blinding: false,
}
}
}

impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
PolynomialBatch<F, C, D>
{
Expand Down
9 changes: 5 additions & 4 deletions plonky2/src/gates/gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,11 @@ impl<T: Gate<F, D>, F: RichField + Extendable<D>, const D: usize> AnyGate<F, D>
}

/// A wrapper around an `Arc<AnyGate>` which implements `PartialEq`, `Eq` and `Hash` based on gate IDs.
#[derive(Clone)]
pub struct GateRef<F: RichField + Extendable<D>, const D: usize>(pub Arc<dyn AnyGate<F, D>>);
#[derive(Clone, derivative::Derivative)]
#[derivative(Eq)]
pub struct GateRef<F: RichField + Extendable<D>, const D: usize>(
#[derivative(Eq(bound = ""))] pub Arc<dyn AnyGate<F, D>>,
);

impl<F: RichField + Extendable<D>, const D: usize> GateRef<F, D> {
pub fn new<G: Gate<F, D>>(gate: G) -> GateRef<F, D> {
Expand All @@ -286,8 +289,6 @@ impl<F: RichField + Extendable<D>, const D: usize> Hash for GateRef<F, D> {
}
}

impl<F: RichField + Extendable<D>, const D: usize> Eq for GateRef<F, D> {}

impl<F: RichField + Extendable<D>, const D: usize> Debug for GateRef<F, D> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(f, "{}", self.0.id())
Expand Down
8 changes: 1 addition & 7 deletions plonky2/src/hash/hash_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl RichField for GoldilocksField {}
pub const NUM_HASH_OUT_ELTS: usize = 4;

/// Represents a ~256 bit hash output.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct HashOut<F: Field> {
pub elements: [F; NUM_HASH_OUT_ELTS],
Expand Down Expand Up @@ -106,12 +106,6 @@ impl<F: RichField> GenericHashOut<F> for HashOut<F> {
}
}

impl<F: Field> Default for HashOut<F> {
fn default() -> Self {
Self::ZERO
}
}

/// Represents a ~256 bit hash output.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct HashOutTarget {
Expand Down
21 changes: 4 additions & 17 deletions plonky2/src/hash/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ use crate::util::log2_strict;
/// It can be used in place of the root to verify Merkle paths, which are `h` elements shorter.
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(bound = "")]
#[derive(derivative::Derivative)]
#[derivative(Default)]
// TODO: Change H to GenericHashOut<F>, since this only cares about the hash, not the hasher.
pub struct MerkleCap<F: RichField, H: Hasher<F>>(pub Vec<H::Hash>);

impl<F: RichField, H: Hasher<F>> Default for MerkleCap<F, H> {
fn default() -> Self {
Self(Vec::new())
}
}

impl<F: RichField, H: Hasher<F>> MerkleCap<F, H> {
pub fn len(&self) -> usize {
self.0.len()
Expand All @@ -42,7 +38,8 @@ impl<F: RichField, H: Hasher<F>> MerkleCap<F, H> {
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq, derivative::Derivative)]
#[derivative(Default(bound = ""))]
pub struct MerkleTree<F: RichField, H: Hasher<F>> {
/// The data in the leaves of the Merkle tree.
pub leaves: Vec<Vec<F>>,
Expand All @@ -61,16 +58,6 @@ pub struct MerkleTree<F: RichField, H: Hasher<F>> {
pub cap: MerkleCap<F, H>,
}

impl<F: RichField, H: Hasher<F>> Default for MerkleTree<F, H> {
fn default() -> Self {
Self {
leaves: Vec::new(),
digests: Vec::new(),
cap: MerkleCap::default(),
}
}
}

fn capacity_up_to_mut<T>(v: &mut Vec<T>, len: usize) -> &mut [MaybeUninit<T>] {
assert!(v.capacity() >= len);
let v_ptr = v.as_mut_ptr().cast::<MaybeUninit<T>>();
Expand Down

0 comments on commit 8acdee3

Please sign in to comment.