-
Notifications
You must be signed in to change notification settings - Fork 773
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
EIP 6690: EVMMAX implementation #3816
Open
scorbajio
wants to merge
17
commits into
master
Choose a base branch
from
evmmax-0
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4f24ab4
Add evmmax to common
scorbajio 7c85448
Add opcode handler skeletons
scorbajio f830abc
Merge branch 'master' of github.com:ethereumjs/ethereumjs-monorepo in…
scorbajio 3595599
Install bigint-mod-arith package
scorbajio 6b5f895
Add helper function
scorbajio 58c2dae
Add helper function
scorbajio 553272e
Add base for FieldContext class
scorbajio 40561c3
Remove utils and update arith helpers
scorbajio d827327
Complete implementation of fieldContext for binary modulus
scorbajio f76f420
Move helper and constant to arith
scorbajio d546bfb
Test fieldContext for binary modulus arithmetic
scorbajio 70f3562
Export fieldContext
scorbajio bb4fd1b
Merge branch 'master' of github.com:ethereumjs/ethereumjs-monorepo in…
scorbajio d432767
Fix linting issues
scorbajio 0074f51
Fix lint/type issues and debug binary arithmetic issues
scorbajio 006b104
Fix tests
scorbajio 164a358
Complete and clean up fieldContext add, sub, and mod tests
scorbajio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
const MASK_64 = (1n << 64n) - 1n | ||
|
||
export function putUint64BE(dst: Uint8Array, offset: number, value: bigint): void { | ||
value = BigInt.asUintN(64, value) | ||
const hex = value.toString(16).padStart(16, '0') | ||
for (let i = 0; i < 8; i++) { | ||
dst[offset + i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16) | ||
} | ||
} | ||
|
||
export function negModInverse(mod: bigint): bigint { | ||
let k0 = (2n - mod) & MASK_64 | ||
let t = (mod - 1n) & MASK_64 | ||
|
||
for (let i = 1; i < 64; i <<= 1) { | ||
t = (t * t) & MASK_64 | ||
k0 = (k0 * ((t + 1n) & MASK_64)) & MASK_64 | ||
} | ||
k0 = -k0 & MASK_64 | ||
|
||
return k0 | ||
} | ||
|
||
export function bytesToLimbs(b: Uint8Array): bigint[] { | ||
const wordCount = Math.ceil(b.length / 8) | ||
const paddedSize = wordCount * 8 | ||
|
||
const paddedBytes = new Uint8Array(paddedSize) | ||
paddedBytes.set(b, paddedSize - b.length) | ||
|
||
const limbs: bigint[] = new Array(wordCount) | ||
|
||
// Extract each 64-bit word in big-endian order | ||
for (let i = 0; i < wordCount; i++) { | ||
const offset = i * 8 | ||
// Construct the 64-bit limb as a bigint | ||
const limb = | ||
(BigInt(paddedBytes[offset]) << 56n) | | ||
(BigInt(paddedBytes[offset + 1]) << 48n) | | ||
(BigInt(paddedBytes[offset + 2]) << 40n) | | ||
(BigInt(paddedBytes[offset + 3]) << 32n) | | ||
(BigInt(paddedBytes[offset + 4]) << 24n) | | ||
(BigInt(paddedBytes[offset + 5]) << 16n) | | ||
(BigInt(paddedBytes[offset + 6]) << 8n) | | ||
BigInt(paddedBytes[offset + 7]) | ||
limbs[i] = limb | ||
} | ||
|
||
// Reverse the limbs to get little-endian | ||
limbs.reverse() | ||
|
||
return limbs | ||
} | ||
|
||
function limbsToBytes(limbs: bigint[]): Uint8Array { | ||
const limbCount = limbs.length | ||
const result = new Uint8Array(limbCount * 8) | ||
|
||
for (let i = 0; i < limbCount; i++) { | ||
const limb = limbs[limbCount - 1 - i] | ||
// Extract 8 bytes in big-endian order | ||
const offset = i * 8 | ||
result[offset] = Number((limb >> 56n) & 0xffn) | ||
result[offset + 1] = Number((limb >> 48n) & 0xffn) | ||
result[offset + 2] = Number((limb >> 40n) & 0xffn) | ||
result[offset + 3] = Number((limb >> 32n) & 0xffn) | ||
result[offset + 4] = Number((limb >> 24n) & 0xffn) | ||
result[offset + 5] = Number((limb >> 16n) & 0xffn) | ||
result[offset + 6] = Number((limb >> 8n) & 0xffn) | ||
result[offset + 7] = Number(limb & 0xffn) | ||
} | ||
|
||
// Remove leading zeros: | ||
let firstNonZero = 0 | ||
while (firstNonZero < result.length && result[firstNonZero] === 0) { | ||
firstNonZero++ | ||
} | ||
|
||
return firstNonZero === result.length ? new Uint8Array([0]) : result.slice(firstNonZero) | ||
} | ||
|
||
function limbsToInt(limbs: bigint[]): bigint { | ||
const numBytes = limbsToBytes(limbs) | ||
return uint8ArrayToBigint(numBytes) | ||
} | ||
|
||
// Helper function to convert a Uint8Array (big-endian) to bigint | ||
function uint8ArrayToBigint(arr: Uint8Array): bigint { | ||
if (arr.length === 0) return 0n | ||
const hex = '0x' + Array.from(arr, (byte) => byte.toString(16).padStart(2, '0')).join('') | ||
return BigInt(hex) | ||
} | ||
|
||
function placeBEBytesInOutput(out: bigint[], b: Uint8Array): void { | ||
const padded = new Uint8Array(out.length * 8) | ||
padded.set(b, padded.length - b.length) | ||
|
||
const resultLimbs = out.length | ||
for (let i = 0; i < resultLimbs; i++) { | ||
const offset = i * 8 | ||
let limb = 0n | ||
limb |= BigInt(padded[offset]) << 56n | ||
limb |= BigInt(padded[offset + 1]) << 48n | ||
limb |= BigInt(padded[offset + 2]) << 40n | ||
limb |= BigInt(padded[offset + 3]) << 32n | ||
limb |= BigInt(padded[offset + 4]) << 24n | ||
limb |= BigInt(padded[offset + 5]) << 16n | ||
limb |= BigInt(padded[offset + 6]) << 8n | ||
limb |= BigInt(padded[offset + 7]) | ||
|
||
out[resultLimbs - 1 - i] = limb | ||
} | ||
} | ||
|
||
function intToBEBytes(value: bigint): Uint8Array { | ||
if (value === 0n) return new Uint8Array([0]) | ||
let hex = value.toString(16) | ||
if (hex.length % 2 !== 0) { | ||
hex = '0' + hex | ||
} | ||
const arr = new Uint8Array(hex.length / 2) | ||
for (let i = 0; i < arr.length; i++) { | ||
arr[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16) | ||
} | ||
return arr | ||
} | ||
|
||
export function mulModBinary( | ||
z: bigint[], | ||
x: bigint[], | ||
y: bigint[], | ||
modulus: bigint[], | ||
modInv: bigint, | ||
) { | ||
const X = limbsToInt(x) | ||
const Y = limbsToInt(y) | ||
const M = limbsToInt(modulus) | ||
|
||
const result = (X * Y) % M | ||
const resultBytes = intToBEBytes(result) | ||
placeBEBytesInOutput(z, resultBytes) | ||
} | ||
|
||
export function addModBinary(z: bigint[], x: bigint[], y: bigint[], modulus: bigint[]) { | ||
const X = limbsToInt(x) | ||
const Y = limbsToInt(y) | ||
const M = limbsToInt(modulus) | ||
|
||
const result = (X + Y) % M | ||
const resultBytes = intToBEBytes(result) | ||
placeBEBytesInOutput(z, resultBytes) | ||
} | ||
|
||
export function subModBinary(z: bigint[], x: bigint[], y: bigint[], modulus: bigint[]) { | ||
const X = limbsToInt(x) | ||
const Y = limbsToInt(y) | ||
const M = limbsToInt(modulus) | ||
|
||
let result = (X - Y) % M | ||
if (result < 0n) { | ||
result += M | ||
} | ||
const resultBytes = intToBEBytes(result) | ||
placeBEBytesInOutput(z, resultBytes) | ||
} | ||
|
||
export function lt(x: bigint[], y: bigint[]): boolean { | ||
for (let i = x.length; i > 0; i--) { | ||
if (x[i - 1] < y[i - 1]) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should do an
evmmax
hardfork for this: if EVMMAX should be activated then the 6690 EIP should be activated (so instantiate a common and activate 6990 to use EVMMAX). I might have missed something from the EIP though. It seems to me that the EIP introduces new opcodes?