Skip to content

Commit

Permalink
feat: fix new_generator (#192)
Browse files Browse the repository at this point in the history
- Fixed `pub fn new_generator(label: &'static str)` too few bytes error
(DigestTooShort)
- Added a test case for `pub fn new_generator`
  • Loading branch information
SWvheerden authored Aug 7, 2023
2 parents 2147522 + 5504fb2 commit 0d463b0
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/ristretto/ristretto_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ use curve25519_dalek::{
scalar::Scalar,
traits::MultiscalarMul,
};
use digest::{
consts::{U32, U64},
Digest,
};
use digest::{consts::U64, Digest};
use once_cell::sync::OnceCell;
use rand::{CryptoRng, Rng};
use tari_utilities::{hex::Hex, ByteArray, ByteArrayError, Hashable};
Expand Down Expand Up @@ -299,7 +296,7 @@ impl RistrettoPublicKey {
/// A verifiable group generator using a domain separated hasher
pub fn new_generator(label: &'static str) -> Result<RistrettoPublicKey, HashingError> {
// This function requires 512 bytes of data, so let's be opinionated here and use blake2b
let hash = DomainSeparatedHasher::<Blake2b<U32>, RistrettoGeneratorPoint>::new_with_label(label).finalize();
let hash = DomainSeparatedHasher::<Blake2b<U64>, RistrettoGeneratorPoint>::new_with_label(label).finalize();
if hash.as_ref().len() < 64 {
return Err(HashingError::DigestTooShort(64));
}
Expand Down Expand Up @@ -600,6 +597,7 @@ impl From<RistrettoPublicKey> for CompressedRistretto {

#[cfg(test)]
mod test {
use digest::consts::U32;
use tari_utilities::{message_format::MessageFormat, ByteArray};

use super::*;
Expand All @@ -611,6 +609,15 @@ mod test {
assert_eq!(k1.compressed, k2.compressed);
}

#[test]
fn test_new_generator() {
let pk = RistrettoPublicKey::new_generator("test");
assert_eq!(
pk.unwrap().to_hex(),
"c23db69dabfbd30f3a6c8f0dcea712e01382b998f5aa232183cf833287921371".to_string()
);
}

#[test]
fn test_generation() {
let mut rng = rand::thread_rng();
Expand Down

0 comments on commit 0d463b0

Please sign in to comment.