Skip to content

Commit 6f022b9

Browse files
committed
make hash_to_field output an array
1 parent 275d265 commit 6f022b9

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

hash2curve/src/group_digest.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Traits for handling hash to curve.
22
33
use super::{ExpandMsg, MapToCurve, hash_to_field};
4+
use digest::consts::{U1, U2};
45
use elliptic_curve::array::typenum::Unsigned;
56
use elliptic_curve::{ProjectivePoint, Result};
67

@@ -36,8 +37,7 @@ pub trait GroupDigest: MapToCurve {
3637
where
3738
X: ExpandMsg<Self::K>,
3839
{
39-
let mut u = [Self::FieldElement::default(), Self::FieldElement::default()];
40-
hash_to_field::<X, _, _>(msg, dst, &mut u)?;
40+
let u = hash_to_field::<X, _, Self::FieldElement, U2>(msg, dst)?;
4141
let q0 = Self::map_to_curve(u[0]);
4242
let q1 = Self::map_to_curve(u[1]);
4343
Ok(Self::add_and_map_to_subgroup(q0, q1))
@@ -67,8 +67,7 @@ pub trait GroupDigest: MapToCurve {
6767
where
6868
X: ExpandMsg<Self::K>,
6969
{
70-
let mut u = [Self::FieldElement::default()];
71-
hash_to_field::<X, _, _>(msg, dst, &mut u)?;
70+
let u = hash_to_field::<X, _, Self::FieldElement, U1>(msg, dst)?;
7271
let q0 = Self::map_to_curve(u[0]);
7372
Ok(Self::map_to_subgroup(q0))
7473
}
@@ -91,8 +90,7 @@ pub trait GroupDigest: MapToCurve {
9190
where
9291
X: ExpandMsg<Self::K>,
9392
{
94-
let mut u = [Self::Scalar::default()];
95-
hash_to_field::<X, _, _>(msg, dst, &mut u)?;
93+
let u = hash_to_field::<X, _, Self::Scalar, U1>(msg, dst)?;
9694
Ok(u[0])
9795
}
9896
}

hash2curve/src/hash2field.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,23 @@ 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<E, K, T, C>(data: &[&[u8]], domain: &[&[u8]]) -> Result<Array<T, C>>
4242
where
4343
E: ExpandMsg<K>,
4444
T: FromOkm + Default,
45+
C: ArraySize,
4546
{
4647
let len_in_bytes = T::Length::USIZE
47-
.checked_mul(out.len())
48+
.checked_mul(C::USIZE)
4849
.and_then(|len| len.try_into().ok())
4950
.and_then(NonZeroU16::new)
5051
.ok_or(Error)?;
5152
let mut tmp = Array::<u8, <T as FromOkm>::Length>::default();
5253
let mut expander = E::expand_message(data, domain, len_in_bytes)?;
54+
let mut out = Array::<T, C>::default();
5355
for o in out.iter_mut() {
5456
expander.fill_bytes(&mut tmp);
5557
*o = T::from_okm(&tmp);
5658
}
57-
Ok(())
59+
Ok(out)
5860
}

0 commit comments

Comments
 (0)