Skip to content

Commit

Permalink
Merge pull request #1359 from 0xPolygonHermez/add-vector-tests-fork13
Browse files Browse the repository at this point in the history
feat: add test vectors fork13
  • Loading branch information
MorettiGeorgiev authored Oct 24, 2024
2 parents 688b8bf + 68cf901 commit 2723db7
Show file tree
Hide file tree
Showing 9 changed files with 7,081 additions and 232 deletions.
169 changes: 52 additions & 117 deletions core/vm/virtual_counters_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ import (
"math/big"
)

var BASE = new(big.Int).Lsh(big.NewInt(1), 256)

func expectedModExpCounters(lenB, lenE, lenM int, B *big.Int, E *big.Int, M *big.Int) (*big.Int, *big.Int, *big.Int) {
q_b_m := big.NewInt(0).Div(B, M)
r_b_m := big.NewInt(0).Mod(B, M)
bsq := big.NewInt(0).Mul(B, B)
nz_bsq := newInt(0).SetUint64(uint64(2*lenB - computeLenThisBase(bsq)))
q_bsq_m := big.NewInt(0).Div(bsq, M)
r_bsq_m := big.NewInt(0).Mod(bsq, M)
bm := big.NewInt(0).Mul(B, M)

e2 := newInt(0).SetUint64(uint64(math.Floor(float64(lenE) / 2)))
lenQE2 := newInt(0).SetUint64(uint64(math.Floor(float64(lenE) / 2)))

base := big.NewInt(0)
nTimesOdd := big.NewInt(0)
Expand All @@ -26,9 +19,9 @@ func expectedModExpCounters(lenB, lenE, lenM int, B *big.Int, E *big.Int, M *big
}
nTimesEven := big.NewInt(0).Mul(big.NewInt(0).SetUint64(uint64(lenE)), big.NewInt(256))

stepFirstDiv, binaryFirstDiv, arithFirstDiv := setupAndFirstDivCounters(lenB, lenM, q_b_m, r_b_m, B, M)
stepHalfLoop, binaryHalfLoop, arithHalfLoop := halfLoopCounters(lenB, lenM, lenE, e2, q_bsq_m, r_bsq_m, bsq, M, nz_bsq)
stepFullLoop, binaryFullLoop, arithFullLoop := fullLoopCounters(lenB, lenM, lenE, e2, q_bsq_m, r_bsq_m, bm, M, bsq, nz_bsq)
stepFirstDiv, binaryFirstDiv, arithFirstDiv := setupAndFirstDivCounters(lenB, lenM, q_b_m, r_b_m)
stepHalfLoop, binaryHalfLoop, arithHalfLoop := halfLoopCounters(lenB, lenM, lenE, lenQE2)
stepFullLoop, binaryFullLoop, arithFullLoop := fullLoopCounters(lenB, lenM, lenE, lenQE2)

steps := combineResults(nTimesOdd, nTimesEven, stepFirstDiv, stepHalfLoop, stepFullLoop)
binary := combineResults(nTimesOdd, nTimesEven, binaryFirstDiv, binaryHalfLoop, binaryFullLoop)
Expand Down Expand Up @@ -65,101 +58,64 @@ func computeLenThisBase(x *big.Int) int {
return length
}

func setupAndFirstDivCounters(lenB, lenM int, q_b_m, r_b_m, B, M *big.Int) (*big.Int, *big.Int, *big.Int) {
steps := newInt(218)
steps.Add(steps, newMul(newInt(39), newInt(lenB)))
steps.Add(steps, newMul(newInt(45), newInt(lenM)))
steps.Add(steps, newMul(newInt(computeLenThisBase(q_b_m)), newInt(30*30*lenM)))
steps.Add(steps, newMul(newInt(17), newInt(computeLenThisBase(r_b_m))))
steps.Sub(steps, newMul(newInt(14), newInt(firstDiffChunk(B, M))))
steps.Sub(steps, newMul(newInt(7), newInt(firstDiffChunk(M, r_b_m))))

binaries := newInt(12)
binaries.Add(binaries, newMul(newInt(6), newInt(lenB)))
binaries.Add(binaries, newMul(newInt(3), newInt(lenM)))
binaries.Add(binaries, newMul(newInt(computeLenThisBase(q_b_m)), newInt(1+4*lenM)))
func setupAndFirstDivCounters(lenB, lenM int, q_b_m, r_b_m *big.Int) (*big.Int, *big.Int, *big.Int) {
steps := newInt(84)
steps.Add(steps, newInt(2))
steps.Add(steps, newMul(newInt(10), newInt(lenB)))
steps.Add(steps, newMul(newInt(3), newInt(lenM)))
steps.Add(steps, newMul(newInt(8+19*lenM), newInt(computeLenThisBase(q_b_m))))
steps.Add(steps, newMul(newInt(12), newInt(computeLenThisBase(r_b_m))))

binaries := newInt(4)
binaries.Sub(binaries, newInt(lenM))
binaries.Add(binaries, newInt(computeLenThisBase(r_b_m)))
binaries.Sub(binaries, newMul(newInt(4), newInt(firstDiffChunk(B, M))))
binaries.Sub(binaries, newMul(newInt(2), newInt(firstDiffChunk(M, r_b_m))))
binaries.Add(binaries, newMul(newInt(2), newInt(computeLenThisBase(q_b_m)), newInt(lenM)))

ariths := newInt(1)
ariths.Add(ariths, newMul(newInt(computeLenThisBase(q_b_m)), newInt(lenM)))
ariths := newInt(lenM * computeLenThisBase(q_b_m))

return steps, binaries, ariths
}

func halfLoopCounters(lenB, lenM, lenE int, E2, q_bsq_m, r_bsq_m, bsq, M, nz_bsq *big.Int) (*big.Int, *big.Int, *big.Int) {
steps := newInt(399)
steps.Add(steps, newMul(newInt(100), newInt(lenB)))
steps.Add(steps, newMul(newInt(61), newInt((lenB*(lenB+1))/2)))
steps.Add(steps, newMul(newInt(48), newInt(lenM)))
steps.Add(steps, newMul(newInt(19), newInt(lenE)))
steps.Add(steps, newMul(newInt(44), E2))
steps.Add(steps, newMul(newInt(computeLenThisBase(q_bsq_m)), newInt(30*30*lenM)))
steps.Add(steps, newMul(newInt(14), newInt(computeLenThisBase(r_bsq_m))))
steps.Add(steps, newMul(newInt(14), newInt(firstDiffChunk(bsq, M))))
steps.Sub(steps, newMul(newInt(14), newInt(firstDiffChunk(bsq, M))))
steps.Sub(steps, newMul(newInt(7), newInt(firstDiffChunk(M, r_bsq_m))))
steps.Sub(steps, newMul(newInt(5), nz_bsq))

binaries := newInt(23)
binaries.Add(binaries, newMul(newInt(14), newInt(lenB)))
binaries.Add(binaries, newMul(newInt(9), newInt((lenB*(lenB+1))/2)))
binaries.Add(binaries, newMul(newInt(3), newInt(lenM)))
binaries.Add(binaries, newMul(newInt(2), newInt(lenE)))
binaries.Add(binaries, newMul(newInt(3), E2))
binaries.Add(binaries, newMul(newInt(computeLenThisBase(q_bsq_m)), newInt(1+4+lenM)))
binaries.Add(binaries, newInt(computeLenThisBase(r_bsq_m)))
binaries.Sub(binaries, newMul(newInt(4), newInt(firstDiffChunk(bsq, M))))
binaries.Sub(binaries, newMul(newInt(2), newInt(firstDiffChunk(M, r_bsq_m))))
binaries.Sub(binaries, nz_bsq)

ariths := newInt(2)
ariths.Add(ariths, newInt(lenB))
ariths.Add(ariths, newInt((lenB*(lenB+1))/2))
ariths.Add(ariths, E2)
ariths.Add(ariths, newMul(newInt(computeLenThisBase(q_bsq_m)), newInt(lenM)))
func halfLoopCounters(lenB, lenM, lenE int, lenQE2 *big.Int) (*big.Int, *big.Int, *big.Int) {
steps := newInt(153)
steps.Add(steps, newMul(newInt(82), newInt(lenM)))
steps.Add(steps, newMul(newInt(6), newInt(lenE)))
steps.Add(steps, newInt((80*lenM*(lenM-1))/2))
steps.Add(steps, newMul(newInt(19), newInt(lenM*lenM)))
steps.Add(steps, newMul(newInt(25), lenQE2))

binaries := newInt(9)
binaries.Add(binaries, newMul(newInt(6), newInt(lenM)))
binaries.Add(binaries, newInt((23*lenM*(lenM-1))/2))
binaries.Add(binaries, newMul(newInt(2), newInt(lenM*lenM)))
binaries.Add(binaries, newMul(newInt(3), lenQE2))

ariths := newInt(-1)
ariths.Add(ariths, newMul(newInt(2), newInt(lenM)))
ariths.Add(ariths, newInt((2*lenM*(lenM-1))/2))
ariths.Add(ariths, newInt(lenM*lenM))

return steps, binaries, ariths
}

func fullLoopCounters(lenB, lenM, lenE int, E2, q_bsq_m, r_bsq_m, BM, M, bsq, nz_bsq *big.Int) (*big.Int, *big.Int, *big.Int) {
steps := newInt(674)
steps.Add(steps, newMul(newInt(180), newInt(lenB)))
steps.Add(steps, newMul(newInt(61), newInt((lenB*(lenB+1))/2)))
steps.Add(steps, newMul(newInt(149), newInt(lenM)))
steps.Add(steps, newMul(newInt(19), newInt(lenE)))
steps.Add(steps, newMul(newInt(44), E2))
steps.Add(steps, newMul(newInt(66), newInt(lenB), newInt(lenM)))
steps.Add(steps, newMul(newInt(computeLenThisBase(q_bsq_m)), newInt(30*30*lenM)))
steps.Add(steps, newMul(newInt(14), newInt(computeLenThisBase(r_bsq_m))))
steps.Sub(steps, newMul(newInt(14), newInt(firstDiffChunk(BM, M))))
steps.Sub(steps, newMul(newInt(14), newInt(firstDiffChunk(bsq, M))))
steps.Sub(steps, newMul(newInt(7), newInt(firstDiffChunk(M, newInt(0)))))
steps.Sub(steps, newMul(newInt(7), newInt(firstDiffChunk(M, r_bsq_m))))
steps.Sub(steps, newMul(newInt(5), nz_bsq))

binaries := newInt(36)
binaries.Add(binaries, newMul(newInt(21), newInt(lenB)))
binaries.Add(binaries, newMul(newInt(9), newInt((lenB*(lenB+1))/2)))
binaries.Add(binaries, newMul(newInt(12), newInt(lenM)))
binaries.Add(binaries, newMul(newInt(2), newInt(lenE)))
binaries.Add(binaries, newMul(newInt(3), E2))
binaries.Add(binaries, newMul(newInt(8), newInt(lenB), newInt(lenM)))
binaries.Add(binaries, newMul(newInt(computeLenThisBase(q_bsq_m)), newInt(1+4*lenM)))
binaries.Add(binaries, newInt(computeLenThisBase(r_bsq_m)))
binaries.Sub(binaries, newMul(newInt(4), newInt(firstDiffChunk(BM, M))))
binaries.Sub(binaries, newMul(newInt(4), newInt(firstDiffChunk(bsq, M))))
binaries.Sub(binaries, newMul(newInt(2), newInt(firstDiffChunk(M, newInt(0)))))
binaries.Sub(binaries, newMul(newInt(2), newInt(firstDiffChunk(M, r_bsq_m))))
binaries.Sub(binaries, nz_bsq)

ariths := newInt(4)
ariths.Add(ariths, newInt(lenB))
ariths.Add(ariths, newInt((lenB*(lenB+1))/2))
ariths.Add(ariths, E2)
ariths.Add(ariths, newMul(newInt(2), newInt(lenB), newInt(lenM)))
ariths.Add(ariths, newMul(newInt(computeLenThisBase(q_bsq_m)), newInt(lenM)))
func fullLoopCounters(lenB, lenM, lenE int, lenQE2 *big.Int) (*big.Int, *big.Int, *big.Int) {
steps := newInt(263)
steps.Add(steps, newMul(newInt(114), newInt(lenM)))
steps.Add(steps, newMul(newInt(6), newInt(lenE)))
steps.Add(steps, newInt((80*lenM*(lenM-1))/2))
steps.Add(steps, newMul(newInt(57), newInt(lenM*lenM)))
steps.Add(steps, newMul(newInt(25), lenQE2))

binaries := newInt(17)
binaries.Add(binaries, newMul(newInt(3), newInt(lenM)))
binaries.Add(binaries, newInt((23*lenM*(lenM-1))/2))
binaries.Add(binaries, newMul(newInt(6), newInt(lenM*lenM)))
binaries.Add(binaries, newMul(newInt(3), lenQE2))

ariths := newInt(-1)
ariths.Add(ariths, newMul(newInt(2), newInt(lenM)))
ariths.Add(ariths, newInt((2*lenM*(lenM-1))/2))
ariths.Add(ariths, newMul(newInt(3), newInt(lenM*lenM)))

return steps, binaries, ariths
}
Expand All @@ -175,24 +131,3 @@ func newMul(x *big.Int, vals ...*big.Int) *big.Int {
}
return res
}

func firstDiffChunk(x, y *big.Int) int {
xLen := computeLenThisBase(x)
yLen := computeLenThisBase(y)

if xLen != yLen {
return xLen
}

i := xLen - 1
mask := new(big.Int).SetBytes([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
for ; i >= 0; i-- {
xShifted := new(big.Int).Rsh(x, uint(i)*256)
yShifted := new(big.Int).Rsh(y, uint(i)*256)
if new(big.Int).And(xShifted, mask).Cmp(new(big.Int).And(yShifted, mask)) != 0 {
break
}
}

return i + 1
}
121 changes: 121 additions & 0 deletions zk/tests/testdata/benchmark-keccaks-fork13.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
[
{
"id": 0,
"description": "Tx for keccaks benchmarks",
"sequencerAddress": "0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D",
"sequencerPvtKey": "0x28b2b0318721be8c8339199172cd7cc8f5e273800a35616ec893083a4b32c02e",
"genesis": [
{
"address": "0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D",
"nonce": "0",
"balance": "100000000000000000000",
"pvtKey": "0x28b2b0318721be8c8339199172cd7cc8f5e273800a35616ec893083a4b32c02e"
},
{
"address": "0x4d5Cf5032B2a844602278b01199ED191A86c93ff",
"nonce": "0",
"balance": "200000000000000000000",
"pvtKey": "0x4d27a600dce8c29b7bd080e29a26972377dbb04d7a27d919adbb602bf13cfd23"
},
{
"address": "0x1275fbb540c8efc58b812ba83b0d0b8b9917ae98",
"nonce": "1",
"balance": "0",
"bytecode": "0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c1171f2314610030575b600080fd5b61004a600480360381019061004591906100f1565b610060565b6040516100579190610137565b60405180910390f35b6000806040518060600160405280604081526020016101cb604091399050600080600090505b848110156100ab578280519060200120915080806100a390610181565b915050610086565b508092505050919050565b600080fd5b6000819050919050565b6100ce816100bb565b81146100d957600080fd5b50565b6000813590506100eb816100c5565b92915050565b600060208284031215610107576101066100b6565b5b6000610115848285016100dc565b91505092915050565b6000819050919050565b6101318161011e565b82525050565b600060208201905061014c6000830184610128565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600061018c826100bb565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8214156101bf576101be610152565b5b60018201905091905056fe68656c6c6f776f726c6468656c6c6f776f726c6468656c6c6f776f726c6468656c6c6f776f726c6468656c6c6f776f726c6468656c6c6f776f726c6431323334a2646970667358221220b269f4e4dcbb2fa953e2720742d09c9128810d3666ea6c4875ef091f392267be64736f6c63430008090033",
"abi": [
{
"inputs": [
{
"internalType": "uint256",
"name": "count",
"type": "uint256"
}
],
"name": "testKeccak256Hash",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "pure",
"type": "function"
}
],
"storage": {}
}
],
"expectedOldRoot": "0x900b1009adeb5c278d0aceac46f41a107d41349b74852fb9159e7df0ba3df7b6",
"txs": [
{
"type": 11,
"deltaTimestamp": "1944498031",
"l1Info": {
"globalExitRoot": "0x090bcaf734c4f06c93954a827b45a6e8c67b8e0fd1e0a35a1c5982d6961828f9",
"blockHash": "0x24a5871d68723340d9eadc674aa8ad75f3e33b61d5a9db7db92af856a19270bb",
"timestamp": "42"
},
"indexL1InfoTree": 0
},
{
"from": "0x4d5Cf5032B2a844602278b01199ED191A86c93ff",
"to": "0x1275fbb540c8efc58b812ba83b0d0b8b9917ae98",
"nonce": "0",
"value": "0",
"data": "0xc1171f23000000000000000000000000000000000000000000000000000000000000000a",
"gasLimit": 1000000,
"gasPrice": "1",
"chainId": 1000
}
],
"expectedNewRoot": "0x08f118bed2c658f4ac5133a247ae825fbff95edf5126d98c407eb97e23fa31ed",
"expectedNewLeafs": {
"0x617b3a3528F9cDd6630fd3301B9c8911F7Bf063D": {
"balance": "100000000000000024345",
"nonce": "0",
"storage": null
},
"0x4d5Cf5032B2a844602278b01199ED191A86c93ff": {
"balance": "199999999999999975655",
"nonce": "1",
"storage": null
},
"0x1275fbb540c8efc58b812ba83b0d0b8b9917ae98": {
"balance": "0",
"nonce": "1",
"storage": {},
"hashBytecode": "0xeb6089487fdd09def5002ce37d81574ad6a96c66f126a647491055c5ab81ad8f",
"bytecodeLength": 576
},
"0x000000000000000000000000000000005ca1ab1e": {
"balance": "0",
"nonce": "0",
"storage": {
"0x0000000000000000000000000000000000000000000000000000000000000000": "0x01",
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x73e6af6f",
"0xa6eef7e35abe7026729641147f7915573c7e97b47efa546f5f6e3230263bcb49": "0x900b1009adeb5c278d0aceac46f41a107d41349b74852fb9159e7df0ba3df7b6",
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x567d6301f8bf5156f715832895cfbc343ad055db28fdaa7c399aef6f6171382b"
}
}
},
"newLocalExitRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"batchHashData": "0x75fb978203dbdebe3bb79964cd075475493f6daec52a0feb64e5f9b87b19aec8",
"batchL2Data": "0x0b73e6af6f00000000f8468001830f4240941275fbb540c8efc58b812ba83b0d0b8b9917ae9880a4c1171f23000000000000000000000000000000000000000000000000000000000000000a8203e880800d9e6d7a26b75d607d384f9ae58bc4ad92e092a5af9efc768c114ef8ebaab9f655b4cfd61939e4b838508e2f73baba13e4efc6d5af032c034070ad7128ec147f1bff",
"forkID": 13,
"l1InfoRoot": "0x090bcaf734c4f06c93954a827b45a6e8c67b8e0fd1e0a35a1c5982d6961828f9",
"timestampLimit": "1944498031",
"chainID": 1000,
"oldAccInputHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"virtualCounters": {
"steps": 48968,
"arith": 949,
"binary": 2177,
"memAlign": 117,
"keccaks": 20,
"padding": 23,
"poseidon": 921,
"sha256": 0
}
}
]
Loading

0 comments on commit 2723db7

Please sign in to comment.