-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement_test.go
More file actions
121 lines (104 loc) · 3.44 KB
/
Copy pathstatement_test.go
File metadata and controls
121 lines (104 loc) · 3.44 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright Consensys Software Inc.
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
package loom_test
import (
"testing"
"github.com/consensys/gnark-crypto/field/koalabear"
"github.com/consensys/loom"
"github.com/consensys/loom/board"
"github.com/consensys/loom/expr"
"github.com/consensys/loom/prover"
"github.com/consensys/loom/trace"
"github.com/consensys/loom/verifier"
)
func TestStatementWitnessProveVerify(t *testing.T) {
statement, witness := equalityStatementWitness(t)
prf, err := loom.Prove(statement, witness, prover.SkipFRI())
if err != nil {
t.Fatal(err)
}
if err := loom.Verify(statement, prf, verifier.SkipFRI()); err != nil {
t.Fatal(err)
}
}
func TestStatementWitnessProveVerifySHA256HashBackend(t *testing.T) {
statement, witness := equalityStatementWitness(t)
backend := loom.SHA256HashBackend()
prf, err := loom.Prove(statement, witness, loom.WithProverHashBackend(backend), prover.SkipFRI())
if err != nil {
t.Fatal(err)
}
if got, want := prf.HashBackendID, backend.ID; got != want {
t.Fatalf("proof hash backend = %q, want %q", got, want)
}
if err := loom.Verify(statement, prf, loom.WithVerifierHashBackend(backend), verifier.SkipFRI()); err != nil {
t.Fatal(err)
}
if err := loom.Verify(statement, prf, verifier.SkipFRI()); err == nil {
t.Fatal("expected verifier hash backend mismatch")
}
}
func TestSetupHashBackendIsUsedByProveAndVerify(t *testing.T) {
statement, witness := equalityStatementWitness(t)
builder := board.NewBuilder()
module := board.NewModule("main")
module.N = 4
module.AssertZero(expr.Setup("A").Sub(expr.Col("B")))
builder.AddModule(module)
program, err := board.Compile(&builder)
if err != nil {
t.Fatal(err)
}
statement.Program = program
backend := loom.SHA256HashBackend()
pk, vk, err := loom.Setup(witness.Trace, program, loom.WithSetupHashBackend(backend))
if err != nil {
t.Fatal(err)
}
statement.VerificationKey = vk
witness.ProvingKey = pk
prf, err := loom.Prove(statement, witness, prover.SkipFRI())
if err != nil {
t.Fatal(err)
}
if got, want := prf.HashBackendID, backend.ID; got != want {
t.Fatalf("proof hash backend = %q, want %q", got, want)
}
if err := loom.Verify(statement, prf, verifier.SkipFRI()); err != nil {
t.Fatal(err)
}
}
func equalityStatementWitness(t *testing.T) (loom.Statement, loom.Witness) {
t.Helper()
builder := board.NewBuilder()
module := board.NewModule("main")
module.N = 4
module.AssertZero(expr.Col("A").Sub(expr.Col("B")))
builder.AddModule(module)
program, err := board.Compile(&builder)
if err != nil {
t.Fatal(err)
}
values := make([]koalabear.Element, module.N)
for i := range values {
values[i].SetUint64(uint64(i + 1))
}
valuesCopy := make([]koalabear.Element, len(values))
copy(valuesCopy, values)
tr := trace.New()
tr.SetBase("A", values)
tr.SetBase("B", valuesCopy)
statement := loom.Statement{Program: program}
witness := loom.Witness{Trace: tr}
return statement, witness
}