github.com/consensys/gnark@v0.11.0/backend/groth16/bn254/commitment_test.go (about) 1 // Copyright 2020 ConsenSys Software Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Code generated by gnark DO NOT EDIT 16 17 package groth16_test 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/consensys/gnark-crypto/ecc" 24 "github.com/consensys/gnark/backend/groth16" 25 "github.com/consensys/gnark/backend/witness" 26 "github.com/consensys/gnark/constraint" 27 "github.com/consensys/gnark/frontend" 28 "github.com/consensys/gnark/frontend/cs/r1cs" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 type singleSecretCommittedCircuit struct { 33 One frontend.Variable 34 } 35 36 func (c *singleSecretCommittedCircuit) Define(api frontend.API) error { 37 api.AssertIsEqual(c.One, 1) 38 commitCompiler, ok := api.Compiler().(frontend.Committer) 39 if !ok { 40 return fmt.Errorf("compiler does not commit") 41 } 42 commit, err := commitCompiler.Commit(c.One) 43 if err != nil { 44 return err 45 } 46 api.AssertIsDifferent(commit, 0) 47 return nil 48 } 49 50 func setup(t *testing.T, circuit frontend.Circuit) (constraint.ConstraintSystem, groth16.ProvingKey, groth16.VerifyingKey) { 51 _r1cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, circuit) 52 assert.NoError(t, err) 53 54 pk, vk, err := groth16.Setup(_r1cs) 55 assert.NoError(t, err) 56 57 return _r1cs, pk, vk 58 } 59 60 func prove(t *testing.T, assignment frontend.Circuit, cs constraint.ConstraintSystem, pk groth16.ProvingKey) (witness.Witness, groth16.Proof) { 61 _witness, err := frontend.NewWitness(assignment, ecc.BN254.ScalarField()) 62 assert.NoError(t, err) 63 64 proof, err := groth16.Prove(cs, pk, _witness) 65 assert.NoError(t, err) 66 67 public, err := _witness.Public() 68 assert.NoError(t, err) 69 return public, proof 70 } 71 72 func test(t *testing.T, circuit frontend.Circuit, assignment frontend.Circuit) { 73 74 _r1cs, pk, vk := setup(t, circuit) 75 76 public, proof := prove(t, assignment, _r1cs, pk) 77 78 assert.NoError(t, groth16.Verify(proof, vk, public)) 79 } 80 81 func TestSingleSecretCommitted(t *testing.T) { 82 circuit := singleSecretCommittedCircuit{} 83 assignment := singleSecretCommittedCircuit{One: 1} 84 test(t, &circuit, &assignment) 85 } 86 87 type noCommitmentCircuit struct { // to see if unadulterated groth16 is still correct 88 One frontend.Variable 89 } 90 91 func (c *noCommitmentCircuit) Define(api frontend.API) error { 92 api.AssertIsEqual(c.One, 1) 93 return nil 94 } 95 96 func TestNoCommitmentCircuit(t *testing.T) { 97 circuit := noCommitmentCircuit{} 98 assignment := noCommitmentCircuit{One: 1} 99 100 test(t, &circuit, &assignment) 101 } 102 103 // Just to see if the A,B,C values are computed correctly 104 type singleSecretFauxCommitmentCircuit struct { 105 One frontend.Variable `gnark:",public"` 106 Commitment frontend.Variable `gnark:",public"` 107 } 108 109 func (c *singleSecretFauxCommitmentCircuit) Define(api frontend.API) error { 110 api.AssertIsEqual(c.One, 1) 111 api.AssertIsDifferent(c.Commitment, 0) 112 return nil 113 } 114 115 func TestSingleSecretFauxCommitmentCircuit(t *testing.T) { 116 test(t, &singleSecretFauxCommitmentCircuit{}, &singleSecretFauxCommitmentCircuit{ 117 One: 1, 118 Commitment: 2, 119 }) 120 } 121 122 type oneSecretOnePublicCommittedCircuit struct { 123 One frontend.Variable 124 Two frontend.Variable `gnark:",public"` 125 } 126 127 func (c *oneSecretOnePublicCommittedCircuit) Define(api frontend.API) error { 128 commitCompiler, ok := api.Compiler().(frontend.Committer) 129 if !ok { 130 return fmt.Errorf("compiler does not commit") 131 } 132 commit, err := commitCompiler.Commit(c.One, c.Two) 133 if err != nil { 134 return err 135 } 136 137 // constrain vars 138 api.AssertIsDifferent(commit, 0) 139 api.AssertIsEqual(c.One, 1) 140 api.AssertIsEqual(c.Two, 2) 141 142 return nil 143 } 144 145 func TestOneSecretOnePublicCommitted(t *testing.T) { 146 test(t, &oneSecretOnePublicCommittedCircuit{}, &oneSecretOnePublicCommittedCircuit{ 147 One: 1, 148 Two: 2, 149 }) 150 } 151 152 type twoSecretCommitmentsCircuit struct { 153 One frontend.Variable 154 Two frontend.Variable 155 } 156 157 func (c *twoSecretCommitmentsCircuit) Define(api frontend.API) error { 158 commitCompiler, ok := api.Compiler().(frontend.Committer) 159 if !ok { 160 return fmt.Errorf("compiler does not commit") 161 } 162 commit1, err := commitCompiler.Commit(c.One) 163 if err != nil { 164 return err 165 } 166 commit2, err := commitCompiler.Commit(c.Two) 167 if err != nil { 168 return err 169 } 170 // constrain vars 171 api.AssertIsDifferent(commit1, 0) 172 api.AssertIsDifferent(commit2, 0) 173 return nil 174 } 175 176 func TestTwoSecretCommitments(t *testing.T) { 177 test(t, &twoSecretCommitmentsCircuit{}, &twoSecretCommitmentsCircuit{ 178 One: 1, 179 Two: 2, 180 }) 181 }