Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 4 additions & 6 deletions hash2curve/src/group_digest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Traits for handling hash to curve.

use super::{ExpandMsg, MapToCurve, hash_to_field};
use digest::consts::{U1, U2};
use elliptic_curve::array::typenum::Unsigned;
use elliptic_curve::{ProjectivePoint, Result};

Expand Down Expand Up @@ -36,8 +37,7 @@ pub trait GroupDigest: MapToCurve {
where
X: ExpandMsg<Self::K>,
{
let mut u = [Self::FieldElement::default(), Self::FieldElement::default()];
hash_to_field::<X, _, _>(msg, dst, &mut u)?;
let u = hash_to_field::<X, _, Self::FieldElement, U2>(msg, dst)?;
let q0 = Self::map_to_curve(u[0]);
let q1 = Self::map_to_curve(u[1]);
Ok(Self::add_and_map_to_subgroup(q0, q1))
Expand Down Expand Up @@ -67,8 +67,7 @@ pub trait GroupDigest: MapToCurve {
where
X: ExpandMsg<Self::K>,
{
let mut u = [Self::FieldElement::default()];
hash_to_field::<X, _, _>(msg, dst, &mut u)?;
let u = hash_to_field::<X, _, Self::FieldElement, U1>(msg, dst)?;
let q0 = Self::map_to_curve(u[0]);
Ok(Self::map_to_subgroup(q0))
}
Expand All @@ -91,8 +90,7 @@ pub trait GroupDigest: MapToCurve {
where
X: ExpandMsg<Self::K>,
{
let mut u = [Self::Scalar::default()];
hash_to_field::<X, _, _>(msg, dst, &mut u)?;
let u = hash_to_field::<X, _, Self::Scalar, U1>(msg, dst)?;
Ok(u[0])
}
}
8 changes: 5 additions & 3 deletions hash2curve/src/hash2field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,23 @@ pub trait FromOkm {
/// [`ExpandMsgXmd`]: crate::hash2field::ExpandMsgXmd
/// [`ExpandMsgXof`]: crate::hash2field::ExpandMsgXof
#[doc(hidden)]
pub fn hash_to_field<E, K, T>(data: &[&[u8]], domain: &[&[u8]], out: &mut [T]) -> Result<()>
pub fn hash_to_field<E, K, T, C>(data: &[&[u8]], domain: &[&[u8]]) -> Result<Array<T, C>>
where
E: ExpandMsg<K>,
T: FromOkm + Default,
C: ArraySize,
Copy link
Contributor

@daxpedda daxpedda Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if we leave this to ArraySize instead of const C, we could do the following:

Suggested change
T: FromOkm + Default,
C: ArraySize,
T: FromOkm + Default,
C: ArraySize,
T::Length: Mul<C, Output: IsLess<U65536, Output = True>>,

Eliminating one more run-time error.

However propagating this requirement up for users might be quite ugly. So I'm in favor of actually going ahead with the const generic, but thought I'd drop the thought at least.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is a good idea, but I don't like the complex trait bounds either. With inline consts we could do something like:

const { if N * T::Length > u16::MAX {
    compile_error!("...");
} };

Although this causes non-local errors and so I did not add it for now.

{
let len_in_bytes = T::Length::USIZE
.checked_mul(out.len())
.checked_mul(C::USIZE)
.and_then(|len| len.try_into().ok())
.and_then(NonZeroU16::new)
.ok_or(Error)?;
let mut tmp = Array::<u8, <T as FromOkm>::Length>::default();
let mut expander = E::expand_message(data, domain, len_in_bytes)?;
let mut out = Array::<T, C>::default();
for o in out.iter_mut() {
expander.fill_bytes(&mut tmp);
*o = T::from_okm(&tmp);
}
Ok(())
Ok(out)
}