Skip to content

Commit 579190a

Browse files
authored
hash2curve: make hash_to_field output an array instead of taking an out parameter. (#1296)
1 parent 425e867 commit 579190a

File tree

6 files changed

+24
-28
lines changed

6 files changed

+24
-28
lines changed

hash2curve/src/group_digest.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ pub trait GroupDigest: MapToCurve {
3636
where
3737
X: ExpandMsg<Self::K>,
3838
{
39-
let mut u = [Self::FieldElement::default(), Self::FieldElement::default()];
40-
hash_to_field::<X, _, _>(msg, dst, &mut u)?;
41-
let q0 = Self::map_to_curve(u[0]);
42-
let q1 = Self::map_to_curve(u[1]);
39+
let [u0, u1] = hash_to_field::<2, X, _, Self::FieldElement>(msg, dst)?;
40+
let q0 = Self::map_to_curve(u0);
41+
let q1 = Self::map_to_curve(u1);
4342
Ok(Self::add_and_map_to_subgroup(q0, q1))
4443
}
4544

@@ -67,9 +66,8 @@ pub trait GroupDigest: MapToCurve {
6766
where
6867
X: ExpandMsg<Self::K>,
6968
{
70-
let mut u = [Self::FieldElement::default()];
71-
hash_to_field::<X, _, _>(msg, dst, &mut u)?;
72-
let q0 = Self::map_to_curve(u[0]);
69+
let [u] = hash_to_field::<1, X, _, Self::FieldElement>(msg, dst)?;
70+
let q0 = Self::map_to_curve(u);
7371
Ok(Self::map_to_subgroup(q0))
7472
}
7573

@@ -91,8 +89,7 @@ pub trait GroupDigest: MapToCurve {
9189
where
9290
X: ExpandMsg<Self::K>,
9391
{
94-
let mut u = [Self::Scalar::default()];
95-
hash_to_field::<X, _, _>(msg, dst, &mut u)?;
96-
Ok(u[0])
92+
let [u] = hash_to_field::<1, X, _, Self::Scalar>(msg, dst)?;
93+
Ok(u)
9794
}
9895
}

hash2curve/src/hash2field.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,20 @@ pub trait FromOkm {
3838
/// [`ExpandMsgXmd`]: crate::hash2field::ExpandMsgXmd
3939
/// [`ExpandMsgXof`]: crate::hash2field::ExpandMsgXof
4040
#[doc(hidden)]
41-
pub fn hash_to_field<E, K, T>(data: &[&[u8]], domain: &[&[u8]], out: &mut [T]) -> Result<()>
41+
pub fn hash_to_field<const N: usize, E, K, T>(data: &[&[u8]], domain: &[&[u8]]) -> Result<[T; N]>
4242
where
4343
E: ExpandMsg<K>,
4444
T: FromOkm + Default,
4545
{
4646
let len_in_bytes = T::Length::USIZE
47-
.checked_mul(out.len())
47+
.checked_mul(N)
4848
.and_then(|len| len.try_into().ok())
4949
.and_then(NonZeroU16::new)
5050
.ok_or(Error)?;
5151
let mut tmp = Array::<u8, <T as FromOkm>::Length>::default();
5252
let mut expander = E::expand_message(data, domain, len_in_bytes)?;
53-
for o in out.iter_mut() {
53+
Ok(core::array::from_fn(|_| {
5454
expander.fill_bytes(&mut tmp);
55-
*o = T::from_okm(&tmp);
56-
}
57-
Ok(())
55+
T::from_okm(&tmp)
56+
}))
5857
}

k256/src/arithmetic/hash2curve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,12 @@ mod tests {
353353

354354
for test_vector in TEST_VECTORS {
355355
// in parts
356-
let mut u = [FieldElement::default(), FieldElement::default()];
357-
hash2curve::hash_to_field::<
356+
let u = hash2curve::hash_to_field::<
357+
2,
358358
ExpandMsgXmd<Sha256>,
359359
<Secp256k1 as GroupDigest>::K,
360360
FieldElement,
361-
>(&[test_vector.msg], &[DST], &mut u)
361+
>(&[test_vector.msg], &[DST])
362362
.unwrap();
363363
assert_eq!(u[0].to_bytes().as_slice(), test_vector.u_0);
364364
assert_eq!(u[1].to_bytes().as_slice(), test_vector.u_1);

p256/src/arithmetic/hash2curve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ mod tests {
204204

205205
for test_vector in TEST_VECTORS {
206206
// in parts
207-
let mut u = [FieldElement::default(), FieldElement::default()];
208-
hash2curve::hash_to_field::<
207+
let u = hash2curve::hash_to_field::<
208+
2,
209209
ExpandMsgXmd<Sha256>,
210210
<NistP256 as GroupDigest>::K,
211211
FieldElement,
212-
>(&[test_vector.msg], &[DST], &mut u)
212+
>(&[test_vector.msg], &[DST])
213213
.unwrap();
214214

215215
/// Assert that the provided projective point matches the given test vector.

p384/src/arithmetic/hash2curve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ mod tests {
209209

210210
for test_vector in TEST_VECTORS {
211211
// in parts
212-
let mut u = [FieldElement::default(), FieldElement::default()];
213-
hash2curve::hash_to_field::<
212+
let u = hash2curve::hash_to_field::<
213+
2,
214214
ExpandMsgXmd<Sha384>,
215215
<NistP384 as GroupDigest>::K,
216216
FieldElement,
217-
>(&[test_vector.msg], &[DST], &mut u)
217+
>(&[test_vector.msg], &[DST])
218218
.unwrap();
219219

220220
/// Assert that the provided projective point matches the given test vector.

p521/src/arithmetic/hash2curve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,12 @@ mod tests {
212212

213213
for test_vector in TEST_VECTORS {
214214
// in parts
215-
let mut u = [FieldElement::default(), FieldElement::default()];
216-
hash2curve::hash_to_field::<
215+
let u = hash2curve::hash_to_field::<
216+
2,
217217
ExpandMsgXmd<Sha512>,
218218
<NistP521 as GroupDigest>::K,
219219
FieldElement,
220-
>(&[test_vector.msg], &[DST], &mut u)
220+
>(&[test_vector.msg], &[DST])
221221
.unwrap();
222222

223223
/// Assert that the provided projective point matches the given test vector.

0 commit comments

Comments
 (0)