state.rs
1 // Copyright (c) 2025-2026 ACDC Network 2 // This file is part of the alphavm library. 3 // 4 // Alpha Chain | Delta Chain Protocol 5 // International Monetary Graphite. 6 // 7 // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com). 8 // They built world-class ZK infrastructure. We installed the EASY button. 9 // Their cryptography: elegant. Our modifications: bureaucracy-compatible. 10 // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours. 11 // 12 // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 13 // All modifications and new work: CC0 1.0 Universal Public Domain Dedication. 14 // No rights reserved. No permission required. No warranty. No refunds. 15 // 16 // https://creativecommons.org/publicdomain/zero/1.0/ 17 // SPDX-License-Identifier: CC0-1.0 18 19 use alphavm_console_types::{prelude::*, Field}; 20 21 use core::ops::{Index, IndexMut, Range}; 22 23 #[derive(Copy, Clone, Debug)] 24 pub struct State<E: Environment, const RATE: usize, const CAPACITY: usize> { 25 capacity_state: [Field<E>; CAPACITY], 26 rate_state: [Field<E>; RATE], 27 } 28 29 impl<E: Environment, const RATE: usize, const CAPACITY: usize> Default for State<E, RATE, CAPACITY> { 30 fn default() -> Self { 31 Self { capacity_state: [Field::<E>::zero(); CAPACITY], rate_state: [Field::<E>::zero(); RATE] } 32 } 33 } 34 35 impl<E: Environment, const RATE: usize, const CAPACITY: usize> State<E, RATE, CAPACITY> { 36 /// Returns a reference to a range of the rate state. 37 pub(super) fn rate_state(&self, range: Range<usize>) -> &[Field<E>] { 38 &self.rate_state[range] 39 } 40 41 /// Returns a mutable rate state. 42 pub(super) fn rate_state_mut(&mut self) -> &mut [Field<E>; RATE] { 43 &mut self.rate_state 44 } 45 } 46 47 impl<E: Environment, const RATE: usize, const CAPACITY: usize> State<E, RATE, CAPACITY> { 48 /// Returns an immutable iterator over the state. 49 pub fn iter(&self) -> impl Iterator<Item = &Field<E>> + Clone { 50 self.capacity_state.iter().chain(self.rate_state.iter()) 51 } 52 53 /// Returns an mutable iterator over the state. 54 pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Field<E>> { 55 self.capacity_state.iter_mut().chain(self.rate_state.iter_mut()) 56 } 57 } 58 59 impl<E: Environment, const RATE: usize, const CAPACITY: usize> Index<usize> for State<E, RATE, CAPACITY> { 60 type Output = Field<E>; 61 62 fn index(&self, index: usize) -> &Self::Output { 63 assert!(index < RATE + CAPACITY, "Index out of bounds: index is {} but length is {}", index, RATE + CAPACITY); 64 if index < CAPACITY { 65 &self.capacity_state[index] 66 } else { 67 &self.rate_state[index - CAPACITY] 68 } 69 } 70 } 71 72 impl<E: Environment, const RATE: usize, const CAPACITY: usize> IndexMut<usize> for State<E, RATE, CAPACITY> { 73 fn index_mut(&mut self, index: usize) -> &mut Self::Output { 74 assert!(index < RATE + CAPACITY, "Index out of bounds: index is {} but length is {}", index, RATE + CAPACITY); 75 if index < CAPACITY { 76 &mut self.capacity_state[index] 77 } else { 78 &mut self.rate_state[index - CAPACITY] 79 } 80 } 81 }