Skip to content

Commit 3ce6755

Browse files
committed
Move standalone functions to root
1 parent 7c6f898 commit 3ce6755

File tree

2 files changed

+80
-78
lines changed

2 files changed

+80
-78
lines changed

hash2curve/src/group_digest.rs

Lines changed: 3 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
//! Traits for handling hash to curve.
22
3-
use super::{ExpandMsg, MapToCurve, hash_to_field};
3+
use crate::{ExpandMsg, MapToCurve};
44
use elliptic_curve::ProjectivePoint;
5-
use elliptic_curve::array::typenum::NonZero;
6-
use elliptic_curve::array::{Array, ArraySize};
7-
use elliptic_curve::group::cofactor::CofactorGroup;
8-
use elliptic_curve::ops::Reduce;
95

106
/// Hash arbitrary byte sequences to a valid group element.
117
pub trait GroupDigest: MapToCurve {
@@ -40,7 +36,7 @@ pub trait GroupDigest: MapToCurve {
4036
dst: &[u8],
4137
) -> Result<ProjectivePoint<Self>, <Self::ExpandMsg as ExpandMsg<Self::SecurityLevel>>::Error>
4238
{
43-
hash_from_bytes::<Self, Self::ExpandMsg>(&[msg], &[dst])
39+
crate::hash_from_bytes::<Self, Self::ExpandMsg>(&[msg], &[dst])
4440
}
4541

4642
/// Computes the encode to curve routine.
@@ -65,77 +61,6 @@ pub trait GroupDigest: MapToCurve {
6561
dst: &[u8],
6662
) -> Result<ProjectivePoint<Self>, <Self::ExpandMsg as ExpandMsg<Self::SecurityLevel>>::Error>
6763
{
68-
encode_from_bytes::<Self, Self::ExpandMsg>(&[msg], &[dst])
64+
crate::encode_from_bytes::<Self, Self::ExpandMsg>(&[msg], &[dst])
6965
}
7066
}
71-
72-
/// Computes the hash to curve routine.
73-
/// See [`GroupDigest::hash_from_bytes()`] for more details.
74-
///
75-
/// For the `expand_message` call, `len_in_bytes = <Self::FieldElement as FromOkm>::Length * 2`.
76-
/// This value must be less than `u16::MAX` or otherwise a compiler error will occur.
77-
///
78-
/// # Errors
79-
///
80-
/// When the chosen [`ExpandMsg`] implementation returns an error. See [`ExpandMsgXmdError`]
81-
/// and [`ExpandMsgXofError`] for examples.
82-
///
83-
/// [`ExpandMsgXmdError`]: crate::ExpandMsgXmdError
84-
/// [`ExpandMsgXofError`]: crate::ExpandMsgXofError
85-
pub fn hash_from_bytes<C, X>(msg: &[&[u8]], dst: &[&[u8]]) -> Result<ProjectivePoint<C>, X::Error>
86-
where
87-
C: MapToCurve,
88-
X: ExpandMsg<C::SecurityLevel>,
89-
{
90-
let [u0, u1] = hash_to_field::<2, X, _, C::FieldElement, C::Length>(msg, dst)?;
91-
let q0 = C::map_to_curve(u0);
92-
let q1 = C::map_to_curve(u1);
93-
Ok((q0 + q1).clear_cofactor())
94-
}
95-
96-
/// Computes the encode to curve routine.
97-
/// See [`GroupDigest::encode_from_bytes()`] for more details.
98-
///
99-
/// For the `expand_message` call, `len_in_bytes = <Self::FieldElement as FromOkm>::Length`.
100-
///
101-
/// # Errors
102-
///
103-
/// When the chosen [`ExpandMsg`] implementation returns an error. See [`ExpandMsgXmdError`]
104-
/// and [`ExpandMsgXofError`] for examples.
105-
///
106-
/// [`ExpandMsgXmdError`]: crate::ExpandMsgXmdError
107-
/// [`ExpandMsgXofError`]: crate::ExpandMsgXofError
108-
pub fn encode_from_bytes<C, X>(msg: &[&[u8]], dst: &[&[u8]]) -> Result<ProjectivePoint<C>, X::Error>
109-
where
110-
C: MapToCurve,
111-
X: ExpandMsg<C::SecurityLevel>,
112-
{
113-
let [u] = hash_to_field::<1, X, _, C::FieldElement, C::Length>(msg, dst)?;
114-
let q0 = C::map_to_curve(u);
115-
Ok(q0.clear_cofactor())
116-
}
117-
118-
/// Computes the hash to field routine according to
119-
/// <https://www.rfc-editor.org/rfc/rfc9380.html#section-5-4>
120-
/// and returns a scalar.
121-
///
122-
/// For the `expand_message` call, `len_in_bytes = <Self::FieldElement as FromOkm>::Length`.
123-
/// This value must be less than `u16::MAX` or otherwise a compiler error will occur.
124-
///
125-
/// # Errors
126-
///
127-
/// When the chosen [`ExpandMsg`] implementation returns an error. See [`ExpandMsgXmdError`]
128-
/// and [`ExpandMsgXofError`] for examples.
129-
///
130-
/// [`ExpandMsgXmdError`]: crate::ExpandMsgXmdError
131-
/// [`ExpandMsgXofError`]: crate::ExpandMsgXofError
132-
pub fn hash_to_scalar<C, X, L>(msg: &[&[u8]], dst: &[&[u8]]) -> Result<C::Scalar, X::Error>
133-
where
134-
C: MapToCurve,
135-
X: ExpandMsg<C::SecurityLevel>,
136-
L: ArraySize + NonZero,
137-
C::Scalar: Reduce<Array<u8, L>>,
138-
{
139-
let [u] = hash_to_field::<1, X, _, C::Scalar, L>(msg, dst)?;
140-
Ok(u)
141-
}

hash2curve/src/lib.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,80 @@ pub use group_digest::*;
3131
pub use hash2field::*;
3232
pub use map2curve::*;
3333
pub use oprf::*;
34+
35+
use elliptic_curve::ProjectivePoint;
36+
use elliptic_curve::array::typenum::NonZero;
37+
use elliptic_curve::array::{Array, ArraySize};
38+
use elliptic_curve::group::cofactor::CofactorGroup;
39+
use elliptic_curve::ops::Reduce;
40+
41+
/// Computes the hash to curve routine.
42+
/// See [`GroupDigest::hash_from_bytes()`] for more details.
43+
///
44+
/// For the `expand_message` call, `len_in_bytes = <Self::FieldElement as FromOkm>::Length * 2`.
45+
/// This value must be less than `u16::MAX` or otherwise a compiler error will occur.
46+
///
47+
/// # Errors
48+
///
49+
/// When the chosen [`ExpandMsg`] implementation returns an error. See [`ExpandMsgXmdError`]
50+
/// and [`ExpandMsgXofError`] for examples.
51+
///
52+
/// [`ExpandMsgXmdError`]: crate::ExpandMsgXmdError
53+
/// [`ExpandMsgXofError`]: crate::ExpandMsgXofError
54+
pub fn hash_from_bytes<C, X>(msg: &[&[u8]], dst: &[&[u8]]) -> Result<ProjectivePoint<C>, X::Error>
55+
where
56+
C: MapToCurve,
57+
X: ExpandMsg<C::SecurityLevel>,
58+
{
59+
let [u0, u1] = hash_to_field::<2, X, _, C::FieldElement, C::Length>(msg, dst)?;
60+
let q0 = C::map_to_curve(u0);
61+
let q1 = C::map_to_curve(u1);
62+
Ok((q0 + q1).clear_cofactor())
63+
}
64+
65+
/// Computes the encode to curve routine.
66+
/// See [`GroupDigest::encode_from_bytes()`] for more details.
67+
///
68+
/// For the `expand_message` call, `len_in_bytes = <Self::FieldElement as FromOkm>::Length`.
69+
///
70+
/// # Errors
71+
///
72+
/// When the chosen [`ExpandMsg`] implementation returns an error. See [`ExpandMsgXmdError`]
73+
/// and [`ExpandMsgXofError`] for examples.
74+
///
75+
/// [`ExpandMsgXmdError`]: crate::ExpandMsgXmdError
76+
/// [`ExpandMsgXofError`]: crate::ExpandMsgXofError
77+
pub fn encode_from_bytes<C, X>(msg: &[&[u8]], dst: &[&[u8]]) -> Result<ProjectivePoint<C>, X::Error>
78+
where
79+
C: MapToCurve,
80+
X: ExpandMsg<C::SecurityLevel>,
81+
{
82+
let [u] = hash_to_field::<1, X, _, C::FieldElement, C::Length>(msg, dst)?;
83+
let q0 = C::map_to_curve(u);
84+
Ok(q0.clear_cofactor())
85+
}
86+
87+
/// Computes the hash to field routine according to
88+
/// <https://www.rfc-editor.org/rfc/rfc9380.html#section-5-4>
89+
/// and returns a scalar.
90+
///
91+
/// For the `expand_message` call, `len_in_bytes = <Self::FieldElement as FromOkm>::Length`.
92+
/// This value must be less than `u16::MAX` or otherwise a compiler error will occur.
93+
///
94+
/// # Errors
95+
///
96+
/// When the chosen [`ExpandMsg`] implementation returns an error. See [`ExpandMsgXmdError`]
97+
/// and [`ExpandMsgXofError`] for examples.
98+
///
99+
/// [`ExpandMsgXmdError`]: crate::ExpandMsgXmdError
100+
/// [`ExpandMsgXofError`]: crate::ExpandMsgXofError
101+
pub fn hash_to_scalar<C, X, L>(msg: &[&[u8]], dst: &[&[u8]]) -> Result<C::Scalar, X::Error>
102+
where
103+
C: MapToCurve,
104+
X: ExpandMsg<C::SecurityLevel>,
105+
L: ArraySize + NonZero,
106+
C::Scalar: Reduce<Array<u8, L>>,
107+
{
108+
let [u] = hash_to_field::<1, X, _, C::Scalar, L>(msg, dst)?;
109+
Ok(u)
110+
}

0 commit comments

Comments
 (0)