|
1 | | -use starknet::Store; |
2 | 1 | use starknet::storage::{ |
3 | 2 | Mutable, StorageAsPointer, StoragePointer, StoragePointerReadAccess, StoragePointerWriteAccess, |
4 | 3 | }; |
| 4 | +use starknet::{EthAddress, Store}; |
| 5 | + |
5 | 6 |
|
6 | 7 | pub trait AddToStorage<T> { |
7 | 8 | type Value; |
@@ -60,3 +61,154 @@ pub impl StoragePathSubFromStorageImpl< |
60 | 61 | new_value |
61 | 62 | } |
62 | 63 | } |
| 64 | + |
| 65 | + |
| 66 | +/// Trait for types that can be stored as 160 bits. |
| 67 | +/// |
| 68 | +/// The format is a tuple `(low: u128, high: u32)`. |
| 69 | +/// |
| 70 | +/// # Requirements |
| 71 | +/// - `encode` should be injective: distinct values map to distinct tuples. |
| 72 | +/// - `decode` should be the inverse of `encode` on its image: |
| 73 | +/// decode(encode(v)) == v |
| 74 | +pub trait Castable160<V> { |
| 75 | + /// Convert a value into its 160-bit representation `(low, high)`. |
| 76 | + fn encode(value: V) -> (u128, u32); |
| 77 | + |
| 78 | + /// Convert a 160-bit representation back into the original value. |
| 79 | + fn decode(value: (u128, u32)) -> V; |
| 80 | +} |
| 81 | + |
| 82 | +/// `Castable160` implementation for primitive integer types ('u8', 'u16', 'u32', 'u64' and 'u128'.) |
| 83 | +/// that fit entirely in 128 bits. |
| 84 | +pub impl PrimitiveCastable160<T, +Into<T, u128>, +TryInto<u128, T>, +Drop<T>> of Castable160<T> { |
| 85 | + fn encode(value: T) -> (u128, u32) { |
| 86 | + (value.into(), 0) |
| 87 | + } |
| 88 | + fn decode(value: (u128, u32)) -> T { |
| 89 | + let (low, high) = value; |
| 90 | + assert(high == 0, 'Castable160: high bits not 0'); |
| 91 | + low.try_into().unwrap() |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// Trait to define the offset for signed integer. |
| 96 | +trait SignedIntegerOffset<T> { |
| 97 | + fn offset() -> felt252; |
| 98 | +} |
| 99 | + |
| 100 | +impl I8Offset of SignedIntegerOffset<i8> { |
| 101 | + fn offset() -> felt252 { |
| 102 | + // 2 ** 7 |
| 103 | + 128 |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl I16Offset of SignedIntegerOffset<i16> { |
| 108 | + fn offset() -> felt252 { |
| 109 | + // 2** 15 |
| 110 | + 32768 |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl I32Offset of SignedIntegerOffset<i32> { |
| 115 | + fn offset() -> felt252 { |
| 116 | + // 2** 31 |
| 117 | + 2147483648 |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl I64Offset of SignedIntegerOffset<i64> { |
| 122 | + fn offset() -> felt252 { |
| 123 | + // 2** 63 |
| 124 | + 9223372036854775808 |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +impl I128Offset of SignedIntegerOffset<i128> { |
| 129 | + fn offset() -> felt252 { |
| 130 | + // 2** 127 |
| 131 | + 170141183460469231731687303715884105728 |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +pub impl SignedIntegerCastable160< |
| 136 | + T, +SignedIntegerOffset<T>, +Into<T, felt252>, +TryInto<felt252, T>, +Drop<T>, |
| 137 | +> of Castable160<T> { |
| 138 | + fn encode(value: T) -> (u128, u32) { |
| 139 | + let val_felt: felt252 = value.into(); |
| 140 | + let offset = SignedIntegerOffset::<T>::offset(); |
| 141 | + let val_u128: u128 = (val_felt + offset).try_into().unwrap(); |
| 142 | + (val_u128, 0) |
| 143 | + } |
| 144 | + fn decode(value: (u128, u32)) -> T { |
| 145 | + let (low, high) = value; |
| 146 | + assert(high == 0, 'Castable160: high bits not 0'); |
| 147 | + let val_felt: felt252 = low.into(); |
| 148 | + let offset = SignedIntegerOffset::<T>::offset(); |
| 149 | + (val_felt - offset).try_into().unwrap() |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +pub impl EthAddressCastable160 of Castable160<EthAddress> { |
| 154 | + fn encode(value: EthAddress) -> (u128, u32) { |
| 155 | + let felt_val: felt252 = value.into(); |
| 156 | + let u256_val: u256 = felt_val.into(); |
| 157 | + let low = u256_val.low; |
| 158 | + let high: u32 = u256_val.high.try_into().unwrap(); |
| 159 | + (low, high) |
| 160 | + } |
| 161 | + fn decode(value: (u128, u32)) -> EthAddress { |
| 162 | + let (low, high) = value; |
| 163 | + let u256_val = u256 { low, high: high.into() }; |
| 164 | + u256_val.try_into().unwrap() |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +/// Trait for types that can be stored as 64 bits. |
| 169 | +/// |
| 170 | +/// The format is a `u64`. |
| 171 | +/// |
| 172 | +/// # Requirements |
| 173 | +/// - `encode` should be injective: distinct values map to distinct tuples. |
| 174 | +/// - `decode` should be the inverse of `encode` on its image: |
| 175 | +/// decode(encode(v)) == v |
| 176 | +pub trait Castable64<V> { |
| 177 | + /// Convert a value into its 64-bit representation. |
| 178 | + fn encode(value: V) -> u64; |
| 179 | + /// Convert a 64-bit representation back into the original value. |
| 180 | + fn decode(value: u64) -> V; |
| 181 | +} |
| 182 | + |
| 183 | +pub impl PrimitiveCastable64<T, +Into<T, u64>, +TryInto<u64, T>, +Drop<T>> of Castable64<T> { |
| 184 | + fn encode(value: T) -> u64 { |
| 185 | + value.into() |
| 186 | + } |
| 187 | + fn decode(value: u64) -> T { |
| 188 | + value.try_into().unwrap() |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | + |
| 193 | +/// Trait to check if a type fits in 64 bits. |
| 194 | +trait FitsIn64<T> {} |
| 195 | +impl I8FitsIn64 of FitsIn64<i8> {} |
| 196 | +impl I16FitsIn64 of FitsIn64<i16> {} |
| 197 | +impl I32FitsIn64 of FitsIn64<i32> {} |
| 198 | +impl I64FitsIn64 of FitsIn64<i64> {} |
| 199 | + |
| 200 | +pub impl SignedIntegerCastable64< |
| 201 | + T, +FitsIn64<T>, +SignedIntegerOffset<T>, +Into<T, felt252>, +TryInto<felt252, T>, +Drop<T>, |
| 202 | +> of Castable64<T> { |
| 203 | + fn encode(value: T) -> u64 { |
| 204 | + let val_felt: felt252 = value.into(); |
| 205 | + let offset = SignedIntegerOffset::<T>::offset(); |
| 206 | + let val_u64: u64 = (val_felt + offset).try_into().unwrap(); |
| 207 | + val_u64 |
| 208 | + } |
| 209 | + fn decode(value: u64) -> T { |
| 210 | + let val_felt: felt252 = value.into(); |
| 211 | + let offset = SignedIntegerOffset::<T>::offset(); |
| 212 | + (val_felt - offset).try_into().unwrap() |
| 213 | + } |
| 214 | +} |
0 commit comments