Skip to content

Commit

Permalink
make serialization consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
conradoplg committed Jun 19, 2024
1 parent 8be644f commit efa9294
Show file tree
Hide file tree
Showing 36 changed files with 419 additions and 561 deletions.
12 changes: 12 additions & 0 deletions frost-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ Entries are listed in reverse chronological order.
`VerifiableSecretSharingCommitment::serialize()`,
`NonceCommitment::serialize()`, `Signature::serialize()`,
`VerifyingKey::serialize()` can now all return an error.
* Changed the `serialize()` and `deserialize()` methods of all Scalar- and
Element-wrapping structs; instead of taking or returning a
`Field::Serialization` or `Element::Serialization` traits (which are usually
defined by ciphersuites as arrays of specific sizes), they simply respectively
take `&[u8]` and return `Vec<u8>`, exactly as the other structs, which should
greatly simplify non-serde serialization code. You can port existing code with
e.g. `x.serialize().as_ref()` -> `x.serialize()` and
`X::deserialize(bytes.try_into().unwrap())` -> `X::deserialize(&bytes)`.
* Removed the `ops::{Mul, MulAssign, Sub}` implementation for `Identifier`.
These were being used internally, but library users shouldn't need to use them.
If you have low-level code that relied on it, use `Identifier::{new,
to_scalar}` to handle the underlying scalar.
* Removed `batch::Item::into()` which created a batch Item from a triple of
VerifyingKey, Signature and message. Use the new `batch::Item::new()` instead
(which can return an error).
Expand Down
105 changes: 27 additions & 78 deletions frost-core/src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use std::{
hash::{Hash, Hasher},
};

use crate::{Ciphersuite, Error, Field, FieldError, Group, Scalar};

#[cfg(feature = "serde")]
use crate::serialization::ScalarSerialization;
use crate::{
serialization::SerializableScalar, Ciphersuite, Error, Field, FieldError, Group, Scalar,
};

/// A FROST participant identifier.
///
Expand All @@ -18,23 +17,31 @@ use crate::serialization::ScalarSerialization;
#[derive(Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(bound = "C: Ciphersuite"))]
#[cfg_attr(feature = "serde", serde(try_from = "ScalarSerialization<C>"))]
#[cfg_attr(feature = "serde", serde(into = "ScalarSerialization<C>"))]
pub struct Identifier<C: Ciphersuite>(Scalar<C>);
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Identifier<C: Ciphersuite>(SerializableScalar<C>);

impl<C> Identifier<C>
where
C: Ciphersuite,
{
/// Create a new Identifier from a scalar. For internal use only.
fn new(scalar: Scalar<C>) -> Result<Self, Error<C>> {
#[cfg_attr(feature = "internals", visibility::make(pub))]
#[cfg_attr(docsrs, doc(cfg(feature = "internals")))]
pub(crate) fn new(scalar: Scalar<C>) -> Result<Self, Error<C>> {
if scalar == <<C::Group as Group>::Field>::zero() {
Err(FieldError::InvalidZeroScalar.into())
} else {
Ok(Self(scalar))
Ok(Self(SerializableScalar(scalar)))
}
}

/// Get the inner scalar.
#[cfg_attr(feature = "internals", visibility::make(pub))]
#[cfg_attr(docsrs, doc(cfg(feature = "internals")))]
pub(crate) fn to_scalar(&self) -> Scalar<C> {
self.0 .0
}

/// Derive an Identifier from an arbitrary byte string.
///
/// This feature is not part of the specification and is just a convenient
Expand All @@ -50,39 +57,14 @@ where
}

/// Serialize the identifier using the ciphersuite encoding.
pub fn serialize(&self) -> <<C::Group as Group>::Field as Field>::Serialization {
<<C::Group as Group>::Field>::serialize(&self.0)
pub fn serialize(&self) -> Vec<u8> {
self.0.serialize()
}

/// Deserialize an Identifier from a serialized buffer.
/// Returns an error if it attempts to deserialize zero.
pub fn deserialize(
buf: &<<C::Group as Group>::Field as Field>::Serialization,
) -> Result<Self, Error<C>> {
let scalar = <<C::Group as Group>::Field>::deserialize(buf)?;
Self::new(scalar)
}
}

#[cfg(feature = "serde")]
impl<C> TryFrom<ScalarSerialization<C>> for Identifier<C>
where
C: Ciphersuite,
{
type Error = Error<C>;

fn try_from(value: ScalarSerialization<C>) -> Result<Self, Self::Error> {
Self::deserialize(&value.0)
}
}

#[cfg(feature = "serde")]
impl<C> From<Identifier<C>> for ScalarSerialization<C>
where
C: Ciphersuite,
{
fn from(value: Identifier<C>) -> Self {
Self(value.serialize())
pub fn deserialize(bytes: &[u8]) -> Result<Self, Error<C>> {
Ok(Self(SerializableScalar::deserialize(bytes)?))
}
}

Expand All @@ -94,9 +76,7 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("Identifier")
.field(&hex::encode(
<<C::Group as Group>::Field>::serialize(&self.0).as_ref(),
))
.field(&hex::encode(self.serialize()))
.finish()
}
}
Expand All @@ -107,9 +87,7 @@ where
C: Ciphersuite,
{
fn hash<H: Hasher>(&self, state: &mut H) {
<<C::Group as Group>::Field>::serialize(&self.0)
.as_ref()
.hash(state)
self.serialize().hash(state)
}
}

Expand All @@ -118,8 +96,10 @@ where
C: Ciphersuite,
{
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let serialized_self = <<C::Group as Group>::Field>::little_endian_serialize(&self.0);
let serialized_other = <<C::Group as Group>::Field>::little_endian_serialize(&other.0);
let serialized_self =
<<C::Group as Group>::Field>::little_endian_serialize(&self.to_scalar());
let serialized_other =
<<C::Group as Group>::Field>::little_endian_serialize(&other.to_scalar());
// The default cmp uses lexicographic order; so we need the elements in big endian
serialized_self
.as_ref()
Expand All @@ -138,37 +118,6 @@ where
}
}

impl<C> std::ops::Mul<Scalar<C>> for Identifier<C>
where
C: Ciphersuite,
{
type Output = Scalar<C>;

fn mul(self, scalar: Scalar<C>) -> Scalar<C> {
self.0 * scalar
}
}

impl<C> std::ops::MulAssign<Identifier<C>> for Scalar<C>
where
C: Ciphersuite,
{
fn mul_assign(&mut self, identifier: Identifier<C>) {
*self = *self * identifier.0
}
}

impl<C> std::ops::Sub for Identifier<C>
where
C: Ciphersuite,
{
type Output = Self;

fn sub(self, rhs: Identifier<C>) -> Self::Output {
Self(self.0 - rhs.0)
}
}

impl<C> TryFrom<u16> for Identifier<C>
where
C: Ciphersuite,
Expand All @@ -191,7 +140,7 @@ where
sum = sum + one;
}
}
Ok(Self(sum))
Self::new(sum)
}
}
}
Loading

0 comments on commit efa9294

Please sign in to comment.