Skip to content

Commit 39f36f1

Browse files
andrewwhiteheadstr4d
authored andcommitted
only implement Message for iterators
Signed-off-by: Andrew Whitehead <[email protected]>
1 parent 16f8d99 commit 39f36f1

File tree

4 files changed

+39
-67
lines changed

4 files changed

+39
-67
lines changed

src/hash_to_curve/expand_msg.rs

+10-60
Original file line numberDiff line numberDiff line change
@@ -102,71 +102,21 @@ pub trait Message {
102102
///
103103
/// The parameters to successive calls to `f` are treated as a
104104
/// single concatenated octet string.
105-
fn consume(self, f: impl FnMut(&[u8]));
105+
fn input_message(self, f: impl FnMut(&[u8]));
106106
}
107107

108-
impl Message for &[u8] {
109-
#[inline]
110-
fn consume(self, mut f: impl FnMut(&[u8])) {
111-
f(self)
112-
}
113-
}
114-
115-
impl<const N: usize> Message for &[u8; N] {
116-
#[inline]
117-
fn consume(self, mut f: impl FnMut(&[u8])) {
118-
f(self)
119-
}
120-
}
121-
122-
impl Message for &str {
123-
#[inline]
124-
fn consume(self, mut f: impl FnMut(&[u8])) {
125-
f(self.as_bytes())
126-
}
127-
}
128-
129-
impl Message for &[&[u8]] {
130-
#[inline]
131-
fn consume(self, mut f: impl FnMut(&[u8])) {
108+
impl<M, I> Message for I
109+
where
110+
M: AsRef<[u8]>,
111+
I: IntoIterator<Item = M>,
112+
{
113+
fn input_message(self, mut f: impl FnMut(&[u8])) {
132114
for msg in self {
133-
f(msg);
115+
f(msg.as_ref())
134116
}
135117
}
136118
}
137119

138-
#[cfg(feature = "alloc")]
139-
impl Message for Vec<u8> {
140-
#[inline]
141-
fn consume(self, mut f: impl FnMut(&[u8])) {
142-
f(self.as_slice())
143-
}
144-
}
145-
146-
#[cfg(feature = "alloc")]
147-
impl Message for &Vec<u8> {
148-
#[inline]
149-
fn consume(self, mut f: impl FnMut(&[u8])) {
150-
f(self.as_slice())
151-
}
152-
}
153-
154-
#[cfg(feature = "alloc")]
155-
impl Message for alloc::string::String {
156-
#[inline]
157-
fn consume(self, mut f: impl FnMut(&[u8])) {
158-
f(self.as_bytes())
159-
}
160-
}
161-
162-
#[cfg(feature = "alloc")]
163-
impl Message for &alloc::string::String {
164-
#[inline]
165-
fn consume(self, mut f: impl FnMut(&[u8])) {
166-
f(self.as_bytes())
167-
}
168-
}
169-
170120
/// A trait for message expansion methods supported by hash-to-curve.
171121
pub trait ExpandMessage {
172122
/// Initializes a message expander.
@@ -230,7 +180,7 @@ where
230180

231181
let dst = ExpandMsgDst::for_xof::<H, L>(dst);
232182
let mut hash = H::default();
233-
message.consume(|m| hash.update(m));
183+
message.input_message(|m| hash.update(m));
234184
let reader = hash
235185
.chain((len_in_bytes as u16).to_be_bytes())
236186
.chain(dst.data())
@@ -294,7 +244,7 @@ where
294244
let dst = ExpandMsgDst::for_xmd::<H>(dst);
295245
let mut hash_b_0 =
296246
H::default().chain(GenericArray::<u8, <H as BlockInput>::BlockSize>::default());
297-
message.consume(|m| hash_b_0.update(m));
247+
message.input_message(|m| hash_b_0.update(m));
298248
let b_0 = hash_b_0
299249
.chain((len_in_bytes as u16).to_be_bytes())
300250
.chain([0u8])

tests/expand_msg.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ use hex_literal::hex;
44
use sha2::{Sha256, Sha512};
55
use sha3::{Shake128, Shake256};
66

7+
#[test]
8+
fn test_expand_message_parts() {
9+
const EXPAND_LEN: usize = 16;
10+
let mut b1 = [0u8; EXPAND_LEN];
11+
let mut b2 = [0u8; EXPAND_LEN];
12+
<ExpandMsgXmd<Sha256> as ExpandMessage>::init_expand::<_, U32>(
13+
[b"sig" as &[u8], b"nature"],
14+
&[],
15+
EXPAND_LEN,
16+
)
17+
.read_into(&mut b1);
18+
<ExpandMsgXmd<Sha256> as ExpandMessage>::init_expand::<_, U32>([b"signature"], &[], EXPAND_LEN)
19+
.read_into(&mut b2);
20+
assert_eq!(b1, b2);
21+
}
22+
723
struct TestCase {
824
msg: &'static [u8],
925
dst: &'static [u8],
@@ -16,7 +32,7 @@ impl TestCase {
1632
pub fn run<E: ExpandMessage>(self) {
1733
let mut buf = [0u8; 128];
1834
let output = &mut buf[..self.len_in_bytes];
19-
E::init_expand::<_, U32>(self.msg, self.dst, self.len_in_bytes).read_into(output);
35+
E::init_expand::<_, U32>([self.msg], self.dst, self.len_in_bytes).read_into(output);
2036
if output != self.uniform_bytes {
2137
panic!(
2238
"Failed: expand_message.\n\

tests/hash_to_curve_g1.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ fn hash_to_curve_works_for_draft16_testvectors_g1_sha256_ro() {
9696
];
9797

9898
for case in cases {
99-
let g =
100-
<G1Projective as HashToCurve<ExpandMsgXmd<Sha256>>>::hash_to_curve(case.msg, case.dst);
99+
let g = <G1Projective as HashToCurve<ExpandMsgXmd<Sha256>>>::hash_to_curve(
100+
[case.msg],
101+
case.dst,
102+
);
101103
let aff = G1Affine::from(g);
102104
let g_uncompressed = aff.to_uncompressed();
103105
case.check_output(&g_uncompressed);
@@ -175,7 +177,8 @@ fn encode_to_curve_works_for_draft16_testvectors_g1_sha256_nu() {
175177

176178
for case in cases {
177179
let g = <G1Projective as HashToCurve<ExpandMsgXmd<Sha256>>>::encode_to_curve(
178-
case.msg, case.dst,
180+
[case.msg],
181+
case.dst,
179182
);
180183
let aff = G1Affine::from(g);
181184
let g_uncompressed = aff.to_uncompressed();

tests/hash_to_curve_g2.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ fn hash_to_curve_works_for_draft16_testvectors_g2_sha256_ro() {
116116
];
117117

118118
for case in cases {
119-
let g =
120-
<G2Projective as HashToCurve<ExpandMsgXmd<Sha256>>>::hash_to_curve(case.msg, case.dst);
119+
let g = <G2Projective as HashToCurve<ExpandMsgXmd<Sha256>>>::hash_to_curve(
120+
[case.msg],
121+
case.dst,
122+
);
121123
let aff = G2Affine::from(g);
122124
let g_uncompressed = aff.to_uncompressed();
123125
case.check_output(&g_uncompressed);
@@ -215,7 +217,8 @@ fn encode_to_curve_works_for_draft16_testvectors_g2_sha256_nu() {
215217

216218
for case in cases {
217219
let g = <G2Projective as HashToCurve<ExpandMsgXmd<Sha256>>>::encode_to_curve(
218-
case.msg, case.dst,
220+
[case.msg],
221+
case.dst,
219222
);
220223
let aff = G2Affine::from(g);
221224
let g_uncompressed = aff.to_uncompressed();

0 commit comments

Comments
 (0)