From 563401b24d1af2714f3e2be2796c7988e1efdfcf Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Sun, 17 Jul 2022 09:14:24 -0700 Subject: [PATCH 1/3] More basic ASM utility functions To be used in upcoming RLP code. --- evm/src/cpu/kernel/asm/assertions.asm | 74 ++++++++++++++++++++++++ evm/src/cpu/kernel/asm/basic_macros.asm | 77 +++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 evm/src/cpu/kernel/asm/assertions.asm diff --git a/evm/src/cpu/kernel/asm/assertions.asm b/evm/src/cpu/kernel/asm/assertions.asm new file mode 100644 index 00000000..2a7d845e --- /dev/null +++ b/evm/src/cpu/kernel/asm/assertions.asm @@ -0,0 +1,74 @@ +// It is convenient to have a single panic routine, which we can jump to from +// anywhere. +global panic: + JUMPDEST + PANIC + +// Consumes the top element and asserts that it is zero. +%macro assert_zero + %jumpi panic +%endmacro + +// Consumes the top element and asserts that it is nonzero. +%macro assert_nonzero + ISZERO + %jumpi panic +%endmacro + +%macro assert_eq + EQ + %assert_nonzero +%endmacro + +%macro assert_lt + LT + %assert_nonzero +%endmacro + +%macro assert_le + LE + %assert_nonzero +%endmacro + +%macro assert_gt + GT + %assert_nonzero +%endmacro + +%macro assert_ge + GE + %assert_nonzero +%endmacro + +%macro assert_eq_const(c) + %eq_const(c) + %assert_nonzero +%endmacro + +%macro assert_lt_const(c) + // %assert_zero is cheaper than %assert_nonzero, so we will leverage the + // fact that (x < c) == !(x >= c). + %ge_const(c) + %assert_zero +%endmacro + +%macro assert_le_const(c) + // %assert_zero is cheaper than %assert_nonzero, so we will leverage the + // fact that (x <= c) == !(x > c). + %gt_const(c) + %assert_zero +%endmacro + +%macro assert_gt_const(c) + // %assert_zero is cheaper than %assert_nonzero, so we will leverage the + // fact that (x > c) == !(x <= c). + %le_const(c) + %assert_zero +%endmacro + +%macro assert_ge_const(c) + // %assert_zero is cheaper than %assert_nonzero, so we will leverage the + // fact that (x >= c) == !(x < c). + %lt_const(c) + %assert_zero +%endmacro diff --git a/evm/src/cpu/kernel/asm/basic_macros.asm b/evm/src/cpu/kernel/asm/basic_macros.asm index 9e884fea..7230772d 100644 --- a/evm/src/cpu/kernel/asm/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/basic_macros.asm @@ -26,6 +26,83 @@ %endrep %endmacro +%macro add_const(c) + // stack: input, ... + PUSH $c + ADD + // stack: input + c, ... +%endmacro + +// Slightly inefficient as we need to swap the inputs. +// Consider avoiding this in performance-critical code. +%macro sub_const(c) + // stack: input, ... + PUSH $c + // stack: c, input, ... + SWAP1 + // stack: input, c, ... + SUB + // stack: input - c, ... +%endmacro + +%macro mul_const(c) + // stack: input, ... + PUSH $c + MUL + // stack: input * c, ... +%endmacro + +// Slightly inefficient as we need to swap the inputs. +// Consider avoiding this in performance-critical code. +%macro div_const(c) + // stack: input, ... + PUSH $c + // stack: c, input, ... + SWAP1 + // stack: input, c, ... + SUB + // stack: input / c, ... +%endmacro + +%macro eq_const(c) + // stack: input, ... + PUSH $c + EQ + // stack: input == c, ... +%endmacro + +%macro lt_const(c) + // stack: input, ... + PUSH $c + // stack: c, input, ... + GT // Check it backwards: (input < c) == (c > input) + // stack: input <= c, ... +%endmacro + +%macro le_const(c) + // stack: input, ... + PUSH $c + // stack: c, input, ... + GE // Check it backwards: (input <= c) == (c >= input) + // stack: input <= c, ... +%endmacro + +%macro gt_const(c) + // stack: input, ... + PUSH $c + // stack: c, input, ... + LT // Check it backwards: (input > c) == (c < input) + // stack: input >= c, ... +%endmacro + +%macro ge_const(c) + // stack: input, ... + PUSH $c + // stack: c, input, ... + LE // Check it backwards: (input >= c) == (c <= input) + // stack: input >= c, ... +%endmacro + // If pred is zero, yields z; otherwise, yields nz %macro select // stack: pred, nz, z From 36f1692ee5f06f4fbfd0ba40566f3408911201d4 Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Sun, 17 Jul 2022 09:23:37 -0700 Subject: [PATCH 2/3] tweaks --- evm/src/cpu/kernel/asm/assertions.asm | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/evm/src/cpu/kernel/asm/assertions.asm b/evm/src/cpu/kernel/asm/assertions.asm index 2a7d845e..a8e65036 100644 --- a/evm/src/cpu/kernel/asm/assertions.asm +++ b/evm/src/cpu/kernel/asm/assertions.asm @@ -21,23 +21,31 @@ global panic: %endmacro %macro assert_lt - LT - %assert_nonzero + // %assert_zero is cheaper than %assert_nonzero, so we will leverage the + // fact that (x < y) == !(x >= y). + GE + %assert_zero %endmacro %macro assert_le - LE - %assert_nonzero + // %assert_zero is cheaper than %assert_nonzero, so we will leverage the + // fact that (x <= y) == !(x > y). + GT + %assert_zero %endmacro %macro assert_gt - GT - %assert_nonzero + // %assert_zero is cheaper than %assert_nonzero, so we will leverage the + // fact that (x > y) == !(x <= y). + LE + %assert_zero %endmacro %macro assert_ge - GE - %assert_nonzero + // %assert_zero is cheaper than %assert_nonzero, so we will leverage the + // fact that (x >= y) == !(x < y). + LT + %assert_zero %endmacro %macro assert_eq_const(c) From 4aaceabd18e039f4c0969b46cba211a011b79458 Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Sun, 17 Jul 2022 16:08:58 -0700 Subject: [PATCH 3/3] Include assertions, disabled for now --- evm/src/cpu/kernel/aggregator.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/evm/src/cpu/kernel/aggregator.rs b/evm/src/cpu/kernel/aggregator.rs index 28a9c597..c45f69c6 100644 --- a/evm/src/cpu/kernel/aggregator.rs +++ b/evm/src/cpu/kernel/aggregator.rs @@ -23,6 +23,7 @@ pub fn evm_constants() -> HashMap { #[allow(dead_code)] // TODO: Should be used once witness generation is done. pub(crate) fn combined_kernel() -> Kernel { let files = vec![ + // include_str!("asm/assertions.asm"), // TODO: Should work once PR 619 is merged. include_str!("asm/basic_macros.asm"), include_str!("asm/exp.asm"), include_str!("asm/curve_mul.asm"),