Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hash to scalar #4

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}