Skip to content

Commit c83a650

Browse files
authored
Merge pull request #501 from dedis/kilian-add-benchmark
Add some benchmarks
2 parents e1d90cf + ca0da5a commit c83a650

File tree

4 files changed

+148
-3
lines changed

4 files changed

+148
-3
lines changed

encrypt/ecies/ecies_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package ecies
22

33
import (
4+
"crypto/rand"
45
"testing"
56

67
"github.com/stretchr/testify/require"
8+
"go.dedis.ch/kyber/v3"
9+
"go.dedis.ch/kyber/v3/group/curve25519"
710
"go.dedis.ch/kyber/v3/group/edwards25519"
11+
"go.dedis.ch/kyber/v3/group/nist"
812
"go.dedis.ch/kyber/v3/util/random"
913
)
1014

@@ -44,3 +48,37 @@ func TestECIESFailCiphertext(t *testing.T) {
4448
_, err = Decrypt(suite, private, ciphertext, nil)
4549
require.NotNil(t, err)
4650
}
51+
52+
func BenchmarkECIES(b *testing.B) {
53+
suites := []struct {
54+
kyber.Group
55+
}{
56+
{edwards25519.NewBlakeSHA256Ed25519()},
57+
{curve25519.NewBlakeSHA256Curve25519(false)},
58+
{curve25519.NewBlakeSHA256Curve25519(true)},
59+
{nist.NewBlakeSHA256P256()},
60+
{nist.NewBlakeSHA256QR512()},
61+
}
62+
63+
message := make([]byte, 100_000)
64+
_, _ = rand.Read(message)
65+
rand := random.New()
66+
67+
for _, suite := range suites {
68+
private := suite.Scalar().Pick(rand)
69+
public := suite.Point().Mul(private, nil)
70+
71+
var ct []byte
72+
b.Run("Encrypt/"+suite.String(), func(b *testing.B) {
73+
for i := 0; i < b.N; i++ {
74+
ct, _ = Encrypt(suite, public, message, nil)
75+
}
76+
})
77+
78+
b.Run("Decrypt/"+suite.String(), func(b *testing.B) {
79+
for i := 0; i < b.N; i++ {
80+
_, _ = Decrypt(suite, private, ct, nil)
81+
}
82+
})
83+
}
84+
}

pairing/bn256/suite_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,43 @@ func TestNegPairAll(t *testing.T) {
406406
require.True(t, pair2.Equal(pair3))
407407
require.True(t, pair3.Equal(pair4))
408408
}
409+
410+
func BenchmarkBn256(b *testing.B) {
411+
suite := NewSuite()
412+
c := suite.G1().Scalar().Pick(random.New())
413+
d := suite.G1().Scalar().Pick(random.New())
414+
e := suite.G2().Scalar()
415+
416+
p1 := newPointG1()
417+
p2 := newPointG2()
418+
419+
b.Run("Add", func(b *testing.B) {
420+
for i := 0; i < b.N; i++ {
421+
e.Add(c, d)
422+
}
423+
})
424+
425+
b.Run("Sub", func(b *testing.B) {
426+
for i := 0; i < b.N; i++ {
427+
e.Sub(c, d)
428+
}
429+
})
430+
431+
b.Run("Mul", func(b *testing.B) {
432+
for i := 0; i < b.N; i++ {
433+
e.Mul(c, d)
434+
}
435+
})
436+
437+
b.Run("Div", func(b *testing.B) {
438+
for i := 0; i < b.N; i++ {
439+
e.Div(c, d)
440+
}
441+
})
442+
443+
b.Run("Pairing", func(b *testing.B) {
444+
for i := 0; i < b.N; i++ {
445+
suite.Pair(p1, p2)
446+
}
447+
})
448+
}

proof/proof.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Suite interface {
2525
/*
2626
A Predicate is a composable logic expression in a knowledge proof system,
2727
representing a "knowledge specification set" in Camenisch/Stadler terminology.
28-
Atomic predicates in this system are statements of the form P=x1*B1+...+xn+Bn,
28+
Atomic predicates in this system are statements of the form P=x1*B1+...+xn*Bn,
2929
indicating the prover knows secrets x1,...,xn that make the statement true,
3030
where P and B1,...,Bn are public points known to the verifier.
3131
These atomic Rep (representation) predicates may be combined
@@ -157,7 +157,6 @@ type repPred struct {
157157
// A Rep statement of the form Rep(P,x1,B1,...,xn,Bn)
158158
// indicates that the prover knows secrets x1,...,xn
159159
// such that point P is the sum x1*B1+...+xn*Bn.
160-
//
161160
func Rep(P string, SB ...string) Predicate {
162161
if len(SB)&1 != 0 {
163162
panic("mismatched Scalar")

proof/proof_test.go

+69-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package proof
33
import (
44
"encoding/hex"
55
"fmt"
6+
"strconv"
67
"testing"
78

89
"go.dedis.ch/kyber/v3"
10+
"go.dedis.ch/kyber/v3/group/curve25519"
911
"go.dedis.ch/kyber/v3/group/edwards25519"
12+
"go.dedis.ch/kyber/v3/group/nist"
1013
"go.dedis.ch/kyber/v3/xof/blake2xb"
1114
)
1215

@@ -140,7 +143,6 @@ func Example_rep2() {
140143
// If the prover does know the relationship between B1 and B2, however,
141144
// then X does not serve as a useful commitment:
142145
// the prover can trivially compute the x1 corresponding to an arbitrary x2.
143-
//
144146
func Example_rep3() {
145147
pred := Rep("X", "x1", "B1", "x2", "B2")
146148
fmt.Println(pred.String())
@@ -246,3 +248,69 @@ func Example_or2() {
246248
// 000000b0 4d 97 a9 bf 1a 28 27 6d 3b 71 04 e1 c0 86 96 08 |M....('m;q......|
247249
// Proof verified.
248250
}
251+
252+
func BenchmarkProof(b *testing.B) {
253+
rand := blake2xb.New([]byte("random"))
254+
predicateSize := 100
255+
suites := []struct {
256+
Suite
257+
}{
258+
{edwards25519.NewBlakeSHA256Ed25519()},
259+
{curve25519.NewBlakeSHA256Curve25519(false)},
260+
{curve25519.NewBlakeSHA256Curve25519(true)},
261+
{nist.NewBlakeSHA256P256()},
262+
{nist.NewBlakeSHA256QR512()},
263+
}
264+
265+
for _, suite := range suites {
266+
P := suite.Point().Null()
267+
268+
sval := map[string]kyber.Scalar{}
269+
pval := map[string]kyber.Point{}
270+
predicateBuilder := make([]string, 0)
271+
272+
for i := 0; i < predicateSize; i++ {
273+
s := suite.Scalar().Pick(rand)
274+
index := strconv.Itoa(i)
275+
276+
publicPoint := suite.Point().Mul(s, nil)
277+
278+
sval["x"+index] = s
279+
predicateBuilder = append(predicateBuilder, "x"+index)
280+
predicateBuilder = append(predicateBuilder, "B"+index)
281+
pval["B"+index] = suite.Point().Base()
282+
283+
P = suite.Point().Add(P, publicPoint)
284+
}
285+
286+
pval["P"] = P
287+
288+
var proof []byte
289+
var err error
290+
var pred Predicate
291+
292+
b.Run(suite.String()+"/ProofBuild", func(b *testing.B) {
293+
for i := 0; i < b.N; i++ {
294+
pred = Rep("P", predicateBuilder...)
295+
// Prove P = x0*B + x1*B + ... + xN*B
296+
prover := pred.Prover(suite, sval, pval, nil)
297+
proof, err = HashProve(suite, "TEST", prover)
298+
if err != nil {
299+
b.Log(err.Error())
300+
b.Fail()
301+
}
302+
}
303+
})
304+
305+
b.Run(suite.String()+"/ProofVerify", func(b *testing.B) {
306+
for i := 0; i < b.N; i++ {
307+
verifier := pred.Verifier(suite, pval)
308+
err = HashVerify(suite, "TEST", verifier, proof)
309+
if err != nil {
310+
b.Log(err.Error())
311+
b.Fail()
312+
}
313+
}
314+
})
315+
}
316+
}

0 commit comments

Comments
 (0)