1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::poseidon::{
helpers::{AlgebraicSponge, DuplexSpongeMode},
State,
};
use snarkvm_console_types::{prelude::*, Field};
use snarkvm_fields::PoseidonParameters;
use smallvec::SmallVec;
use std::{ops::DerefMut, sync::Arc};
/// A duplex sponge based using the Poseidon permutation.
///
/// This implementation of Poseidon is entirely from Fractal's implementation in [COS20][cos]
/// with small syntax changes.
///
/// [cos]: https://eprint.iacr.org/2019/1076
#[derive(Clone, Debug)]
pub struct PoseidonSponge<E: Environment, const RATE: usize, const CAPACITY: usize> {
/// Sponge Parameters
parameters: Arc<PoseidonParameters<E::Field, RATE, CAPACITY>>,
/// Current sponge's state (current elements in the permutation block)
state: State<E, RATE, CAPACITY>,
/// Current mode (whether its absorbing or squeezing)
pub(in crate::poseidon) mode: DuplexSpongeMode,
}
impl<E: Environment, const RATE: usize, const CAPACITY: usize> AlgebraicSponge<E, RATE, CAPACITY>
for PoseidonSponge<E, RATE, CAPACITY>
{
type Parameters = Arc<PoseidonParameters<E::Field, RATE, CAPACITY>>;
fn new(parameters: &Self::Parameters) -> Self {
Self {
parameters: parameters.clone(),
state: State::default(),
mode: DuplexSpongeMode::Absorbing { next_absorb_index: 0 },
}
}
fn absorb(&mut self, input: &[Field<E>]) {
if !input.is_empty() {
match self.mode {
DuplexSpongeMode::Absorbing { mut next_absorb_index } => {
if next_absorb_index == RATE {
self.permute();
next_absorb_index = 0;
}
self.absorb_internal(next_absorb_index, input);
}
DuplexSpongeMode::Squeezing { next_squeeze_index: _ } => {
self.permute();
self.absorb_internal(0, input);
}
}
}
}
fn squeeze(&mut self, num_elements: u16) -> SmallVec<[Field<E>; 10]> {
if num_elements == 0 {
return SmallVec::new();
}
let mut output = if num_elements <= 10 {
smallvec::smallvec_inline![Field::<E>::zero(); 10]
} else {
smallvec::smallvec![Field::<E>::zero(); num_elements as usize]
};
match self.mode {
DuplexSpongeMode::Absorbing { next_absorb_index: _ } => {
self.permute();
self.squeeze_internal(0, &mut output[..num_elements as usize]);
}
DuplexSpongeMode::Squeezing { mut next_squeeze_index } => {
if next_squeeze_index == RATE {
self.permute();
next_squeeze_index = 0;
}
self.squeeze_internal(next_squeeze_index, &mut output[..num_elements as usize]);
}
}
output.truncate(num_elements as usize);
output
}
}
impl<E: Environment, const RATE: usize, const CAPACITY: usize> PoseidonSponge<E, RATE, CAPACITY> {
#[inline]
fn apply_ark(&mut self, round_number: usize) {
for (state_elem, ark_elem) in self.state.iter_mut().zip(&self.parameters.ark[round_number]) {
*state_elem += Field::<E>::new(*ark_elem);
}
}
#[inline]
fn apply_s_box(&mut self, is_full_round: bool) {
// Full rounds apply the S Box (x^alpha) to every element of state
if is_full_round {
for elem in self.state.iter_mut() {
let e = elem.deref_mut();
*e = e.pow([self.parameters.alpha]);
}
}
// Partial rounds apply the S Box (x^alpha) to just the first element of state
else {
let e = self.state[0].deref_mut();
*e = e.pow([self.parameters.alpha]);
}
}
#[inline]
fn apply_mds(&mut self) {
let mut new_state = State::default();
new_state.iter_mut().zip(&self.parameters.mds).for_each(|(new_elem, mds_row)| {
*new_elem = Field::new(E::Field::sum_of_products(self.state.iter().map(|e| e.deref()), mds_row.iter()));
});
self.state = new_state;
}
#[inline]
fn permute(&mut self) {
// Determine the partial rounds range bound.
let partial_rounds = self.parameters.partial_rounds;
let full_rounds = self.parameters.full_rounds;
let full_rounds_over_2 = full_rounds / 2;
let partial_round_range = full_rounds_over_2..(full_rounds_over_2 + partial_rounds);
// Iterate through all rounds to permute.
for i in 0..(partial_rounds + full_rounds) {
let is_full_round = !partial_round_range.contains(&i);
self.apply_ark(i);
self.apply_s_box(is_full_round);
self.apply_mds();
}
}
/// Absorbs everything in elements, this does not end in an absorption.
#[inline]
fn absorb_internal(&mut self, mut rate_start: usize, input: &[Field<E>]) {
if !input.is_empty() {
let first_chunk_size = std::cmp::min(RATE - rate_start, input.len());
let num_elements_remaining = input.len() - first_chunk_size;
let (first_chunk, rest_chunk) = input.split_at(first_chunk_size);
let rest_chunks = rest_chunk.chunks(RATE);
// The total number of chunks is `elements[num_elements_remaining..].len() / RATE`, plus 1
// for the remainder.
let total_num_chunks = 1 + // 1 for the first chunk
// We add all the chunks that are perfectly divisible by `RATE`
(num_elements_remaining / RATE) +
// And also add 1 if the last chunk is non-empty
// (i.e. if `num_elements_remaining` is not a multiple of `RATE`)
usize::from((num_elements_remaining % RATE) != 0);
// Absorb the input elements, `RATE` elements at a time, except for the first chunk, which
// is of size `RATE - rate_start`.
for (i, chunk) in std::iter::once(first_chunk).chain(rest_chunks).enumerate() {
for (element, state_elem) in chunk.iter().zip(&mut self.state.rate_state_mut()[rate_start..]) {
*state_elem += element;
}
// Are we in the last chunk?
// If so, let's wrap up.
if i == total_num_chunks - 1 {
self.mode = DuplexSpongeMode::Absorbing { next_absorb_index: rate_start + chunk.len() };
return;
} else {
self.permute();
}
rate_start = 0;
}
}
}
/// Squeeze |output| many elements. This does not end in a squeeze
#[inline]
fn squeeze_internal(&mut self, mut rate_start: usize, output: &mut [Field<E>]) {
let output_size = output.len();
if output_size != 0 {
let first_chunk_size = std::cmp::min(RATE - rate_start, output.len());
let num_output_remaining = output.len() - first_chunk_size;
let (first_chunk, rest_chunk) = output.split_at_mut(first_chunk_size);
assert_eq!(rest_chunk.len(), num_output_remaining);
let rest_chunks = rest_chunk.chunks_mut(RATE);
// The total number of chunks is `output[num_output_remaining..].len() / RATE`, plus 1
// for the remainder.
let total_num_chunks = 1 + // 1 for the first chunk
// We add all the chunks that are perfectly divisible by `RATE`
(num_output_remaining / RATE) +
// And also add 1 if the last chunk is non-empty
// (i.e. if `num_output_remaining` is not a multiple of `RATE`)
usize::from((num_output_remaining % RATE) != 0);
// Absorb the input output, `RATE` output at a time, except for the first chunk, which
// is of size `RATE - rate_start`.
for (i, chunk) in std::iter::once(first_chunk).chain(rest_chunks).enumerate() {
let range = rate_start..(rate_start + chunk.len());
debug_assert_eq!(
chunk.len(),
self.state.rate_state(range.clone()).len(),
"Failed to squeeze {output_size} at rate {RATE} & rate_start {rate_start}"
);
chunk.copy_from_slice(self.state.rate_state(range));
// Are we in the last chunk?
// If so, let's wrap up.
if i == total_num_chunks - 1 {
self.mode = DuplexSpongeMode::Squeezing { next_squeeze_index: (rate_start + chunk.len()) };
return;
} else {
self.permute();
}
rate_start = 0;
}
}
}
}