-
Notifications
You must be signed in to change notification settings - Fork 44
/
urs_test.go
105 lines (94 loc) · 2.22 KB
/
urs_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
// Copyright 2014 Hein Meling and Haibin Zhang. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package urs
import (
"crypto/elliptic"
crand "crypto/rand"
"fmt"
"math/rand"
"runtime"
"testing"
)
const numOfKeys = 1000
var (
DefaultCurve = elliptic.P256()
keyring *PublicKeyRing
testkey *PrivateKey
testmsg []byte
testsig *RingSign
)
func TestGenerateKey(t *testing.T) {
runtime.GOMAXPROCS(4)
var err error
testkey, err = GenerateKey(DefaultCurve, crand.Reader)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
}
func TestNewPublicKeyRing(t *testing.T) {
keyring = NewPublicKeyRing(1)
keyring.Add(testkey.PublicKey)
expectedLen := 1
if len(keyring.Ring) != expectedLen {
t.Errorf("len(keyring)=%d, expected %d", len(keyring.Ring), expectedLen)
}
}
func TestPopulateKeyRing(t *testing.T) {
keyring = NewPublicKeyRing(numOfKeys)
rand.Seed(23)
k := rand.Intn(numOfKeys)
fmt.Println("Index of my key: ", k)
for i := 0; i < numOfKeys; i++ {
key, err := GenerateKey(DefaultCurve, crand.Reader)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
if i == k { // designate this as my key
testkey = key
}
// add the public key part to the ring
keyring.Add(key.PublicKey)
}
if len(keyring.Ring) != numOfKeys {
t.Errorf("len(keyring)=%d, expected %d", len(keyring.Ring), numOfKeys)
}
}
func TestSign(t *testing.T) {
testmsg = []byte("Hello, world.")
var err error
testsig, err = Sign(crand.Reader, testkey, keyring, testmsg)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
// fmt.Printf("%s\n", testsig)
}
func TestVerify(t *testing.T) {
if !Verify(keyring, testmsg, testsig) {
fmt.Println("urs: signature verification failed")
t.FailNow()
}
}
func BenchmarkSign(b *testing.B) {
runtime.GOMAXPROCS(8)
var err error
for i := 0; i < b.N; i++ {
testsig, err = Sign(crand.Reader, testkey, keyring, testmsg)
if err != nil {
fmt.Println(err.Error())
b.FailNow()
}
}
}
func BenchmarkVerify(b *testing.B) {
runtime.GOMAXPROCS(8)
for i := 0; i < b.N; i++ {
if !Verify(keyring, testmsg, testsig) {
fmt.Println("urs: signature verification failed")
b.FailNow()
}
}
}