Skip to content

Commit

Permalink
Merge branch 'master' into matteo-fuzz-testing1
Browse files Browse the repository at this point in the history
  • Loading branch information
matteosz authored Mar 19, 2024
2 parents f55531d + c83a650 commit 8490a2b
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 3 deletions.
38 changes: 38 additions & 0 deletions encrypt/ecies/ecies_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package ecies

import (
"crypto/rand"
"testing"

"github.com/stretchr/testify/require"
"go.dedis.ch/kyber/v3"
"go.dedis.ch/kyber/v3/group/curve25519"
"go.dedis.ch/kyber/v3/group/edwards25519"
"go.dedis.ch/kyber/v3/group/nist"
"go.dedis.ch/kyber/v3/util/random"
)

Expand Down Expand Up @@ -44,3 +48,37 @@ func TestECIESFailCiphertext(t *testing.T) {
_, err = Decrypt(suite, private, ciphertext, nil)
require.NotNil(t, err)
}

func BenchmarkECIES(b *testing.B) {
suites := []struct {
kyber.Group
}{
{edwards25519.NewBlakeSHA256Ed25519()},
{curve25519.NewBlakeSHA256Curve25519(false)},
{curve25519.NewBlakeSHA256Curve25519(true)},
{nist.NewBlakeSHA256P256()},
{nist.NewBlakeSHA256QR512()},
}

message := make([]byte, 100_000)
_, _ = rand.Read(message)
rand := random.New()

for _, suite := range suites {
private := suite.Scalar().Pick(rand)
public := suite.Point().Mul(private, nil)

var ct []byte
b.Run("Encrypt/"+suite.String(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
ct, _ = Encrypt(suite, public, message, nil)
}
})

b.Run("Decrypt/"+suite.String(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Decrypt(suite, private, ct, nil)
}
})
}
}
40 changes: 40 additions & 0 deletions pairing/bn256/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,43 @@ func TestNegPairAll(t *testing.T) {
require.True(t, pair2.Equal(pair3))
require.True(t, pair3.Equal(pair4))
}

func BenchmarkBn256(b *testing.B) {
suite := NewSuite()
c := suite.G1().Scalar().Pick(random.New())
d := suite.G1().Scalar().Pick(random.New())
e := suite.G2().Scalar()

p1 := newPointG1()
p2 := newPointG2()

b.Run("Add", func(b *testing.B) {
for i := 0; i < b.N; i++ {
e.Add(c, d)
}
})

b.Run("Sub", func(b *testing.B) {
for i := 0; i < b.N; i++ {
e.Sub(c, d)
}
})

b.Run("Mul", func(b *testing.B) {
for i := 0; i < b.N; i++ {
e.Mul(c, d)
}
})

b.Run("Div", func(b *testing.B) {
for i := 0; i < b.N; i++ {
e.Div(c, d)
}
})

b.Run("Pairing", func(b *testing.B) {
for i := 0; i < b.N; i++ {
suite.Pair(p1, p2)
}
})
}
3 changes: 1 addition & 2 deletions proof/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Suite interface {
/*
A Predicate is a composable logic expression in a knowledge proof system,
representing a "knowledge specification set" in Camenisch/Stadler terminology.
Atomic predicates in this system are statements of the form P=x1*B1+...+xn+Bn,
Atomic predicates in this system are statements of the form P=x1*B1+...+xn*Bn,
indicating the prover knows secrets x1,...,xn that make the statement true,
where P and B1,...,Bn are public points known to the verifier.
These atomic Rep (representation) predicates may be combined
Expand Down Expand Up @@ -157,7 +157,6 @@ type repPred struct {
// A Rep statement of the form Rep(P,x1,B1,...,xn,Bn)
// indicates that the prover knows secrets x1,...,xn
// such that point P is the sum x1*B1+...+xn*Bn.
//
func Rep(P string, SB ...string) Predicate {
if len(SB)&1 != 0 {
panic("mismatched Scalar")
Expand Down
70 changes: 69 additions & 1 deletion proof/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package proof
import (
"encoding/hex"
"fmt"
"strconv"
"testing"

"go.dedis.ch/kyber/v3"
"go.dedis.ch/kyber/v3/group/curve25519"
"go.dedis.ch/kyber/v3/group/edwards25519"
"go.dedis.ch/kyber/v3/group/nist"
"go.dedis.ch/kyber/v3/xof/blake2xb"
)

Expand Down Expand Up @@ -140,7 +143,6 @@ func Example_rep2() {
// If the prover does know the relationship between B1 and B2, however,
// then X does not serve as a useful commitment:
// the prover can trivially compute the x1 corresponding to an arbitrary x2.
//
func Example_rep3() {
pred := Rep("X", "x1", "B1", "x2", "B2")
fmt.Println(pred.String())
Expand Down Expand Up @@ -246,3 +248,69 @@ func Example_or2() {
// 000000b0 4d 97 a9 bf 1a 28 27 6d 3b 71 04 e1 c0 86 96 08 |M....('m;q......|
// Proof verified.
}

func BenchmarkProof(b *testing.B) {
rand := blake2xb.New([]byte("random"))
predicateSize := 100
suites := []struct {
Suite
}{
{edwards25519.NewBlakeSHA256Ed25519()},
{curve25519.NewBlakeSHA256Curve25519(false)},
{curve25519.NewBlakeSHA256Curve25519(true)},
{nist.NewBlakeSHA256P256()},
{nist.NewBlakeSHA256QR512()},
}

for _, suite := range suites {
P := suite.Point().Null()

sval := map[string]kyber.Scalar{}
pval := map[string]kyber.Point{}
predicateBuilder := make([]string, 0)

for i := 0; i < predicateSize; i++ {
s := suite.Scalar().Pick(rand)
index := strconv.Itoa(i)

publicPoint := suite.Point().Mul(s, nil)

sval["x"+index] = s
predicateBuilder = append(predicateBuilder, "x"+index)
predicateBuilder = append(predicateBuilder, "B"+index)
pval["B"+index] = suite.Point().Base()

P = suite.Point().Add(P, publicPoint)
}

pval["P"] = P

var proof []byte
var err error
var pred Predicate

b.Run(suite.String()+"/ProofBuild", func(b *testing.B) {
for i := 0; i < b.N; i++ {
pred = Rep("P", predicateBuilder...)
// Prove P = x0*B + x1*B + ... + xN*B
prover := pred.Prover(suite, sval, pval, nil)
proof, err = HashProve(suite, "TEST", prover)
if err != nil {
b.Log(err.Error())
b.Fail()
}
}
})

b.Run(suite.String()+"/ProofVerify", func(b *testing.B) {
for i := 0; i < b.N; i++ {
verifier := pred.Verifier(suite, pval)
err = HashVerify(suite, "TEST", verifier, proof)
if err != nil {
b.Log(err.Error())
b.Fail()
}
}
})
}
}

0 comments on commit 8490a2b

Please sign in to comment.