1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use cfg_if::cfg_if;
use core::{
fmt::{
self,
Debug,
Display,
Formatter,
},
ops::AddAssign,
};
#[cfg(feature = "std")]
use scale_info::{
build::Fields,
Path,
Type,
TypeInfo,
};
#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Key([u8; 32]);
impl Key {
#[inline]
pub const fn new(bytes: [u8; 32]) -> Self {
Self(bytes)
}
}
impl From<[u8; 32]> for Key {
#[inline]
fn from(bytes: [u8; 32]) -> Self {
Self::new(bytes)
}
}
impl AsRef<[u8; 32]> for Key {
#[inline]
fn as_ref(&self) -> &[u8; 32] {
&self.0
}
}
impl AsMut<[u8; 32]> for Key {
#[inline]
fn as_mut(&mut self) -> &mut [u8; 32] {
&mut self.0
}
}
impl Key {
fn write_bytes(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "0x")?;
let bytes = self.as_ref();
let len_bytes = bytes.len();
let len_chunk = 4;
let len_chunks = len_bytes / len_chunk;
for i in 0..len_chunks {
let offset = i * len_chunk;
write!(
f,
"_{:02X}{:02X}{:02X}{:02X}",
bytes[offset],
bytes[offset + 1],
bytes[offset + 2],
bytes[offset + 3]
)?;
}
Ok(())
}
}
impl Debug for Key {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Key(")?;
self.write_bytes(f)?;
write!(f, ")")?;
Ok(())
}
}
impl Display for Key {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.write_bytes(f)
}
}
impl Key {
#[cfg(target_endian = "little")]
fn reinterpret_as_u64x4(&self) -> &[u64; 4] {
unsafe { &*(&self.0 as *const [u8; 32] as *const [u64; 4]) }
}
#[cfg(target_endian = "little")]
fn reinterpret_as_u64x4_mut(&mut self) -> &mut [u64; 4] {
unsafe { &mut *(&mut self.0 as *mut [u8; 32] as *mut [u64; 4]) }
}
}
impl scale::Encode for Key {
#[inline]
fn size_hint(&self) -> usize {
32
}
#[inline]
fn encode_to<O>(&self, output: &mut O)
where
O: scale::Output + ?Sized,
{
output.write(self.as_ref());
}
#[inline]
fn using_encoded<R, F>(&self, f: F) -> R
where
F: FnOnce(&[u8]) -> R,
{
f(self.as_ref())
}
#[inline]
fn encoded_size(&self) -> usize {
self.size_hint()
}
}
impl scale::EncodeLike<[u8; 32]> for Key {}
impl scale::Decode for Key {
#[inline]
fn decode<I>(input: &mut I) -> Result<Self, scale::Error>
where
I: scale::Input,
{
let bytes = <[u8; 32] as scale::Decode>::decode(input)?;
Ok(Self::from(bytes))
}
#[inline]
fn encoded_fixed_size() -> Option<usize> {
Some(32)
}
}
#[cfg(feature = "std")]
impl TypeInfo for Key {
type Identity = Self;
fn type_info() -> Type {
Type::builder()
.path(Path::new("Key", "ink_primitives"))
.composite(
Fields::unnamed().field(|f| f.ty::<[u8; 32]>().type_name("[u8; 32]")),
)
}
}
impl Key {
#[cfg(target_endian = "little")]
fn add_assign_u64_le(&mut self, rhs: u64) {
let words = self.reinterpret_as_u64x4_mut();
let (res0, ovfl) = words[0].overflowing_add(rhs);
let (res1, ovfl) = words[1].overflowing_add(ovfl as u64);
let (res2, ovfl) = words[2].overflowing_add(ovfl as u64);
let (res3, _ovfl) = words[3].overflowing_add(ovfl as u64);
words[0] = res0;
words[1] = res1;
words[2] = res2;
words[3] = res3;
}
#[cfg(target_endian = "little")]
fn add_assign_u64_le_using(&self, rhs: u64, result: &mut Key) {
let input = self.reinterpret_as_u64x4();
let result = result.reinterpret_as_u64x4_mut();
let (res0, ovfl) = input[0].overflowing_add(rhs);
let (res1, ovfl) = input[1].overflowing_add(ovfl as u64);
let (res2, ovfl) = input[2].overflowing_add(ovfl as u64);
let (res3, _ovfl) = input[3].overflowing_add(ovfl as u64);
result[0] = res0;
result[1] = res1;
result[2] = res2;
result[3] = res3;
}
#[cfg(target_endian = "big")]
fn add_assign_u64_be(&mut self, rhs: u64) {
let rhs_bytes = rhs.to_be_bytes();
let lhs_bytes = self.as_mut();
let len_rhs = rhs_bytes.len();
let len_lhs = lhs_bytes.len();
let mut carry = 0;
for i in 0..len_rhs {
let (res, ovfl) =
lhs_bytes[i].overflowing_add(rhs_bytes[i].wrapping_add(carry));
lhs_bytes[i] = res;
carry = ovfl as u8;
}
for i in len_rhs..len_lhs {
let (res, ovfl) = lhs_bytes[i].overflowing_add(carry);
lhs_bytes[i] = res;
carry = ovfl as u8;
if carry == 0 {
return
}
}
}
#[cfg(target_endian = "big")]
fn add_assign_u64_be_using(&self, rhs: u64, result: &mut Key) {
let rhs_bytes = rhs.to_be_bytes();
let lhs_bytes = self.as_ref();
let result_bytes = result.as_mut();
let len_rhs = rhs_bytes.len();
let len_lhs = lhs_bytes.len();
let mut carry = 0;
for i in 0..len_rhs {
let (res, ovfl) =
lhs_bytes[i].overflowing_add(rhs_bytes[i].wrapping_add(carry));
result_bytes[i] = res;
carry = ovfl as u8;
}
for i in len_rhs..len_lhs {
let (res, ovfl) = lhs_bytes[i].overflowing_add(carry);
result_bytes[i] = res;
carry = ovfl as u8;
}
}
#[inline]
pub fn add_assign_using<T>(&self, rhs: T, result: &mut Key)
where
T: Into<u64>,
{
let rhs = rhs.into();
cfg_if! {
if #[cfg(target_endian = "little")] {
self.add_assign_u64_le_using(rhs, result);
} else {
self.add_assign_u64_be_using(rhs, result);
}
}
}
}
impl AddAssign<u64> for Key {
#[inline]
fn add_assign(&mut self, rhs: u64) {
cfg_if! {
if #[cfg(target_endian = "little")] {
self.add_assign_u64_le(rhs);
} else {
self.add_assign_u64_be(rhs);
}
}
}
}
impl AddAssign<&u64> for Key {
#[inline]
fn add_assign(&mut self, rhs: &u64) {
<Self as AddAssign<u64>>::add_assign(self, *rhs)
}
}