Fix/aggregation integration test (#3909)

* refac moving some utils around
* perf remove profiling
* test change nCircuits to 10
* chore remove unnecessary func
* fix bw6 public input fed into bn254 proof

---------

Co-authored-by: Arya Tabaie <15056835+Tabaie@users.noreply.github.com>
This commit is contained in:
Arya Tabaie
2024-09-09 14:53:10 -05:00
committed by GitHub
parent 8755f315a1
commit daba463d77
12 changed files with 173 additions and 201 deletions

View File

@@ -2,9 +2,11 @@ package aggregation_test
import (
"context"
"testing"
"github.com/consensys/gnark-crypto/ecc"
frBls "github.com/consensys/gnark-crypto/ecc/bls12-377/fr"
"github.com/consensys/gnark-crypto/ecc/bw6-761/fr"
frBw6 "github.com/consensys/gnark-crypto/ecc/bw6-761/fr"
"github.com/consensys/gnark/backend/plonk"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/scs"
@@ -14,7 +16,9 @@ import (
"github.com/consensys/zkevm-monorepo/prover/circuits/aggregation"
"github.com/consensys/zkevm-monorepo/prover/circuits/dummy"
"github.com/consensys/zkevm-monorepo/prover/circuits/internal"
"github.com/consensys/zkevm-monorepo/prover/circuits/internal/test_utils"
snarkTestUtils "github.com/consensys/zkevm-monorepo/prover/circuits/internal/test_utils"
"github.com/consensys/zkevm-monorepo/prover/utils/test_utils"
pi_interconnection "github.com/consensys/zkevm-monorepo/prover/circuits/pi-interconnection"
"github.com/consensys/zkevm-monorepo/prover/circuits/pi-interconnection/keccak"
"github.com/consensys/zkevm-monorepo/prover/config"
@@ -23,7 +27,6 @@ import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestPublicInput(t *testing.T) {
@@ -62,7 +65,7 @@ func TestPublicInput(t *testing.T) {
var res [32]frontend.Variable
assert.NoError(t, internal.CopyHexEncodedBytes(res[:], testCases[i].GetPublicInputHex()))
test_utils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
snarkTestUtils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
sum := sfpi.Sum(api, keccak.NewHasher(api, 500))
return sum[:]
}, res[:]...)(t)
@@ -98,7 +101,7 @@ func testAggregation(t *testing.T, nCircuits int, ncs ...int) {
}
aggregationPIBytes := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
var aggregationPI fr.Element
var aggregationPI frBw6.Element
aggregationPI.SetBytes(aggregationPIBytes)
logrus.Infof("Compiling interconnection circuit")
@@ -130,7 +133,7 @@ func testAggregation(t *testing.T, nCircuits int, ncs ...int) {
require.NoError(t, err)
// Generate proofs claims to aggregate
nProofs := internal.Ite(nc == 0, 0, utils.Max(nc-3, 1))
nProofs := utils.Ite(nc == 0, 0, utils.Max(nc-3, 1))
logrus.Infof("Generating a witness, %v dummy-proofs to aggregates", nProofs)
innerProofClaims := make([]aggregation.ProofClaimAssignment, nProofs)
innerPI := make([]frBls.Element, nc)

View File

@@ -109,7 +109,7 @@ func (i *FunctionalPublicInput) ToSnarkType() (FunctionalPublicInputSnark, error
FunctionalPublicInputQSnark: FunctionalPublicInputQSnark{
Y: [2]frontend.Variable{i.Y[0], i.Y[1]},
SnarkHash: i.SnarkHash,
Eip4844Enabled: internal.Ite(i.Eip4844Enabled, 1, 0),
Eip4844Enabled: utils.Ite(i.Eip4844Enabled, 1, 0),
NbBatches: len(i.BatchSums),
},
}
@@ -149,7 +149,7 @@ func (i *FunctionalPublicInput) Sum(opts ...FPISumOption) ([]byte, error) {
hsh.Write(i.Y[0])
hsh.Write(i.Y[1])
hsh.Write(i.SnarkHash)
hsh.Write(internal.Ite(i.Eip4844Enabled, []byte{1}, []byte{0}))
hsh.Write(utils.Ite(i.Eip4844Enabled, []byte{1}, []byte{0}))
hsh.Write(batchesSum)
return hsh.Sum(nil), nil
}

View File

@@ -1,10 +1,12 @@
package execution
import (
"testing"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/hash/mimc"
"github.com/consensys/zkevm-monorepo/prover/circuits/internal/test_utils"
"testing"
snarkTestUtils "github.com/consensys/zkevm-monorepo/prover/circuits/internal/test_utils"
"github.com/consensys/zkevm-monorepo/prover/utils"
)
func TestPIConsistency(t *testing.T) {
@@ -20,14 +22,14 @@ func TestPIConsistency(t *testing.T) {
ChainID: 7,
}
test_utils.FillRange(pi.DataChecksum[:], 10)
test_utils.FillRange(pi.L2MessageHashes[0][:], 50)
test_utils.FillRange(pi.L2MessageHashes[1][:], 90)
test_utils.FillRange(pi.InitialStateRootHash[:], 130)
test_utils.FillRange(pi.InitialRollingHash[:], 170)
test_utils.FillRange(pi.FinalStateRootHash[:], 210)
test_utils.FillRange(pi.FinalRollingHash[:], 250)
test_utils.FillRange(pi.L2MessageServiceAddr[:], 40)
utils.FillRange(pi.DataChecksum[:], 10)
utils.FillRange(pi.L2MessageHashes[0][:], 50)
utils.FillRange(pi.L2MessageHashes[1][:], 90)
utils.FillRange(pi.InitialStateRootHash[:], 130)
utils.FillRange(pi.InitialRollingHash[:], 170)
utils.FillRange(pi.FinalStateRootHash[:], 210)
utils.FillRange(pi.FinalRollingHash[:], 250)
utils.FillRange(pi.L2MessageServiceAddr[:], 40)
// state root hashes are field elements
pi.InitialStateRootHash[0] &= 0x0f
@@ -36,7 +38,7 @@ func TestPIConsistency(t *testing.T) {
snarkPi := pi.ToSnarkType()
piSum := pi.Sum()
test_utils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
snarkTestUtils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
hsh, err := mimc.NewMiMC(api)
if err != nil {
panic(err)

View File

@@ -6,16 +6,15 @@ import (
"encoding/hex"
"errors"
"fmt"
"math/big"
"strings"
"testing"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/test"
"github.com/consensys/zkevm-monorepo/prover/utils"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/constraints"
"math"
"math/big"
"strings"
"testing"
)
func printAsHexHint(_ *big.Int, ins, outs []*big.Int) error {
@@ -144,23 +143,6 @@ func SnarkFunctionTest(f func(frontend.API) []frontend.Variable, outs ...fronten
}
}
func Range[T constraints.Integer](length int, startingPoints ...T) []T {
if len(startingPoints) == 0 {
startingPoints = []T{0}
}
res := make([]T, length*len(startingPoints))
for i := range startingPoints {
FillRange(res[i*length:(i+1)*length], startingPoints[i])
}
return res
}
func FillRange[T constraints.Integer](dst []T, start T) {
for l := range dst {
dst[l] = T(l) + start
}
}
func BlocksToHex(b ...[][32]byte) []string {
res := make([]string, 0)
for i := range b {
@@ -170,23 +152,3 @@ func BlocksToHex(b ...[][32]byte) []string {
}
return res
}
func RandIntN(n int) int {
var b [8]byte
_, err := rand.Read(b[:])
if err != nil {
panic(err)
}
if n > math.MaxInt {
panic("RandIntN: n too large")
}
return int(binary.BigEndian.Uint64(b[:]) % uint64(n)) // #nosec G115 -- Above check precludes an overflow
}
func RandIntSliceN(length, n int) []int {
res := make([]int, length)
for i := range res {
res[i] = RandIntN(n)
}
return res
}

View File

@@ -614,13 +614,6 @@ func Bls12381ScalarToBls12377Scalars(v interface{}) (r [2][]byte, err error) {
return
}
func Ite[T any](cond bool, ifSo, ifNot T) T {
if cond {
return ifSo
}
return ifNot
}
// PartialSums returns s[0], s[0]+s[1], ..., s[0]+s[1]+...+s[len(s)-1]
func PartialSums(api frontend.API, s []frontend.Variable) []frontend.Variable {
res := make([]frontend.Variable, len(s))

View File

@@ -3,6 +3,8 @@ package internal_test
import (
"crypto/rand"
"fmt"
"testing"
fr377 "github.com/consensys/gnark-crypto/ecc/bls12-377/fr"
fr381 "github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
bn254fr "github.com/consensys/gnark-crypto/ecc/bn254/fr"
@@ -10,15 +12,15 @@ import (
"github.com/consensys/gnark/std/hash/mimc"
"github.com/consensys/gnark/std/math/emulated"
"github.com/consensys/zkevm-monorepo/prover/circuits/internal"
"github.com/consensys/zkevm-monorepo/prover/circuits/internal/test_utils"
snarkTestUtils "github.com/consensys/zkevm-monorepo/prover/circuits/internal/test_utils"
"github.com/consensys/zkevm-monorepo/prover/utils"
"github.com/consensys/zkevm-monorepo/prover/utils/test_utils"
"github.com/stretchr/testify/assert"
"testing"
)
func TestChecksumSlice(t *testing.T) {
sum := internal.ChecksumSlice([][]byte{{0}, {1}, {2}})
test_utils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
snarkTestUtils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
s := internal.VarSlice{
Values: []frontend.Variable{0, 1, 2, 3},
Length: 3,
@@ -41,7 +43,7 @@ func TestChecksumSubSlices(t *testing.T) {
}
func testChecksumSubSlices(t *testing.T, bigSliceLength, lengthsSliceLength int, lengths ...int) {
bigSliceInts := test_utils.Range[uint64](bigSliceLength)
bigSliceInts := utils.RangeSlice[uint64](bigSliceLength)
bigSliceBytes := internal.MapSlice(internal.Uint64To32Bytes, bigSliceInts...)
endPoints := internal.PartialSumsInt(lengths)
@@ -60,7 +62,7 @@ func testChecksumSubSlices(t *testing.T, bigSliceLength, lengthsSliceLength int,
endPointsSnark[n] = n * 234
}
t.Run(fmt.Sprintf("%d,%d,%v", bigSliceLength, lengthsSliceLength, lengths), test_utils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
t.Run(fmt.Sprintf("%d,%d,%v", bigSliceLength, lengthsSliceLength, lengths), snarkTestUtils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
hsh, err := mimc.NewMiMC(api)
if err != nil {
panic(err)
@@ -74,7 +76,7 @@ func testChecksumSubSlices(t *testing.T, bigSliceLength, lengthsSliceLength int,
}
func TestConcat(t *testing.T) {
test_utils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
snarkTestUtils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
res := internal.Concat(api, 3, internal.VarSlice{[]frontend.Variable{2}, 1}, internal.VarSlice{[]frontend.Variable{3}, 0})
return append(res.Values, res.Length)
}, 2, 0, 0, 1)(t)
@@ -102,7 +104,7 @@ func TestReduceBytes(t *testing.T) {
reduced[i] = reducedI[:]
}
test_utils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
snarkTestUtils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
for i := range cases {
got := utils.ReduceBytes[emulated.BN254Fr](api, utils.ToVariableSlice(cases[i]))
internal.AssertSliceEquals(api,
@@ -133,7 +135,7 @@ func TestPartitionSliceEmulated(t *testing.T) {
subs[selectors[i]] = append(subs[selectors[i]], s[i])
}
test_utils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
snarkTestUtils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
field, err := emulated.NewField[emulated.BLS12381Fr](api)
assert.NoError(t, err)
@@ -188,7 +190,7 @@ func TestPartitionSlice(t *testing.T) {
subs[j] = append(subs[j], utils.ToVariableSlice(make([]int, subsSlack[j]))...) // add some padding
}
return test_utils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
return snarkTestUtils.SnarkFunctionTest(func(api frontend.API) []frontend.Variable {
slice := utils.ToVariableSlice(slice)
@@ -206,7 +208,7 @@ func TestPartitionSlice(t *testing.T) {
test([]frontend.Variable{5}, []int{2}, []int{1, 0, 0})(t)
test([]frontend.Variable{1, 2, 3}, []int{0, 1, 2}, []int{0, 0, 0})
test(utils.ToVariableSlice(test_utils.Range[int](10)), []int{0, 1, 2, 0, 0, 0, 1, 1, 1, 2}, []int{0, 0, 0})
test(utils.ToVariableSlice(utils.RangeSlice[int](10)), []int{0, 1, 2, 0, 0, 0, 1, 1, 1, 2}, []int{0, 0, 0})
for i := 0; i < 200; i++ {

View File

@@ -3,7 +3,6 @@ package pi_interconnection
import (
"errors"
"fmt"
"github.com/consensys/zkevm-monorepo/prover/circuits/internal/test_utils"
"math/big"
"slices"
@@ -345,7 +344,7 @@ const (
)
func InnerCircuitTypesToIndexes(cfg *config.PublicInput, types []InnerCircuitType) []int {
indexes := utils.RightPad(utils.Partition(test_utils.Range[int](len(types)), types), 2)
indexes := utils.RightPad(utils.Partition(utils.RangeSlice[int](len(types)), types), 2)
return utils.RightPad(
append(utils.RightPad(indexes[Execution], cfg.MaxNbExecution), indexes[Decompression]...), cfg.MaxNbExecution+cfg.MaxNbDecompression)

View File

@@ -12,11 +12,11 @@ import (
"github.com/consensys/gnark/test"
"github.com/consensys/zkevm-monorepo/prover/backend/aggregation"
"github.com/consensys/zkevm-monorepo/prover/circuits/internal"
"github.com/consensys/zkevm-monorepo/prover/circuits/internal/test_utils"
pi_interconnection "github.com/consensys/zkevm-monorepo/prover/circuits/pi-interconnection"
"github.com/consensys/zkevm-monorepo/prover/circuits/pi-interconnection/keccak"
"github.com/consensys/zkevm-monorepo/prover/config"
"github.com/consensys/zkevm-monorepo/prover/utils"
"github.com/consensys/zkevm-monorepo/prover/utils/test_utils"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/sha3"
)

View File

@@ -261,7 +261,7 @@ func TestCreateColsBoundaryChecks(t *testing.T) {
fail := nbNeededLanes > c.maxNbLanes
t.Run(fmt.Sprintf("%d-%s", i, internal.Ite(fail, "fail", "pass")), func(t *testing.T) {
t.Run(fmt.Sprintf("%d-%s", i, utils.Ite(fail, "fail", "pass")), func(t *testing.T) {
assignment := testCreateColsBoundaryChecks{
InLength: utils.ToVariableSlice(c.inLength),

View File

@@ -3,15 +3,12 @@ package main
import (
"context"
"fmt"
"reflect"
"sync"
"github.com/consensys/gnark-crypto/ecc"
frBls "github.com/consensys/gnark-crypto/ecc/bls12-377/fr"
frBn254 "github.com/consensys/gnark-crypto/ecc/bn254/fr"
frBw6 "github.com/consensys/gnark-crypto/ecc/bw6-761/fr"
"github.com/consensys/gnark/backend/plonk"
"github.com/consensys/gnark/constraint"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/scs"
emPlonk "github.com/consensys/gnark/std/recursion/plonk"
@@ -21,25 +18,19 @@ import (
"github.com/consensys/zkevm-monorepo/prover/circuits/emulation"
pi_interconnection "github.com/consensys/zkevm-monorepo/prover/circuits/pi-interconnection"
"github.com/consensys/zkevm-monorepo/prover/config"
dummyWizard "github.com/consensys/zkevm-monorepo/prover/protocol/compiler/dummy"
public_input "github.com/consensys/zkevm-monorepo/prover/public-input"
"github.com/consensys/zkevm-monorepo/prover/utils"
"github.com/consensys/zkevm-monorepo/prover/utils/test_utils"
"github.com/pkg/profile"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func main() {
p := profile.Start()
defer p.Stop()
var t test_utils.FakeTestingT
// Mock circuits to aggregate
var innerSetups []circuits.Setup
const nCircuits = 1 // TODO @Tabaie change to 10
const nCircuits = 10
logrus.Infof("Initializing many inner-circuits of %v\n", nCircuits)
srsProvider := circuits.NewUnsafeSRSProvider() // This is a dummy SRS provider, not to use in prod.
for i := 0; i < nCircuits; i++ {
@@ -61,53 +52,32 @@ func main() {
bw6Proofs []plonk.Proof
)
//ncs := []int{1, 5, 10, 20} TODO @Tabaie change back to this
ncs := []int{1}
ncs := []int{1, 5, 10, 20}
const hexZeroBlock = "0x0000000000000000000000000000000000000000000000000000000000000000"
aggregationFPIHex := public_input.Aggregation{
FinalShnarf: hexZeroBlock,
ParentAggregationFinalShnarf: hexZeroBlock,
ParentStateRootHash: hexZeroBlock,
ParentAggregationLastBlockTimestamp: 0,
FinalTimestamp: 0,
LastFinalizedBlockNumber: 0,
FinalBlockNumber: 0,
LastFinalizedL1RollingHash: hexZeroBlock,
L1RollingHash: hexZeroBlock,
LastFinalizedL1RollingHashMessageNumber: 0,
L1RollingHashMessageNumber: 0,
L2MsgRootHashes: []string{},
L2MsgMerkleTreeDepth: 1,
}
aggregationPIBytes := aggregationFPIHex.Sum(nil)
aggregationPIBytes := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
var aggregationPI frBw6.Element
aggregationPI.SetBytes(aggregationPIBytes)
logrus.Infof("Compiling interconnection circuit")
piNbEachCircuit := utils.Max(ncs...)
piSetup := getPiInterconnectionSetup(srsProvider, piNbEachCircuit)
logrus.Infof("Assigning interconnection circuit")
piAssignment, err := piSetup.c.Assign(pi_interconnection.Request{
Decompressions: nil,
Executions: nil,
Aggregation: aggregationFPIHex,
})
maxNC := utils.Max(ncs...)
piConfig := config.PublicInput{
MaxNbDecompression: maxNC,
MaxNbExecution: maxNC,
}
piCircuit := pi_interconnection.DummyCircuit{
ExecutionPublicInput: make([]frontend.Variable, piConfig.MaxNbExecution),
ExecutionFPI: make([]frontend.Variable, piConfig.MaxNbExecution),
DecompressionPublicInput: make([]frontend.Variable, piConfig.MaxNbDecompression),
DecompressionFPI: make([]frontend.Variable, piConfig.MaxNbDecompression),
}
piCs, err := frontend.Compile(ecc.BLS12_377.ScalarField(), scs.NewBuilder, &piCircuit)
assert.NoError(t, err)
logrus.Infof("Generating PI proof")
piW, err := frontend.NewWitness(&piAssignment, ecc.BLS12_377.ScalarField())
piSetup, err := circuits.MakeSetup(context.TODO(), circuits.PublicInputInterconnectionCircuitID, piCs, srsProvider, nil)
assert.NoError(t, err)
piProof, err := plonk.Prove(piSetup.Circuit, piSetup.ProvingKey, piW)
assert.NoError(t, err)
piPW, err := piW.Public()
assert.NoError(t, err)
piInfo := aggregation.PiInfo{
Proof: piProof,
PublicWitness: piPW,
ActualIndexes: []int{},
}
fmt.Println("aggregationPIBytes", utils.HexEncodeToString(aggregationPIBytes))
for _, nc := range ncs {
// Compile PI Interconnection sub-circuit
@@ -116,26 +86,27 @@ func main() {
logrus.Infof("Building aggregation circuit for size of %v\n", nc)
ccs, err := aggregation.MakeCS(
nc,
piSetup.Setup,
piSetup,
vkeys)
assert.NoError(t, err)
// Generates the setup
// Generate the setup
logrus.Infof("Generating setup, will take a while to complete..")
ppBw6, err := circuits.MakeSetup(context.Background(), circuits.CircuitID(fmt.Sprintf("aggregation-%d", nc)), ccs, circuits.NewUnsafeSRSProvider(), nil)
assert.NoError(t, err)
// Generate proofs claims to aggregate
nProofs := utils.Max(nc-3, 1)
nProofs := utils.Ite(nc == 0, 0, utils.Max(nc-3, 1))
logrus.Infof("Generating a witness, %v dummy-proofs to aggregates", nProofs)
innerProofClaims := make([]aggregation.ProofClaimAssignment, nProofs)
innerPI := make([]frBls.Element, nc)
for i := range innerProofClaims {
// Assign the dummy circuit for a random value
circID := i % nCircuits
var x frBls.Element
x.SetRandom()
a := dummy.Assign(circuits.MockCircuitID(circID), x)
_, err = innerPI[i].SetRandom()
assert.NoError(t, err)
a := dummy.Assign(circuits.MockCircuitID(circID), innerPI[i])
// Stores the inner-proofs for later
proof, err := circuits.ProveCheck(
@@ -148,10 +119,48 @@ func main() {
innerProofClaims[i] = aggregation.ProofClaimAssignment{
CircuitID: circID,
Proof: proof,
PublicInput: x,
PublicInput: innerPI[i],
}
}
logrus.Info("generating witness for the interconnection circuit")
// assign public input circuit
circuitTypes := make([]pi_interconnection.InnerCircuitType, nc)
for i := range circuitTypes {
circuitTypes[i] = pi_interconnection.InnerCircuitType(test_utils.RandIntN(2)) // #nosec G115 -- value already constrained
}
innerPiPartition := utils.RightPad(utils.Partition(innerPI, circuitTypes), 2)
execPI := utils.RightPad(innerPiPartition[typeExec], len(piCircuit.ExecutionPublicInput))
decompPI := utils.RightPad(innerPiPartition[typeDecomp], len(piCircuit.DecompressionPublicInput))
piAssignment := pi_interconnection.DummyCircuit{
AggregationPublicInput: [2]frontend.Variable{aggregationPIBytes[:16], aggregationPIBytes[16:]},
ExecutionPublicInput: utils.ToVariableSlice(execPI),
DecompressionPublicInput: utils.ToVariableSlice(decompPI),
DecompressionFPI: utils.ToVariableSlice(pow5(decompPI)),
ExecutionFPI: utils.ToVariableSlice(pow5(execPI)),
NbExecution: len(innerPiPartition[typeExec]),
NbDecompression: len(innerPiPartition[typeDecomp]),
}
logrus.Infof("Generating PI proof")
piW, err := frontend.NewWitness(&piAssignment, ecc.BLS12_377.ScalarField())
assert.NoError(t, err)
piProof, err := circuits.ProveCheck(
&piSetup, &piAssignment,
emPlonk.GetNativeProverOptions(ecc.BW6_761.ScalarField(), ecc.BLS12_377.ScalarField()),
emPlonk.GetNativeVerifierOptions(ecc.BW6_761.ScalarField(), ecc.BLS12_377.ScalarField()),
)
assert.NoError(t, err)
piPubW, err := piW.Public()
assert.NoError(t, err)
piInfo := aggregation.PiInfo{
Proof: piProof,
PublicWitness: piPubW,
ActualIndexes: pi_interconnection.InnerCircuitTypesToIndexes(&piConfig, circuitTypes),
}
// Assigning the BW6 circuit
logrus.Infof("Generating the aggregation proof for arity %v", nc)
@@ -171,75 +180,29 @@ func main() {
setupEmulation, err := circuits.MakeSetup(context.Background(), circuits.EmulationCircuitID, ccsEmulation, circuits.NewUnsafeSRSProvider(), nil)
assert.NoError(t, err)
var aggregationPiBn254 frBn254.Element
aggregationPiBn254.SetBytes(aggregationPIBytes)
for k := range bw6Proofs {
logrus.Infof("Generating the proof for the emulation circuit (BW6 Proof #%v)", k)
_, err = emulation.MakeProof(&setupEmulation, k, bw6Proofs[k], frBn254.NewElement(10))
_, err = emulation.MakeProof(&setupEmulation, k, bw6Proofs[k], aggregationPiBn254)
assert.NoError(t, err)
}
}
func getPiInterconnectionSetup(srsProvider circuits.SRSProvider, numCircuits int) piInterconnectionSetup {
var t test_utils.FakeTestingT
const (
typeExec = pi_interconnection.Execution
typeDecomp = pi_interconnection.Decompression
)
circuit, err := pi_interconnection.Compile(config.PublicInput{
MaxNbDecompression: numCircuits,
MaxNbExecution: numCircuits,
MaxNbKeccakF: numCircuits * 10,
ExecutionMaxNbMsg: 1,
L2MsgMerkleDepth: 1,
L2MsgMaxNbMerkle: 1,
}, dummyWizard.Compile)
assert.NoError(t, err)
func pow5(s []frBls.Element) []frBls.Element {
res := make([]frBls.Element, len(s))
for i := range s {
res[i].
Mul(&s[i], &s[i]).
Mul(&res[i], &res[i]).
Mul(&res[i], &s[i])
var (
cs constraint.ConstraintSystem
csCompilation sync.Mutex
)
csCompilation.Lock()
go func() {
var err error
cs, err = frontend.Compile(ecc.BLS12_377.ScalarField(), scs.NewBuilder, circuit.Circuit) // always compiling the circuit in case it has changed
csCompilation.Unlock()
assert.NoError(t, err)
}()
cfg := config.Config{
AssetsDir: "integration/circuit-testing/aggregation/.assets",
}
logrus.Infof("Loading PI setup")
setup, err := circuits.LoadSetup(&cfg, circuits.PublicInputInterconnectionCircuitID)
logrus.Infof("Compiling PI circuit")
csCompilation.Lock()
if err != nil || !reflect.DeepEqual(cs, setup.Circuit) { // TODO make sure this is a good way to compare cs's
fmt.Println("commitment indexes", cs.GetCommitments().CommitmentIndexes())
logrus.Infof("Loading failed. Creating PI setup")
setup, err = circuits.MakeSetup(context.TODO(), circuits.PublicInputInterconnectionCircuitID, cs, srsProvider, nil)
assert.NoError(t, err)
logrus.Infof("Saving PI setup")
/* assert.NoError(t, setup.WriteTo(cfg.PathForSetup(string(circuits.PublicInputInterconnectionCircuitID)))) No use storing the setup unless we can store SRS, TODO fix
logrus.Infof("Copying SRS from gnark cache") TODO Store SRS. Currently we're failing to load every time
dst, err := os.Create(cfg.PathForSRS())
assert.NoError(t, err)
defer assert.NoError(t, dst.Close())
homeDir, err := os.UserHomeDir()
assert.NoError(t, err)
src, err := os.Open(filepath.Join(homeDir, "."+"gnark", "kzg"))
assert.NoError(t, err)
defer assert.NoError(t, src.Close())
_, err = io.Copy(dst, src)
assert.NoError(t, err)*/
}
return piInterconnectionSetup{
c: circuit,
Setup: setup,
}
}
type piInterconnectionSetup struct {
circuits.Setup
c *pi_interconnection.Compiled
return res
}

View File

@@ -1,7 +1,10 @@
package test_utils
import (
"crypto/rand"
"encoding/binary"
"fmt"
"math"
"os"
)
@@ -14,3 +17,23 @@ func (FakeTestingT) Errorf(format string, args ...interface{}) {
func (FakeTestingT) FailNow() {
os.Exit(-1)
}
func RandIntN(n int) int {
var b [8]byte
_, err := rand.Read(b[:])
if err != nil {
panic(err)
}
if n > math.MaxInt {
panic("RandIntN: n too large")
}
return int(binary.BigEndian.Uint64(b[:]) % uint64(n)) // #nosec G115 -- Above check precludes an overflow
}
func RandIntSliceN(length, n int) []int {
res := make([]int, length)
for i := range res {
res[i] = RandIntN(n)
}
return res
}

View File

@@ -4,12 +4,13 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/consensys/gnark/frontend"
"golang.org/x/exp/constraints"
"io"
"math"
"math/big"
"reflect"
"github.com/consensys/gnark/frontend"
"golang.org/x/exp/constraints"
)
/*
@@ -266,3 +267,27 @@ func Partition[T any, I constraints.Integer](s []T, index []I) [][]T {
}
return partitions
}
func Ite[T any](cond bool, ifSo, ifNot T) T {
if cond {
return ifSo
}
return ifNot
}
func RangeSlice[T constraints.Integer](length int, startingPoints ...T) []T {
if len(startingPoints) == 0 {
startingPoints = []T{0}
}
res := make([]T, length*len(startingPoints))
for i := range startingPoints {
FillRange(res[i*length:(i+1)*length], startingPoints[i])
}
return res
}
func FillRange[T constraints.Integer](dst []T, start T) {
for l := range dst {
dst[l] = T(l) + start
}
}