Skip to content

Commit 03095a2

Browse files
authored
Merge pull request #409 from bane-labs/sync-prague-precompiles
core: update BLS12381 precompiles to Prague release
2 parents 0cc6824 + 08a5d97 commit 03095a2

File tree

15 files changed

+1672
-2058
lines changed

15 files changed

+1672
-2058
lines changed

contracts/solidity/libraries/BLS12381.sol

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,10 @@ library BLS12381 {
4949
function g1Mul(
5050
bytes memory point, // g1 point, 128 bytes
5151
uint256 scalar // big int, 32 bytes
52-
) internal view returns (bytes memory c) {
52+
) internal view returns (bytes memory) {
5353
bytes memory input = abi.encodePacked(point, scalar);
5454
require(input.length == 160, "_g1Mul malformed input");
55-
bool success;
56-
c = new bytes(128);
57-
assembly {
58-
success := staticcall(
59-
gas(),
60-
0x0c,
61-
add(input, 0x20),
62-
160,
63-
add(c, 0x20),
64-
128
65-
)
66-
switch success
67-
case 0 {
68-
invalid()
69-
}
70-
}
55+
return g1MultiExp(input);
7156
}
7257

7358
function g1MultiExp(
@@ -79,7 +64,7 @@ library BLS12381 {
7964
assembly {
8065
success := staticcall(
8166
gas(),
82-
0x0d,
67+
0x0c,
8368
add(input, 0x20),
8469
mload(input),
8570
add(c, 0x20),
@@ -101,7 +86,7 @@ library BLS12381 {
10186
assembly {
10287
success := staticcall(
10388
gas(),
104-
0x11,
89+
0x0f,
10590
add(input, 0x20),
10691
mload(input),
10792
add(res, 0x20),

core/vm/contracts.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,12 @@ var PrecompiledContractsNeoXDKG = map[common.Address]PrecompiledContract{
108108
common.BytesToAddress([]byte{0x09}): &blake2F{},
109109
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{}, // Include Cancun-level precompile to be compatible with Cancun BLS precompile addresses.
110110
common.BytesToAddress([]byte{0x0b}): &bls12381G1Add{},
111-
common.BytesToAddress([]byte{0x0c}): &bls12381G1Mul{},
112-
common.BytesToAddress([]byte{0x0d}): &bls12381G1MultiExp{},
113-
common.BytesToAddress([]byte{0x0e}): &bls12381G2Add{},
114-
common.BytesToAddress([]byte{0x0f}): &bls12381G2Mul{},
115-
common.BytesToAddress([]byte{0x10}): &bls12381G2MultiExp{},
116-
common.BytesToAddress([]byte{0x11}): &bls12381Pairing{},
117-
common.BytesToAddress([]byte{0x12}): &bls12381MapG1{},
118-
common.BytesToAddress([]byte{0x13}): &bls12381MapG2{},
111+
common.BytesToAddress([]byte{0x0c}): &bls12381G1MultiExp{},
112+
common.BytesToAddress([]byte{0x0d}): &bls12381G2Add{},
113+
common.BytesToAddress([]byte{0x0e}): &bls12381G2MultiExp{},
114+
common.BytesToAddress([]byte{0x0f}): &bls12381Pairing{},
115+
common.BytesToAddress([]byte{0x10}): &bls12381MapG1{},
116+
common.BytesToAddress([]byte{0x11}): &bls12381MapG2{},
119117
}
120118

121119
// PrecompiledContractsCancun contains the default set of pre-compiled Ethereum
@@ -132,14 +130,12 @@ var PrecompiledContractsCancun = map[common.Address]PrecompiledContract{
132130
common.BytesToAddress([]byte{9}): &blake2F{},
133131
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},
134132
common.BytesToAddress([]byte{0x0b}): &bls12381G1Add{},
135-
common.BytesToAddress([]byte{0x0c}): &bls12381G1Mul{},
136-
common.BytesToAddress([]byte{0x0d}): &bls12381G1MultiExp{},
137-
common.BytesToAddress([]byte{0x0e}): &bls12381G2Add{},
138-
common.BytesToAddress([]byte{0x0f}): &bls12381G2Mul{},
139-
common.BytesToAddress([]byte{0x10}): &bls12381G2MultiExp{},
140-
common.BytesToAddress([]byte{0x11}): &bls12381Pairing{},
141-
common.BytesToAddress([]byte{0x12}): &bls12381MapG1{},
142-
common.BytesToAddress([]byte{0x13}): &bls12381MapG2{},
133+
common.BytesToAddress([]byte{0x0c}): &bls12381G1MultiExp{},
134+
common.BytesToAddress([]byte{0x0d}): &bls12381G2Add{},
135+
common.BytesToAddress([]byte{0x0e}): &bls12381G2MultiExp{},
136+
common.BytesToAddress([]byte{0x0f}): &bls12381Pairing{},
137+
common.BytesToAddress([]byte{0x10}): &bls12381MapG1{},
138+
common.BytesToAddress([]byte{0x11}): &bls12381MapG2{},
143139
}
144140

145141
// PrecompiledContractsBLS contains the set of pre-compiled Ethereum
@@ -781,10 +777,10 @@ func (c *bls12381G1MultiExp) RequiredGas(input []byte) uint64 {
781777
}
782778
// Lookup discount value for G1 point, scalar value pair length
783779
var discount uint64
784-
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
785-
discount = params.Bls12381MultiExpDiscountTable[k-1]
780+
if dLen := len(params.Bls12381G1MultiExpDiscountTable); k < dLen {
781+
discount = params.Bls12381G1MultiExpDiscountTable[k-1]
786782
} else {
787-
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
783+
discount = params.Bls12381G1MultiExpDiscountTable[dLen-1]
788784
}
789785
// Calculate gas and return the result
790786
return (uint64(k) * params.Bls12381G1MulGas * discount) / 1000
@@ -912,10 +908,10 @@ func (c *bls12381G2MultiExp) RequiredGas(input []byte) uint64 {
912908
}
913909
// Lookup discount value for G2 point, scalar value pair length
914910
var discount uint64
915-
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
916-
discount = params.Bls12381MultiExpDiscountTable[k-1]
911+
if dLen := len(params.Bls12381G2MultiExpDiscountTable); k < dLen {
912+
discount = params.Bls12381G2MultiExpDiscountTable[k-1]
917913
} else {
918-
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
914+
discount = params.Bls12381G2MultiExpDiscountTable[dLen-1]
919915
}
920916
// Calculate gas and return the result
921917
return (uint64(k) * params.Bls12381G2MulGas * discount) / 1000

0 commit comments

Comments
 (0)