mod.rs
 1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
 2  // This file is part of the deltavm library.
 3  
 4  // Licensed under the Apache License, Version 2.0 (the "License");
 5  // you may not use this file except in compliance with the License.
 6  // You may obtain a copy of the License at:
 7  
 8  // http://www.apache.org/licenses/LICENSE-2.0
 9  
10  // Unless required by applicable law or agreed to in writing, software
11  // distributed under the License is distributed on an "AS IS" BASIS,
12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  // See the License for the specific language governing permissions and
14  // limitations under the License.
15  
16  mod sponge;
17  pub(super) use sponge::*;
18  
19  mod state;
20  pub(super) use state::*;
21  
22  use deltavm_console_types::{Field, prelude::*};
23  
24  use smallvec::SmallVec;
25  
26  /// The interface for a cryptographic sponge.
27  /// A sponge can `absorb` or take in inputs and later `squeeze` or output bytes or field elements.
28  /// The outputs are dependent on previous `absorb` and `squeeze` calls.
29  pub trait AlgebraicSponge<E: Environment, const RATE: usize, const CAPACITY: usize>: Clone + Debug {
30      /// Parameters used by the sponge.
31      type Parameters;
32  
33      /// Initialize a new instance of the sponge.
34      fn new(params: &Self::Parameters) -> Self;
35  
36      /// Absorb an input into the sponge.
37      fn absorb(&mut self, input: &[Field<E>]);
38  
39      /// Squeeze `num_elements` field elements from the sponge.
40      fn squeeze(&mut self, num_elements: u16) -> SmallVec<[Field<E>; 10]>;
41  }
42  
43  /// The mode structure for duplex sponges.
44  #[derive(PartialEq, Eq, Clone, Debug)]
45  pub enum DuplexSpongeMode {
46      /// The sponge is currently absorbing data.
47      Absorbing {
48          /// The next position of the state to be XOR-ed when absorbing.
49          next_absorb_index: usize,
50      },
51      /// The sponge is currently squeezing data out.
52      Squeezing {
53          /// The next position of the state to be outputted when squeezing.
54          next_squeeze_index: usize,
55      },
56  }