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

Canonical serialize trait #255

Merged
merged 1 commit into from
May 29, 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
20 changes: 12 additions & 8 deletions mpc/mpc-core/src/commit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//! This module provides a hash commitment scheme for types which implement `serde::Serialize`
//! This module provides a hash commitment scheme for types which implement
//! [`CanonicalSerialize`](crate::serialize::CanonicalSerialize)

use crate::hash::{Hash, SecureHash};
use crate::{
hash::{Hash, SecureHash},
serialize::CanonicalSerialize,
};
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};

Expand All @@ -27,15 +31,15 @@ impl Nonce {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Decommitment<T>
where
T: Serialize,
T: CanonicalSerialize,
{
nonce: Nonce,
data: T,
}

impl<T> Decommitment<T>
where
T: Serialize,
T: CanonicalSerialize,
{
/// Creates a new decommitment
pub fn new(data: T) -> Self {
Expand Down Expand Up @@ -80,10 +84,10 @@ where
}
}

/// A trait for committing to arbitrary data which implements `serde::Serialize`
/// A trait for committing to arbitrary data which implements [`CanonicalSerialize`](crate::serialize::CanonicalSerialize)
pub trait HashCommit
where
Self: serde::Serialize + Sized,
Self: CanonicalSerialize + Sized,
{
/// Creates a hash commitment to self
fn hash_commit(self) -> (Decommitment<Self>, Hash) {
Expand Down Expand Up @@ -113,7 +117,7 @@ mod test {
let message = [0, 1, 2, 3u8];
let (mut decommitment, commitment) = message.hash_commit();

decommitment.nonce.0[0] = decommitment.nonce.0[0] - 1;
decommitment.nonce.0[0] -= 1;

let err = decommitment.verify(&commitment).unwrap_err();

Expand All @@ -125,7 +129,7 @@ mod test {
let message = [0, 1, 2, 3u8];
let (mut decommitment, commitment) = message.hash_commit();

decommitment.data[0] = decommitment.data[0] + 1;
decommitment.data[0] += 1;

let err = decommitment.verify(&commitment).unwrap_err();

Expand Down
10 changes: 5 additions & 5 deletions mpc/mpc-core/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use blake3::Hasher;
use serde::{Deserialize, Serialize};

use crate::serialize::CanonicalSerialize;

/// A secure hash
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Hash([u8; 32]);
Expand All @@ -27,12 +29,11 @@ impl From<[u8; 32]> for Hash {
/// A trait for hashing serde serializable types
pub trait SecureHash
where
Self: serde::Serialize,
Self: CanonicalSerialize,
{
/// Creates a hash of self
fn hash(&self) -> Hash {
let bytes = bcs::to_bytes(self).expect("serialization should not fail");
Hash(blake3::hash(&bytes).into())
Hash(blake3::hash(&self.to_bytes()).into())
}
}

Expand All @@ -48,9 +49,8 @@ where

/// Creates a hash of self with a domain separator
fn domain_separated_hash(&self) -> Hash {
let bytes = bcs::to_bytes(self).expect("serialization should not fail");
let mut hasher = Self::hasher();
hasher.update(&bytes);
hasher.update(&self.to_bytes());
Hash(hasher.finalize().into())
}
}
Expand Down
1 change: 1 addition & 0 deletions mpc/mpc-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
mod block;
pub mod commit;
pub mod hash;
pub mod serialize;
pub mod utils;
pub mod value;

Expand Down
14 changes: 14 additions & 0 deletions mpc/mpc-core/src/serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Traits for canonical serialization of serde serializable types.

/// A trait for canonical serialization of serde serializable types.
///
/// This trait provides a default implementation which uses
/// [Binary Canonical Serialization (BCS)](https://docs.rs/bcs/latest/bcs/).
pub trait CanonicalSerialize: serde::Serialize {
/// Serializes self into a byte vector
fn to_bytes(&self) -> Vec<u8> {
bcs::to_bytes(self).expect("serialization should not fail")
}
}

impl<T> CanonicalSerialize for T where T: serde::Serialize {}
Loading