Skip to content

Commit

Permalink
Fix hash to scalar
Browse files Browse the repository at this point in the history
Resolves #3
  • Loading branch information
moCello committed Feb 8, 2024
1 parent 04f54b4 commit 02ebd03
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Change the implementation for hashing a slice of bytes into a BlsScalar to `BlsScalar::hash_to_scalar` [#3]

## [0.1.0] - 2024-01-08

### Added

- Add initial commit, this package continues the development of [dusk-bls12_381-sign](https://github.com/dusk-network/bls12_381-sign/) at version `0.6.0` under the new name: `bls12_381-bls` and without the go related code.

<!-- ISSUES -->
[#3]: https://github.com/dusk-network/bls12_381-bls/issues/3

<!-- VERSIONS -->
[Unreleased]: https://github.com/dusk-network/bls12_381-bls/compare/v0.1.0...HEAD
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ exclude = [
license = "MPL-2.0"

[dependencies]
blake2 = { version = "0.10", default-features = false }
dusk-bls12_381 = { version = "0.13", default-features = false, features = ["alloc", "pairings"] }
dusk-bytes = "0.1"
rand_core = { version = "0.6", default-features = false }
Expand Down
19 changes: 2 additions & 17 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,20 @@
use crate::PublicKey;

use blake2::digest::consts::U32;
use blake2::Digest;
use dusk_bls12_381::{BlsScalar, G1Affine};
use dusk_bytes::Serializable;

type Blake2b = blake2::Blake2b<U32>;

/// Hash an arbitrary slice of bytes into a [`BlsScalar`]
fn h(msg: &[u8]) -> BlsScalar {
let mut digest: [u8; BlsScalar::SIZE] = Blake2b::digest(msg).into();

// Truncate the contract id to fit bls
digest[31] &= 0x3f;

let hash: Option<BlsScalar> = BlsScalar::from_bytes(&digest).into();
hash.unwrap_or_default()
}

/// h0 is the hash-to-curve-point function.
/// Hₒ : M -> Gₒ
pub fn h0(msg: &[u8]) -> G1Affine {
// Now multiply this message by the G1 base point,
// to generate a G1Affine.
(G1Affine::generator() * h(msg)).into()
(G1Affine::generator() * BlsScalar::hash_to_scalar(msg)).into()
}

/// h1 is the hashing function used in the modified BLS
/// multi-signature construction.
/// H₁ : G₂ -> R
pub fn h1(pk: &PublicKey) -> BlsScalar {
h(&pk.to_bytes())
BlsScalar::hash_to_scalar(&pk.to_bytes())
}

0 comments on commit 02ebd03

Please sign in to comment.