Skip to content

Commit

Permalink
Implement hash_to_scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jan 7, 2022
1 parent 08c4fd5 commit d24d2ac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ alloc = ["der/alloc", "sec1/alloc", "zeroize/alloc"] # todo: use weak activation
arithmetic = ["ff", "group"]
bits = ["arithmetic", "ff/bits"]
dev = ["arithmetic", "hex-literal", "pem", "pkcs8"]
hash2curve = ["digest", "ff", "group"]
hash2curve = ["arithmetic", "digest", "ff", "group"]
ecdh = ["arithmetic"]
hazmat = []
jwk = ["alloc", "base64ct/alloc", "serde", "serde_json", "zeroize/alloc"]
Expand Down
16 changes: 14 additions & 2 deletions elliptic-curve/src/hash2curve/group_digest.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::MapToCurve;
use crate::{
hash2field::{hash_to_field, ExpandMsg, FromOkm},
Result,
ProjectiveArithmetic, Result,
};
use group::cofactor::CofactorGroup;

/// Adds hashing arbitrary byte sequences to a valid group element
pub trait GroupDigest {
pub trait GroupDigest: ProjectiveArithmetic<ProjectivePoint = Self::Output> {
/// The field element representation for a group value with multiple elements
type FieldElement: FromOkm + MapToCurve<Output = Self::Output> + Default + Copy;
/// The resulting group element
Expand Down Expand Up @@ -68,4 +68,16 @@ pub trait GroupDigest {
let q0 = u[0].map_to_curve();
Ok(q0.clear_cofactor())
}

/// Computes the hash to field routine according to
/// <https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-13.html#section-5>
/// and returns a scalar.
fn hash_to_scalar<X: ExpandMsg>(msgs: &[&[u8]], dst: &'static [u8]) -> Result<Self::Scalar>
where
Self::Scalar: FromOkm,
{
let mut u = [Self::Scalar::default()];
hash_to_field::<X, _>(msgs, dst, &mut u)?;
Ok(u[0])
}
}

0 comments on commit d24d2ac

Please sign in to comment.