Skip to content

Commit

Permalink
Remove AsRef again (doesn't compile for some mysterious reason)
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed Mar 1, 2024
1 parent ce7cf66 commit ef11838
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
3 changes: 0 additions & 3 deletions src/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ use bytecheck::CheckBytes;
#[cfg(feature = "rkyv-impl")]
use rkyv::{Archive, Deserialize, Serialize};

#[cfg(feature = "zk")]
pub(crate) use zk::{decrypt, encrypt};

const MESSAGE_CAPACITY: usize = 2;
const CIPHER_SIZE: usize = MESSAGE_CAPACITY + 1;
const CIPHER_BYTES_SIZE: usize = CIPHER_SIZE * BlsScalar::SIZE;
Expand Down
27 changes: 14 additions & 13 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ impl Domain {
}
}

fn io_pattern<R: AsRef<[T]>, T>(
fn io_pattern(
domain: Domain,
input: &[R],
input: &[&[BlsScalar]],
output_len: usize,
) -> Result<Vec<Call>, Error> {
let mut io_pattern = Vec::new();
// check total input length against domain
let input_len = input
.iter()
.fold(0, |acc, input| acc + input.as_ref().len());
let input_len = input.iter().fold(0, |acc, input| acc + input.len());
match domain {
Domain::Merkle2 if input_len != 2 || output_len != 1 => {
return Err(Error::IOPatternViolation);
Expand All @@ -69,7 +67,7 @@ fn io_pattern<R: AsRef<[T]>, T>(
_ => {}
}
for input in input.iter() {
io_pattern.push(Call::Absorb(input.as_ref().len()));
io_pattern.push(Call::Absorb(input.len()));
}
io_pattern.push(Call::Squeeze(output_len));

Expand All @@ -81,13 +79,13 @@ fn io_pattern<R: AsRef<[T]>, T>(
/// only call `squeeze` once at the finalization of the hash.
/// The output length is set to 1 element per default, but this can be
/// overridden with [`Hash::output_len`].
pub struct Hash<R: AsRef<[BlsScalar]>> {
pub struct Hash<'a> {
domain: Domain,
input: Vec<R>,
input: Vec<&'a [BlsScalar]>,
output_len: usize,
}

impl<R: AsRef<[BlsScalar]>> Hash<R> {
impl<'a> Hash<'a> {
/// Create a new hash.
pub fn new(domain: Domain) -> Self {
Self {
Expand All @@ -103,7 +101,7 @@ impl<R: AsRef<[BlsScalar]>> Hash<R> {
}

/// Update the hash input.
pub fn update(&mut self, input: R) {
pub fn update(&mut self, input: &'a [BlsScalar]) {
self.input.push(input);
}

Expand All @@ -121,7 +119,7 @@ impl<R: AsRef<[BlsScalar]>> Hash<R> {
Sponge::start(ScalarPermutation::new(), io_pattern, domain_sep)?;
// absorb the input
for input in self.input.iter() {
sponge.absorb(input.as_ref().len(), input)?;
sponge.absorb(input.len(), input)?;
}
// squeeze the output
sponge.squeeze(self.output_len)?;
Expand Down Expand Up @@ -154,7 +152,10 @@ impl<R: AsRef<[BlsScalar]>> Hash<R> {
}

/// Digest an input and calculate the hash immediately
pub fn digest(domain: Domain, input: R) -> Result<Vec<BlsScalar>, Error> {
pub fn digest(
domain: Domain,
input: &'a [BlsScalar],
) -> Result<Vec<BlsScalar>, Error> {
let mut hash = Self::new(domain);
hash.update(input);
hash.finalize()
Expand All @@ -163,7 +164,7 @@ impl<R: AsRef<[BlsScalar]>> Hash<R> {
/// Digest an input and calculate the hash as jubjub-scalar immediately
pub fn digest_truncated(
domain: Domain,
input: R,
input: &'a [BlsScalar],
) -> Result<Vec<JubJubScalar>, Error> {
let mut hash = Self::new(domain);
hash.update(input);
Expand Down
14 changes: 7 additions & 7 deletions src/hash/gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ use crate::{Domain, Error};
use super::io_pattern;

/// Hash struct.
pub struct HashGadget<R: AsRef<[Witness]>> {
pub struct HashGadget<'a> {
domain: Domain,
input: Vec<R>,
input: Vec<&'a [Witness]>,
output_len: usize,
}

impl<R: AsRef<[Witness]>> HashGadget<R> {
impl<'a> HashGadget<'a> {
/// Create a new hash.
pub fn new(domain: Domain) -> Self {
Self {
Expand All @@ -37,7 +37,7 @@ impl<R: AsRef<[Witness]>> HashGadget<R> {
}

/// Update the hash input.
pub fn update(&mut self, input: R) {
pub fn update(&mut self, input: &'a [Witness]) {
self.input.push(input);
}

Expand All @@ -61,7 +61,7 @@ impl<R: AsRef<[Witness]>> HashGadget<R> {
)?;
// absorb the input
for input in self.input.iter() {
sponge.absorb(input.as_ref().len(), input)?;
sponge.absorb(input.len(), input)?;
}
// squeeze the output
sponge.squeeze(self.output_len as usize)?;
Expand Down Expand Up @@ -89,7 +89,7 @@ impl<R: AsRef<[Witness]>> HashGadget<R> {
pub fn digest(
domain: Domain,
composer: &mut Composer,
input: R,
input: &'a [Witness],
) -> Result<Vec<Witness>, Error> {
let mut hash = Self::new(domain);
hash.update(input);
Expand All @@ -100,7 +100,7 @@ impl<R: AsRef<[Witness]>> HashGadget<R> {
pub fn digest_truncated(
domain: Domain,
composer: &mut Composer,
input: R,
input: &'a [Witness],
) -> Result<Vec<Witness>, Error> {
let mut hash = Self::new(domain);
hash.update(input);
Expand Down

0 comments on commit ef11838

Please sign in to comment.