From c50c0bf2f875d5584018df232f8b9837ed86976f Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Thu, 10 Oct 2024 04:05:21 +0200 Subject: [PATCH 1/5] vm/tx: update 7702 to devnet-4 --- packages/tx/src/params.ts | 2 +- packages/tx/src/util.ts | 17 ++++++++--------- packages/vm/src/runTx.ts | 16 ++++++++++++++-- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/tx/src/params.ts b/packages/tx/src/params.ts index 8eac4ae249..66ce2302bb 100644 --- a/packages/tx/src/params.ts +++ b/packages/tx/src/params.ts @@ -49,7 +49,7 @@ export const paramsTx: ParamsDict = { 7702: { // TODO: Set correct minimum hardfork // gasPrices - perAuthBaseGas: 2500, // Gas cost of each authority item, provided the authority exists in the trie + perAuthBaseGas: 12500, // Gas cost of each authority item, provided the authority exists in the trie perEmptyAccountCost: 25000, // Gas cost of each authority item, in case the authority does not exist in the trie }, } diff --git a/packages/tx/src/util.ts b/packages/tx/src/util.ts index 7e6f30964b..983111fdae 100644 --- a/packages/tx/src/util.ts +++ b/packages/tx/src/util.ts @@ -1,10 +1,7 @@ import { - BIGINT_0, - BIGINT_1, MAX_INTEGER, MAX_UINT64, type PrefixedHexString, - SECP256K1_ORDER_DIV_2, TypeOutput, bytesToBigInt, bytesToHex, @@ -213,21 +210,23 @@ export class AuthorizationLists { if (address.length !== 20) { throw new Error('Invalid EIP-7702 transaction: address length should be 20 bytes') } - if (bytesToBigInt(chainId) > MAX_INTEGER) { - throw new Error('Invalid EIP-7702 transaction: chainId exceeds 2^256 - 1') + if (bytesToBigInt(chainId) > MAX_UINT64) { + throw new Error('Invalid EIP-7702 transaction: chainId exceeds 2^64 - 1') } if (bytesToBigInt(nonce) > MAX_UINT64) { throw new Error('Invalid EIP-7702 transaction: nonce exceeds 2^64 - 1') } const yParityBigInt = bytesToBigInt(yParity) - if (yParityBigInt !== BIGINT_0 && yParityBigInt !== BIGINT_1) { - throw new Error('Invalid EIP-7702 transaction: yParity should be 0 or 1') + if (yParityBigInt >= BigInt(2 ** 8)) { + throw new Error( + 'Invalid EIP-7702 transaction: yParity should be fit within 1 byte (0 - 255)', + ) } if (bytesToBigInt(r) > MAX_INTEGER) { throw new Error('Invalid EIP-7702 transaction: r exceeds 2^256 - 1') } - if (bytesToBigInt(s) > SECP256K1_ORDER_DIV_2) { - throw new Error('Invalid EIP-7702 transaction: s > secp256k1n/2') + if (bytesToBigInt(s) > MAX_INTEGER) { + throw new Error('Invalid EIP-7702 transaction: s exceeds 2^256 - 1') } } } diff --git a/packages/vm/src/runTx.ts b/packages/vm/src/runTx.ts index 79b75871b2..53df636f55 100644 --- a/packages/vm/src/runTx.ts +++ b/packages/vm/src/runTx.ts @@ -10,6 +10,7 @@ import { BIGINT_0, BIGINT_1, KECCAK256_NULL, + MAX_UINT64, bytesToBigInt, bytesToHex, bytesToUnprefixedHex, @@ -456,6 +457,11 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise { // Address to take code from const address = data[1] const nonce = data[2] + if (bytesToBigInt(nonce) >= MAX_UINT64) { + // authority nonce >= 2^64 - 1. Bumping this nonce by one will not make this fit in an uint64. + // EIPs PR: https://github.com/ethereum/EIPs/pull/8938 + continue + } const yParity = bytesToBigInt(data[3]) const r = data[4] const s = data[5] @@ -506,8 +512,14 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise { account.nonce++ await vm.evm.journal.putAccount(authority, account) - const addressCode = concatBytes(DELEGATION_7702_FLAG, address) - await vm.stateManager.putCode(authority, addressCode) + if (equalsBytes(address, new Uint8Array(20))) { + // Special case (see EIP PR: https://github.com/ethereum/EIPs/pull/8929) + // If delegated to the zero address, clear the delegation of authority + await vm.stateManager.putCode(authority, new Uint8Array()) + } else { + const addressCode = concatBytes(DELEGATION_7702_FLAG, address) + await vm.stateManager.putCode(authority, addressCode) + } } } From 389d6105bcefdbdba48fa09cf9311d66e2b9f5fb Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Sat, 12 Oct 2024 13:45:40 +0200 Subject: [PATCH 2/5] vm: add `s` check back to 7702 --- packages/vm/src/runTx.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/vm/src/runTx.ts b/packages/vm/src/runTx.ts index 53df636f55..6e49793d8e 100644 --- a/packages/vm/src/runTx.ts +++ b/packages/vm/src/runTx.ts @@ -11,6 +11,7 @@ import { BIGINT_1, KECCAK256_NULL, MAX_UINT64, + SECP256K1_ORDER_DIV_2, bytesToBigInt, bytesToHex, bytesToUnprefixedHex, @@ -462,9 +463,15 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise { // EIPs PR: https://github.com/ethereum/EIPs/pull/8938 continue } + const s = data[5] + if (bytesToBigInt(s) > SECP256K1_ORDER_DIV_2) { + // Malleability protection to avoid "flipping" a valid signature to get + // another valid signature (which yields the same account on `ecrecover`) + // This is invalid, so skip this auth tuple + continue + } const yParity = bytesToBigInt(data[3]) const r = data[4] - const s = data[5] const rlpdSignedMessage = RLP.encode([chainId, address, nonce]) const toSign = keccak256(concatBytes(MAGIC, rlpdSignedMessage)) From 560a8317857ade6d0ea82f945fde4ab38c01833c Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Thu, 24 Oct 2024 20:12:16 +0200 Subject: [PATCH 3/5] vm/tx: ensure yParity values of 7702 are skipped if > 1 --- packages/vm/src/runTx.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/vm/src/runTx.ts b/packages/vm/src/runTx.ts index 6e49793d8e..365acf747b 100644 --- a/packages/vm/src/runTx.ts +++ b/packages/vm/src/runTx.ts @@ -471,6 +471,11 @@ async function _runTx(vm: VM, opts: RunTxOpts): Promise { continue } const yParity = bytesToBigInt(data[3]) + + if (yParity > BIGINT_1) { + continue + } + const r = data[4] const rlpdSignedMessage = RLP.encode([chainId, address, nonce]) From fa76edce1606c14bd3bba08bb0cf677dff01d081 Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Thu, 31 Oct 2024 22:47:39 +0100 Subject: [PATCH 4/5] tx: fix 7702 tests --- packages/tx/test/eip7702.spec.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/tx/test/eip7702.spec.ts b/packages/tx/test/eip7702.spec.ts index da31c6b6b7..85e3abfeb5 100644 --- a/packages/tx/test/eip7702.spec.ts +++ b/packages/tx/test/eip7702.spec.ts @@ -3,7 +3,6 @@ import { BIGINT_1, MAX_INTEGER, MAX_UINT64, - SECP256K1_ORDER_DIV_2, bigIntToHex, createAddressFromPrivateKey, createZeroAddress, @@ -86,9 +85,9 @@ describe('[EOACode7702Transaction]', () => { { nonce: bigIntToHex(MAX_UINT64 + BIGINT_1) }, 'Invalid EIP-7702 transaction: nonce exceeds 2^64 - 1', ], - [{ yParity: '0x2' }, 'yParity should be 0 or 1'], + [{ yParity: '0x0100' }, 'yParity should be < 2^8'], [{ r: bigIntToHex(MAX_INTEGER + BIGINT_1) }, 'r exceeds 2^256 - 1'], - [{ s: bigIntToHex(SECP256K1_ORDER_DIV_2 + BIGINT_1) }, 's > secp256k1n/2'], + [{ s: bigIntToHex(MAX_INTEGER + BIGINT_1) }, 's exceeds 2^256 - 1'], [{ yParity: '0x0002' }, 'yParity cannot have leading zeros'], [{ r: '0x0001' }, 'r cannot have leading zeros'], [{ s: '0x0001' }, 's cannot have leading zeros'], From bcfc8f682f4e88b7b9a2f8e9a39f67fbc675c862 Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Thu, 31 Oct 2024 23:06:47 +0100 Subject: [PATCH 5/5] vm: fix state test runner for 7702 --- packages/vm/test/util.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/vm/test/util.ts b/packages/vm/test/util.ts index a1b1e67f6c..a478879cef 100644 --- a/packages/vm/test/util.ts +++ b/packages/vm/test/util.ts @@ -136,8 +136,17 @@ export function makeTx( if (signature.v !== undefined) { signature.yParity = bytesToHex(unpadBytes(hexToBytes(signature.v))) } - if (signature.nonce !== undefined && signature.nonce[0] === '0x00') { - signature.nonce[0] = '0x' + if (signature.r !== undefined) { + signature.r = bytesToHex(unpadBytes(hexToBytes(signature.r))) + } + if (signature.s !== undefined) { + signature.s = bytesToHex(unpadBytes(hexToBytes(signature.s))) + } + if (signature.chainId !== undefined) { + signature.chainId = bytesToHex(unpadBytes(hexToBytes(signature.chainId))) + } + if (signature.nonce !== undefined && signature.nonce === '0x00') { + signature.nonce = '0x' } } tx = createEOACode7702Tx(txData, opts)