Skip to main content

snarkvm_console_algorithms/poseidon/helpers/
state.rs

1// Copyright (c) 2019-2026 Provable Inc.
2// This file is part of the snarkVM 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
16use snarkvm_console_types::{Field, prelude::*};
17
18use core::ops::{Index, IndexMut, Range};
19
20#[derive(Copy, Clone, Debug)]
21pub struct State<E: Environment, const RATE: usize, const CAPACITY: usize> {
22    capacity_state: [Field<E>; CAPACITY],
23    rate_state: [Field<E>; RATE],
24}
25
26impl<E: Environment, const RATE: usize, const CAPACITY: usize> Default for State<E, RATE, CAPACITY> {
27    fn default() -> Self {
28        Self { capacity_state: [Field::<E>::zero(); CAPACITY], rate_state: [Field::<E>::zero(); RATE] }
29    }
30}
31
32impl<E: Environment, const RATE: usize, const CAPACITY: usize> State<E, RATE, CAPACITY> {
33    /// Returns a reference to a range of the rate state.
34    pub(super) fn rate_state(&self, range: Range<usize>) -> &[Field<E>] {
35        &self.rate_state[range]
36    }
37
38    /// Returns a mutable rate state.
39    pub(super) fn rate_state_mut(&mut self) -> &mut [Field<E>; RATE] {
40        &mut self.rate_state
41    }
42}
43
44impl<E: Environment, const RATE: usize, const CAPACITY: usize> State<E, RATE, CAPACITY> {
45    /// Returns an immutable iterator over the state.
46    pub fn iter(&self) -> impl Iterator<Item = &Field<E>> + Clone {
47        self.capacity_state.iter().chain(self.rate_state.iter())
48    }
49
50    /// Returns an mutable iterator over the state.
51    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Field<E>> {
52        self.capacity_state.iter_mut().chain(self.rate_state.iter_mut())
53    }
54}
55
56impl<E: Environment, const RATE: usize, const CAPACITY: usize> Index<usize> for State<E, RATE, CAPACITY> {
57    type Output = Field<E>;
58
59    fn index(&self, index: usize) -> &Self::Output {
60        assert!(index < RATE + CAPACITY, "Index out of bounds: index is {} but length is {}", index, RATE + CAPACITY);
61        if index < CAPACITY { &self.capacity_state[index] } else { &self.rate_state[index - CAPACITY] }
62    }
63}
64
65impl<E: Environment, const RATE: usize, const CAPACITY: usize> IndexMut<usize> for State<E, RATE, CAPACITY> {
66    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
67        assert!(index < RATE + CAPACITY, "Index out of bounds: index is {} but length is {}", index, RATE + CAPACITY);
68        if index < CAPACITY { &mut self.capacity_state[index] } else { &mut self.rate_state[index - CAPACITY] }
69    }
70}