Skip to content

Commit aafb503

Browse files
committed
Rework expand_message_xmd and remove utility
1 parent f6a1bac commit aafb503

File tree

8 files changed

+329
-366
lines changed

8 files changed

+329
-366
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ p256 = [
2424
"once_cell",
2525
"p256_",
2626
]
27-
ristretto255 = []
27+
ristretto255 = ["generic-array/more_lengths"]
2828
ristretto255_fiat_u32 = ["curve25519-dalek/fiat_u32_backend", "ristretto255"]
2929
ristretto255_fiat_u64 = ["curve25519-dalek/fiat_u64_backend", "ristretto255"]
3030
ristretto255_simd = ["curve25519-dalek/simd_backend", "ristretto255"]

src/group/expand.rs

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,82 @@
55
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
66
// of this source tree.
77

8-
use core::ops::Add;
8+
use core::convert::TryFrom;
99

10-
use digest::core_api::BlockSizeUser;
10+
use digest::core_api::{Block, BlockSizeUser};
1111
use digest::{Digest, FixedOutputReset};
12-
use generic_array::sequence::Concat;
13-
use generic_array::typenum::{Unsigned, U1, U2};
12+
use generic_array::typenum::{IsLess, NonZero, Unsigned, U65536};
1413
use generic_array::{ArrayLength, GenericArray};
1514

16-
use crate::util::i2osp;
1715
use crate::{Error, Result};
1816

19-
// Computes ceil(x / y)
20-
fn div_ceil(x: usize, y: usize) -> usize {
21-
let additive = (x % y != 0) as usize;
22-
x / y + additive
23-
}
24-
2517
fn xor<L: ArrayLength<u8>>(x: GenericArray<u8, L>, y: GenericArray<u8, L>) -> GenericArray<u8, L> {
2618
x.into_iter().zip(y).map(|(x1, x2)| x1 ^ x2).collect()
2719
}
2820

2921
/// Corresponds to the expand_message_xmd() function defined in
3022
/// <https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-10.txt>
31-
pub fn expand_message_xmd<
32-
'a,
33-
H: BlockSizeUser + Digest + FixedOutputReset,
34-
L: ArrayLength<u8>,
35-
M: IntoIterator<Item = &'a [u8]>,
36-
D: ArrayLength<u8> + Add<U1>,
37-
>(
38-
msg: M,
39-
dst: GenericArray<u8, D>,
23+
pub fn expand_message_xmd<H: BlockSizeUser + Digest + FixedOutputReset, L: ArrayLength<u8>>(
24+
msg: &[&[u8]],
25+
dst: &[u8],
4026
) -> Result<GenericArray<u8, L>>
4127
where
42-
<D as Add<U1>>::Output: ArrayLength<u8>,
28+
// Constraint set by `expand_message_xmd`:
29+
// https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-13.html#section-5.4.1-6
30+
L: NonZero + IsLess<U65536>,
4331
{
44-
let digest_len = H::OutputSize::USIZE;
45-
let ell = div_ceil(L::USIZE, digest_len);
46-
if ell > 255 {
32+
// DST, a byte string of at most 255 bytes.
33+
let dst_len = u8::try_from(dst.len()).map_err(|_| Error::HashToCurveError)?;
34+
35+
// b_in_bytes, b / 8 for b the output size of H in bits.
36+
let b_in_bytes = H::OutputSize::to_usize();
37+
38+
// Constraint set by `expand_message_xmd`:
39+
// https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-13.html#section-5.4.1-4
40+
if b_in_bytes > H::BlockSize::USIZE {
4741
return Err(Error::HashToCurveError);
4842
}
49-
let dst_prime = dst.concat(i2osp::<U1>(D::USIZE)?);
50-
let z_pad = i2osp::<H::BlockSize>(0)?;
51-
let l_i_b_str = i2osp::<U2>(L::USIZE)?;
5243

53-
let mut h = H::new();
44+
// ell = ceil(len_in_bytes / b_in_bytes)
45+
// ABORT if ell > 255
46+
let ell = u8::try_from((L::USIZE + b_in_bytes - 1) / b_in_bytes)
47+
.map_err(|_| Error::HashToCurveError)?;
48+
49+
let mut hash = H::new();
5450

51+
// b_0 = H(msg_prime)
5552
// msg_prime = Z_pad || msg || l_i_b_str || I2OSP(0, 1) || DST_prime
56-
Digest::update(&mut h, z_pad);
57-
for bytes in msg {
58-
Digest::update(&mut h, bytes)
53+
// Z_pad = I2OSP(0, s_in_bytes)
54+
// s_in_bytes, the input block size of H, measured in bytes
55+
Digest::update(&mut hash, Block::<H>::default());
56+
for msg in msg {
57+
Digest::update(&mut hash, msg);
5958
}
60-
Digest::update(&mut h, l_i_b_str);
61-
Digest::update(&mut h, i2osp::<U1>(0)?);
62-
Digest::update(&mut h, &dst_prime);
59+
// l_i_b_str = I2OSP(len_in_bytes, 2)
60+
Digest::update(&mut hash, L::U16.to_be_bytes());
61+
Digest::update(&mut hash, [0]);
62+
// DST_prime = DST || I2OSP(len(DST), 1)
63+
Digest::update(&mut hash, dst);
64+
Digest::update(&mut hash, [dst_len]);
65+
let b_0 = hash.finalize_reset();
6366

64-
// b[0]
65-
let b_0 = h.finalize_reset();
6667
let mut b_i = GenericArray::default();
6768

6869
let mut uniform_bytes = GenericArray::default();
6970

70-
for (i, chunk) in (1..(ell + 1)).zip(uniform_bytes.chunks_mut(digest_len)) {
71-
Digest::update(&mut h, xor(b_0.clone(), b_i.clone()));
72-
Digest::update(&mut h, i2osp::<U1>(i)?);
73-
Digest::update(&mut h, &dst_prime);
74-
b_i = h.finalize_reset();
75-
chunk.copy_from_slice(&b_i[..digest_len.min(chunk.len())]);
71+
// b_1 = H(b_0 || I2OSP(1, 1) || DST_prime)
72+
// for i in (2, ..., ell):
73+
for (i, chunk) in (1..(ell + 1)).zip(uniform_bytes.chunks_mut(b_in_bytes)) {
74+
// b_i = H(strxor(b_0, b_(i - 1)) || I2OSP(i, 1) || DST_prime)
75+
Digest::update(&mut hash, xor(b_0.clone(), b_i.clone()));
76+
Digest::update(&mut hash, [i]);
77+
// DST_prime = DST || I2OSP(len(DST), 1)
78+
Digest::update(&mut hash, dst);
79+
Digest::update(&mut hash, [dst_len]);
80+
b_i = hash.finalize_reset();
81+
// uniform_bytes = b_1 || ... || b_ell
82+
// return substr(uniform_bytes, 0, len_in_bytes)
83+
chunk.copy_from_slice(&b_i[..b_in_bytes.min(chunk.len())]);
7684
}
7785

7886
Ok(uniform_bytes)
@@ -81,7 +89,6 @@ where
8189
#[cfg(test)]
8290
mod tests {
8391
use generic_array::typenum::{U128, U32};
84-
use generic_array::GenericArray;
8592

8693
struct Params {
8794
msg: &'static str,
@@ -91,6 +98,8 @@ mod tests {
9198

9299
#[test]
93100
fn test_expand_message_xmd() {
101+
const DST: [u8; 27] = *b"QUUX-V01-CS02-with-expander";
102+
94103
// Test vectors taken from Section K.1 of https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-10.txt
95104
let test_vectors: alloc::vec::Vec<Params> = alloc::vec![
96105
Params {
@@ -190,20 +199,13 @@ mod tests {
190199
378fba044a31f5cb44583a892f5969dcd73b3fa128816e",
191200
},
192201
];
193-
let dst = GenericArray::from(*b"QUUX-V01-CS02-with-expander");
194202

195203
for tv in test_vectors {
196204
let uniform_bytes = match tv.len_in_bytes {
197-
32 => super::expand_message_xmd::<sha2::Sha256, U32, _, _>(
198-
Some(tv.msg.as_bytes()),
199-
dst,
200-
)
201-
.map(|bytes| bytes.to_vec()),
202-
128 => super::expand_message_xmd::<sha2::Sha256, U128, _, _>(
203-
Some(tv.msg.as_bytes()),
204-
dst,
205-
)
206-
.map(|bytes| bytes.to_vec()),
205+
32 => super::expand_message_xmd::<sha2::Sha256, U32>(&[tv.msg.as_bytes()], &DST)
206+
.map(|bytes| bytes.to_vec()),
207+
128 => super::expand_message_xmd::<sha2::Sha256, U128>(&[tv.msg.as_bytes()], &DST)
208+
.map(|bytes| bytes.to_vec()),
207209
_ => unimplemented!(),
208210
}
209211
.unwrap();

src/group/mod.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ use core::ops::{Add, Mul, Sub};
1818

1919
use digest::core_api::BlockSizeUser;
2020
use digest::{Digest, FixedOutputReset};
21-
use generic_array::typenum::U1;
2221
use generic_array::{ArrayLength, GenericArray};
2322
use rand_core::{CryptoRng, RngCore};
2423
#[cfg(feature = "ristretto255")]
2524
pub use ristretto::Ristretto255;
2625
use subtle::ConstantTimeEq;
2726
use zeroize::Zeroize;
2827

28+
use crate::voprf::Mode;
2929
use crate::Result;
3030

31+
pub(crate) const STR_HASH_TO_SCALAR: [u8; 13] = *b"HashToScalar-";
32+
pub(crate) const STR_HASH_TO_GROUP: [u8; 12] = *b"HashToGroup-";
33+
3134
/// A prime-order subgroup of a base field (EC, prime-order field ...). This
3235
/// subgroup is noted additively — as in the draft RFC — in this trait.
3336
pub trait Group {
@@ -58,25 +61,16 @@ pub trait Group {
5861
type ScalarLen: ArrayLength<u8> + 'static;
5962

6063
/// transforms a password and domain separation tag (DST) into a curve point
61-
fn hash_to_curve<H: BlockSizeUser + Digest + FixedOutputReset, D: ArrayLength<u8> + Add<U1>>(
62-
msg: &[u8],
63-
dst: GenericArray<u8, D>,
64-
) -> Result<Self::Elem>
65-
where
66-
<D as Add<U1>>::Output: ArrayLength<u8>;
64+
fn hash_to_curve<H: BlockSizeUser + Digest + FixedOutputReset>(
65+
msg: &[&[u8]],
66+
mode: Mode,
67+
) -> Result<Self::Elem>;
6768

6869
/// Hashes a slice of pseudo-random bytes to a scalar
69-
fn hash_to_scalar<
70-
'a,
71-
H: BlockSizeUser + Digest + FixedOutputReset,
72-
D: ArrayLength<u8> + Add<U1>,
73-
I: IntoIterator<Item = &'a [u8]>,
74-
>(
75-
input: I,
76-
dst: GenericArray<u8, D>,
77-
) -> Result<Self::Scalar>
78-
where
79-
<D as Add<U1>>::Output: ArrayLength<u8>;
70+
fn hash_to_scalar<H: BlockSizeUser + Digest + FixedOutputReset>(
71+
input: &[&[u8]],
72+
mode: Mode,
73+
) -> Result<Self::Scalar>;
8074

8175
/// Get the base point for the group
8276
fn base_elem() -> Self::Elem;

src/group/p256.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use core::str::FromStr;
1818

1919
use digest::core_api::BlockSizeUser;
2020
use digest::{Digest, FixedOutputReset};
21-
use generic_array::typenum::{Unsigned, U1, U2, U32, U33, U48};
21+
use generic_array::sequence::Concat;
22+
use generic_array::typenum::{Unsigned, U2, U32, U33, U48};
2223
use generic_array::{ArrayLength, GenericArray};
2324
use num_bigint::{BigInt, Sign};
2425
use num_integer::Integer;
@@ -34,6 +35,8 @@ use rand_core::{CryptoRng, RngCore};
3435
use subtle::{Choice, ConditionallySelectable};
3536

3637
use super::Group;
38+
use crate::group::{STR_HASH_TO_GROUP, STR_HASH_TO_SCALAR};
39+
use crate::voprf::{self, Mode};
3740
use crate::{Error, Result};
3841

3942
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-8.2
@@ -54,13 +57,13 @@ impl Group for NistP256 {
5457

5558
// Implements the `hash_to_curve()` function from
5659
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-3
57-
fn hash_to_curve<H: BlockSizeUser + Digest + FixedOutputReset, D: ArrayLength<u8> + Add<U1>>(
58-
msg: &[u8],
59-
dst: GenericArray<u8, D>,
60-
) -> Result<Self::Elem>
61-
where
62-
<D as Add<U1>>::Output: ArrayLength<u8>,
63-
{
60+
fn hash_to_curve<H: BlockSizeUser + Digest + FixedOutputReset>(
61+
msg: &[&[u8]],
62+
mode: Mode,
63+
) -> Result<Self::Elem> {
64+
let dst =
65+
GenericArray::from(STR_HASH_TO_GROUP).concat(voprf::get_context_string::<Self>(mode));
66+
6467
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-8.2
6568
// `p: 2^256 - 2^224 + 2^192 + 2^96 - 1`
6669
const P: Lazy<BigInt> = Lazy::new(|| {
@@ -87,7 +90,7 @@ impl Group for NistP256 {
8790
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-5.3
8891
// `hash_to_field` calls `expand_message` with a `len_in_bytes` of `count * L`
8992
let uniform_bytes =
90-
super::expand::expand_message_xmd::<H, <L as Mul<U2>>::Output, _, _>(Some(msg), dst)?;
93+
super::expand::expand_message_xmd::<H, <L as Mul<U2>>::Output>(msg, &dst)?;
9194

9295
// hash to curve
9396
let (q0x, q0y) = hash_to_curve_simple_swu(&uniform_bytes[..L::USIZE], &A, &B, &P, &Z);
@@ -108,18 +111,13 @@ impl Group for NistP256 {
108111
}
109112

110113
// Implements the `HashToScalar()` function
111-
fn hash_to_scalar<
112-
'a,
113-
H: BlockSizeUser + Digest + FixedOutputReset,
114-
D: ArrayLength<u8> + Add<U1>,
115-
I: IntoIterator<Item = &'a [u8]>,
116-
>(
117-
input: I,
118-
dst: GenericArray<u8, D>,
119-
) -> Result<Self::Scalar>
120-
where
121-
<D as Add<U1>>::Output: ArrayLength<u8>,
122-
{
114+
fn hash_to_scalar<H: BlockSizeUser + Digest + FixedOutputReset>(
115+
input: &[&[u8]],
116+
mode: Mode,
117+
) -> Result<Self::Scalar> {
118+
let dst =
119+
GenericArray::from(STR_HASH_TO_SCALAR).concat(voprf::get_context_string::<Self>(mode));
120+
123121
// https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf#[{%22num%22:211,%22gen%22:0},{%22name%22:%22XYZ%22},70,700,0]
124122
// P-256 `n` is defined as
125123
// `115792089210356248762697446949407573529996955224135760342
@@ -133,7 +131,7 @@ impl Group for NistP256 {
133131

134132
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-5.3
135133
// `HashToScalar` is `hash_to_field`
136-
let uniform_bytes = super::expand::expand_message_xmd::<H, L, _, _>(input, dst)?;
134+
let uniform_bytes = super::expand::expand_message_xmd::<H, L>(input, &dst)?;
137135
let bytes = BigInt::from_bytes_be(Sign::Plus, &uniform_bytes)
138136
.mod_floor(&N)
139137
.to_bytes_be()
@@ -464,6 +462,8 @@ mod tests {
464462

465463
#[test]
466464
fn hash_to_curve_simple_swu() {
465+
const DST: [u8; 44] = *b"QUUX-V01-CS02-with-P256_XMD:SHA-256_SSWU_RO_";
466+
467467
const P: Lazy<BigInt> = Lazy::new(|| {
468468
BigInt::from_str(
469469
"115792089210356248762697446949407573530086143415290314195533631308867097853951",
@@ -549,15 +549,13 @@ mod tests {
549549
q1y: "f6ed88a7aab56a488100e6f1174fa9810b47db13e86be999644922961206e184",
550550
},
551551
];
552-
let dst = GenericArray::from(*b"QUUX-V01-CS02-with-P256_XMD:SHA-256_SSWU_RO_");
553552

554553
for tv in test_vectors {
555-
let uniform_bytes =
556-
super::super::expand::expand_message_xmd::<sha2::Sha256, U96, _, _>(
557-
Some(tv.msg.as_bytes()),
558-
dst,
559-
)
560-
.unwrap();
554+
let uniform_bytes = super::super::expand::expand_message_xmd::<sha2::Sha256, U96>(
555+
&[tv.msg.as_bytes()],
556+
&DST,
557+
)
558+
.unwrap();
561559

562560
let u0 = BigInt::from_bytes_be(Sign::Plus, &uniform_bytes[..48]).mod_floor(&P);
563561
let u1 = BigInt::from_bytes_be(Sign::Plus, &uniform_bytes[48..]).mod_floor(&P);

0 commit comments

Comments
 (0)