Fix EIP-2537: Precompile for BLS12-381 curve operations (#2603)

* Fix EIP-2537: Precompile for BLS12-381 curve operations

* Update test vectors
This commit is contained in:
andri lim 2024-09-10 13:56:08 +07:00 committed by GitHub
parent 838c9854e7
commit 5464f8e5f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 234 additions and 139 deletions

View File

@ -8,10 +8,10 @@ PrecompileTests
+ blsG1MultiExp.json OK + blsG1MultiExp.json OK
+ blsG2Add.json OK + blsG2Add.json OK
+ blsG2Mul.json OK + blsG2Mul.json OK
blsG2MultiExp.json Skip + blsG2MultiExp.json OK
blsMapG1.json Skip + blsMapG1.json OK
blsMapG2.json Skip + blsMapG2.json OK
blsPairing.json Skip + blsPairing.json OK
+ bn256Add.json OK + bn256Add.json OK
+ bn256Add_istanbul.json OK + bn256Add_istanbul.json OK
+ bn256mul.json OK + bn256mul.json OK
@ -25,7 +25,7 @@ PrecompileTests
+ ripemd160.json OK + ripemd160.json OK
+ sha256.json OK + sha256.json OK
``` ```
OK: 18/22 Fail: 0/22 Skip: 4/22 OK: 22/22 Fail: 0/22 Skip: 0/22
---TOTAL--- ---TOTAL---
OK: 18/22 Fail: 0/22 Skip: 4/22 OK: 22/22 Fail: 0/22 Skip: 0/22

View File

@ -46,11 +46,10 @@ type
paBlsG1MultiExp = 0x0d, paBlsG1MultiExp = 0x0d,
paBlsG2Add = 0x0e, paBlsG2Add = 0x0e,
paBlsG2Mul = 0x0f, paBlsG2Mul = 0x0f,
# EIP-2537: disabled; reason: gas price discrepancies, TODO paBlsG2MultiExp = 0x10,
# paBlsG2MultiExp paBlsPairing = 0x11,
# paBlsPairing paBlsMapG1 = 0x12,
# paBlsMapG1 paBlsMapG2 = 0x13
# paBlsMapG2
SigRes = object SigRes = object
msgHash: array[32, byte] msgHash: array[32, byte]
@ -389,7 +388,7 @@ func blake2bf(c: Computation): EvmResultVoid =
assign(c.output, output) assign(c.output, output)
ok() ok()
func blsG1Add*(c: Computation): EvmResultVoid = func blsG1Add(c: Computation): EvmResultVoid =
template input: untyped = template input: untyped =
c.msg.data c.msg.data
@ -412,7 +411,7 @@ func blsG1Add*(c: Computation): EvmResultVoid =
return err(prcErr(PrcInvalidPoint)) return err(prcErr(PrcInvalidPoint))
ok() ok()
func blsG1Mul*(c: Computation): EvmResultVoid = func blsG1Mul(c: Computation): EvmResultVoid =
template input: untyped = template input: untyped =
c.msg.data c.msg.data
@ -467,7 +466,7 @@ func calcBlsMultiExpGas(K: GasInt, gasCost: GasInt): GasInt =
# Calculate gas and return the result # Calculate gas and return the result
result = (K * gasCost * discount) div 1000 result = (K * gasCost * discount) div 1000
func blsG1MultiExp*(c: Computation): EvmResultVoid = func blsG1MultiExp(c: Computation): EvmResultVoid =
template input: untyped = template input: untyped =
c.msg.data c.msg.data
@ -509,7 +508,7 @@ func blsG1MultiExp*(c: Computation): EvmResultVoid =
return err(prcErr(PrcInvalidPoint)) return err(prcErr(PrcInvalidPoint))
ok() ok()
func blsG2Add*(c: Computation): EvmResultVoid = func blsG2Add(c: Computation): EvmResultVoid =
template input: untyped = template input: untyped =
c.msg.data c.msg.data
@ -532,7 +531,7 @@ func blsG2Add*(c: Computation): EvmResultVoid =
return err(prcErr(PrcInvalidPoint)) return err(prcErr(PrcInvalidPoint))
ok() ok()
func blsG2Mul*(c: Computation): EvmResultVoid = func blsG2Mul(c: Computation): EvmResultVoid =
template input: untyped = template input: untyped =
c.msg.data c.msg.data
@ -556,7 +555,7 @@ func blsG2Mul*(c: Computation): EvmResultVoid =
return err(prcErr(PrcInvalidPoint)) return err(prcErr(PrcInvalidPoint))
ok() ok()
func blsG2MultiExp*(c: Computation): EvmResultVoid = func blsG2MultiExp(c: Computation): EvmResultVoid =
template input: untyped = template input: untyped =
c.msg.data c.msg.data
@ -598,7 +597,7 @@ func blsG2MultiExp*(c: Computation): EvmResultVoid =
return err(prcErr(PrcInvalidPoint)) return err(prcErr(PrcInvalidPoint))
ok() ok()
func blsPairing*(c: Computation): EvmResultVoid = func blsPairing(c: Computation): EvmResultVoid =
template input: untyped = template input: untyped =
c.msg.data c.msg.data
@ -648,7 +647,7 @@ func blsPairing*(c: Computation): EvmResultVoid =
c.output[^1] = 1.byte c.output[^1] = 1.byte
ok() ok()
func blsMapG1*(c: Computation): EvmResultVoid = func blsMapG1(c: Computation): EvmResultVoid =
template input: untyped = template input: untyped =
c.msg.data c.msg.data
@ -668,7 +667,7 @@ func blsMapG1*(c: Computation): EvmResultVoid =
return err(prcErr(PrcInvalidPoint)) return err(prcErr(PrcInvalidPoint))
ok() ok()
func blsMapG2*(c: Computation): EvmResultVoid = func blsMapG2(c: Computation): EvmResultVoid =
template input: untyped = template input: untyped =
c.msg.data c.msg.data
@ -748,11 +747,10 @@ proc execPrecompiles*(c: Computation, fork: EVMFork): bool =
of paBlsG1MultiExp: blsG1MultiExp(c) of paBlsG1MultiExp: blsG1MultiExp(c)
of paBlsG2Add: blsG2Add(c) of paBlsG2Add: blsG2Add(c)
of paBlsG2Mul: blsG2Mul(c) of paBlsG2Mul: blsG2Mul(c)
# EIP 2537: disabled; gas price changes/discrepancies in test vectors of paBlsG2MultiExp: blsG2MultiExp(c)
# of paBlsG2MultiExp: blsG2MultiExp(c) of paBlsPairing: blsPairing(c)
# of paBlsPairing: blsPairing(c) of paBlsMapG1: blsMapG1(c)
# of paBlsMapG1: blsMapG1(c) of paBlsMapG2: blsMapG2(c)
# of paBlsMapG2: blsMapG2(c)
if res.isErr: if res.isErr:
if res.error.code == EvmErrorCode.OutOfGas: if res.error.code == EvmErrorCode.OutOfGas:

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{ {
"func": "blsMapG1", "func": "blsMapG1",
"fork": "berlin", "fork": "prague",
"data": "data":
[ [
{ {

View File

@ -1,6 +1,6 @@
{ {
"func": "blsMapG2", "func": "blsMapG2",
"fork": "berlin", "fork": "prague",
"data": "data":
[ [
{ {

View File

@ -1,6 +1,6 @@
{ {
"func": "blsPairing", "func": "blsPairing",
"fork": "berlin", "fork": "prague",
"data": "data":
[ [
{ {

View File

@ -61,8 +61,8 @@ template doTest(fixture: JsonNode; vmState: BaseVMState; address: PrecompileAddr
check fixtureResult.gasUsed == gasExpected.get check fixtureResult.gasUsed == gasExpected.get
proc parseFork(x: string): string = proc parseFork(x: string): string =
result = x.capitalizeAscii result = x.capitalizeAscii
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) = proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
let let
label = fixtures["func"].getStr label = fixtures["func"].getStr
@ -91,11 +91,10 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus) =
of "blsg1multiexp" : data.doTest(vmState, paBlsG1MultiExp) of "blsg1multiexp" : data.doTest(vmState, paBlsG1MultiExp)
of "blsg2add" : data.doTest(vmState, paBlsG2Add) of "blsg2add" : data.doTest(vmState, paBlsG2Add)
of "blsg2mul" : data.doTest(vmState, paBlsG2Mul) of "blsg2mul" : data.doTest(vmState, paBlsG2Mul)
# EIP 2537: disabled due to gas price changes/discprepancies of "blsg2multiexp": data.doTest(vmState, paBlsG2MultiExp)
#of "blsg2multiexp": data.doTest(vmState, paBlsG2MultiExp) of "blspairing": data.doTest(vmState, paBlsPairing)
#of "blspairing": data.doTest(vmState, paBlsPairing) of "blsmapg1": data.doTest(vmState, paBlsMapG1)
#of "blsmapg1": data.doTest(vmState, paBlsMapG1) of "blsmapg2": data.doTest(vmState, paBlsMapG2)
#of "blsmapg2": data.doTest(vmState, paBlsMapG2)
else: else:
echo "Unknown test vector '" & $label & "'" echo "Unknown test vector '" & $label & "'"
testStatusIMPL = SKIPPED testStatusIMPL = SKIPPED