Skip to content

Commit

Permalink
Start moving to a generically applicable bls interface with some shif…
Browse files Browse the repository at this point in the history
…ted set of abstractions
  • Loading branch information
holgerd77 committed Jun 24, 2024
1 parent dbb0f05 commit 09295bc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
6 changes: 5 additions & 1 deletion packages/evm/src/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { Journal } from './journal.js'
import { EVMPerformanceLogger } from './logger.js'
import { Message } from './message.js'
import { getOpcodesForHF } from './opcodes/index.js'
import { getActivePrecompiles, getPrecompileName } from './precompiles/index.js'
import { MCLBLS, getActivePrecompiles, getPrecompileName } from './precompiles/index.js'
import { TransientStorage } from './transientStorage.js'
import { DefaultBlockchain } from './types.js'

Expand All @@ -41,6 +41,7 @@ import type {
Block,
Blockchain,
CustomOpcode,
EVMBLSInterface,
EVMEvents,
EVMInterface,
EVMOpts,
Expand Down Expand Up @@ -139,6 +140,8 @@ export class EVM implements EVMInterface {
*/
protected readonly _mcl: any //

protected readonly _bls?: EVMBLSInterface

/**
* EVM is run in DEBUG mode (default: false)
* Taken from DEBUG environment variable
Expand Down Expand Up @@ -244,6 +247,7 @@ export class EVM implements EVMInterface {

if (this.common.isActivatedEIP(2537)) {
this._mcl = mcl
this._bls = new MCLBLS(mcl)
}

this._emit = async (topic: string, data: any): Promise<void> => {
Expand Down
18 changes: 4 additions & 14 deletions packages/evm/src/precompiles/0b-bls12-g1add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { bytesToHex, equalsBytes, short } from '@ethereumjs/util'
import { EvmErrorResult, OOGResult } from '../evm.js'
import { ERROR, EvmError } from '../exceptions.js'

import { BLS12_381_FromG1Point, BLS12_381_ToG1Point } from './bls12_381/mcl.js'

import type { ExecResult } from '../types.js'
import type { EVMBLSInterface, ExecResult } from '../types.js'
import type { PrecompileInput } from './types.js'

export async function precompile0b(opts: PrecompileInput): Promise<ExecResult> {
const mcl = (<any>opts._EVM)._mcl!
const bls = (<any>opts._EVM)._bls! as EVMBLSInterface

const inputData = opts.data

Expand Down Expand Up @@ -55,24 +53,16 @@ export async function precompile0b(opts: PrecompileInput): Promise<ExecResult> {
return EvmErrorResult(new EvmError(ERROR.BLS_12_381_POINT_NOT_ON_CURVE), opts.gasLimit)
}
}

// convert input to mcl G1 points, add them, and convert the output to a Uint8Array.
let mclPoint1
let mclPoint2
let returnValue
try {
mclPoint1 = BLS12_381_ToG1Point(opts.data.subarray(0, 128), mcl, false)
mclPoint2 = BLS12_381_ToG1Point(opts.data.subarray(128, 256), mcl, false)
returnValue = bls.add(inputData)
} catch (e: any) {
if (opts._debug !== undefined) {
opts._debug(`BLS12G1ADD (0x0b) failed: ${e.message}`)
}
return EvmErrorResult(e, opts.gasLimit)
}

const result = mcl.add(mclPoint1, mclPoint2)

const returnValue = BLS12_381_FromG1Point(result)

if (opts._debug !== undefined) {
opts._debug(`BLS12G1ADD (0x0b) return value=${bytesToHex(returnValue)}`)
}
Expand Down
1 change: 1 addition & 0 deletions packages/evm/src/precompiles/bls12_381/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { MCLBLS } from './mcl.js'
20 changes: 20 additions & 0 deletions packages/evm/src/precompiles/bls12_381/mcl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {

import { ERROR, EvmError } from '../../exceptions.js'

import type { EVMBLSInterface } from '../../types.js'

// base field modulus as described in the EIP
const fieldModulus = BigInt(
'0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab'
Expand Down Expand Up @@ -346,6 +348,24 @@ function BLS12_381_ToFp2Point(fpXCoordinate: Uint8Array, fpYCoordinate: Uint8Arr
return fp2
}

export class MCLBLS implements EVMBLSInterface {
protected readonly _mcl: any

constructor(mcl: any) {
this._mcl = mcl
}

add(input: Uint8Array): Uint8Array {
// convert input to mcl G1 points, add them, and convert the output to a Uint8Array.
const mclPoint1 = BLS12_381_ToG1Point(input.subarray(0, 128), this._mcl, false)
const mclPoint2 = BLS12_381_ToG1Point(input.subarray(128, 256), this._mcl, false)

const result = this._mcl.add(mclPoint1, mclPoint2)

return BLS12_381_FromG1Point(result)
}
}

export {
BLS12_381_FromG1Point,
BLS12_381_FromG2Point,
Expand Down
2 changes: 2 additions & 0 deletions packages/evm/src/precompiles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { precompile10 } from './10-bls12-g2msm.js'
import { precompile11 } from './11-bls12-pairing.js'
import { precompile12 } from './12-bls12-map-fp-to-g1.js'
import { precompile13 } from './13-bls12-map-fp2-to-g2.js'
import { MCLBLS } from './bls12_381/index.js'

import type { PrecompileFunc, PrecompileInput } from './types.js'
import type { Common } from '@ethereumjs/common'
Expand Down Expand Up @@ -304,6 +305,7 @@ function getPrecompileName(addressUnprefixedStr: string) {
export {
getActivePrecompiles,
getPrecompileName,
MCLBLS,
precompileEntries,
precompiles,
ripemdPrecompileAddress,
Expand Down
4 changes: 4 additions & 0 deletions packages/evm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ export interface ExecResult {
blobGasUsed?: bigint
}

export type EVMBLSInterface = {
add(input: Uint8Array): Uint8Array
}

/**
* Log that the contract emits.
*/
Expand Down

0 comments on commit 09295bc

Please sign in to comment.