From 3bace28f83e5c00f3680527ce5f1830da25eae61 Mon Sep 17 00:00:00 2001 From: moana Date: Thu, 7 Mar 2024 15:20:46 +0100 Subject: [PATCH] Implement `From` for `Domain` for `u64` encoding Resolves #251 --- CHANGELOG.md | 3 +++ src/hash.rs | 23 ++++++++++------------- src/hash/gadget.rs | 16 +++++----------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52d3865..6023b14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 for u64` implementation [#251] ### Changed @@ -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 @@ -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)`. +[#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 diff --git a/src/hash.rs b/src/hash.rs index f1f9dcd..9f4e708 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -29,14 +29,14 @@ pub enum Domain { Other, } -impl Domain { +impl From 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 @@ -107,21 +107,18 @@ impl<'a> Hash<'a> { /// Finalize the hash. pub fn finalize(&self) -> Result, 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 diff --git a/src/hash/gadget.rs b/src/hash/gadget.rs index f7867dd..09c9dd4 100644 --- a/src/hash/gadget.rs +++ b/src/hash/gadget.rs @@ -46,25 +46,19 @@ impl<'a> HashGadget<'a> { &self, composer: &mut Composer, ) -> Result, 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()?)