Skip to content
Open
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
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use core::iter::Sum;
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use ff::PrimeField;
use rand_core::{RngCore, TryRngCore};
use subtle::{Choice, CtOption};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

pub mod cofactor;
pub mod prime;
Expand Down Expand Up @@ -174,6 +174,23 @@ pub trait GroupEncoding: Sized {
fn to_bytes(&self) -> Self::Repr;
}

/// A marker trait that `<Self as GroupEncoding>::to_bytes()` always yields a canonical encoding.
pub trait GroupCanonicalEncoding:
Sized + Default + ConditionallySelectable + GroupEncoding
{
/// Attempts to deserialize a group element from its canonical encoding.
///
/// For any returned point, it will be returned if and only if the exact representation present
/// within `bytes` was passed into this function. This implies checking the coordinate(s) were
/// reduced, and flags, sign bits, etc were minimal.
fn from_canonical_bytes(bytes: &Self::Repr) -> CtOption<Self> {
let res = Self::from_bytes(bytes).unwrap_or(Self::default());
// Safe due to the bound points are always encoded canonically
let canonical = res.to_bytes().as_ref().ct_eq(bytes.as_ref());
CtOption::new(res, canonical)
}
}

/// Affine representation of a point on an elliptic curve that has a defined uncompressed
/// encoding.
pub trait UncompressedEncoding: Sized {
Expand Down