Skip to content

Commit

Permalink
Allow to change multiscalar algo
Browse files Browse the repository at this point in the history
  • Loading branch information
survived committed Apr 19, 2024
1 parent 1a788de commit d8dd7fc
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions givre/src/ciphersuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,85 @@ pub fn is_key_share_normalized<C: Ciphersuite>(
) -> bool {
C::is_normalized(&key_share.shared_public_key)
}

/// Overrides multiscalar multiplication used with the ciphersuite
pub struct WithMultiscalar<C, M> {
_ph: core::marker::PhantomData<fn() -> (C, M)>,
}

impl<C, M> Clone for WithMultiscalar<C, M> {
fn clone(&self) -> Self {
Self {
_ph: core::marker::PhantomData,
}
}
}
impl<C, M> Copy for WithMultiscalar<C, M> {}
impl<C, M> core::fmt::Debug for WithMultiscalar<C, M> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("WithMultiscalar")
}
}

impl<C: Ciphersuite, M: generic_ec::multiscalar::MultiscalarMul<C::Curve>> Ciphersuite
for WithMultiscalar<C, M>
{
const NAME: &'static str = C::NAME;
type Curve = C::Curve;
type Digest = C::Digest;
type MultiscalarMul = M;

fn h1(msg: &[&[u8]]) -> Scalar<Self::Curve> {
C::h1(msg)
}
fn compute_challenge(
group_commitment: &NormalizedPoint<Self, Point<Self::Curve>>,
group_public_key: &NormalizedPoint<Self, NonZero<Point<Self::Curve>>>,
msg: &[u8],
) -> Scalar<Self::Curve> {
let group_commitment = NormalizedPoint(group_commitment.0, core::marker::PhantomData);
let group_public_key = NormalizedPoint(group_public_key.0, core::marker::PhantomData);
C::compute_challenge(&group_commitment, &group_public_key, msg)
}
fn h3(msg: &[&[u8]]) -> Scalar<Self::Curve> {
C::h3(msg)
}
fn h4() -> Self::Digest {
C::h4()
}
fn h5() -> Self::Digest {
C::h5()
}

type PointBytes = C::PointBytes;
fn serialize_point(point: &Point<Self::Curve>) -> Self::PointBytes {
C::serialize_point(point)
}
fn deserialize_point(bytes: &[u8]) -> Result<Point<Self::Curve>, InvalidPoint> {
C::deserialize_point(bytes)
}

type ScalarBytes = C::ScalarBytes;
const SCALAR_SIZE: usize = C::SCALAR_SIZE;
fn serialize_scalar(scalar: &Scalar<Self::Curve>) -> Self::ScalarBytes {
C::serialize_scalar(scalar)
}
fn deserialize_scalar(bytes: &[u8]) -> Result<Scalar<Self::Curve>, InvalidScalar> {
C::deserialize_scalar(bytes)
}

type NormalizedPointBytes = C::NormalizedPointBytes;
const NORMALIZED_POINT_SIZE: usize = C::NORMALIZED_POINT_SIZE;
fn serialize_normalized_point<P: AsRef<Point<Self::Curve>>>(
point: &NormalizedPoint<Self, P>,
) -> Self::NormalizedPointBytes {
let point = NormalizedPoint(*point.0.as_ref(), core::marker::PhantomData);
C::serialize_normalized_point(&point)
}
fn deserialize_normalized_point(
bytes: &[u8],
) -> Result<NormalizedPoint<Self, Point<Self::Curve>>, InvalidPoint> {
C::deserialize_normalized_point(bytes)
.map(|point| NormalizedPoint(point.0, core::marker::PhantomData))
}
}

0 comments on commit d8dd7fc

Please sign in to comment.