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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
use ethnum::U256;
use fuel_asm::{
PanicReason,
RegId,
wideint::*,
};
use fuel_types::Word;
use super::super::{
ExecutableTransaction,
Interpreter,
internal::inc_pc,
is_unsafe_math,
is_wrapping,
};
use crate::{
constraints::reg_key::*,
error::SimpleResult,
interpreter::Memory,
};
// This macro is used to duplicate the implementation for both 128-bit and 256-bit
// versions. It takes two type parameters: the current type and type that has double-width
// of it. The appropriate type is chosen based on benchmarks for each operation.
// Currently, `primitive_types` is used for anything requiring division, modulo or 512-bit
// precision. Otherwise, `ethnum` is used for 256-bit operations, and the builtin `u128`
// for 128-bit operations.
macro_rules! wideint_ops {
($t:ident, $wider_t:ident) => {
paste::paste! {
// Conversion helpers
/// Converts to `primitive_types` version of the current type
fn [<to_prim_ $t:lower>](value: $t) -> primitive_types::[<$t:upper>] {
let mut buffer = [0u8; core::mem::size_of::<$t>()];
buffer[..].copy_from_slice(&value.to_le_bytes());
primitive_types::[<$t:upper>]::from_little_endian(&buffer)
}
/// Converts to `primitive_types` version that has double the size of the current type
fn [<to_wider_prim_ $t:lower>](value: $t) -> primitive_types::[<$wider_t:upper>] {
let mut buffer = [0u8; 2 * core::mem::size_of::<$t>()];
buffer[..core::mem::size_of::<$t>()].copy_from_slice(&value.to_le_bytes());
primitive_types::[<$wider_t:upper>]::from_little_endian(&buffer)
}
/// Converts to `u128` or `ethnum::U256`
fn [<from_prim_ $t:lower>](value: primitive_types::[<$t:upper>]) -> $t {
let mut buffer = [0u8; ::core::mem::size_of::<$t>()];
value.to_little_endian(&mut buffer);
$t::from_le_bytes(buffer)
}
/// Drops higher half of the value and converts to `u128` or `ethnum::U256`
fn [<truncate_from_prim_ $t:lower>](value: primitive_types::[<$wider_t:upper>]) -> $t {
const S: usize = ::core::mem::size_of::<$t>();
let mut buffer = [0u8; 2 * S];
value.to_little_endian(&mut buffer);
let truncated: [u8; S] = buffer[..S].try_into().unwrap_or_else(|_| unreachable!());
$t::from_le_bytes(truncated)
}
impl<M, S, Tx, Ecal, V> Interpreter<M, S, Tx, Ecal, V>
where
M: Memory,
Tx: ExecutableTransaction,
{
pub(crate) fn [<alu_wideint_cmp_ $t:lower>](
&mut self,
ra: RegId,
b: Word,
c: Word,
args: CompareArgs,
) -> SimpleResult<()> {
let (SystemRegisters { mut of, mut err, pc, .. }, mut w) = split_registers(&mut self.registers);
let dest: &mut Word = &mut w[ra.try_into()?];
// LHS argument is always indirect, load it
let lhs: $t = $t::from_be_bytes(self.memory.as_ref().read_bytes(b)?);
// RHS is only indirect if the flag is set
let rhs: $t = if args.indirect_rhs {
$t::from_be_bytes(self.memory.as_ref().read_bytes(c)?)
} else {
c.into()
};
*dest = [<cmp_ $t:lower>](lhs, rhs, args.mode);
*of = 0;
*err = 0;
inc_pc(pc);
Ok(())
}
pub(crate) fn [<alu_wideint_op_ $t:lower>](
&mut self,
dest_addr: Word,
b: Word,
c: Word,
args: MathArgs,
) -> SimpleResult<()> {
let owner_regs = self.ownership_registers();
let (SystemRegisters { flag, mut of, mut err, pc, .. }, _) = split_registers(&mut self.registers);
// LHS argument is always indirect, load it
let lhs: $t = $t::from_be_bytes(self.memory.as_ref().read_bytes(b)?);
// RHS is only indirect if the flag is set
let rhs: $t = if args.indirect_rhs {
$t::from_be_bytes(self.memory.as_ref().read_bytes(c)?)
} else {
c.into()
};
let (wrapped, overflow) = [<op_overflowing_ $t:lower>](lhs, rhs, args);
if overflow && !is_wrapping(flag.into()) {
return Err(PanicReason::ArithmeticOverflow.into());
}
*of = overflow as Word;
*err = 0;
self.memory.as_mut().write_bytes(owner_regs, dest_addr, wrapped.to_be_bytes())?;
inc_pc(pc);
Ok(())
}
pub(crate) fn [<alu_wideint_mul_ $t:lower>](
&mut self,
dest_addr: Word,
b: Word,
c: Word,
args: MulArgs,
) -> SimpleResult<()> {
let owner_regs = self.ownership_registers();
let (SystemRegisters { flag, mut of, mut err, pc, .. }, _) = split_registers(&mut self.registers);
// LHS is only indirect if the flag is set
let lhs: $t = if args.indirect_lhs {
$t::from_be_bytes(self.memory.as_ref().read_bytes(b)?)
} else {
b.into()
};
// RHS is only indirect if the flag is set
let rhs: $t = if args.indirect_rhs {
$t::from_be_bytes(self.memory.as_ref().read_bytes(c)?)
} else {
c.into()
};
let (wrapped, overflow) = $t::overflowing_mul(lhs, rhs);
if overflow && !is_wrapping(flag.into()) {
return Err(PanicReason::ArithmeticOverflow.into());
}
*of = overflow as Word;
*err = 0;
self.memory.as_mut().write_bytes(owner_regs, dest_addr, wrapped.to_be_bytes())?;
inc_pc(pc);
Ok(())
}
pub(crate) fn [<alu_wideint_div_ $t:lower>](
&mut self,
dest_addr: Word,
b: Word,
c: Word,
args: DivArgs,
) -> SimpleResult<()> {
let owner_regs = self.ownership_registers();
let (SystemRegisters { flag, mut of, mut err, pc, .. }, _) = split_registers(&mut self.registers);
// LHS is always indirect
let lhs: $t = $t::from_be_bytes(self.memory.as_ref().read_bytes(b)?);
// RHS is only indirect if the flag is set
let rhs: $t = if args.indirect_rhs {
$t::from_be_bytes(self.memory.as_ref().read_bytes(c)?)
} else {
c.into()
};
let lhs = [<to_prim_ $t:lower>](lhs);
let rhs = [<to_prim_ $t:lower>](rhs);
let result = match lhs.checked_div(rhs) {
Some(d) => {
*err = 0;
[<from_prim_ $t:lower>](d)
},
None => {
if is_unsafe_math(flag.into()) {
*err = 1;
$t::default() // Zero
} else {
return Err(PanicReason::ArithmeticError.into());
}
}
};
*of = 0;
self.memory.as_mut().write_bytes(owner_regs, dest_addr, result.to_be_bytes())?;
inc_pc(pc);
Ok(())
}
pub(crate) fn [<alu_wideint_addmod_ $t:lower>](
&mut self,
dest_addr: Word,
b: Word,
c: Word,
d: Word,
) -> SimpleResult<()> {
let owner_regs = self.ownership_registers();
let (SystemRegisters { flag, mut of, mut err, pc, .. }, _) = split_registers(&mut self.registers);
let lhs: $t = $t::from_be_bytes(self.memory.as_ref().read_bytes(b)?);
let rhs: $t = $t::from_be_bytes(self.memory.as_ref().read_bytes(c)?);
let modulus: $t = $t::from_be_bytes(self.memory.as_ref().read_bytes(d)?);
// Use wider types to avoid overflow
let lhs = [<to_wider_prim_ $t:lower>](lhs);
let rhs = [<to_wider_prim_ $t:lower>](rhs);
let modulus = [<to_wider_prim_ $t:lower>](modulus);
let pre_mod = lhs.checked_add(rhs)
.expect("Cannot overflow as we're using wider types");
let result: $t = match pre_mod.checked_rem(modulus) {
Some(result) => {
*err = 0;
// Truncate never loses data as modulus is still in domain of the original type
[<truncate_from_prim_ $t:lower>](result)
},
None => {
if is_unsafe_math(flag.into()) {
*err = 1;
$t::default() // Zero
} else {
return Err(PanicReason::ArithmeticError.into());
}
}
};
*of = 0;
self.memory.as_mut().write_bytes(owner_regs, dest_addr, result.to_be_bytes())?;
inc_pc(pc);
Ok(())
}
pub(crate) fn [<alu_wideint_mulmod_ $t:lower>](
&mut self,
dest_addr: Word,
b: Word,
c: Word,
d: Word,
) -> SimpleResult<()> {
let owner_regs = self.ownership_registers();
let (SystemRegisters { flag, mut of, mut err, pc, .. }, _) = split_registers(&mut self.registers);
let lhs: $t = $t::from_be_bytes(self.memory.as_ref().read_bytes(b)?);
let rhs: $t = $t::from_be_bytes(self.memory.as_ref().read_bytes(c)?);
let modulus: $t = $t::from_be_bytes(self.memory.as_ref().read_bytes(d)?);
let lhs = [<to_prim_ $t:lower>](lhs);
let rhs = [<to_prim_ $t:lower>](rhs);
let modulus = [<to_wider_prim_ $t:lower>](modulus);
let result = match lhs.full_mul(rhs).checked_rem(modulus) {
None => {
if is_unsafe_math(flag.into()) {
*err = 1;
$t::default() // Zero
} else {
return Err(PanicReason::ArithmeticError.into());
}
},
Some(result) => {
*err = 0;
// This never loses data, since the modulus type has same width as the result
[<truncate_from_prim_ $t:lower>](result)
}
};
*of = 0;
self.memory.as_mut().write_bytes(owner_regs, dest_addr, result.to_be_bytes())?;
inc_pc(pc);
Ok(())
}
pub(crate) fn [<alu_wideint_muldiv_ $t:lower>](
&mut self,
dest_addr: Word,
b: Word,
c: Word,
d: Word,
) -> SimpleResult<()> {
let owner_regs = self.ownership_registers();
let (SystemRegisters { mut of, mut err, pc, flag, .. }, _) = split_registers(&mut self.registers);
let lhs: $t = $t::from_be_bytes(self.memory.as_ref().read_bytes(b)?);
let rhs: $t = $t::from_be_bytes(self.memory.as_ref().read_bytes(c)?);
let divider: $t = $t::from_be_bytes(self.memory.as_ref().read_bytes(d)?);
const S: usize = core::mem::size_of::<$t>();
let lhs = [<to_prim_ $t:lower>](lhs);
let rhs = [<to_prim_ $t:lower>](rhs);
let product = lhs.full_mul(rhs);
#[allow(clippy::arithmetic_side_effects)] // Safety: the shift has less bits than the product
let product_div_max = product >> (S * 8);
let result = product.checked_div([<to_wider_prim_ $t:lower>](divider)).unwrap_or(product_div_max);
let mut buffer = [0u8; 2 * S];
result.to_little_endian(&mut buffer);
let lower_half: [u8; S] = buffer[..S].try_into().unwrap_or_else(|_| unreachable!());
let higher_half: [u8; S] = buffer[S..].try_into().unwrap_or_else(|_| unreachable!());
let result = $t::from_le_bytes(lower_half);
let overflows = higher_half != [0u8; S];
if overflows && !is_wrapping(flag.into()) {
return Err(PanicReason::ArithmeticOverflow.into());
}
*of = overflows as Word;
*err = 0;
self.memory.as_mut().write_bytes(owner_regs, dest_addr, result.to_be_bytes())?;
inc_pc(pc);
Ok(())
}
}
pub(crate) fn [<cmp_ $t:lower>](
lhs: $t,
rhs: $t,
mode: CompareMode,
) -> Word {
match mode {
CompareMode::EQ => (lhs == rhs) as Word,
CompareMode::NE => (lhs != rhs) as Word,
CompareMode::GT => (lhs > rhs) as Word,
CompareMode::LT => (lhs < rhs) as Word,
CompareMode::GTE => (lhs >= rhs) as Word,
CompareMode::LTE => (lhs <= rhs) as Word,
CompareMode::LZC => lhs.leading_zeros() as Word,
}
}
/// Returns (wrapped, overflow) just like overflowing_* operations of Rust integers
pub(crate) fn [<op_overflowing_ $t:lower>] (
lhs: $t,
rhs: $t,
args: MathArgs,
) -> ($t, bool) {
match args.op {
MathOp::ADD => { $t::overflowing_add(lhs, rhs) }
MathOp::SUB => { $t::overflowing_sub(lhs, rhs) }
MathOp::OR => { (lhs | rhs, false) }
MathOp::XOR => { (lhs ^ rhs, false) }
MathOp::AND => {(lhs & rhs, false) }
MathOp::NOT => { (!lhs, false) }
MathOp::SHL => {
if let Ok(rhs) = rhs.try_into() {
($t::checked_shl(lhs, rhs).unwrap_or_default(), false)
} else {
($t::default(), false) // Zero
}
}
MathOp::SHR => {
if let Ok(rhs) = rhs.try_into() {
($t::checked_shr(lhs, rhs).unwrap_or_default(), false)
} else {
($t::default(), false) // Zero
}
}
}
}
}
};
}
wideint_ops!(u128, U256);
wideint_ops!(U256, U512);