From 3449e741972f2b0ac746865a11d9e75379b71b6f Mon Sep 17 00:00:00 2001 From: Thomas Piellard Date: Fri, 5 Jun 2020 15:15:37 +0200 Subject: [PATCH] moved rollup/ into examples/; added MIT license in Neboulous code (merkle trees); fixed integration tests with bigInt r1cs; modified config.yml to not run too long tests in circleCI server; removed the build tags --- .circleci/config.yml | 4 +- backend/assignment_test.backup | 30 -------- backend/bls377/fft_test.go | 2 - backend/bls377/groth16/computeh_test.go | 14 +++- cmd/prove.go | 14 ++-- cmd/setup.go | 16 +++-- {rollup => examples/rollup}/account.go | 0 {rollup => examples/rollup}/account_test.go | 0 {rollup => examples/rollup}/circuit.go | 0 {rollup => examples/rollup}/circuit_test.go | 12 ++++ {rollup => examples/rollup}/error.go | 0 {rollup => examples/rollup}/operator.go | 0 {rollup => examples/rollup}/rollup_test.go | 0 {rollup => examples/rollup}/test_utils.go | 0 {rollup => examples/rollup}/transfer.go | 0 gadgets/accumulator/merkle/verify.go | 28 ++++++++ gadgets/accumulator/merkle/verify_test.go | 68 ------------------- ...tion_test.go.backup => integration_test.go | 11 ++- 18 files changed, 76 insertions(+), 123 deletions(-) delete mode 100644 backend/assignment_test.backup rename {rollup => examples/rollup}/account.go (100%) rename {rollup => examples/rollup}/account_test.go (100%) rename {rollup => examples/rollup}/circuit.go (100%) rename {rollup => examples/rollup}/circuit_test.go (98%) rename {rollup => examples/rollup}/error.go (100%) rename {rollup => examples/rollup}/operator.go (100%) rename {rollup => examples/rollup}/rollup_test.go (100%) rename {rollup => examples/rollup}/test_utils.go (100%) rename {rollup => examples/rollup}/transfer.go (100%) rename integration_test.go.backup => integration_test.go (93%) diff --git a/.circleci/config.yml b/.circleci/config.yml index aa6de99473..3cca4e84a1 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -15,9 +15,7 @@ jobs: - run: go test -v ./cmd/ -run=VersionIsGenerated # ensure version is generated - run: go get golang.org/x/tools/cmd/goimports - run: go generate ./... - - run: go test -v -tags bls377,debug ./... - - run: go test -v -tags bls381,debug ./... - - run: go test -v -tags bn256,debug ./... + - run: go test -v -short -tags debug ./... - save_cache: key: go-mod-v1-{{ checksum "go.sum" }} paths: diff --git a/backend/assignment_test.backup b/backend/assignment_test.backup deleted file mode 100644 index 7188d69d65..0000000000 --- a/backend/assignment_test.backup +++ /dev/null @@ -1,30 +0,0 @@ -package backend - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestDuplicateAssignment(t *testing.T) { - - defer func() { - if r := recover(); r == nil { - t.Fatalf("duplicate assignment will panic.") - } - }() - - a := NewAssignment() - a.Assign(Public, "x", 1) - a.Assign(Secret, "x", 1) -} - -func TestVisibility(t *testing.T) { - assert := require.New(t) - a := NewAssignment() - a.Assign(Public, "x", 1) - a.Assign(Secret, "y", 1) - - assert.True(a["x"].IsPublic) - assert.False(a["y"].IsPublic) -} diff --git a/backend/bls377/fft_test.go b/backend/bls377/fft_test.go index 2227080296..d000a60910 100644 --- a/backend/bls377/fft_test.go +++ b/backend/bls377/fft_test.go @@ -1,5 +1,3 @@ -// +build bls377 !bn256,!bls381 - /* Copyright © 2020 ConsenSys diff --git a/backend/bls377/groth16/computeh_test.go b/backend/bls377/groth16/computeh_test.go index 77c884c2fd..db2d12c224 100644 --- a/backend/bls377/groth16/computeh_test.go +++ b/backend/bls377/groth16/computeh_test.go @@ -1,4 +1,16 @@ -// +build bls377 !bn256,!bls381 +// Copyright 2020 ConsenSys AG +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. package groth16 diff --git a/cmd/prove.go b/cmd/prove.go index 5313535115..9d2bf7a487 100644 --- a/cmd/prove.go +++ b/cmd/prove.go @@ -30,6 +30,7 @@ import ( backend_bn256 "github.com/consensys/gnark/backend/bn256" groth16_bn256 "github.com/consensys/gnark/backend/bn256/groth16" "github.com/consensys/gnark/encoding/gob" + "github.com/consensys/gnark/frontend" "github.com/consensys/gurvy" "github.com/spf13/cobra" ) @@ -104,13 +105,14 @@ func cmdProve(cmd *cobra.Command, args []string) { os.Exit(-1) } // TODO clean that up with interfaces and type casts + var bigIntR1cs frontend.R1CS switch curveID { case gurvy.BLS377: - var r1cs backend_bls377.R1CS - if err := gob.Read(circuitPath, &r1cs, curveID); err != nil { + if err := gob.Read(circuitPath, &bigIntR1cs, curveID); err != nil { fmt.Println("error:", err) os.Exit(-1) } + r1cs := backend_bls377.Cast(&bigIntR1cs) fmt.Printf("%-30s %-30s %-d constraints\n", "loaded circuit", circuitPath, r1cs.NbConstraints) // run setup var pk groth16_bls377.ProvingKey @@ -158,11 +160,11 @@ func cmdProve(cmd *cobra.Command, args []string) { fmt.Printf("%-30s %-30s %-30s\n", "generated proof", proofPath, duration) case gurvy.BLS381: - var r1cs backend_bls381.R1CS - if err := gob.Read(circuitPath, &r1cs, curveID); err != nil { + if err := gob.Read(circuitPath, &bigIntR1cs, curveID); err != nil { fmt.Println("error:", err) os.Exit(-1) } + r1cs := backend_bls381.Cast(&bigIntR1cs) fmt.Printf("%-30s %-30s %-d constraints\n", "loaded circuit", circuitPath, r1cs.NbConstraints) // run setup var pk groth16_bls381.ProvingKey @@ -210,11 +212,11 @@ func cmdProve(cmd *cobra.Command, args []string) { fmt.Printf("%-30s %-30s %-30s\n", "generated proof", proofPath, duration) case gurvy.BN256: - var r1cs backend_bn256.R1CS - if err := gob.Read(circuitPath, &r1cs, curveID); err != nil { + if err := gob.Read(circuitPath, &bigIntR1cs, curveID); err != nil { fmt.Println("error:", err) os.Exit(-1) } + r1cs := backend_bn256.Cast(&bigIntR1cs) fmt.Printf("%-30s %-30s %-d constraints\n", "loaded circuit", circuitPath, r1cs.NbConstraints) // run setup var pk groth16_bn256.ProvingKey diff --git a/cmd/setup.go b/cmd/setup.go index 52110b57ef..9c6eece129 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -29,6 +29,7 @@ import ( backend_bn256 "github.com/consensys/gnark/backend/bn256" groth16_bn256 "github.com/consensys/gnark/backend/bn256/groth16" "github.com/consensys/gnark/encoding/gob" + "github.com/consensys/gnark/frontend" "github.com/consensys/gurvy" "github.com/spf13/cobra" ) @@ -79,20 +80,21 @@ func cmdSetup(cmd *cobra.Command, args []string) { os.Exit(-1) } - // check curve ID + // check curve ID (TODO is curve.ID necessary now? Because the circuits are serialized with big.Int, here the curve.ID is "unknown") curveID, err := gob.PeekCurveID(circuitPath) if err != nil { fmt.Println("error:", err) os.Exit(-1) } // TODO clean that up with interfaces and type casts + var bigIntR1cs frontend.R1CS switch curveID { case gurvy.BLS377: - var r1cs backend_bls377.R1CS - if err := gob.Read(circuitPath, &r1cs, curveID); err != nil { + if err := gob.Read(circuitPath, &bigIntR1cs, curveID); err != nil { fmt.Println("error:", err) os.Exit(-1) } + r1cs := backend_bls377.Cast(&bigIntR1cs) fmt.Printf("%-30s %-30s %-d constraints\n", "loaded circuit", circuitPath, r1cs.NbConstraints) // run setup var pk groth16_bls377.ProvingKey @@ -113,11 +115,11 @@ func cmdSetup(cmd *cobra.Command, args []string) { } fmt.Printf("%-30s %s\n", "generated proving key", pkPath) case gurvy.BLS381: - var r1cs backend_bls381.R1CS - if err := gob.Read(circuitPath, &r1cs, curveID); err != nil { + if err := gob.Read(circuitPath, &bigIntR1cs, curveID); err != nil { fmt.Println("error:", err) os.Exit(-1) } + r1cs := backend_bls381.Cast(&bigIntR1cs) fmt.Printf("%-30s %-30s %-d constraints\n", "loaded circuit", circuitPath, r1cs.NbConstraints) // run setup var pk groth16_bls381.ProvingKey @@ -138,11 +140,11 @@ func cmdSetup(cmd *cobra.Command, args []string) { } fmt.Printf("%-30s %s\n", "generated proving key", pkPath) case gurvy.BN256: - var r1cs backend_bn256.R1CS - if err := gob.Read(circuitPath, &r1cs, curveID); err != nil { + if err := gob.Read(circuitPath, &bigIntR1cs, curveID); err != nil { fmt.Println("error:", err) os.Exit(-1) } + r1cs := backend_bn256.Cast(&bigIntR1cs) fmt.Printf("%-30s %-30s %-d constraints\n", "loaded circuit", circuitPath, r1cs.NbConstraints) // run setup var pk groth16_bn256.ProvingKey diff --git a/rollup/account.go b/examples/rollup/account.go similarity index 100% rename from rollup/account.go rename to examples/rollup/account.go diff --git a/rollup/account_test.go b/examples/rollup/account_test.go similarity index 100% rename from rollup/account_test.go rename to examples/rollup/account_test.go diff --git a/rollup/circuit.go b/examples/rollup/circuit.go similarity index 100% rename from rollup/circuit.go rename to examples/rollup/circuit.go diff --git a/rollup/circuit_test.go b/examples/rollup/circuit_test.go similarity index 98% rename from rollup/circuit_test.go rename to examples/rollup/circuit_test.go index 4ddd0abad7..7829e71e4b 100644 --- a/rollup/circuit_test.go +++ b/examples/rollup/circuit_test.go @@ -140,6 +140,10 @@ func TestCircuitSignature(t *testing.T) { func TestCircuitInclusionProof(t *testing.T) { + if testing.Short() { + t.Skip("skipping rollup tests for circleCI") + } + notInInpuList := " is not in the input list" // 16 accounts so we know that the proof length is 5 @@ -262,6 +266,10 @@ func TestCircuitInclusionProof(t *testing.T) { func TestCircuitUpdateAccount(t *testing.T) { + if testing.Short() { + t.Skip("skipping rollup tests for circleCI") + } + notInInpuList := " is not in the input list" // 16 accounts so we know that the proof length is 5 @@ -375,6 +383,10 @@ func TestCircuitUpdateAccount(t *testing.T) { func TestCircuitFull(t *testing.T) { + if testing.Short() { + t.Skip("skipping rollup tests for circleCI") + } + nbAccounts := 16 // 16 accounts so we know that the proof length is 5 depth := 5 // size fo the inclusion proofs batchSize := 1 // nbTranfers to batch in a proof diff --git a/rollup/error.go b/examples/rollup/error.go similarity index 100% rename from rollup/error.go rename to examples/rollup/error.go diff --git a/rollup/operator.go b/examples/rollup/operator.go similarity index 100% rename from rollup/operator.go rename to examples/rollup/operator.go diff --git a/rollup/rollup_test.go b/examples/rollup/rollup_test.go similarity index 100% rename from rollup/rollup_test.go rename to examples/rollup/rollup_test.go diff --git a/rollup/test_utils.go b/examples/rollup/test_utils.go similarity index 100% rename from rollup/test_utils.go rename to examples/rollup/test_utils.go diff --git a/rollup/transfer.go b/examples/rollup/transfer.go similarity index 100% rename from rollup/transfer.go rename to examples/rollup/transfer.go diff --git a/gadgets/accumulator/merkle/verify.go b/gadgets/accumulator/merkle/verify.go index 33bcb755cc..b41c6cf789 100644 --- a/gadgets/accumulator/merkle/verify.go +++ b/gadgets/accumulator/merkle/verify.go @@ -1,3 +1,31 @@ +/* + Original Copyright 2015 https://gitlab.com/NebulousLabs +*/ + +/* +The MIT License (MIT) + +Copyright (c) 2015 Nebulous + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + /* Copyright © 2020 ConsenSys diff --git a/gadgets/accumulator/merkle/verify_test.go b/gadgets/accumulator/merkle/verify_test.go index 3a01e40bdd..9edbd1a0a6 100644 --- a/gadgets/accumulator/merkle/verify_test.go +++ b/gadgets/accumulator/merkle/verify_test.go @@ -32,74 +32,6 @@ import ( "github.com/consensys/gurvy/bn256/fr" ) -// func TestLeaf(t *testing.T) { - -// // computation of expected result -// leafPrefix := []byte{0x00} -// var leaf fr.Element -// leaf.SetRandom() -// h := bn256.NewMiMC("seed") -// h.Write(leafPrefix) -// h.Write(leaf.Bytes()) -// binExpectedRes := h.Sum([]byte{}) - -// var tmp fr.Element -// tmp.SetBytes(binExpectedRes) -// expectedRes := make(map[string]fr.Element) -// expectedRes["res"] = tmp - -// // computation of leafNode using gadget -// circuit := frontend.New() -// hg, err := mimc.NewMiMCGadget("seed", gurvy.BN256) -// if err != nil { -// t.Fatal(err) -// } -// res := leafSum(&circuit, hg, circuit.SECRET_INPUT("leaf")) -// res.Tag("res") -// assignment := backend_common.NewAssignment() -// assignment.Assign(backend_common.Secret, "leaf", leaf) - -// r1cs := backend_bn256.New(&circuit) - -// assert := groth16.NewAssert(t) -// assert.CorrectExecution(&r1cs, assignment, expectedRes) -// } - -// func TestNode(t *testing.T) { - -// // computation of expected result -// nodePrefix := []byte{0x01} -// var node1, node2 fr.Element -// node1.SetRandom() -// node2.SetRandom() -// h := bn256.NewMiMC("seed") -// h.Write(nodePrefix) -// h.Write(node1.Bytes()) -// h.Write(node2.Bytes()) -// binExpectedRes := h.Sum([]byte{}) -// var tmp fr.Element -// tmp.SetBytes(binExpectedRes) -// expectedRes := make(map[string]fr.Element) -// expectedRes["res"] = tmp - -// // computation of leafNode using gadget -// circuit := frontend.New() -// hg, err := mimc.NewMiMCGadget("seed", gurvy.BN256) -// if err != nil { -// t.Fatal(err) -// } -// res := nodeSum(&circuit, hg, circuit.SECRET_INPUT("node1"), circuit.SECRET_INPUT("node2")) -// res.Tag("res") -// assignment := backend_common.NewAssignment() -// assignment.Assign(backend_common.Secret, "node1", node1) -// assignment.Assign(backend_common.Secret, "node2", node2) - -// r1cs := backend_bn256.New(&circuit) - -// assert := groth16.NewAssert(t) -// assert.CorrectExecution(&r1cs, assignment, expectedRes) -// } - func TestVerify(t *testing.T) { // generate random data diff --git a/integration_test.go.backup b/integration_test.go similarity index 93% rename from integration_test.go.backup rename to integration_test.go index d12e72dcdb..f15d83fdb1 100644 --- a/integration_test.go.backup +++ b/integration_test.go @@ -23,13 +23,14 @@ import ( "testing" "github.com/consensys/gnark/backend" + "github.com/consensys/gnark/encoding/gob" "github.com/consensys/gnark/internal/generators/testcircuits/circuits" - "github.com/consensys/gnark/internal/utils/encoding/gob" + "github.com/consensys/gurvy" ) func TestIntegration(t *testing.T) { if testing.Short() { - t.Skip("skipping integration tests") + t.Skip("skipping integration tests for circleCI") } // create temporary dir for integration test parentDir := "./integration_test" @@ -50,7 +51,7 @@ func TestIntegration(t *testing.T) { fInputGood := filepath.Join(parentDir, name+".good.input") fInputBad := filepath.Join(parentDir, name+".bad.input") - buildTags := curve.ID.String() + ",debug" + buildTags := "debug" // 2: input files to disk if err := good.WriteFile(fInputGood); err != nil { t.Fatal(err) @@ -106,11 +107,9 @@ func TestIntegration(t *testing.T) { } // serialize to disk fCircuit := filepath.Join(parentDir, name+".r1cs") - if err := gob.Write(fCircuit, circuit.R1CS, curve.ID); err != nil { + if err := gob.Write(fCircuit, circuit.R1CS, gurvy.BN256); err != nil { t.Fatal(err) } - spv(name, circuit.Good, circuit.Bad) - } }