-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnizk_test.go
113 lines (102 loc) · 2.5 KB
/
nizk_test.go
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
package nizk_test
import (
"crypto/rand"
"io"
"testing"
"github.com/eliastor/go-nizk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func makeMsgStamp() (msg, stamp []byte) {
msg = make([]byte, 4096)
io.ReadFull(rand.Reader, msg)
stamp = make([]byte, 1024)
io.ReadFull(rand.Reader, stamp)
return msg, stamp
}
func makeProove(t *testing.T, msg, stamp []byte, zk nizk.Nizk) ([]byte, []byte) {
var err error
fingerprint1 := zk.Fingerprint(msg)
proof, fingerprint2, err := zk.Proove(msg, stamp)
require.NoError(t, err)
assert.Equal(t, fingerprint1, fingerprint2)
return proof, fingerprint2
}
func testSuccessFlow(t *testing.T, id, stamp, proof []byte, zk nizk.Nizk) {
t.Run("SuccessFlow", func(t *testing.T) {
valid, err := zk.Verify(id, stamp, proof)
require.NoError(t, err)
assert.True(t, valid)
})
}
func testFailFlow(t *testing.T, id, stamp []byte, proof []byte, zk nizk.Nizk) {
t.Run("FailFlow", func(t *testing.T) {
proof[0] ^= 0xFF
valid, _ := zk.Verify(id, stamp, proof)
// require.NoError(t, err) there can be the error with invalid point encoding
assert.False(t, valid)
})
}
func testFlows(t *testing.T, zk nizk.Nizk) {
msg, stamp := makeMsgStamp()
proof, id := makeProove(t, msg, stamp, zk)
testSuccessFlow(t, id, stamp, proof, zk)
testFailFlow(t, id, stamp, proof, zk)
}
func TestAllSuites(t *testing.T) {
suites := []struct {
Name string
Creator func() nizk.Nizk
}{
{"ed25519_sha3", nizk.NewEd25519Sha3},
{"ed25519_sha256", nizk.NewEd25519Sha256},
}
for _, suite := range suites {
name := suite.Name
zk := suite.Creator()
t.Run(name, func(t *testing.T) {
testFlows(t, zk)
})
}
}
func benchmarkSuite(b *testing.B, zk nizk.Nizk) {
msg, stamp := makeMsgStamp()
id := zk.Fingerprint(msg)
var proof nizk.Proof
var err error
b.Run("Proove", func(b *testing.B) {
for i := 0; i < b.N; i++ {
proof, _, err = zk.Proove(msg, stamp)
if err != nil {
b.Fatal(err)
}
}
})
b.Run("Verify", func(b *testing.B) {
for i := 0; i < b.N; i++ {
valid, err := zk.Verify(id, stamp, proof)
if err != nil {
b.Fatal(err)
}
if !valid {
b.Fatal("failed verification")
}
}
})
}
func BenchmarkAllSuites(b *testing.B) {
suites := []struct {
Name string
Creator func() nizk.Nizk
}{
{"ed25519_sha3", nizk.NewEd25519Sha3},
{"ed25519_sha256", nizk.NewEd25519Sha256},
}
for _, suite := range suites {
name := suite.Name
zk := suite.Creator()
b.Run(name, func(b *testing.B) {
benchmarkSuite(b, zk)
})
}
}