-
Notifications
You must be signed in to change notification settings - Fork 6
/
blind_test.go
100 lines (82 loc) · 2.2 KB
/
blind_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
package blinding
import (
"crypto/rsa"
"testing"
"fmt"
)
func TestIntegration(t *testing.T) {
// Set up the signer
signer, _ := NewSigner()
// Set up the employee
salary := []byte("a living wage")
employee, _ := NewEmployee(signer.PublicKey)
// add employees to signer
signer.AddEmployees([]rsa.PublicKey{*employee.PublicKey})
// employee blinds the salary
blindedmessage, err := employee.BlindSalary(salary)
if err != nil {
t.Fatal(err)
}
// employee sends it to the signer
// signer signs the blinded salary
blindsig, err := signer.SignSalary(blindedmessage)
if err != nil {
t.Fatal(err)
}
// signer returns the signature to the employee
// employee unblinds the signature and checks it against her original salary
sig, err := employee.Unblind(blindsig)
if err != nil {
t.Fatal(err)
}
// employee posts the salary and sign somewhere annonymously
// someone checks the salaries signature
err = employee.VerifySallary(salary, sig, signer.PublicKey)
if err != nil {
t.Fatal(err)
}
}
// Creates a signer, some employees, and registers them
func setup(nemployees int) (*Signer, []*Employee) {
signer, _ := NewSigner()
employees := make([]*Employee, nemployees)
keys := make([]rsa.PublicKey, nemployees)
for i := 0; i < nemployees; i++ {
e, _ := NewEmployee(signer.PublicKey)
employees[i] = e
keys[i] = *e.PublicKey
}
return signer, employees
}
func TestOneSigPerEmployee(t *testing.T) {
// test employee can't get two sigs
signer, employees := setup(1)
employee := employees[0]
bmsg, _ := employee.BlindSalary([]byte("message one"))
// try to sign twice
signer.SignSalary(bmsg)
_, err := signer.SignSalary(bmsg)
if err == nil {
t.Fatal()
}
}
func TestOnlyRegisteredEmployees(t *testing.T) {
// test employee can't get two sigs
signer, _ := setup(0)
employee, _ := NewEmployee(signer.PublicKey)
bmsg, _ := employee.BlindSalary([]byte("message one"))
// try to sign while not registered
_, err := signer.SignSalary(bmsg)
if err == nil {
t.Fatal()
}
}
func TestEmployeeCanOnlyBlindOnce(t *testing.T) {
_, employees := setup(1)
employee := employees[0]
employee.BlindSalary([]byte("once"))
_, err = employee.BlindSalary([]byte("twice"))
if err == nil {
t.Fatal()
}
}