forked from cloudflare/circl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dleq_test.go
159 lines (132 loc) · 4.57 KB
/
dleq_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package dleq_test
import (
"bytes"
"crypto"
"crypto/rand"
"fmt"
"testing"
"github.com/cloudflare/circl/group"
"github.com/cloudflare/circl/internal/test"
"github.com/cloudflare/circl/zk/dleq"
)
func TestDLEQ(t *testing.T) {
for _, g := range []group.Group{
group.P256,
group.P384,
group.P521,
group.Ristretto255,
} {
t.Run(g.(fmt.Stringer).String(), func(t *testing.T) {
params := dleq.Params{g, crypto.SHA256, []byte("domain_sep_string")}
Peggy := dleq.Prover{params}
Victor := dleq.Verifier{params}
k := g.RandomScalar(rand.Reader)
A := g.RandomElement(rand.Reader)
kA := g.NewElement().Mul(A, k)
B := g.RandomElement(rand.Reader)
kB := g.NewElement().Mul(B, k)
proof, err := Peggy.Prove(k, A, kA, B, kB, rand.Reader)
test.CheckNoErr(t, err, "wrong proof generation")
test.CheckOk(Victor.Verify(A, kA, B, kB, proof), "proof must verify", t)
rr := g.RandomScalar(rand.Reader)
proof, err = Peggy.ProveWithRandomness(k, A, kA, B, kB, rr)
test.CheckNoErr(t, err, "wrong proof generation")
test.CheckOk(Victor.Verify(A, kA, B, kB, proof), "proof must verify", t)
const N = 4
C := make([]group.Element, N)
kC := make([]group.Element, N)
for i := 0; i < N; i++ {
C[i] = g.RandomElement(rand.Reader)
kC[i] = g.NewElement().Mul(C[i], k)
}
proof, err = Peggy.ProveBatch(k, A, kA, C, kC, rand.Reader)
test.CheckNoErr(t, err, "wrong proof generation")
test.CheckOk(Victor.VerifyBatch(A, kA, C, kC, proof), "proof must verify", t)
testMarshal(t, g, proof)
testErrors(t, &Peggy, &Victor, g, k, A, kA, B, kB)
})
}
}
func testMarshal(t *testing.T, g group.Group, proof *dleq.Proof) {
t.Helper()
wantProofBytes, err := proof.MarshalBinary()
test.CheckNoErr(t, err, "error on marshaling proof")
gotProof := new(dleq.Proof)
err = gotProof.UnmarshalBinary(g, wantProofBytes)
test.CheckNoErr(t, err, "error on unmarshaling proof")
gotProofBytes, err := gotProof.MarshalBinary()
test.CheckNoErr(t, err, "error on marshaling proof")
if !bytes.Equal(gotProofBytes, wantProofBytes) {
test.ReportError(t, gotProofBytes, wantProofBytes)
}
}
func testErrors(
t *testing.T,
Peggy *dleq.Prover,
Victor *dleq.Verifier,
g group.Group,
k group.Scalar, a, ka, b, kb group.Element,
) {
goodProof, err := Peggy.Prove(k, a, ka, b, kb, rand.Reader)
test.CheckNoErr(t, err, "wrong proof generation")
proofBytes, err := goodProof.MarshalBinary()
test.CheckNoErr(t, err, "error on marshaling proof")
// Tamper proof (in transit)
_, _ = rand.Read(proofBytes)
tamperedProof := new(dleq.Proof)
err = tamperedProof.UnmarshalBinary(g, proofBytes[:5])
test.CheckIsErr(t, err, "unmarshal must fail")
err = tamperedProof.UnmarshalBinary(g, proofBytes)
test.CheckNoErr(t, err, "proof must be unmarshaled")
test.CheckOk(false == Victor.Verify(a, ka, b, kb, tamperedProof), "proof must not verify", t)
// Tamper elements
bada := g.NewElement().Neg(a)
test.CheckOk(false == Victor.Verify(bada, ka, b, kb, goodProof), "proof must not verify", t)
badka := g.NewElement().Neg(ka)
test.CheckOk(false == Victor.Verify(a, badka, b, kb, goodProof), "proof must not verify", t)
badb := g.NewElement().Neg(b)
test.CheckOk(false == Victor.Verify(a, ka, badb, kb, goodProof), "proof must not verify", t)
badkb := g.NewElement().Neg(kb)
test.CheckOk(false == Victor.Verify(a, ka, b, badkb, goodProof), "proof must not verify", t)
}
func BenchmarkDLEQ(b *testing.B) {
g := group.P256
params := dleq.Params{g, crypto.SHA256, []byte("domain_sep_string")}
Peggy := dleq.Prover{params}
Victor := dleq.Verifier{params}
k := g.RandomScalar(rand.Reader)
A := g.Generator()
kA := g.NewElement().MulGen(k)
B := g.RandomElement(rand.Reader)
kB := g.NewElement().Mul(B, k)
rr := g.RandomScalar(rand.Reader)
proof, _ := Peggy.ProveWithRandomness(k, A, kA, B, kB, rr)
const N = 4
C := make([]group.Element, N)
kC := make([]group.Element, N)
for i := 0; i < N; i++ {
C[i] = g.RandomElement(rand.Reader)
kC[i] = g.NewElement().Mul(C[i], k)
}
proofBatched, _ := Peggy.ProveBatchWithRandomness(k, A, kA, C, kC, rr)
b.Run("Prove", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Peggy.ProveWithRandomness(k, A, kA, B, kB, rr)
}
})
b.Run("Verify", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Victor.Verify(A, kA, B, kB, proof)
}
})
b.Run(fmt.Sprint("ProveBatch=", N), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Peggy.ProveBatchWithRandomness(k, A, kA, C, kC, rr)
}
})
b.Run(fmt.Sprint("VerifyBatch=", N), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Victor.VerifyBatch(A, kA, C, kC, proofBatched)
}
})
}