Skip to content

Commit

Permalink
chore migrate from old repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabaie committed Sep 11, 2024
1 parent 6eb6bd8 commit 6f09464
Show file tree
Hide file tree
Showing 23 changed files with 786 additions and 406 deletions.
5 changes: 2 additions & 3 deletions prover/backend/blobsubmission/blobcompression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/encode"
"os"
"strings"
"testing"

blob "github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/v1"

fr381 "github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
"github.com/consensys/zkevm-monorepo/prover/utils"
gokzg4844 "github.com/crate-crypto/go-kzg-4844"
Expand Down Expand Up @@ -270,7 +269,7 @@ func TestKZGWithPoint(t *testing.T) {
}

// Compute all the prover fields
snarkHash, err := blob.MiMCChecksumPackedData(blobBytes[:], fr381.Bits-1, blob.NoTerminalSymbol())
snarkHash, err := encode.MiMCChecksumPackedData(blobBytes[:], fr381.Bits-1, encode.NoTerminalSymbol())
assert.NoError(t, err)

xUnreduced := evaluationChallenge(snarkHash, blobHash[:])
Expand Down
4 changes: 2 additions & 2 deletions prover/backend/blobsubmission/craft.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"encoding/base64"
"errors"
"fmt"
"github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/encode"
"hash"

"github.com/consensys/zkevm-monorepo/prover/crypto/mimc"

fr381 "github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
blob "github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/v1"
"github.com/consensys/zkevm-monorepo/prover/utils"
"golang.org/x/crypto/sha3"
)
Expand Down Expand Up @@ -72,7 +72,7 @@ func CraftResponseCalldata(req *Request) (*Response, error) {
}

// Compute all the prover fields
snarkHash, err := blob.MiMCChecksumPackedData(compressedStream, fr381.Bits-1, blob.NoTerminalSymbol())
snarkHash, err := encode.MiMCChecksumPackedData(compressedStream, fr381.Bits-1, encode.NoTerminalSymbol())
if err != nil {
return nil, fmt.Errorf("crafting response: could not compute snark hash: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion prover/backend/blobsubmission/craft_eip4844.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"errors"
"fmt"
"github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/encode"

blob "github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/v1"

Expand Down Expand Up @@ -91,7 +92,7 @@ func CraftResponse(req *Request) (*Response, error) {
}

// Compute all the prover fields
snarkHash, err := blob.MiMCChecksumPackedData(append(compressedStream, make([]byte, blob.MaxUsableBytes-len(compressedStream))...), fr381.Bits-1, blob.NoTerminalSymbol())
snarkHash, err := encode.MiMCChecksumPackedData(append(compressedStream, make([]byte, blob.MaxUsableBytes-len(compressedStream))...), fr381.Bits-1, encode.NoTerminalSymbol())
if err != nil {
return nil, fmt.Errorf("crafting response: could not compute snark hash: %w", err)
}
Expand Down
62 changes: 31 additions & 31 deletions prover/backend/ethereum/tx_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ func decodeDynamicFeeTx(b *bytes.Reader, tx *types.Transaction) (err error) {

parsedTx := types.DynamicFeeTx{}
err = errors.Join(
tryCast(&parsedTx.ChainID, decTx[0], "chainID"),
tryCast(&parsedTx.Nonce, decTx[1], "nonce"),
tryCast(&parsedTx.GasTipCap, decTx[2], "gas-tip-cap"),
tryCast(&parsedTx.GasFeeCap, decTx[3], "gas-fee-cap"),
tryCast(&parsedTx.Gas, decTx[4], "gas"),
tryCast(&parsedTx.To, decTx[5], "to"),
tryCast(&parsedTx.Value, decTx[6], "value"),
tryCast(&parsedTx.Data, decTx[7], "data"),
tryCast(&parsedTx.AccessList, decTx[8], "access-list"),
TryCast(&parsedTx.ChainID, decTx[0], "chainID"),
TryCast(&parsedTx.Nonce, decTx[1], "nonce"),
TryCast(&parsedTx.GasTipCap, decTx[2], "gas-tip-cap"),
TryCast(&parsedTx.GasFeeCap, decTx[3], "gas-fee-cap"),
TryCast(&parsedTx.Gas, decTx[4], "gas"),
TryCast(&parsedTx.To, decTx[5], "to"),
TryCast(&parsedTx.Value, decTx[6], "value"),
TryCast(&parsedTx.Data, decTx[7], "data"),
TryCast(&parsedTx.AccessList, decTx[8], "access-list"),
)
*tx = *types.NewTx(&parsedTx)
return err
Expand All @@ -176,14 +176,14 @@ func decodeAccessListTx(b *bytes.Reader, tx *types.Transaction) (err error) {

parsedTx := types.AccessListTx{}
err = errors.Join(
tryCast(&parsedTx.ChainID, decTx[0], "chainID"),
tryCast(&parsedTx.Nonce, decTx[1], "nonce"),
tryCast(&parsedTx.GasPrice, decTx[2], "gas-price"),
tryCast(&parsedTx.Gas, decTx[3], "gas"),
tryCast(&parsedTx.To, decTx[4], "to"),
tryCast(&parsedTx.Value, decTx[5], "value"),
tryCast(&parsedTx.Data, decTx[6], "data"),
tryCast(&parsedTx.AccessList, decTx[7], "access-list"),
TryCast(&parsedTx.ChainID, decTx[0], "chainID"),
TryCast(&parsedTx.Nonce, decTx[1], "nonce"),
TryCast(&parsedTx.GasPrice, decTx[2], "gas-price"),
TryCast(&parsedTx.Gas, decTx[3], "gas"),
TryCast(&parsedTx.To, decTx[4], "to"),
TryCast(&parsedTx.Value, decTx[5], "value"),
TryCast(&parsedTx.Data, decTx[6], "data"),
TryCast(&parsedTx.AccessList, decTx[7], "access-list"),
)

*tx = *types.NewTx(&parsedTx)
Expand Down Expand Up @@ -211,30 +211,30 @@ func decodeLegacyTx(b *bytes.Reader, tx *types.Transaction) (err error) {

parsedTx := types.LegacyTx{}
err = errors.Join(
tryCast(&parsedTx.Nonce, decTx[0], "nonce"),
tryCast(&parsedTx.GasPrice, decTx[1], "gas-price"),
tryCast(&parsedTx.Gas, decTx[2], "gas"),
tryCast(&parsedTx.To, decTx[3], "to"),
tryCast(&parsedTx.Value, decTx[4], "value"),
tryCast(&parsedTx.Data, decTx[5], "data"),
TryCast(&parsedTx.Nonce, decTx[0], "nonce"),
TryCast(&parsedTx.GasPrice, decTx[1], "gas-price"),
TryCast(&parsedTx.Gas, decTx[2], "gas"),
TryCast(&parsedTx.To, decTx[3], "to"),
TryCast(&parsedTx.Value, decTx[4], "value"),
TryCast(&parsedTx.Data, decTx[5], "data"),
)

*tx = *types.NewTx(&parsedTx)
return err
}

// tryCast will attempt to set t with the underlying value of `from` will return
// TryCast will attempt to set t with the underlying value of `from` will return
// an error if the type does not match. The explainer string is used to generate
// the error if any.
func tryCast[T any](into *T, from any, explainer string) error {
func TryCast[T any](into *T, from any, explainer string) error {

if into == nil || from == nil {
return fmt.Errorf("from or into is/are nil")
}

// The rlp encoding is not "type-aware", if the underlying field is an
// access-list, it will decode into []interface{} (and we recursively parse
// it) otherwise, it always decode to `[]byte`
// it) otherwise, it always decodes to `[]byte`
if list, ok := (from).([]interface{}); ok {

var (
Expand All @@ -249,7 +249,7 @@ func tryCast[T any](into *T, from any, explainer string) error {
for i := range accessList {
err = errors.Join(
err,
tryCast(&accessList[i], list[i], fmt.Sprintf("%v[%v]", explainer, i)),
TryCast(&accessList[i], list[i], fmt.Sprintf("%v[%v]", explainer, i)),
)
}
*into = (any(accessList)).(T)
Expand All @@ -258,16 +258,16 @@ func tryCast[T any](into *T, from any, explainer string) error {
case types.AccessTuple:
tuple := types.AccessTuple{}
err = errors.Join(
tryCast(&tuple.Address, list[0], fmt.Sprintf("%v.%v", explainer, "address")),
tryCast(&tuple.StorageKeys, list[1], fmt.Sprintf("%v.%v", explainer, "storage-key")),
TryCast(&tuple.Address, list[0], fmt.Sprintf("%v.%v", explainer, "address")),
TryCast(&tuple.StorageKeys, list[1], fmt.Sprintf("%v.%v", explainer, "storage-key")),
)
*into = (any(tuple)).(T)
return err

case []common.Hash:
hashes := make([]common.Hash, length)
for i := range hashes {
tryCast(&hashes[i], list[i], fmt.Sprintf("%v[%v]", explainer, i))
TryCast(&hashes[i], list[i], fmt.Sprintf("%v[%v]", explainer, i))
}
*into = (any(hashes)).(T)
return err
Expand Down Expand Up @@ -295,7 +295,7 @@ func tryCast[T any](into *T, from any, explainer string) error {
*into = any(address).(T)
case common.Hash:
// Parse the bytes as an UTF8 string (= direct casting in go).
// Then, the string as an hexstring encoded address.
// Then, the string as a hexstring encoded address.
hash := common.BytesToHash(fromBytes)
*into = any(hash).(T)
case *big.Int:
Expand Down
5 changes: 4 additions & 1 deletion prover/circuits/blobdecompression/v0/assign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"encoding/json"
v0 "github.com/consensys/zkevm-monorepo/prover/circuits/blobdecompression/v0"
"github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/dictionary"
"github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/v1/test_utils"
"os"
"testing"
Expand Down Expand Up @@ -73,7 +74,9 @@ func mustGetTestCompressedData(t *testing.T) (resp blobsubmission.Response, blob
blobBytes, err = base64.StdEncoding.DecodeString(resp.CompressedData)
assert.NoError(t, err)

_, _, _, err = blob.DecompressBlob(blobBytes, dict)
dictStore, err := dictionary.SingletonStore(dict, 0)
assert.NoError(t, err)
_, _, _, err = blob.DecompressBlob(blobBytes, dictStore)
assert.NoError(t, err)

return
Expand Down
8 changes: 7 additions & 1 deletion prover/circuits/blobdecompression/v0/prelude.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/consensys/zkevm-monorepo/prover/circuits/internal"
"github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/dictionary"

"github.com/consensys/gnark-crypto/ecc/bls12-377/fr"
fr381 "github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
Expand Down Expand Up @@ -54,7 +55,12 @@ func Assign(blobData, dict []byte, eip4844Enabled bool, x [32]byte, y fr381.Elem
return
}

header, uncompressedData, _, err := blob.DecompressBlob(blobData, dict)
dictStore, err := dictionary.SingletonStore(dict, 0)
if err != nil {
err = fmt.Errorf("failed to create dictionary store %w", err)
return
}
header, uncompressedData, _, err := blob.DecompressBlob(blobData, dictStore)
if err != nil {
err = fmt.Errorf("decompression circuit assignment : could not decompress the data : %w", err)
return
Expand Down
5 changes: 4 additions & 1 deletion prover/circuits/blobdecompression/v1/assign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package v1_test
import (
"encoding/base64"
"encoding/hex"
"github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/dictionary"
"testing"

"github.com/consensys/gnark-crypto/ecc"
Expand All @@ -27,7 +28,9 @@ func prepareTestBlob(t require.TestingT) (c, a frontend.Circuit) {

func prepare(t require.TestingT, blobBytes []byte) (c *v1.Circuit, a frontend.Circuit) {

_, payload, _, err := blobcompressorv1.DecompressBlob(blobBytes, blobtestutils.GetDict(t))
dictStore, err := dictionary.SingletonStore(blobtestutils.GetDict(t), 1)
assert.NoError(t, err)
_, payload, _, err := blobcompressorv1.DecompressBlob(blobBytes, dictStore)
assert.NoError(t, err)

resp, err := blobsubmission.CraftResponse(&blobsubmission.Request{
Expand Down
11 changes: 9 additions & 2 deletions prover/circuits/blobdecompression/v1/circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"errors"
"fmt"
"github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/dictionary"
"github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/encode"
"math/big"

"github.com/consensys/gnark-crypto/ecc"
Expand Down Expand Up @@ -235,7 +237,12 @@ func AssignFPI(blobBytes, dict []byte, eip4844Enabled bool, x [32]byte, y fr381.
return
}

header, payload, _, err := blob.DecompressBlob(blobBytes, dict)
dictStore, err := dictionary.SingletonStore(dict, 1)
if err != nil {
err = fmt.Errorf("failed to create dictionary store %w", err)
return
}
header, payload, _, err := blob.DecompressBlob(blobBytes, dictStore)
if err != nil {
return
}
Expand Down Expand Up @@ -266,7 +273,7 @@ func AssignFPI(blobBytes, dict []byte, eip4844Enabled bool, x [32]byte, y fr381.
if len(blobBytes) != 128*1024 {
panic("blobBytes length is not 128*1024")
}
fpi.SnarkHash, err = blob.MiMCChecksumPackedData(blobBytes, fr381.Bits-1, blob.NoTerminalSymbol()) // TODO if forced to remove the above check, pad with zeros
fpi.SnarkHash, err = encode.MiMCChecksumPackedData(blobBytes, fr381.Bits-1, encode.NoTerminalSymbol()) // TODO if forced to remove the above check, pad with zeros

return
}
Expand Down
31 changes: 19 additions & 12 deletions prover/circuits/blobdecompression/v1/snark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"crypto/rand"
"errors"
"github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/dictionary"
"github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/encode"
"github.com/consensys/zkevm-monorepo/prover/utils"
"testing"

Expand All @@ -19,7 +21,7 @@ import (
"github.com/consensys/gnark/std/hash/mimc"
"github.com/consensys/gnark/test"
blob "github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/v1"
blobtesting "github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/v1/test_utils"
blobtestutils "github.com/consensys/zkevm-monorepo/prover/lib/compressor/blob/v1/test_utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -29,7 +31,7 @@ func TestParseHeader(t *testing.T) {
maxBlobSize := 1024

blobs := [][]byte{
blobtesting.GenTestBlob(t, 100000),
blobtestutils.GenTestBlob(t, 100000),
}

for _, blobData := range blobs {
Expand All @@ -47,14 +49,17 @@ func TestParseHeader(t *testing.T) {
test.NoTestEngine(),
}

dictStore, err := dictionary.SingletonStore(blobtestutils.GetDict(t), 1)
assert.NoError(t, err)

for _, blobData := range blobs {

header, _, blocks, err := blob.DecompressBlob(blobData, blobtesting.GetDict(t))
header, _, blocks, err := blob.DecompressBlob(blobData, dictStore)
assert.NoError(t, err)

assert.LessOrEqual(t, len(blocks), MaxNbBatches, "too many batches")

unpacked, err := blob.UnpackAlign(blobData, fr381.Bits-1, false)
unpacked, err := encode.UnpackAlign(blobData, fr381.Bits-1, false)
require.NoError(t, err)

assignment := &testParseHeaderCircuit{
Expand Down Expand Up @@ -87,9 +92,9 @@ func TestChecksumBatches(t *testing.T) {

var batchEndss [nbAssignments][]int
for i := range batchEndss {
batchEndss[i] = make([]int, blobtesting.RandIntn(MaxNbBatches)+1)
batchEndss[i] = make([]int, blobtestutils.RandIntn(MaxNbBatches)+1)
for j := range batchEndss[i] {
batchEndss[i][j] = 31 + blobtesting.RandIntn(62)
batchEndss[i][j] = 31 + blobtestutils.RandIntn(62)
if j > 0 {
batchEndss[i][j] += batchEndss[i][j-1]
}
Expand Down Expand Up @@ -160,7 +165,7 @@ func testChecksumBatches(t *testing.T, blob []byte, batchEndss ...[]int) {
Sums: sums,
NbBatches: len(batchEnds),
}
assignment.Sums[blobtesting.RandIntn(len(batchEnds))] = 3
assignment.Sums[blobtestutils.RandIntn(len(batchEnds))] = 3

assert.Error(t, test.IsSolved(&circuit, &assignment, ecc.BLS12_377.ScalarField()))

Expand Down Expand Up @@ -223,7 +228,7 @@ func TestUnpackCircuit(t *testing.T) {

runTest := func(b []byte) {
var packedBuf bytes.Buffer
_, err := blob.PackAlign(&packedBuf, b, fr381.Bits-1) // todo use two different slices
_, err := encode.PackAlign(&packedBuf, b, fr381.Bits-1) // todo use two different slices
assert.NoError(t, err)

circuit := unpackCircuit{
Expand Down Expand Up @@ -307,7 +312,7 @@ func TestBlobChecksum(t *testing.T) { // aka "snark hash"
assignment := testDataChecksumCircuit{
DataBytes: dataVarsPadded[:nPadded],
}
assignment.Checksum, err = blob.MiMCChecksumPackedData(dataPadded[:nPadded], fr381.Bits-1, blob.NoTerminalSymbol())
assignment.Checksum, err = encode.MiMCChecksumPackedData(dataPadded[:nPadded], fr381.Bits-1, encode.NoTerminalSymbol())
assert.NoError(t, err)

assert.NoError(t, test.IsSolved(&circuit, &assignment, ecc.BLS12_377.ScalarField()))
Expand Down Expand Up @@ -337,9 +342,11 @@ func (c *testDataChecksumCircuit) Define(api frontend.API) error {
}

func TestDictHash(t *testing.T) {
blobBytes := blobtesting.GenTestBlob(t, 1)
dict := blobtesting.GetDict(t)
header, _, _, err := blob.DecompressBlob(blobBytes, dict) // a bit roundabout, but the header field is not public
blobBytes := blobtestutils.GenTestBlob(t, 1)
dict := blobtestutils.GetDict(t)
dictStore, err := dictionary.SingletonStore(blobtestutils.GetDict(t), 1)
assert.NoError(t, err)
header, _, _, err := blob.DecompressBlob(blobBytes, dictStore) // a bit roundabout, but the header field is not public
assert.NoError(t, err)

circuit := testDataDictHashCircuit{
Expand Down
Loading

0 comments on commit 6f09464

Please sign in to comment.