@@ -3,9 +3,11 @@ use core::array::ArrayTrait;
3
3
use core :: keccak :: cairo_keccak;
4
4
use core :: integer :: u128_byte_reverse;
5
5
use core :: fmt :: {Debug , Formatter };
6
+ use pyth :: util :: {UNEXPECTED_OVERFLOW , UNEXPECTED_ZERO , one_shift_left_bytes_u128 };
6
7
7
- pub const EOF : felt252 = ' unexpected end of input' ;
8
- pub const UNEXPECTED_OVERFLOW : felt252 = ' unexpected overflow' ;
8
+ pub mod error_codes {
9
+ pub const EOF : felt252 = ' unexpected end of input' ;
10
+ }
9
11
10
12
/// A byte array with storage format similar to `core::ByteArray`, but
11
13
/// suitable for reading data from it.
@@ -70,8 +72,7 @@ pub impl ByteArrayImpl of ByteArrayTrait {
70
72
}
71
73
}
72
74
73
- /// Allows to read data from a byte array.
74
- /// Uses big endian unless specified otherwise.
75
+ /// Allows to read data from a byte array as big endian integers.
75
76
/// All methods return `EOF` error if attempted to
76
77
/// read more bytes than is available.
77
78
#[derive(Drop , Clone )]
@@ -94,15 +95,16 @@ pub impl ReaderImpl of ReaderTrait {
94
95
}
95
96
96
97
/// Reads the specified number of bytes (up to 16) as a big endian unsigned integer.
97
- fn read (ref self : Reader , num_bytes : u8 ) -> Result <u128 , felt252 > {
98
+ fn read_num_bytes (ref self : Reader , num_bytes : u8 ) -> Result <u128 , felt252 > {
99
+ assert! (num_bytes <= 16 , " Reader::read_num_bytes: num_bytes is too large" );
98
100
if num_bytes <= self . num_current_bytes {
99
101
let x = self . read_from_current (num_bytes );
100
102
return Result :: Ok (x );
101
103
}
102
104
let num_low_bytes = num_bytes - self . num_current_bytes;
103
105
let high = self . current;
104
106
self . fetch_next ()? ;
105
- let low = self . read (num_low_bytes )? ;
107
+ let low = self . read_num_bytes (num_low_bytes )? ;
106
108
let value = if num_low_bytes == 16 {
107
109
low
108
110
} else {
@@ -112,28 +114,34 @@ pub impl ReaderImpl of ReaderTrait {
112
114
}
113
115
114
116
fn read_u256 (ref self : Reader ) -> Result <u256 , felt252 > {
115
- let high = self . read (16 )? ;
116
- let low = self . read (16 )? ;
117
+ let high = self . read_num_bytes (16 )? ;
118
+ let low = self . read_num_bytes (16 )? ;
119
+ let value = u256 { high , low };
120
+ Result :: Ok (value )
121
+ }
122
+ fn read_u160 (ref self : Reader ) -> Result <u256 , felt252 > {
123
+ let high = self . read_num_bytes (4 )? ;
124
+ let low = self . read_num_bytes (16 )? ;
117
125
let value = u256 { high , low };
118
126
Result :: Ok (value )
119
127
}
120
128
fn read_u128 (ref self : Reader ) -> Result <u128 , felt252 > {
121
- self . read (16 )
129
+ self . read_num_bytes (16 )
122
130
}
123
131
fn read_u64 (ref self : Reader ) -> Result <u64 , felt252 > {
124
- let value = self . read (8 )? . try_into (). expect (UNEXPECTED_OVERFLOW );
132
+ let value = self . read_num_bytes (8 )? . try_into (). expect (UNEXPECTED_OVERFLOW );
125
133
Result :: Ok (value )
126
134
}
127
135
fn read_u32 (ref self : Reader ) -> Result <u32 , felt252 > {
128
- let value = self . read (4 )? . try_into (). expect (UNEXPECTED_OVERFLOW );
136
+ let value = self . read_num_bytes (4 )? . try_into (). expect (UNEXPECTED_OVERFLOW );
129
137
Result :: Ok (value )
130
138
}
131
139
fn read_u16 (ref self : Reader ) -> Result <u16 , felt252 > {
132
- let value = self . read (2 )? . try_into (). expect (UNEXPECTED_OVERFLOW );
140
+ let value = self . read_num_bytes (2 )? . try_into (). expect (UNEXPECTED_OVERFLOW );
133
141
Result :: Ok (value )
134
142
}
135
143
fn read_u8 (ref self : Reader ) -> Result <u8 , felt252 > {
136
- let value = self . read (1 )? . try_into (). expect (UNEXPECTED_OVERFLOW );
144
+ let value = self . read_num_bytes (1 )? . try_into (). expect (UNEXPECTED_OVERFLOW );
137
145
Result :: Ok (value )
138
146
}
139
147
@@ -142,7 +150,7 @@ pub impl ReaderImpl of ReaderTrait {
142
150
let mut result = Result :: Ok (());
143
151
while num_bytes > 0 {
144
152
if num_bytes > 16 {
145
- match self . read (16 ) {
153
+ match self . read_num_bytes (16 ) {
146
154
Result :: Ok (_ ) => {},
147
155
Result :: Err (err ) => {
148
156
result = Result :: Err (err );
@@ -151,7 +159,7 @@ pub impl ReaderImpl of ReaderTrait {
151
159
}
152
160
num_bytes -= 16 ;
153
161
} else {
154
- match self . read (num_bytes ) {
162
+ match self . read_num_bytes (num_bytes ) {
155
163
Result :: Ok (_ ) => {},
156
164
Result :: Err (err ) => {
157
165
result = Result :: Err (err );
@@ -165,7 +173,7 @@ pub impl ReaderImpl of ReaderTrait {
165
173
}
166
174
167
175
/// Reads the specified number of bytes as a new byte array.
168
- fn read_bytes (ref self : Reader , num_bytes : usize ) -> Result <ByteArray , felt252 > {
176
+ fn read_byte_array (ref self : Reader , num_bytes : usize ) -> Result <ByteArray , felt252 > {
169
177
let mut array : Array <bytes31 > = array! [];
170
178
let mut num_last_bytes = Option :: None ;
171
179
let mut num_remaining_bytes = num_bytes ;
@@ -204,40 +212,6 @@ pub impl ReaderImpl of ReaderTrait {
204
212
};
205
213
self . num_current_bytes. into () + num_next_bytes + self . array. len ()
206
214
}
207
-
208
- /// Reads the specified number of bytes (up to 16) as a little endian unsigned integer.
209
- fn read_le (ref self : Reader , num_bytes : u8 ) -> Result <u128 , felt252 > {
210
- if num_bytes == 0 {
211
- return Result :: Ok (0 );
212
- }
213
- let value = u128_byte_reverse (self . read (num_bytes )? )
214
- / one_shift_left_bytes_u128 (16 - num_bytes );
215
- Result :: Ok (value )
216
- }
217
-
218
- /// Reads and hashes all the remaining data.
219
- fn keccak256 (ref self : Reader ) -> Result <u256 , felt252 > {
220
- let mut data : Array <u64 > = array! [];
221
-
222
- let mut result = Result :: Ok (());
223
- while self . len () >= 8 {
224
- match self . read_le (8 ) {
225
- Result :: Ok (value ) => { data . append (value . try_into (). expect (UNEXPECTED_OVERFLOW )); },
226
- Result :: Err (err ) => {
227
- result = Result :: Err (err );
228
- break ;
229
- },
230
- }
231
- };
232
- result ? ;
233
-
234
- let last_len = self . len ();
235
- // last_len < 8
236
- let last = self . read_le (last_len . try_into (). expect (UNEXPECTED_OVERFLOW ))? ;
237
- let last = last . try_into (). expect (UNEXPECTED_OVERFLOW );
238
- let hash = cairo_keccak (ref data , last , last_len );
239
- Result :: Ok (hash )
240
- }
241
215
}
242
216
243
217
#[generate_trait]
@@ -246,9 +220,10 @@ impl ReaderPrivateImpl of ReaderPrivateTrait {
246
220
/// Panics if attempted to read more than `self.num_current_bytes`.
247
221
fn read_from_current (ref self : Reader , num_bytes : u8 ) -> u128 {
248
222
let num_remaining_bytes = self . num_current_bytes - num_bytes ;
249
- let divisor = one_shift_left_bytes_u128 (num_remaining_bytes );
250
- // divisor != 0
251
- let (high , low ) = DivRem :: div_rem (self . current, divisor . try_into (). unwrap ());
223
+ let divisor = one_shift_left_bytes_u128 (num_remaining_bytes )
224
+ . try_into ()
225
+ . expect (UNEXPECTED_ZERO );
226
+ let (high , low ) = DivRem :: div_rem (self . current, divisor );
252
227
self . current = low ;
253
228
self . num_current_bytes = num_remaining_bytes ;
254
229
high
@@ -265,7 +240,7 @@ impl ReaderPrivateImpl of ReaderPrivateTrait {
265
240
self . num_current_bytes = 16 ;
266
241
},
267
242
Option :: None => {
268
- let (value , bytes ) = self . array. pop_front (). ok_or (EOF )? ;
243
+ let (value , bytes ) = self . array. pop_front (). ok_or (error_codes :: EOF )? ;
269
244
let value : u256 = value . into ();
270
245
if bytes > 16 {
271
246
self . current = value . high;
@@ -285,49 +260,25 @@ impl ReaderPrivateImpl of ReaderPrivateTrait {
285
260
ref self : Reader , num_bytes : usize , ref array : Array <bytes31 >
286
261
) -> Result <(usize , bool ), felt252 > {
287
262
if num_bytes >= 31 {
288
- let high = self . read (15 )? ;
289
- let low = self . read (16 )? ;
263
+ let high = self . read_num_bytes (15 )? ;
264
+ let low = self . read_num_bytes (16 )? ;
290
265
let value : felt252 = u256 { high , low }. try_into (). expect (UNEXPECTED_OVERFLOW );
291
266
array . append (value . try_into (). expect (UNEXPECTED_OVERFLOW ));
292
267
Result :: Ok ((31 , false ))
293
268
} else if num_bytes > 16 {
294
269
// num_bytes < 31
295
- let high = self . read ((num_bytes - 16 ). try_into (). expect (UNEXPECTED_OVERFLOW ))? ;
296
- let low = self . read (16 )? ;
270
+ let high = self
271
+ . read_num_bytes ((num_bytes - 16 ). try_into (). expect (UNEXPECTED_OVERFLOW ))? ;
272
+ let low = self . read_num_bytes (16 )? ;
297
273
let value : felt252 = u256 { high , low }. try_into (). expect (UNEXPECTED_OVERFLOW );
298
274
array . append (value . try_into (). expect (UNEXPECTED_OVERFLOW ));
299
275
Result :: Ok ((num_bytes , true ))
300
276
} else {
301
277
// bytes < 16
302
- let low = self . read (num_bytes . try_into (). expect (UNEXPECTED_OVERFLOW ))? ;
278
+ let low = self . read_num_bytes (num_bytes . try_into (). expect (UNEXPECTED_OVERFLOW ))? ;
303
279
let value : felt252 = low . try_into (). expect (UNEXPECTED_OVERFLOW );
304
280
array . append (value . try_into (). expect (UNEXPECTED_OVERFLOW ));
305
281
Result :: Ok ((num_bytes , true ))
306
282
}
307
283
}
308
284
}
309
-
310
- // Returns 1 << (8 * `n_bytes`) as u128, where `n_bytes` must be < BYTES_IN_U128.
311
- //
312
- // Panics if `n_bytes >= 16`.
313
- fn one_shift_left_bytes_u128 (n_bytes : u8 ) -> u128 {
314
- match n_bytes {
315
- 0 => 0x1 ,
316
- 1 => 0x100 ,
317
- 2 => 0x10000 ,
318
- 3 => 0x1000000 ,
319
- 4 => 0x100000000 ,
320
- 5 => 0x10000000000 ,
321
- 6 => 0x1000000000000 ,
322
- 7 => 0x100000000000000 ,
323
- 8 => 0x10000000000000000 ,
324
- 9 => 0x1000000000000000000 ,
325
- 10 => 0x100000000000000000000 ,
326
- 11 => 0x10000000000000000000000 ,
327
- 12 => 0x1000000000000000000000000 ,
328
- 13 => 0x100000000000000000000000000 ,
329
- 14 => 0x10000000000000000000000000000 ,
330
- 15 => 0x1000000000000000000000000000000 ,
331
- _ => core :: panic_with_felt252 (' n_bytes too big' ),
332
- }
333
- }
0 commit comments