Skip to content

Commit

Permalink
chore(frost-p256, frost-secp256k1): do not use allocator for domain s…
Browse files Browse the repository at this point in the history
…eparators
  • Loading branch information
StackOverflowExcept1on committed Nov 10, 2024
1 parent ca33dad commit 4a4144a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
23 changes: 8 additions & 15 deletions frost-p256/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

extern crate alloc;

use alloc::borrow::ToOwned;
use alloc::collections::BTreeMap;

use frost_rerandomized::RandomizedCiphersuite;
Expand Down Expand Up @@ -159,9 +158,9 @@ fn hash_to_array(inputs: &[&[u8]]) -> [u8; 32] {
output
}

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

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

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

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

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

/// HID for FROST(P-256, SHA-256)
fn HID(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
Some(hash_to_scalar(
(CONTEXT_STRING.to_owned() + "id").as_bytes(),
m,
))
Some(hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"id"], m))
}
}

impl RandomizedCiphersuite for P256Sha256 {
fn hash_randomizer(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
Some(hash_to_scalar(
(CONTEXT_STRING.to_owned() + "randomizer").as_bytes(),
&[CONTEXT_STRING.as_bytes(), b"randomizer"],
m,
))
}
Expand Down
23 changes: 8 additions & 15 deletions frost-secp256k1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

extern crate alloc;

use alloc::borrow::ToOwned;
use alloc::collections::BTreeMap;

use frost_rerandomized::RandomizedCiphersuite;
Expand Down Expand Up @@ -159,9 +158,9 @@ fn hash_to_array(inputs: &[&[u8]]) -> [u8; 32] {
output
}

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

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

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

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

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

/// HID for FROST(secp256k1, SHA-256)
fn HID(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
Some(hash_to_scalar(
(CONTEXT_STRING.to_owned() + "id").as_bytes(),
m,
))
Some(hash_to_scalar(&[CONTEXT_STRING.as_bytes(), b"id"], m))
}
}

impl RandomizedCiphersuite for Secp256K1Sha256 {
fn hash_randomizer(m: &[u8]) -> Option<<<Self::Group as Group>::Field as Field>::Scalar> {
Some(hash_to_scalar(
(CONTEXT_STRING.to_owned() + "randomizer").as_bytes(),
&[CONTEXT_STRING.as_bytes(), b"randomizer"],
m,
))
}
Expand Down

0 comments on commit 4a4144a

Please sign in to comment.