Skip to content

Commit

Permalink
Use arrays for batched CurlP (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz authored Sep 9, 2021
1 parent 3dad74d commit a48d1d8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
39 changes: 33 additions & 6 deletions bee-crypto/src/ternary/sponge/batched_curlp/bct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ impl BcTritBuf {
inner: vec![BcTrit::zero(); len],
}
}

pub(crate) fn filled(value: usize, len: usize) -> Self {
Self {
inner: vec![BcTrit(value, value); len],
}
}
}

impl Deref for BcTritBuf {
Expand All @@ -53,6 +47,39 @@ impl DerefMut for BcTritBuf {
}
}

#[derive(Clone)]
pub(crate) struct BcTritArr<const N: usize> {
inner: [BcTrit; N],
}

impl<const N: usize> BcTritArr<N> {
pub(crate) fn zeros() -> Self {
Self {
inner: [BcTrit::zero(); N],
}
}

pub(crate) fn filled(value: usize) -> Self {
Self {
inner: [BcTrit(value, value); N],
}
}
}

impl<const N: usize> Deref for BcTritArr<N> {
type Target = BcTrits;

fn deref(&self) -> &Self::Target {
unsafe { &*(self.inner.as_ref() as *const [BcTrit] as *const BcTrits) }
}
}

impl<const N: usize> DerefMut for BcTritArr<N> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self.inner.as_mut() as *mut [BcTrit] as *mut BcTrits) }
}
}

#[repr(transparent)]
pub(crate) struct BcTrits {
inner: [BcTrit],
Expand Down
14 changes: 7 additions & 7 deletions bee-crypto/src/ternary/sponge/batched_curlp/bct_curlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::ternary::{
sponge::{
batched_curlp::{
bct::{BcTrit, BcTritBuf},
bct::{BcTrit, BcTritArr, BcTrits},
HIGH_BITS,
},
CurlPRounds,
Expand All @@ -14,8 +14,8 @@ use crate::ternary::{

pub(crate) struct BctCurlP {
rounds: CurlPRounds,
state: BcTritBuf,
scratch_pad: BcTritBuf,
state: BcTritArr<{ 3 * HASH_LENGTH }>,
scratch_pad: BcTritArr<{ 3 * HASH_LENGTH }>,
}

impl BctCurlP {
Expand All @@ -25,8 +25,8 @@ impl BctCurlP {
assert!(3 * HASH_LENGTH > 728);
Self {
rounds,
state: BcTritBuf::filled(HIGH_BITS, 3 * HASH_LENGTH),
scratch_pad: BcTritBuf::filled(HIGH_BITS, 3 * HASH_LENGTH),
state: BcTritArr::filled(HIGH_BITS),
scratch_pad: BcTritArr::filled(HIGH_BITS),
}
}

Expand Down Expand Up @@ -83,7 +83,7 @@ impl BctCurlP {
}
}

pub(crate) fn absorb(&mut self, bc_trits: &BcTritBuf) {
pub(crate) fn absorb(&mut self, bc_trits: &BcTrits) {
let mut length = bc_trits.len();
let mut offset = 0;

Expand All @@ -106,7 +106,7 @@ impl BctCurlP {

// This method shouldn't assume that `result` has any particular content, just that it has an
// adequate size.
pub(crate) fn squeeze_into(&mut self, result: &mut BcTritBuf) {
pub(crate) fn squeeze_into(&mut self, result: &mut BcTrits) {
let trit_count = result.len();

let hash_count = trit_count / HASH_LENGTH;
Expand Down
6 changes: 3 additions & 3 deletions bee-crypto/src/ternary/sponge/batched_curlp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod bct_curlp;

use crate::ternary::sponge::{CurlP, CurlPRounds, Sponge, HASH_LENGTH};

use bct::{BcTrit, BcTritBuf};
use bct::{BcTrit, BcTritArr, BcTritBuf};
use bct_curlp::BctCurlP;

use bee_ternary::{
Expand All @@ -31,7 +31,7 @@ pub struct BatchHasher<B: RawEncodingBuf> {
/// An interleaved representation of the input trits.
bct_inputs: BcTritBuf,
/// An interleaved representation of the output trits.
bct_hashes: BcTritBuf,
bct_hashes: BcTritArr<HASH_LENGTH>,
/// A buffer for demultiplexing.
buf_demux: TritBuf,
/// The CurlP hasher for binary coded trits.
Expand All @@ -53,7 +53,7 @@ where
Self {
trit_inputs: Vec::with_capacity(BATCH_SIZE),
bct_inputs: BcTritBuf::zeros(input_length),
bct_hashes: BcTritBuf::zeros(HASH_LENGTH),
bct_hashes: BcTritArr::<HASH_LENGTH>::zeros(),
buf_demux: TritBuf::zeros(HASH_LENGTH),
bct_curlp: BctCurlP::new(rounds),
curlp: CurlP::new(rounds),
Expand Down

0 comments on commit a48d1d8

Please sign in to comment.