From e0485220a46094df613d0cbd57e4efe124eafafa Mon Sep 17 00:00:00 2001 From: Arya Tabaie <15056835+Tabaie@users.noreply.github.com> Date: Mon, 3 Feb 2025 12:44:32 -0600 Subject: [PATCH] build: generate unit tests for all groth16 mpc --- .../groth16/bls12-377/mpcsetup/setup_test.go | 274 +++++++++++++++++ .../groth16/bls12-381/mpcsetup/setup_test.go | 274 +++++++++++++++++ .../groth16/bls24-315/mpcsetup/setup_test.go | 274 +++++++++++++++++ .../groth16/bls24-317/mpcsetup/setup_test.go | 274 +++++++++++++++++ backend/groth16/bn254/mpcsetup/setup_test.go | 272 +++++++++++++++++ backend/groth16/bn254/mpcsetup/unit_test.go | 286 ------------------ .../groth16/bw6-633/mpcsetup/setup_test.go | 274 +++++++++++++++++ .../groth16/bw6-761/mpcsetup/setup_test.go | 274 +++++++++++++++++ .../backend/template/imports.go.tmpl | 4 +- .../groth16/mpcsetup/setup_test.go.tmpl | 282 ++++++++++++++++- 10 files changed, 2199 insertions(+), 289 deletions(-) delete mode 100644 backend/groth16/bn254/mpcsetup/unit_test.go diff --git a/backend/groth16/bls12-377/mpcsetup/setup_test.go b/backend/groth16/bls12-377/mpcsetup/setup_test.go index 1a3094059c..c292f050c2 100644 --- a/backend/groth16/bls12-377/mpcsetup/setup_test.go +++ b/backend/groth16/bls12-377/mpcsetup/setup_test.go @@ -7,18 +7,24 @@ package mpcsetup import ( "bytes" + "fmt" "github.com/consensys/gnark-crypto/ecc" curve "github.com/consensys/gnark-crypto/ecc/bls12-377" "github.com/consensys/gnark-crypto/ecc/bls12-377/fr" + groth16Impl "github.com/consensys/gnark/backend/groth16/bls12-377" cs "github.com/consensys/gnark/constraint/bls12-377" "io" + "math/big" "slices" "sync" "testing" "github.com/consensys/gnark/backend/groth16" + "github.com/consensys/gnark/constraint" "github.com/consensys/gnark/frontend" "github.com/consensys/gnark/frontend/cs/r1cs" + "github.com/consensys/gnark/internal/utils/test_utils" + gnarkio "github.com/consensys/gnark/io" "github.com/consensys/gnark/std/hash/mimc" "github.com/stretchr/testify/require" @@ -212,3 +218,271 @@ func proveVerifyCircuit(t *testing.T, pk groth16.ProvingKey, vk groth16.Verifyin err = groth16.Verify(proof, vk, pubWitness) require.NoError(t, err) } + +// unit tests: normally skipped for all curves except BN254 so as not to slow down the CI + +// TestSetupBeaconOnly tests the setup/key extraction +// as well as the random beacon contribution +// without any untrusted contributors +func TestSetupBeaconOnly(t *testing.T) { + + // Compile the circuit + ccs := getTestCircuit() + domainSize := ecc.NextPowerOfTwo(uint64(ccs.GetNbConstraints())) + + var ( + p1 Phase1 + p2 Phase2 + ) + p1.Initialize(domainSize) + commons := p1.Seal([]byte("beacon 1")) + + evals := p2.Initialize(ccs, &commons) + pk, vk := p2.Seal(&commons, &evals, []byte("beacon 2")) + + _pk := pk.(*groth16Impl.ProvingKey) + + rpk, rvk, err := groth16.Setup(ccs) + require.NoError(t, err) + _rpk := rpk.(*groth16Impl.ProvingKey) + + // assert everything is of the same size + require.Equal(t, _rpk.Domain.Cardinality, _pk.Domain.Cardinality) + require.Equal(t, len(_rpk.G1.A), len(_pk.G1.A)) + require.Equal(t, len(_rpk.G1.B), len(_pk.G1.B)) + require.Equal(t, len(_rpk.G1.K), len(_pk.G1.K)) + require.Equal(t, len(_rpk.G1.Z), len(_pk.G1.Z)) + require.Equal(t, len(_rpk.G2.B), len(_pk.G2.B)) + require.Equal(t, len(_rpk.CommitmentKeys), len(_pk.CommitmentKeys)) + for i := range _rpk.CommitmentKeys { + require.Equal(t, len(_rpk.CommitmentKeys[i].BasisExpSigma), len(_pk.CommitmentKeys[i].BasisExpSigma)) + require.Equal(t, len(_rpk.CommitmentKeys[i].Basis), len(_pk.CommitmentKeys[i].Basis)) + } + + proveVerifyCircuit(t, rpk, rvk) + fmt.Println("regular proof verified") + proveVerifyCircuit(t, pk, vk) + fmt.Println("mpc proof verified") +} + +// TestNoContributors tests the beacon and some of the serialization +func TestNoContributors(t *testing.T) { + testAll(t, 0, 0) +} + +func TestOnePhase1Contribute(t *testing.T) { + testAll(t, 2, 0) +} + +func commonsSmallValues(N, tau, alpha, beta uint64) SrsCommons { + var ( + res SrsCommons + I big.Int + coeff fr.Element + ) + _, _, g1, g2 := curve.Generators() + tauPowers := powersI(tau, int(2*N-1)) + res.G1.Tau = make([]curve.G1Affine, 2*N-1) + for i := range res.G1.Tau { + tauPowers[i].BigInt(&I) + res.G1.Tau[i].ScalarMultiplication(&g1, &I) + } + + res.G2.Tau = make([]curve.G2Affine, N) + for i := range res.G2.Tau { + tauPowers[i].BigInt(&I) + res.G2.Tau[i].ScalarMultiplication(&g2, &I) + } + + res.G1.AlphaTau = make([]curve.G1Affine, N) + coeff.SetUint64(alpha) + for i := range res.G1.AlphaTau { + var x fr.Element + x.Mul(&tauPowers[i], &coeff) + x.BigInt(&I) + res.G1.AlphaTau[i].ScalarMultiplication(&g1, &I) + } + + res.G1.BetaTau = make([]curve.G1Affine, N) + coeff.SetUint64(beta) + for i := range res.G1.BetaTau { + var x fr.Element + x.Mul(&tauPowers[i], &coeff) + x.BigInt(&I) + res.G1.BetaTau[i].ScalarMultiplication(&g1, &I) + } + + I.SetUint64(beta) + res.G2.Beta.ScalarMultiplication(&g2, &I) + + return res +} + +func powersI(x uint64, n int) []fr.Element { + var y fr.Element + y.SetUint64(x) + return powers(&y, n) +} + +func TestPowers(t *testing.T) { + var x fr.Element + x.SetUint64(2) + x2 := powers(&x, 10) + for i := range x2 { + require.True(t, x2[i].IsUint64()) + require.Equal(t, x2[i].Uint64(), uint64(1<