From b34dcbbc39f53193da805a83e3072ab9e90f905e Mon Sep 17 00:00:00 2001 From: jangko Date: Wed, 5 Jun 2024 13:32:25 +0700 Subject: [PATCH] Remove unused EIP-2537 and EIP-2315 --- nimbus/core/eip4844.nim | 3 +- nimbus/evm/blscurve.nim | 210 ----- .../interpreter/op_handlers/oph_memory.nim | 72 -- nimbus/evm/precompiles.nim | 322 +------- nimbus/evm/state_transactions.nim | 5 - nimbus/vm_precompiles.nim | 11 +- tests/fixtures/PrecompileTests/blsG1Add.json | 765 ------------------ tests/fixtures/PrecompileTests/blsG1Mul.json | 765 ------------------ .../PrecompileTests/blsG1MultiExp.json | 758 ----------------- tests/fixtures/PrecompileTests/blsG2Add.json | 765 ------------------ tests/fixtures/PrecompileTests/blsG2Mul.json | 765 ------------------ .../PrecompileTests/blsG2MultiExp.json | 758 ----------------- tests/fixtures/PrecompileTests/blsMapG1.json | 727 ----------------- tests/fixtures/PrecompileTests/blsMapG2.json | 727 ----------------- .../fixtures/PrecompileTests/blsPairing.json | 747 ----------------- tests/test_precompiles.nim | 11 - 16 files changed, 5 insertions(+), 7406 deletions(-) delete mode 100644 nimbus/evm/blscurve.nim delete mode 100644 tests/fixtures/PrecompileTests/blsG1Add.json delete mode 100644 tests/fixtures/PrecompileTests/blsG1Mul.json delete mode 100644 tests/fixtures/PrecompileTests/blsG1MultiExp.json delete mode 100644 tests/fixtures/PrecompileTests/blsG2Add.json delete mode 100644 tests/fixtures/PrecompileTests/blsG2Mul.json delete mode 100644 tests/fixtures/PrecompileTests/blsG2MultiExp.json delete mode 100644 tests/fixtures/PrecompileTests/blsMapG1.json delete mode 100644 tests/fixtures/PrecompileTests/blsMapG2.json delete mode 100644 tests/fixtures/PrecompileTests/blsPairing.json diff --git a/nimbus/core/eip4844.nim b/nimbus/core/eip4844.nim index 46728e96a..2fed8d30a 100644 --- a/nimbus/core/eip4844.nim +++ b/nimbus/core/eip4844.nim @@ -15,7 +15,8 @@ import results, stint, ../constants, - ../common/common + ../common/common, + blscurve/bls_backend # trigger blscurve library compilation {.push raises: [].} diff --git a/nimbus/evm/blscurve.nim b/nimbus/evm/blscurve.nim deleted file mode 100644 index f72b6e9ff..000000000 --- a/nimbus/evm/blscurve.nim +++ /dev/null @@ -1,210 +0,0 @@ -# Nimbus -# Copyright (c) 2020-2024 Status Research & Development GmbH -# Licensed under either of -# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or -# http://www.apache.org/licenses/LICENSE-2.0) -# * MIT license ([LICENSE-MIT](LICENSE-MIT) or -# http://opensource.org/licenses/MIT) -# at your option. This file may not be copied, modified, or distributed except -# according to those terms. - -import blscurve/bls_backend, stint - -import blscurve/blst/[blst_lowlevel] - -type - BLS_G1* = blst_p1 - BLS_G2* = blst_p2 - BLS_FP* = blst_fp - BLS_FP2* = blst_fp2 - BLS_SCALAR* = blst_scalar - BLS_FE* = blst_fp - BLS_FE2* = blst_fp2 - BLS_ACC* = blst_fp12 - BLS_G1P* = blst_p1_affine - BLS_G2P* = blst_p2_affine - -func fromBytes*(ret: var BLS_SCALAR, raw: openArray[byte]): bool = - const L = 32 - if raw.len < L: - return false - let pa = cast[ptr array[L, byte]](raw[0].unsafeAddr) - blst_scalar_from_bendian(ret, pa[]) - true - -func fromBytes(ret: var BLS_FP, raw: openArray[byte]): bool = - const L = 48 - if raw.len < L: - return false - let pa = cast[ptr array[L, byte]](raw[0].unsafeAddr) - blst_fp_from_bendian(ret, pa[]) - true - -func toBytes(fp: BLS_FP, output: var openArray[byte]): bool = - const L = 48 - if output.len < L: - return false - let pa = cast[ptr array[L, byte]](output[0].unsafeAddr) - blst_bendian_from_fp(pa[], fp) - true - -func pack(g: var BLS_G1, x, y: BLS_FP): bool = - let src = blst_p1_affine(x: x, y: y) - blst_p1_from_affine(g, src) - blst_p1_on_curve(g).int == 1 - -func unpack(g: BLS_G1, x, y: var BLS_FP): bool = - var dst: blst_p1_affine - blst_p1_to_affine(dst, g) - x = dst.x - y = dst.y - true - -func pack(g: var BLS_G2, x0, x1, y0, y1: BLS_FP): bool = - let src = blst_p2_affine(x: blst_fp2(fp: [x0, x1]), y: blst_fp2(fp: [y0, y1])) - blst_p2_from_affine(g, src) - blst_p2_on_curve(g).int == 1 - -func unpack(g: BLS_G2, x0, x1, y0, y1: var BLS_FP): bool = - var dst: blst_p2_affine - blst_p2_to_affine(dst, g) - x0 = dst.x.fp[0] - x1 = dst.x.fp[1] - y0 = dst.y.fp[0] - y1 = dst.y.fp[1] - true - -func nbits(s: BLS_SCALAR): uint = - var k = sizeof(s.l) - 1 - while k >= 0 and s.l[k] == 0: dec k - if k < 0: return 0 - var - bts = k shl 3 - c = s.l[k] - - while c != 0: - c = c shr 1 - inc bts - - result = bts.uint - -func add*(a: var BLS_G1, b: BLS_G1) {.inline.} = - blst_p1_add_or_double(a, a, b) - -func mul*(a: var BLS_G1, b: BLS_SCALAR) {.inline.} = - blst_p1_mult(a, a, b, b.nbits) - -func add*(a: var BLS_G2, b: BLS_G2) {.inline.} = - blst_p2_add_or_double(a, a, b) - -func mul*(a: var BLS_G2, b: BLS_SCALAR) {.inline.} = - blst_p2_mult(a, a, b, b.nbits) - -func mapFPToG1*(fp: BLS_FE): BLS_G1 {.inline.} = - let z: ptr blst_fp = nil - blst_map_to_g1(result, fp, z[]) - -func mapFPToG2*(fp: BLS_FE2): BLS_G2 {.inline.} = - let z: ptr blst_fp2 = nil - blst_map_to_g2(result, fp, z[]) - -func pack(g: var BLS_G1P, x, y: BLS_FP): bool = - g = blst_p1_affine(x: x, y: y) - blst_p1_affine_on_curve(g).int == 1 - -func pack(g: var BLS_G2P, x0, x1, y0, y1: BLS_FP): bool = - g = blst_p2_affine(x: blst_fp2(fp: [x0, x1]), y: blst_fp2(fp: [y0, y1])) - blst_p2_affine_on_curve(g).int == 1 - -func subgroupCheck*(P: BLS_G1P): bool {.inline.} = - blst_p1_affine_in_g1(P).int == 1 - -func subgroupCheck*(P: BLS_G2P): bool {.inline.} = - blst_p2_affine_in_g2(P).int == 1 - -func millerLoop*(P: BLS_G1P, Q: BLS_G2P): BLS_ACC {.inline.} = - blst_miller_loop(result, Q, P) - -proc mul*(a: var BLS_ACC, b: BLS_ACC) {.inline.} = - blst_fp12_mul(a, a, b) - -func check*(x: BLS_ACC): bool {.inline.} = - var ret: BLS_ACC - ret.blst_final_exp(x) - ret.blst_fp12_is_one().int == 1 - -# decodeFieldElement expects 64 byte input with zero top 16 bytes, -# returns lower 48 bytes. -func decodeFieldElement*(res: var BLS_FP, input: openArray[byte]): bool = - if input.len != 64: - return false - - # check top bytes - for i in 0..<16: - if input[i] != 0.byte: - return false - - res.fromBytes input.toOpenArray(16, 63) - -func decodeFE*(res: var BLS_FE, input: openArray[byte]): bool = - const - fieldModulus = StUint[512].fromHex "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab" - if not res.decodeFieldElement(input): - return false - var z: StUint[512] - z.initFromBytesBE(input) - z < fieldModulus - -func decodeFE*(res: var BLS_FE2, input: openArray[byte]): bool = - if input.len != 128: - return false - - if res.fp[0].decodeFE(input.toOpenArray(0, 63)) and - res.fp[1].decodeFE(input.toOpenArray(64, 127)): - result = true - -# DecodePoint given encoded (x, y) coordinates in 128 bytes returns a valid G1 Point. -func decodePoint*(g: var (BLS_G1 | BLS_G1P), data: openArray[byte]): bool = - if data.len != 128: - return false - - var x, y: BLS_FP - if x.decodeFieldElement(data.toOpenArray(0, 63)) and - y.decodeFieldElement(data.toOpenArray(64, 127)): - result = g.pack(x, y) - -# EncodePoint encodes a point into 128 bytes. -func encodePoint*(g: BLS_G1, output: var openArray[byte]): bool = - if output.len != 128: - return false - - var x, y: BLS_FP - if g.unpack(x, y) and - x.toBytes(output.toOpenArray(16, 63)) and - y.toBytes(output.toOpenArray(64+16, 127)): - result = true - -# DecodePoint given encoded (x, y) coordinates in 256 bytes returns a valid G2 Point. -func decodePoint*(g: var (BLS_G2 | BLS_G2P), data: openArray[byte]): bool = - if data.len != 256: - return false - - var x0, x1, y0, y1: BLS_FP - if x0.decodeFieldElement(data.toOpenArray(0, 63)) and - x1.decodeFieldElement(data.toOpenArray(64, 127)) and - y0.decodeFieldElement(data.toOpenArray(128, 191)) and - y1.decodeFieldElement(data.toOpenArray(192, 255)): - result = g.pack(x0, x1, y0, y1) - -# EncodePoint encodes a point into 256 bytes. -func encodePoint*(g: BLS_G2, output: var openArray[byte]): bool = - if output.len != 256: - return false - - var x0, x1, y0, y1: BLS_FP - if g.unpack(x0, x1, y0, y1) and - x0.toBytes(output.toOpenArray(16, 63)) and - x1.toBytes(output.toOpenArray(80, 127)) and - y0.toBytes(output.toOpenArray(144, 192)) and - y1.toBytes(output.toOpenArray(208, 255)): - result = true diff --git a/nimbus/evm/interpreter/op_handlers/oph_memory.nim b/nimbus/evm/interpreter/op_handlers/oph_memory.nim index 9dda1086c..583df2830 100644 --- a/nimbus/evm/interpreter/op_handlers/oph_memory.nim +++ b/nimbus/evm/interpreter/op_handlers/oph_memory.nim @@ -318,50 +318,6 @@ const k.cpt.memory.copy(dstPos, srcPos, len) -#[ - EIP-2315: temporary disabled - Reason : not included in berlin hard fork - beginSubOp: Vm2OpFn = proc (k: var Vm2Ctx) = - ## 0x5c, Marks the entry point to a subroutine - raise newException( - OutOfGas, - "Abort: Attempt to execute BeginSub opcode") - - - returnSubOp: Vm2OpFn = proc (k: var Vm2Ctx) = - ## 0x5d, Returns control to the caller of a subroutine. - if k.cpt.returnStack.len == 0: - raise newException( - OutOfGas, - "Abort: invalid returnStack during ReturnSub") - k.cpt.code.pc = k.cpt.returnStack.pop() - - - jumpSubOp: Vm2OpFn = proc (k: var Vm2Ctx) = - ## 0x5e, Transfers control to a subroutine. - let (jumpTarget) = k.cpt.stack.popInt(1) - - if jumpTarget >= k.cpt.code.len.u256: - raise newException( - InvalidJumpDestination, "JumpSub destination exceeds code len") - - let returnPC = k.cpt.code.pc - let jt = jumpTarget.truncate(int) - k.cpt.code.pc = jt - - let nextOpcode = k.cpt.code.peek - if nextOpcode != BeginSub: - raise newException( - InvalidJumpDestination, "Invalid JumpSub destination") - - if k.cpt.returnStack.len == 1023: - raise newException( - FullStack, "Out of returnStack") - - k.cpt.returnStack.add returnPC - inc k.cpt.code.pc -]# - # ------------------------------------------------------------------------------ # Public, op exec table entries # ------------------------------------------------------------------------------ @@ -532,34 +488,6 @@ const run: mCopyOp, post: vm2OpIgnore))] -#[ - EIP-2315: temporary disabled - Reason : not included in berlin hard fork - (opCode: BeginSub, ## 0x5c, Begin subroutine - forks: Vm2OpBerlinAndLater, - name: "beginSub", - info: " Marks the entry point to a subroutine", - exec: (prep: vm2OpIgnore, - run: beginSubOp, - post: vm2OpIgnore)), - - (opCode: ReturnSub, ## 0x5d, Return - forks: Vm2OpBerlinAndLater, - name: "returnSub", - info: "Returns control to the caller of a subroutine", - exec: (prep: vm2OpIgnore, - run: returnSubOp, - post: vm2OpIgnore)), - - (opCode: JumpSub, ## 0x5e, Call subroutine - forks: Vm2OpBerlinAndLater, - name: "jumpSub", - info: "Transfers control to a subroutine", - exec: (prep: vm2OpIgnore, - run: jumpSubOp, - post: vm2OpIgnore))] -]# - # ------------------------------------------------------------------------------ # End # ------------------------------------------------------------------------------ diff --git a/nimbus/evm/precompiles.nim b/nimbus/evm/precompiles.nim index c495c6125..d343c11c2 100644 --- a/nimbus/evm/precompiles.nim +++ b/nimbus/evm/precompiles.nim @@ -11,7 +11,7 @@ import std/[macros], results, - "."/[types, blake2b_f, blscurve], + "."/[types, blake2b_f], ./interpreter/[gas_meter, gas_costs, utils/utils_numeric], ../errors, eth/[common, keys], chronicles, nimcrypto/[ripemd, sha2, utils], bncurve/[fields, groups], @@ -35,28 +35,12 @@ type paPairing = 0x08, # Istanbul paBlake2bf = 0x09, - paPointEvaluation = 0x0A - # Berlin - # EIP-2537: disabled - # reason: not included in berlin - # paBlsG1Add - # paBlsG1Mul - # paBlsG1MultiExp - # paBlsG2Add - # paBlsG2Mul - # paBlsG2MultiExp - # paBlsPairing - # paBlsMapG1 - # paBlsMapG2 # Cancun - + paPointEvaluation = 0x0A proc getMaxPrecompileAddr(fork: EVMFork): PrecompileAddresses = if fork < FkByzantium: paIdentity elif fork < FkIstanbul: paPairing - # EIP 2537: disabled - # reason: not included in berlin - # elif fork < FkBerlin: paBlake2bf elif fork < FkCancun: paBlake2bf else: PrecompileAddresses.high @@ -382,296 +366,6 @@ proc blake2bf*(c: Computation) = else: c.output = @output -proc blsG1Add*(c: Computation) = - template input: untyped = - c.msg.data - - if input.len != 256: - raise newException(ValidationError, "blsG1Add invalid input len") - - c.gasMeter.consumeGas(Bls12381G1AddGas, reason="blsG1Add Precompile") - - var a, b: BLS_G1 - if not a.decodePoint(input.toOpenArray(0, 127)): - raise newException(ValidationError, "blsG1Add invalid input A") - - if not b.decodePoint(input.toOpenArray(128, 255)): - raise newException(ValidationError, "blsG1Add invalid input B") - - a.add b - - c.output = newSeq[byte](128) - if not encodePoint(a, c.output): - raise newException(ValidationError, "blsG1Add encodePoint error") - -proc blsG1Mul*(c: Computation) = - template input: untyped = - c.msg.data - - if input.len != 160: - raise newException(ValidationError, "blsG1Mul invalid input len") - - c.gasMeter.consumeGas(Bls12381G1MulGas, reason="blsG1Mul Precompile") - - var a: BLS_G1 - if not a.decodePoint(input.toOpenArray(0, 127)): - raise newException(ValidationError, "blsG1Mul invalid input A") - - var scalar: BLS_SCALAR - if not scalar.fromBytes(input.toOpenArray(128, 159)): - raise newException(ValidationError, "blsG1Mul invalid scalar") - - a.mul(scalar) - - c.output = newSeq[byte](128) - if not encodePoint(a, c.output): - raise newException(ValidationError, "blsG1Mul encodePoint error") - -const - Bls12381MultiExpDiscountTable = [ - 1200, 888, 764, 641, 594, 547, 500, 453, 438, 423, - 408, 394, 379, 364, 349, 334, 330, 326, 322, 318, - 314, 310, 306, 302, 298, 294, 289, 285, 281, 277, - 273, 269, 268, 266, 265, 263, 262, 260, 259, 257, - 256, 254, 253, 251, 250, 248, 247, 245, 244, 242, - 241, 239, 238, 236, 235, 233, 232, 231, 229, 228, - 226, 225, 223, 222, 221, 220, 219, 219, 218, 217, - 216, 216, 215, 214, 213, 213, 212, 211, 211, 210, - 209, 208, 208, 207, 206, 205, 205, 204, 203, 202, - 202, 201, 200, 199, 199, 198, 197, 196, 196, 195, - 194, 193, 193, 192, 191, 191, 190, 189, 188, 188, - 187, 186, 185, 185, 184, 183, 182, 182, 181, 180, - 179, 179, 178, 177, 176, 176, 175, 174 - ] - -func calcBlsMultiExpGas(K: int, gasCost: GasInt): GasInt = - # Calculate G1 point, scalar value pair length - if K == 0: - # Return 0 gas for small input length - return 0.GasInt - - const dLen = Bls12381MultiExpDiscountTable.len - # Lookup discount value for G1 point, scalar value pair length - let discount = if K < dLen: Bls12381MultiExpDiscountTable[K-1] - else: Bls12381MultiExpDiscountTable[dLen-1] - - # Calculate gas and return the result - result = (K * gasCost * discount) div 1000 - -proc blsG1MultiExp*(c: Computation) = - template input: untyped = - c.msg.data - - const L = 160 - if (input.len == 0) or ((input.len mod L) != 0): - raise newException(ValidationError, "blsG1MultiExp invalid input len") - - let - K = input.len div L - gas = K.calcBlsMultiExpGas(Bls12381G1MulGas) - - c.gasMeter.consumeGas(gas, reason="blsG1MultiExp Precompile") - - var - p: BLS_G1 - s: BLS_SCALAR - acc: BLS_G1 - - # Decode point scalar pairs - for i in 0..