Skip to content

Commit

Permalink
blueprint: use wrapping operations
Browse files Browse the repository at this point in the history
  • Loading branch information
ioxid committed Dec 20, 2024
1 parent d12f72b commit 30c2175
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ namespace nil {
using word_type = typename zkevm_stack::word_type;
word_type a = machine.stack_top();
word_type b = machine.stack_top(1);
word_type result = is_add ? a + b : a - b;
word_type result = is_add ? add_wrapping(a, b) : subtract_wrapping(a, b);
// TODO: after memory logic would become more complicated here
if (!is_add) {
std::swap(result, a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ namespace nil {
using word_type = typename zkevm_stack::word_type;
word_type a = machine.stack_top();
word_type b = machine.stack_top(1);
word_type result = a * b;
word_type result = multiply_wrapping(a, b);
const std::vector<value_type> a_chunks = zkevm_word_to_field_element<BlueprintFieldType>(a);
const std::vector<value_type> b_chunks = zkevm_word_to_field_element<BlueprintFieldType>(b);
const std::vector<value_type> r_chunks = zkevm_word_to_field_element<BlueprintFieldType>(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,21 @@ namespace nil {
case zkevm_opcode::ADD:{
word_type a = stack_pop();
word_type b = stack_pop();
stack.push(a+b);
stack.push(add_wrapping(a, b));
pc++; gas -= 3;
break;
}
case zkevm_opcode::SUB:{
word_type a = stack_pop();
word_type b = stack_pop();
stack.push(a-b);
stack.push(subtract_wrapping(a, b));
pc++; gas -= 3;
break;
}
case zkevm_opcode::MUL:{
word_type a = stack_pop();
word_type b = stack_pop();
stack.push(a*b);
stack.push(multiply_wrapping(a, b));
pc++; gas -= 5;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ namespace nil {
auto a = w_to_16(current_state.stack_top());
auto b = w_to_16(current_state.stack_top(1));
auto s =
is_add ?
w_to_16(current_state.stack_top() + current_state.stack_top(1)):
w_to_16(current_state.stack_top() - current_state.stack_top(1));
is_add
? w_to_16(add_wrapping(current_state.stack_top(),
current_state.stack_top(1)))
: w_to_16(subtract_wrapping(current_state.stack_top(),
current_state.stack_top(1)));

for( std::size_t i = 0; i < 16; i++){
A[i] = is_add? a[i]: s[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ namespace nil {
if (n == 0x01_big_uint256) return a;

zkevm_word_type exp = exp_by_squaring(a, n >> 1);
zkevm_word_type exp2 = exp * exp;
zkevm_word_type exp2 = multiply_wrapping(exp, exp);
if (n & 1) {
return exp2 * a;
return multiply_wrapping(exp2, a);
}
return exp2;
}
Expand Down

0 comments on commit 30c2175

Please sign in to comment.