From 42320d815e74bfb77933f6ee758f9ea09dfb11b7 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Fri, 30 Sep 2022 09:13:56 -0700 Subject: [PATCH] fix --- evm/Cargo.toml | 1 - evm/src/cpu/kernel/asm/modexp.asm | 76 +++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 evm/src/cpu/kernel/asm/modexp.asm diff --git a/evm/Cargo.toml b/evm/Cargo.toml index 7df7edd5..5ee3b1ff 100644 --- a/evm/Cargo.toml +++ b/evm/Cargo.toml @@ -8,7 +8,6 @@ edition = "2021" plonky2 = { path = "../plonky2", default-features = false, features = ["rand", "timing"] } plonky2_util = { path = "../util" } eth-trie-utils = { git = "https://github.com/mir-protocol/eth-trie-utils.git", rev = "dd3595b4ba7923f8d465450d210f17a2b4e20f96" } -maybe_rayon = { path = "../maybe_rayon" } anyhow = "1.0.40" env_logger = "0.9.0" ethereum-types = "0.14.0" diff --git a/evm/src/cpu/kernel/asm/modexp.asm b/evm/src/cpu/kernel/asm/modexp.asm new file mode 100644 index 00000000..f1b448cb --- /dev/null +++ b/evm/src/cpu/kernel/asm/modexp.asm @@ -0,0 +1,76 @@ +/// Recursive implementation of exp. +/// Equivalent to: +/// def exp(x, e): +/// if e == 0: +/// # The path where JUMPI does not jump to `step_case` +/// return 1 +/// else: +/// # This is under the `step_case` label +/// return (x if e % 2 else 1) * exp(x * x, e // 2) +/// Note that this correctly handles exp(0, 0) == 1. + +global modexp: + // stack: x, e, retdest + dup2 + // stack: e, x, e, retdest + %jumpi(step_case) + // stack: x, e, retdest + pop + // stack: e, retdest + pop + // stack: retdest + push 1 + // stack: 1, retdest + swap1 + // stack: retdest, 1 + jump + +step_case: + // stack: x, e, retdest + push recursion_return + // stack: recursion_return, x, e, retdest + push 2 + // stack: 2, recursion_return, x, e, retdest + dup4 + // stack: e, 2, recursion_return, x, e, retdest + div + // stack: e / 2, recursion_return, x, e, retdest + dup3 + // stack: x, e / 2, recursion_return, x, e, retdest + %square + // stack: x * x, e / 2, recursion_return, x, e, retdest + %jump(exp) +recursion_return: + // stack: exp(x * x, e / 2), x, e, retdest + push 2 + // stack: 2, exp(x * x, e / 2), x, e, retdest + dup4 + // stack: e, 2, exp(x * x, e / 2), x, e, retdest + mod + // stack: e % 2, exp(x * x, e / 2), x, e, retdest + push 1 + // stack: 1, e % 2, exp(x * x, e / 2), x, e, retdest + dup4 + // stack: x, 1, e % 2, exp(x * x, e / 2), x, e, retdest + sub + // stack: x - 1, e % 2, exp(x * x, e / 2), x, e, retdest + mul + // stack: (x - 1) * (e % 2), exp(x * x, e / 2), x, e, retdest + push 1 + // stack: 1, (x - 1) * (e % 2), exp(x * x, e / 2), x, e, retdest + add + // stack: 1 + (x - 1) * (e % 2), exp(x * x, e / 2), x, e, retdest + mul + // stack: (1 + (x - 1) * (e % 2)) * exp(x * x, e / 2), x, e, retdest + swap3 + // stack: retdest, x, e, (1 + (x - 1) * (e % 2)) * exp(x * x, e / 2) + swap2 + // stack: e, x, retdest, (1 + (x - 1) * (e % 2)) * exp(x * x, e / 2) + pop + // stack: x, retdest, (1 + (x - 1) * (e % 2)) * exp(x * x, e / 2) + pop + // stack: retdest, (1 + (x - 1) * (e % 2)) * exp(x * x, e / 2) + jump + +global sys_exp: + PANIC