Skip to content

Commit 3ff40ab

Browse files
committed
Implement hash_to_scalar
1 parent 8ef6fb9 commit 3ff40ab

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

elliptic-curve/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ alloc = ["der/alloc", "sec1/alloc", "zeroize/alloc"] # todo: use weak activation
4949
arithmetic = ["ff", "group"]
5050
bits = ["arithmetic", "ff/bits"]
5151
dev = ["arithmetic", "hex-literal", "pem", "pkcs8"]
52-
hash2curve = ["digest", "ff", "group"]
52+
hash2curve = ["arithmetic", "digest"]
5353
ecdh = ["arithmetic"]
5454
hazmat = []
5555
jwk = ["alloc", "base64ct/alloc", "serde", "serde_json", "zeroize/alloc"]

elliptic-curve/src/hash2curve/group_digest.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
use super::MapToCurve;
44
use crate::{
55
hash2field::{hash_to_field, ExpandMsg, FromOkm},
6-
Result,
6+
ProjectiveArithmetic, Result,
77
};
88
use group::cofactor::CofactorGroup;
99

1010
/// Adds hashing arbitrary byte sequences to a valid group element
11-
pub trait GroupDigest {
11+
pub trait GroupDigest: ProjectiveArithmetic<ProjectivePoint = Self::Output> {
1212
/// The field element representation for a group value with multiple elements
1313
type FieldElement: FromOkm + MapToCurve<Output = Self::Output> + Default + Copy;
1414
/// The resulting group element
@@ -72,4 +72,16 @@ pub trait GroupDigest {
7272
let q0 = u[0].map_to_curve();
7373
Ok(q0.clear_cofactor())
7474
}
75+
76+
/// Computes the hash to field routine according to
77+
/// <https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-13.html#section-5>
78+
/// and returns a scalar.
79+
fn hash_to_scalar<X: ExpandMsg>(msgs: &[&[u8]], dst: &'static [u8]) -> Result<Self::Scalar>
80+
where
81+
Self::Scalar: FromOkm,
82+
{
83+
let mut u = [Self::Scalar::default()];
84+
hash_to_field::<X, _>(msgs, dst, &mut u)?;
85+
Ok(u[0])
86+
}
7587
}

0 commit comments

Comments
 (0)