Skip to content

Commit 958fde3

Browse files
chore(frost-p256, frost-secp256k1): do not use allocator for domain separators (#767)
1 parent ca33dad commit 958fde3

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

frost-p256/src/lib.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
extern crate alloc;
1010

11-
use alloc::borrow::ToOwned;
1211
use alloc::collections::BTreeMap;
1312

1413
use frost_rerandomized::RandomizedCiphersuite;
@@ -159,9 +158,9 @@ fn hash_to_array(inputs: &[&[u8]]) -> [u8; 32] {
159158
output
160159
}
161160

162-
fn hash_to_scalar(domain: &[u8], msg: &[u8]) -> Scalar {
161+
fn hash_to_scalar(domain: &[&[u8]], msg: &[u8]) -> Scalar {
163162
let mut u = [P256ScalarField::zero()];
164-
hash_to_field::<ExpandMsgXmd<Sha256>, Scalar>(&[msg], &[domain], &mut u)
163+
hash_to_field::<ExpandMsgXmd<Sha256>, Scalar>(&[msg], domain, &mut u)
165164
.expect("should never return error according to error cases described in ExpandMsgXmd");
166165
u[0]
167166
}
@@ -188,21 +187,21 @@ impl Ciphersuite for P256Sha256 {
188187
///
189188
/// [spec]: https://datatracker.ietf.org/doc/html/rfc9591#section-6.4-2.4.2.2
190189
fn H1(m: &[u8]) -> <<Self::Group as Group>::Field as Field>::Scalar {
191-
hash_to_scalar((CONTEXT_STRING.to_owned() + "rho").as_bytes(), m)
190+
hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"rho"], m)
192191
}
193192

194193
/// H2 for FROST(P-256, SHA-256)
195194
///
196195
/// [spec]: https://datatracker.ietf.org/doc/html/rfc9591#section-6.4-2.4.2.4
197196
fn H2(m: &[u8]) -> <<Self::Group as Group>::Field as Field>::Scalar {
198-
hash_to_scalar((CONTEXT_STRING.to_owned() + "chal").as_bytes(), m)
197+
hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"chal"], m)
199198
}
200199

201200
/// H3 for FROST(P-256, SHA-256)
202201
///
203202
/// [spec]: https://datatracker.ietf.org/doc/html/rfc9591#section-6.4-2.4.2.6
204203
fn H3(m: &[u8]) -> <<Self::Group as Group>::Field as Field>::Scalar {
205-
hash_to_scalar((CONTEXT_STRING.to_owned() + "nonce").as_bytes(), m)
204+
hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"nonce"], m)
206205
}
207206

208207
/// H4 for FROST(P-256, SHA-256)
@@ -221,25 +220,19 @@ impl Ciphersuite for P256Sha256 {
221220

222221
/// HDKG for FROST(P-256, SHA-256)
223222
fn HDKG(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
224-
Some(hash_to_scalar(
225-
(CONTEXT_STRING.to_owned() + "dkg").as_bytes(),
226-
m,
227-
))
223+
Some(hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"dkg"], m))
228224
}
229225

230226
/// HID for FROST(P-256, SHA-256)
231227
fn HID(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
232-
Some(hash_to_scalar(
233-
(CONTEXT_STRING.to_owned() + "id").as_bytes(),
234-
m,
235-
))
228+
Some(hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"id"], m))
236229
}
237230
}
238231

239232
impl RandomizedCiphersuite for P256Sha256 {
240233
fn hash_randomizer(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
241234
Some(hash_to_scalar(
242-
(CONTEXT_STRING.to_owned() + "randomizer").as_bytes(),
235+
&[CONTEXT_STRING.as_bytes(), b"randomizer"],
243236
m,
244237
))
245238
}

frost-secp256k1/src/lib.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
extern crate alloc;
1010

11-
use alloc::borrow::ToOwned;
1211
use alloc::collections::BTreeMap;
1312

1413
use frost_rerandomized::RandomizedCiphersuite;
@@ -159,9 +158,9 @@ fn hash_to_array(inputs: &[&[u8]]) -> [u8; 32] {
159158
output
160159
}
161160

162-
fn hash_to_scalar(domain: &[u8], msg: &[u8]) -> Scalar {
161+
fn hash_to_scalar(domain: &[&[u8]], msg: &[u8]) -> Scalar {
163162
let mut u = [Secp256K1ScalarField::zero()];
164-
hash_to_field::<ExpandMsgXmd<Sha256>, Scalar>(&[msg], &[domain], &mut u)
163+
hash_to_field::<ExpandMsgXmd<Sha256>, Scalar>(&[msg], domain, &mut u)
165164
.expect("should never return error according to error cases described in ExpandMsgXmd");
166165
u[0]
167166
}
@@ -188,21 +187,21 @@ impl Ciphersuite for Secp256K1Sha256 {
188187
///
189188
/// [spec]: https://datatracker.ietf.org/doc/html/rfc9591#section-6.5-2.4.2.2
190189
fn H1(m: &[u8]) -> <<Self::Group as Group>::Field as Field>::Scalar {
191-
hash_to_scalar((CONTEXT_STRING.to_owned() + "rho").as_bytes(), m)
190+
hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"rho"], m)
192191
}
193192

194193
/// H2 for FROST(secp256k1, SHA-256)
195194
///
196195
/// [spec]: https://datatracker.ietf.org/doc/html/rfc9591#section-6.5-2.4.2.4
197196
fn H2(m: &[u8]) -> <<Self::Group as Group>::Field as Field>::Scalar {
198-
hash_to_scalar((CONTEXT_STRING.to_owned() + "chal").as_bytes(), m)
197+
hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"chal"], m)
199198
}
200199

201200
/// H3 for FROST(secp256k1, SHA-256)
202201
///
203202
/// [spec]: https://datatracker.ietf.org/doc/html/rfc9591#section-6.5-2.4.2.6
204203
fn H3(m: &[u8]) -> <<Self::Group as Group>::Field as Field>::Scalar {
205-
hash_to_scalar((CONTEXT_STRING.to_owned() + "nonce").as_bytes(), m)
204+
hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"nonce"], m)
206205
}
207206

208207
/// H4 for FROST(secp256k1, SHA-256)
@@ -221,25 +220,19 @@ impl Ciphersuite for Secp256K1Sha256 {
221220

222221
/// HDKG for FROST(secp256k1, SHA-256)
223222
fn HDKG(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
224-
Some(hash_to_scalar(
225-
(CONTEXT_STRING.to_owned() + "dkg").as_bytes(),
226-
m,
227-
))
223+
Some(hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"dkg"], m))
228224
}
229225

230226
/// HID for FROST(secp256k1, SHA-256)
231227
fn HID(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
232-
Some(hash_to_scalar(
233-
(CONTEXT_STRING.to_owned() + "id").as_bytes(),
234-
m,
235-
))
228+
Some(hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"id"], m))
236229
}
237230
}
238231

239232
impl RandomizedCiphersuite for Secp256K1Sha256 {
240233
fn hash_randomizer(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
241234
Some(hash_to_scalar(
242-
(CONTEXT_STRING.to_owned() + "randomizer").as_bytes(),
235+
&[CONTEXT_STRING.as_bytes(), b"randomizer"],
243236
m,
244237
))
245238
}

0 commit comments

Comments
 (0)