Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Devnet0 fixtures: BLS Fixes (Common/EVM), Empty System Address (VM) #3400

Merged
merged 13 commits into from
May 6, 2024
Merged
8 changes: 4 additions & 4 deletions packages/common/src/eips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ export const EIPs: EIPsDict = {
gasConfig: {},
gasPrices: {
Bls12381G1AddGas: {
v: 600,
v: 500,
d: 'Gas cost of a single BLS12-381 G1 addition precompile-call',
},
Bls12381G1MulGas: {
v: 12000,
d: 'Gas cost of a single BLS12-381 G1 multiplication precompile-call',
},
Bls12381G2AddGas: {
v: 4500,
v: 800,
d: 'Gas cost of a single BLS12-381 G2 addition precompile-call',
},
Bls12381G2MulGas: {
v: 55000,
v: 45000,
d: 'Gas cost of a single BLS12-381 G2 multiplication precompile-call',
},
Bls12381PairingBaseGas: {
Expand All @@ -102,7 +102,7 @@ export const EIPs: EIPsDict = {
d: 'Gas cost of BLS12-381 map field element to G1',
},
Bls12381MapG2Gas: {
v: 110000,
v: 75000,
d: 'Gas cost of BLS12-381 map field element to G2',
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/common/test/params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('[Common]: Parameter access for param(), paramByHardfork()', () => {
assert.equal(f(), undefined, msg)

msg = 'Should return Bls12381G1AddGas gas price for EIP2537'
assert.equal(c.paramByEIP('gasPrices', 'Bls12381G1AddGas', 2537), BigInt(600), msg)
assert.equal(c.paramByEIP('gasPrices', 'Bls12381G1AddGas', 2537), BigInt(500), msg)
})

it('returns the right block delay for EIP3554', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/evm/src/precompiles/0b-bls12-g1add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export async function precompile0b(opts: PrecompileInput): Promise<ExecResult> {
let mclPoint1
let mclPoint2
try {
mclPoint1 = BLS12_381_ToG1Point(opts.data.subarray(0, 128), mcl)
mclPoint2 = BLS12_381_ToG1Point(opts.data.subarray(128, 256), mcl)
mclPoint1 = BLS12_381_ToG1Point(opts.data.subarray(0, 128), mcl, false)
mclPoint2 = BLS12_381_ToG1Point(opts.data.subarray(128, 256), mcl, false)
} catch (e: any) {
if (opts._debug !== undefined) {
opts._debug(`BLS12G1ADD (0x0b) failed: ${e.message}`)
Expand Down
4 changes: 2 additions & 2 deletions packages/evm/src/precompiles/0e-bls12-g2add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export async function precompile0e(opts: PrecompileInput): Promise<ExecResult> {
let mclPoint2

try {
mclPoint1 = BLS12_381_ToG2Point(opts.data.subarray(0, 256), mcl)
mclPoint2 = BLS12_381_ToG2Point(opts.data.subarray(256, 512), mcl)
mclPoint1 = BLS12_381_ToG2Point(opts.data.subarray(0, 256), mcl, false)
mclPoint2 = BLS12_381_ToG2Point(opts.data.subarray(256, 512), mcl, false)
} catch (e: any) {
return EvmErrorResult(e, opts.gasLimit)
}
Expand Down
31 changes: 22 additions & 9 deletions packages/evm/src/precompiles/util/bls12_381.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,15 @@ export const gasDiscountPairs = [
[127, 175],
[128, 174],
]
// convert an input Uint8Array to a mcl G1 point
// this does /NOT/ do any input checks. the input Uint8Array needs to be of length 128
// it does raise an error if the point is not on the curve.
function BLS12_381_ToG1Point(input: Uint8Array, mcl: any): any {
/**
* Converts an Uint8Array to a MCL G1 point. Raises errors if the point is not on the curve
* and (if activated) if the point is in the subgroup / order check.
* @param input Input Uint8Array. Should be 128 bytes
* @param mcl MCL instance
* @param verifyOrder Perform the subgroup check (defaults to true)
* @returns MCL G1 point
*/
function BLS12_381_ToG1Point(input: Uint8Array, mcl: any, verifyOrder = true): any {
const p_x = bytesToUnprefixedHex(input.subarray(16, 64))
const p_y = bytesToUnprefixedHex(input.subarray(80, 128))

Expand All @@ -171,7 +176,8 @@ function BLS12_381_ToG1Point(input: Uint8Array, mcl: any): any {
G1.setY(Fp_Y)
G1.setZ(One)

if (G1.isValidOrder() === false) {
mcl.verifyOrderG1(verifyOrder)
if (verifyOrder && G1.isValidOrder() === false) {
throw new EvmError(ERROR.BLS_12_381_POINT_NOT_ON_CURVE)
}

Expand Down Expand Up @@ -206,9 +212,15 @@ function BLS12_381_FromG1Point(input: any): Uint8Array {
return concatBytes(xBuffer, yBuffer)
}

// convert an input Uint8Array to a mcl G2 point
// this does /NOT/ do any input checks. the input Uint8Array needs to be of length 256
function BLS12_381_ToG2Point(input: Uint8Array, mcl: any): any {
/**
* Converts an Uint8Array to a MCL G2 point. Raises errors if the point is not on the curve
* and (if activated) if the point is in the subgroup / order check.
* @param input Input Uint8Array. Should be 256 bytes
* @param mcl MCL instance
* @param verifyOrder Perform the subgroup check (defaults to true)
* @returns MCL G2 point
*/
function BLS12_381_ToG2Point(input: Uint8Array, mcl: any, verifyOrder = true): any {
const p_x_1 = input.subarray(0, 64)
const p_x_2 = input.subarray(64, 128)
const p_y_1 = input.subarray(128, 192)
Expand Down Expand Up @@ -245,7 +257,8 @@ function BLS12_381_ToG2Point(input: Uint8Array, mcl: any): any {
mclPoint.setY(Fp2Y)
mclPoint.setZ(Fp2One)

if (mclPoint.isValidOrder() === false) {
mcl.verifyOrderG2(verifyOrder)
if (verifyOrder && mclPoint.isValidOrder() === false) {
throw new EvmError(ERROR.BLS_12_381_POINT_NOT_ON_CURVE)
}

Expand Down
106 changes: 0 additions & 106 deletions packages/evm/test/precompiles/0b-bls12-g1add.spec.ts

This file was deleted.

97 changes: 0 additions & 97 deletions packages/evm/test/precompiles/0c-bls12-g1mul.spec.ts

This file was deleted.

Loading
Loading