zkmemory/
base.rs

1use core::fmt::{Debug, Display};
2use core::ops::{Add, Div, Mul, Rem, Sub};
3use core::usize;
4use ethnum::AsU256;
5pub use ethnum::U256;
6
7/// Base trait for memory address and value
8pub trait Base<const S: usize, T = Self>:
9    Ord
10    + Copy
11    + PartialEq
12    + UsizeConvertible
13    + Display
14    + Debug
15    + Add<T, Output = T>
16    + Mul<T, Output = T>
17    + Sub<T, Output = T>
18    + Rem<T, Output = T>
19    + Div<T, Output = T>
20{
21    /// The max value of the cell
22    const MAX: Self;
23    /// The min value of the cell
24    const MIN: Self;
25    /// The size of the cell
26    const CELL_SIZE: usize = S;
27    /// Check if the value is zero
28    fn is_zero(&self) -> bool;
29    /// Get the zero value
30    fn zero() -> Self;
31    /// Convert to big endian bytes
32    fn to_bytes(&self) -> [u8; S];
33    /// Convert from big endian bytes
34    fn from_bytes(bytes: [u8; S]) -> Self;
35}
36
37/// Convert from/to [usize](core::usize)
38pub trait UsizeConvertible {
39    /// Convert from [usize](core::usize)
40    fn from_usize(value: usize) -> Self;
41    /// Convert to [usize](core::usize)
42    fn to_usize(&self) -> usize;
43}
44
45impl UsizeConvertible for U256 {
46    fn from_usize(value: usize) -> Self {
47        value.as_u256()
48    }
49
50    fn to_usize(&self) -> usize {
51        self.as_usize()
52    }
53}
54
55impl Base<32> for U256 {
56    const MAX: Self = U256::MAX;
57
58    const MIN: Self = U256::MIN;
59
60    fn is_zero(&self) -> bool {
61        self.eq(&U256::ZERO)
62    }
63
64    fn zero() -> Self {
65        U256::ZERO
66    }
67
68    fn to_bytes(&self) -> [u8; 32] {
69        self.to_be_bytes()
70    }
71
72    fn from_bytes(bytes: [u8; 32]) -> Self {
73        Self::from_be_bytes(bytes)
74    }
75}
76
77impl UsizeConvertible for u64 {
78    fn from_usize(value: usize) -> Self {
79        value as u64
80    }
81
82    fn to_usize(&self) -> usize {
83        *self as usize
84    }
85}
86
87impl Base<8> for u64 {
88    const MAX: Self = u64::MAX;
89
90    const MIN: Self = u64::MIN;
91
92    fn is_zero(&self) -> bool {
93        *self == 0
94    }
95
96    fn zero() -> Self {
97        0
98    }
99
100    fn to_bytes(&self) -> [u8; 8] {
101        self.to_be_bytes()
102    }
103
104    fn from_bytes(bytes: [u8; 8]) -> Self {
105        Self::from_be_bytes(bytes)
106    }
107}
108
109impl UsizeConvertible for u32 {
110    fn from_usize(value: usize) -> Self {
111        value as u32
112    }
113
114    fn to_usize(&self) -> usize {
115        *self as usize
116    }
117}
118
119impl Base<4> for u32 {
120    const MAX: Self = u32::MAX;
121
122    const MIN: Self = u32::MIN;
123
124    fn is_zero(&self) -> bool {
125        *self == 0
126    }
127
128    fn zero() -> Self {
129        0
130    }
131
132    fn to_bytes(&self) -> [u8; 4] {
133        self.to_be_bytes()
134    }
135
136    fn from_bytes(bytes: [u8; 4]) -> Self {
137        Self::from_be_bytes(bytes)
138    }
139}