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

Implement From for Domain for u64 encoding #252

Merged
merged 1 commit into from
Mar 7, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add `Hash` struct [#202]
- Add `From<Domain> for u64` implementation [#251]

### Changed

Expand All @@ -19,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Remove `perm_uses` module as it is obsolete with the introduction of SAFE [#248]
- Remove `merkle` feature with the introduction of SAFE [#248]
- Remove `Domain.encoding` method in favor of `From` trait [#251]

## [0.35.0] - 2024-02-28

Expand Down Expand Up @@ -465,6 +467,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Variants of sponge for `Scalar` & `Gadget(Variable/LC)`.

<!-- ISSUES -->
[#251]: https://github.com/dusk-network/poseidon252/issues/251
[#248]: https://github.com/dusk-network/poseidon252/issues/248
[#246]: https://github.com/dusk-network/poseidon252/issues/246
[#243]: https://github.com/dusk-network/poseidon252/issues/243
Expand Down
23 changes: 10 additions & 13 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ pub enum Domain {
Other,
}

impl Domain {
impl From<Domain> for u64 {
/// Encryption for the domain-separator are taken from section 4.2 of the
/// paper adapted to u64.
/// When `Other` is selected we set the domain-separator to zero. We can do
/// this since the io-pattern will be encoded in the tag in any case,
/// ensuring safety from collision attacks.
pub const fn encoding(&self) -> u64 {
match self {
fn from(domain: Domain) -> Self {
match domain {
// 2^4 - 1
Domain::Merkle4 => 0x0000_0000_0000_000f,
// 2^2 - 1
Expand Down Expand Up @@ -107,21 +107,18 @@ impl<'a> Hash<'a> {

/// Finalize the hash.
pub fn finalize(&self) -> Result<Vec<BlsScalar>, Error> {
// generate the io-pattern
let io_pattern = io_pattern(self.domain, &self.input, self.output_len)?;

// set the domain-separator
let domain_sep = self.domain.encoding();

// Generate the hash using the sponge framework.
// Generate the hash using the sponge framework:
// initialize the sponge
let mut sponge =
Sponge::start(ScalarPermutation::new(), io_pattern, domain_sep)?;
let mut sponge = Sponge::start(
ScalarPermutation::new(),
io_pattern(self.domain, &self.input, self.output_len)?,
self.domain.into(),
)?;
// absorb the input
for input in self.input.iter() {
sponge.absorb(input.len(), input)?;
}
// squeeze the output
// squeeze output_len elements
sponge.squeeze(self.output_len)?;

// return the result
Expand Down
16 changes: 5 additions & 11 deletions src/hash/gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,19 @@ impl<'a> HashGadget<'a> {
&self,
composer: &mut Composer,
) -> Result<Vec<Witness>, Error> {
// generate the io-pattern
let io_pattern = io_pattern(self.domain, &self.input, self.output_len)?;

// get the domain-separator
let domain_sep = self.domain.encoding();

// Generate the hash using the sponge framework.
// Generate the hash using the sponge framework:
// initialize the sponge
let mut sponge = Sponge::start(
GadgetPermutation::new(composer),
io_pattern,
domain_sep,
io_pattern(self.domain, &self.input, self.output_len)?,
self.domain.into(),
)?;
// absorb the input
for input in self.input.iter() {
sponge.absorb(input.len(), input)?;
}
// squeeze the output
sponge.squeeze(self.output_len as usize)?;
// squeeze output_len elements
sponge.squeeze(self.output_len)?;

// return the result
Ok(sponge.finish()?)
Expand Down