Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Derive Eq, Hash at a bunch of places #362

Merged
merged 6 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and follow [semantic versioning](https://semver.org/) for our releases.
- [#337](https://github.com/EspressoSystems/jellyfish/pull/337) Port VID from another repo
- [#341](https://github.com/EspressoSystems/jellyfish/pull/341) Port VDF from another repo
- [#343](https://github.com/EspressoSystems/jellyfish/pull/343) Rescue parameter for `ark_bn254::Fq`
- [#362](https://github.com/EspressoSystems/jellyfish/pull/362) Derive Eq, Hash at a bunch of places

### Changed

Expand Down
2 changes: 1 addition & 1 deletion plonk/src/proof_system/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ where
// Compute coefficients for selector polynomial commitments.
// The order: q_lc, q_mul, q_hash, q_o, q_c, q_ecc
// TODO(binyi): get the order from a function.
let mut q_scalars = vec![E::ScalarField::zero(); 2 * GATE_WIDTH + 5];
let mut q_scalars = [E::ScalarField::zero(); 2 * GATE_WIDTH + 5];
q_scalars[0] = w_evals[0];
q_scalars[1] = w_evals[1];
q_scalars[2] = w_evals[2];
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/merkle_tree/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use sha3::{Digest, Sha3_256};
use typenum::U3;

/// Element type for interval merkle tree
#[derive(PartialEq, Eq, Copy, Clone)]
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
pub struct Interval<F: Field>(pub F, pub F);
// impl<F: Field> Element for Interval<F> {}

Expand Down
73 changes: 29 additions & 44 deletions primitives/src/merkle_tree/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ impl<T: NodeValue> MerkleCommitment<T> for MerkleTreeCommitment<T> {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Derivative, Debug, Clone, Serialize, Deserialize)]
#[derivative(Eq, Hash, PartialEq)]
#[serde(bound = "E: CanonicalSerialize + CanonicalDeserialize,
I: CanonicalSerialize + CanonicalDeserialize,")]
pub struct MerkleProof<E, I, T, Arity>
Expand Down Expand Up @@ -831,27 +832,19 @@ where
.to_traversal_path(self.tree_height() - 1)
.iter()
.zip(self.proof.iter().skip(1))
.fold(
Ok(init),
|result, (branch, node)| -> Result<T, PrimitivesError> {
match result {
Ok(val) => match node {
MerkleNode::Branch { value: _, children } => {
let mut data = children
.iter()
.map(|node| node.value())
.collect::<Vec<_>>();
data[*branch] = val;
H::digest(&data)
},
_ => Err(PrimitivesError::ParameterError(
"Incompatible proof for this merkle tree".to_string(),
)),
},
Err(e) => Err(e),
}
},
)?;
.try_fold(init, |val, (branch, node)| -> Result<T, PrimitivesError> {
match node {
MerkleNode::Branch { value: _, children } => {
let mut data =
children.iter().map(|node| node.value()).collect::<Vec<_>>();
data[*branch] = val;
H::digest(&data)
},
_ => Err(PrimitivesError::ParameterError(
"Incompatible proof for this merkle tree".to_string(),
)),
}
})?;
if computed_root == *expected_root {
Ok(Ok(()))
} else {
Expand Down Expand Up @@ -880,28 +873,20 @@ where
.to_traversal_path(self.tree_height() - 1)
.iter()
.zip(self.proof.iter().skip(1))
.fold(
Ok(init),
|result, (branch, node)| -> Result<T, PrimitivesError> {
match result {
Ok(val) => match node {
MerkleNode::Branch { value: _, children } => {
let mut data = children
.iter()
.map(|node| node.value())
.collect::<Vec<_>>();
data[*branch] = val;
H::digest(&data)
},
MerkleNode::Empty => Ok(init),
_ => Err(PrimitivesError::ParameterError(
"Incompatible proof for this merkle tree".to_string(),
)),
},
Err(e) => Err(e),
}
},
)?;
.try_fold(init, |val, (branch, node)| -> Result<T, PrimitivesError> {
match node {
MerkleNode::Branch { value: _, children } => {
let mut data =
children.iter().map(|node| node.value()).collect::<Vec<_>>();
data[*branch] = val;
H::digest(&data)
},
MerkleNode::Empty => Ok(init),
_ => Err(PrimitivesError::ParameterError(
"Incompatible proof for this merkle tree".to_string(),
)),
}
})?;
Ok(computed_root == *expected_root)
} else {
Err(PrimitivesError::ParameterError(
Expand Down
6 changes: 3 additions & 3 deletions primitives/src/merkle_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ impl<F, P, N> LookupResult<F, P, N> {
}

/// An element of a Merkle tree.
pub trait Element: Clone + Eq + PartialEq {}
impl<T: Clone + Eq + PartialEq> Element for T {}
pub trait Element: Clone + Eq + PartialEq + Hash {}
impl<T: Clone + Eq + PartialEq + Hash> Element for T {}

/// An index type of a leaf in a Merkle tree.
pub trait Index: Debug + Eq + PartialEq + Hash + Ord + PartialOrd + Clone {}
Expand Down Expand Up @@ -187,7 +187,7 @@ pub trait MerkleTreeScheme: Sized {
/// Internal and root node value
type NodeValue: NodeValue;
/// Merkle proof
type MembershipProof: Clone;
type MembershipProof: Clone + Eq + Hash;
/// Batch proof
type BatchMembershipProof: Clone;
/// Merkle tree commitment
Expand Down
22 changes: 10 additions & 12 deletions primitives/src/merkle_tree/universal_merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ where
if matches!(&proof.proof[0], MerkleNode::Empty) {
let empty_value = T::default();
let mut path_values = vec![empty_value];
traversal_path.iter().zip(proof.proof.iter().skip(1)).fold(
Ok(empty_value),
|result: Result<T, PrimitivesError>,
(branch, node)|
-> Result<T, PrimitivesError> {
match result {
Ok(val) => match node {
traversal_path
.iter()
.zip(proof.proof.iter().skip(1))
.try_fold(
empty_value,
|val: T, (branch, node)| -> Result<T, PrimitivesError> {
match node {
MerkleNode::Branch { value: _, children } => {
let mut data: Vec<_> =
children.iter().map(|node| node.value()).collect();
Expand All @@ -162,11 +162,9 @@ where
_ => Err(PrimitivesError::ParameterError(
"Incompatible proof for this merkle tree".to_string(),
)),
},
Err(e) => Err(e),
}
},
)?;
}
},
)?;
self.root.remember_internal::<H, Arity>(
self.height,
&traversal_path,
Expand Down
10 changes: 8 additions & 2 deletions primitives/src/pcs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ pub trait PolynomialCommitmentScheme {
/// Polynomial Evaluation
type Evaluation: Field;
/// Commitments
type Commitment: Clone + CanonicalSerialize + CanonicalDeserialize + Debug + PartialEq + Eq;
type Commitment: Clone
+ CanonicalSerialize
+ CanonicalDeserialize
+ Debug
+ PartialEq
+ Eq
+ Hash;
/// Batch commitments
type BatchCommitment: Clone + CanonicalSerialize + CanonicalDeserialize + Debug + PartialEq + Eq;
/// Proofs
type Proof: Clone + CanonicalSerialize + CanonicalDeserialize + Debug + PartialEq + Eq;
type Proof: Clone + CanonicalSerialize + CanonicalDeserialize + Debug + PartialEq + Eq + Hash;
/// Batch proofs
type BatchProof: Clone + CanonicalSerialize + CanonicalDeserialize + Debug + PartialEq + Eq;

Expand Down
3 changes: 2 additions & 1 deletion primitives/src/pcs/multilinear_kzg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ pub struct MultilinearKzgPCS<E: Pairing> {
phantom: PhantomData<E>,
}

#[derive(CanonicalSerialize, CanonicalDeserialize, Clone, Debug, PartialEq, Eq)]
#[derive(Derivative, CanonicalSerialize, CanonicalDeserialize, Clone, Debug, PartialEq, Eq)]
#[derivative(Hash)]
/// proof of opening
pub struct MultilinearKzgProof<E: Pairing> {
/// Evaluation of quotients
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/pcs/poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ where
let coeffs = self
.coeffs
.into_iter()
.zip_longest(rhs.coeffs.into_iter())
.zip_longest(rhs.coeffs)
.map(|pair| match pair {
Both(x, y) => x + y,
Left(x) | Right(x) => x,
Expand Down
12 changes: 3 additions & 9 deletions primitives/src/pcs/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@ use ark_ec::{pairing::Pairing, AffineRepr};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::vec::Vec;

#[derive(Derivative, CanonicalSerialize, CanonicalDeserialize)]
#[derivative(
Default(bound = ""),
Hash(bound = ""),
Clone(bound = ""),
Copy(bound = ""),
Debug(bound = ""),
PartialEq(bound = ""),
Eq(bound = "")
#[derive(
Derivative, Clone, Copy, Debug, PartialEq, Eq, CanonicalSerialize, CanonicalDeserialize,
)]
#[derivative(Default, Hash)]
/// A commitment is an Affine point.
pub struct Commitment<E: Pairing>(
/// the actual commitment is an affine point.
Expand Down
3 changes: 2 additions & 1 deletion primitives/src/pcs/univariate_kzg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ pub struct UnivariateKzgPCS<E: Pairing> {
phantom: PhantomData<E>,
}

#[derive(CanonicalSerialize, CanonicalDeserialize, Clone, Debug, PartialEq, Eq)]
#[derive(Derivative, CanonicalSerialize, CanonicalDeserialize, Clone, Debug, PartialEq, Eq)]
#[derivative(Hash)]
/// proof of opening
pub struct UnivariateKzgProof<E: Pairing> {
/// Evaluation of quotients
Expand Down
11 changes: 3 additions & 8 deletions primitives/src/pcs/univariate_kzg/srs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,10 @@ pub struct UnivariateProverParam<E: Pairing> {

/// `UnivariateVerifierParam` is used to check evaluation proofs for a given
/// commitment.
#[derive(Derivative, CanonicalSerialize, CanonicalDeserialize)]
#[derivative(
Default(bound = ""),
Clone(bound = ""),
Copy(bound = ""),
Debug(bound = ""),
PartialEq(bound = ""),
Eq(bound = "")
#[derive(
Derivative, Clone, Copy, Debug, Eq, CanonicalSerialize, CanonicalDeserialize, PartialEq,
)]
#[derivative(Default)]
pub struct UnivariateVerifierParam<E: Pairing> {
/// The generator of G1.
pub g: E::G1Affine,
Expand Down
4 changes: 2 additions & 2 deletions primitives/src/vid/advz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ where
#[derive(Derivative, Deserialize, Serialize)]
// TODO https://github.com/EspressoSystems/jellyfish/issues/253
// #[derivative(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derivative(Clone, Debug)]
#[derivative(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Share<P, V>
where
P: PolynomialCommitmentScheme,
Expand All @@ -131,7 +131,7 @@ where
#[derive(CanonicalSerialize, CanonicalDeserialize, Derivative, Deserialize, Serialize)]
// TODO https://github.com/EspressoSystems/jellyfish/issues/253
// #[derivative(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derivative(Clone, Debug, Default, Eq, PartialEq)]
#[derivative(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct Common<P, V>
where
P: PolynomialCommitmentScheme,
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/vid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub trait VidScheme {
type Commitment: Clone + Debug + Eq + PartialEq + Sync; // TODO https://github.com/EspressoSystems/jellyfish/issues/253

/// Share-specific data sent to a storage node.
type StorageShare: Clone + Debug + Sync; // TODO https://github.com/EspressoSystems/jellyfish/issues/253
type StorageShare: Clone + Debug + Eq + Sync; // TODO https://github.com/EspressoSystems/jellyfish/issues/253

/// Common data sent to all storage nodes.
type StorageCommon: CanonicalSerialize + CanonicalDeserialize + Clone + Eq + PartialEq + Sync; // TODO https://github.com/EspressoSystems/jellyfish/issues/253
Expand Down