Skip to content

Commit

Permalink
add berlin BLS gas costs. include in G1 add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer committed Jun 20, 2020
1 parent a85c5a0 commit 1d061e3
Showing 3 changed files with 45 additions and 7 deletions.
35 changes: 34 additions & 1 deletion packages/common/src/hardforks/berlin.json
Original file line number Diff line number Diff line change
@@ -6,7 +6,40 @@
"status": "Draft"
},
"gasConfig": {},
"gasPrices": {},
"gasPrices": {
"Bls12381G1AddGas": {
"v": 600,
"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,
"d": "Gas cost of a single BLS12-381 G2 addition precompile-call"
},
"Bls12381G2MulGas": {
"v": 55000,
"d": "Gas cost of a single BLS12-381 G2 multiplication precompile-call"
},
"Bls12381PairingBaseGas": {
"v": 115000,
"d": "Base gas cost of BLS12-381 pairing check"
},
"Bls12381PairingPerPairGas": {
"v": 23000,
"d": "Per-pair gas cost of BLS12-381 pairing check"
},
"Bls12381MapG1Gas": {
"v": 5500,
"d": "Gas cost of BLS12-381 map field element to G1"
},
"Bls12381MapG2Gas": {
"v": 110000,
"d": "Gas cost of BLS12-381 map field element to G2"
},
},
"vm": {},
"pow": {}
}
3 changes: 2 additions & 1 deletion packages/vm/lib/evm/precompiles/0a-bls12-g1add.ts
Original file line number Diff line number Diff line change
@@ -13,7 +13,8 @@ export default async function (opts: PrecompileInput): Promise<ExecResult> {

let inputData = opts.data

let gasUsed = new BN(0) // TODO: add specific gas used to Common
// note: the gas used is constant; even if the input is incorrect.
let gasUsed = new BN(opts._common.param('gasPrices', 'Bls12381G1AddGas'))

if (inputData.length != 256) {
return VmErrorResult(new VmError(ERROR.BLS_12_381_INVALID_INPUT_LENGTH), gasUsed)
14 changes: 9 additions & 5 deletions packages/vm/tests/api/berlin/BLS-tests.js
Original file line number Diff line number Diff line change
@@ -30,18 +30,22 @@ tape('Berlin BLS tests', (t) => {
to: Buffer.from(BLS_G1ADD_Address, 'hex'),
value: new BN(0),
data: Buffer.from(input, 'hex')
})
})

if (result.execResult.returnValue.toString('hex') != output) {
st.fail("BLS G1ADD return value is not the expected value")
}
if (result.execResult.returnValue.toString('hex') != output) {
st.fail("BLS G1ADD return value is not the expected value")
}

if (!result.execResult.gasUsed.eq(new BN(600))) {
st.fail("BLS G1ADD gas used is incorrect")
}
}

st.pass("BLS G1ADD output is correct")

st.end()
})

// TODO: add tests checking for failures (i.e. input length incorrect; input values not on curve)
// TODO: add G1 tests checking for failures (i.e. input length incorrect; input values not on curve)
})

0 comments on commit 1d061e3

Please sign in to comment.