revm_interpreter/instructions/
arithmetic.rs

1use super::i256::{i256_div, i256_mod};
2use crate::{
3    interpreter_types::{InterpreterTypes, StackTr},
4    InstructionContext,
5};
6use primitives::U256;
7
8/// Implements the ADD instruction - adds two values from stack.
9pub fn add<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
10    popn_top!([op1], op2, context.interpreter);
11    *op2 = op1.wrapping_add(*op2);
12}
13
14/// Implements the MUL instruction - multiplies two values from stack.
15pub fn mul<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
16    popn_top!([op1], op2, context.interpreter);
17    *op2 = op1.wrapping_mul(*op2);
18}
19
20/// Implements the SUB instruction - subtracts two values from stack.
21pub fn sub<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
22    popn_top!([op1], op2, context.interpreter);
23    *op2 = op1.wrapping_sub(*op2);
24}
25
26/// Implements the DIV instruction - divides two values from stack.
27pub fn div<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
28    popn_top!([op1], op2, context.interpreter);
29    if !op2.is_zero() {
30        *op2 = op1.wrapping_div(*op2);
31    }
32}
33
34/// Implements the SDIV instruction.
35///
36/// Performs signed division of two values from stack.
37pub fn sdiv<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
38    popn_top!([op1], op2, context.interpreter);
39    *op2 = i256_div(op1, *op2);
40}
41
42/// Implements the MOD instruction.
43///
44/// Pops two values from stack and pushes the remainder of their division.
45pub fn rem<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
46    popn_top!([op1], op2, context.interpreter);
47    if !op2.is_zero() {
48        *op2 = op1.wrapping_rem(*op2);
49    }
50}
51
52/// Implements the SMOD instruction.
53///
54/// Performs signed modulo of two values from stack.
55pub fn smod<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
56    popn_top!([op1], op2, context.interpreter);
57    *op2 = i256_mod(op1, *op2)
58}
59
60/// Implements the ADDMOD instruction.
61///
62/// Pops three values from stack and pushes (a + b) % n.
63pub fn addmod<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
64    popn_top!([op1, op2], op3, context.interpreter);
65    *op3 = op1.add_mod(op2, *op3)
66}
67
68/// Implements the MULMOD instruction.
69///
70/// Pops three values from stack and pushes (a * b) % n.
71pub fn mulmod<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
72    popn_top!([op1, op2], op3, context.interpreter);
73    *op3 = op1.mul_mod(op2, *op3)
74}
75
76/// Implements the EXP instruction - exponentiates two values from stack.
77pub fn exp<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
78    popn_top!([op1], op2, context.interpreter);
79    gas!(
80        context.interpreter,
81        context.interpreter.gas_params.exp_cost(*op2)
82    );
83    *op2 = op1.pow(*op2);
84}
85
86/// Implements the `SIGNEXTEND` opcode as defined in the Ethereum Yellow Paper.
87///
88/// In the yellow paper `SIGNEXTEND` is defined to take two inputs, we will call them
89/// `x` and `y`, and produce one output.
90///
91/// The first `t` bits of the output (numbering from the left, starting from 0) are
92/// equal to the `t`-th bit of `y`, where `t` is equal to `256 - 8(x + 1)`.
93///
94/// The remaining bits of the output are equal to the corresponding bits of `y`.
95///
96/// **Note**: If `x >= 32` then the output is equal to `y` since `t <= 0`.
97///
98/// To efficiently implement this algorithm in the case `x < 32` we do the following.
99///
100/// Let `b` be equal to the `t`-th bit of `y` and let `s = 255 - t = 8x + 7`
101/// (this is effectively the same index as `t`, but numbering the bits from the
102/// right instead of the left).
103///
104/// We can create a bit mask which is all zeros up to and including the `t`-th bit,
105/// and all ones afterwards by computing the quantity `2^s - 1`.
106///
107/// We can use this mask to compute the output depending on the value of `b`.
108///
109/// If `b == 1` then the yellow paper says the output should be all ones up to
110/// and including the `t`-th bit, followed by the remaining bits of `y`; this is equal to
111/// `y | !mask` where `|` is the bitwise `OR` and `!` is bitwise negation.
112///
113/// Similarly, if `b == 0` then the yellow paper says the output should start with all zeros,
114/// then end with bits from `b`; this is equal to `y & mask` where `&` is bitwise `AND`.
115pub fn signextend<WIRE: InterpreterTypes, H: ?Sized>(context: InstructionContext<'_, H, WIRE>) {
116    popn_top!([ext], x, context.interpreter);
117    // For 31 we also don't need to do anything.
118    if ext < U256::from(31) {
119        let ext = ext.as_limbs()[0];
120        let bit_index = (8 * ext + 7) as usize;
121        let bit = x.bit(bit_index);
122        let mask = (U256::from(1) << bit_index) - U256::from(1);
123        *x = if bit { *x | !mask } else { *x & mask };
124    }
125}