/
audit_test.go
193 lines (178 loc) · 4.99 KB
/
audit_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package zkledger
import (
"fmt"
"math/big"
"testing"
"time"
)
func TestSimpleSumAudit(t *testing.T) {
error := false
s := SetupTest(2, 1, 500)
defer FinishTest(s)
if len(s.L.Transactions) != 3 {
t.Errorf("Ledger Process() didn't work")
error = true
}
bank_val, _ := s.B[1].answerSum()
audit_val, x := s.A.computeSum(1)
if x != true {
t.Fatalf("Auditing did not work out! Bank:%v Auditor:%v", bank_val, audit_val)
error = true
}
tmp := big.NewInt(600)
if audit_val.Cmp(tmp) != 0 {
t.Fatalf("Bad audit value, should be 600 auditor: %v\n", audit_val)
}
if audit_val.Cmp(bank_val) != 0 {
t.Fatalf("Mismatched answers to sum for bank 1, should be 600 auditor: %v, bank: %v\n",
audit_val, bank_val)
error = true
}
bank_val, _ = s.B[0].answerSum()
audit_val, x = s.A.computeSum(0)
if x != true {
t.Fatalf("Auditing did not work out! Bank:%v Auditor:%v", bank_val, audit_val)
error = true
}
if audit_val.Cmp(big.NewInt(400)) != 0 {
t.Fatalf("Bad audit value, should be 400 auditor: %v\n", audit_val)
error = true
}
if audit_val.Cmp(bank_val) != 0 {
t.Fatalf("Mismatched answers to sum for bank 0, should be -100 auditor: %v, bank: %v\n",
audit_val, bank_val)
error = true
}
if !error {
fmt.Printf("Passed TestSimpleSumAudit\n")
}
}
func TestAuditWithIssuance(t *testing.T) {
error := false
s := SetupTest(2, 0, 50)
defer FinishTest(s)
v := big.NewInt(50)
//s.B[0].Issue(v)
time.Sleep(20 * time.Millisecond)
bank_val, _ := s.B[0].answerSum()
audit_val, x := s.A.computeSum(0)
if x != true {
t.Fatalf("Auditing did not work out! Bank:%v Auditor:%v", bank_val, audit_val)
error = true
}
if audit_val.Cmp(v) != 0 {
t.Fatalf("Bad audit value, should be 50 auditor: %v\n", audit_val)
}
if audit_val.Cmp(bank_val) != 0 {
t.Fatalf("Mismatched answers to sum for bank 0, should be 50 auditor: %v, bank: %v\n",
audit_val, bank_val)
error = true
}
v = big.NewInt(25)
s.B[0].CreateEncryptedTransaction(1, v)
time.Sleep(20 * time.Millisecond)
bank_val, _ = s.B[0].answerSum()
audit_val, x = s.A.computeSum(0)
if x != true {
t.Fatalf("Auditing did not work out! Bank:%v Auditor:%v", bank_val, audit_val)
error = true
}
if audit_val.Cmp(v) != 0 {
t.Fatalf("Bad audit value, should be 25 auditor: %v\n", audit_val)
}
if audit_val.Cmp(bank_val) != 0 {
t.Fatalf("Mismatched answers to sum for bank 0, should be 25 auditor: %v, bank: %v\n",
audit_val, bank_val)
error = true
}
if !error {
fmt.Printf("Passed TestAuditWithIssuance\n")
}
}
func TestHerfindahl(t *testing.T) {
s := SetupTest(2, 0, 10)
defer FinishTest(s)
hIndex, err := s.A.Herfindahl(true, nil)
if err != nil {
t.Fatalf("Couldn't calculate concentrations some bank didn't verify\n")
}
if hIndex.Cmp(big.NewRat(1, 2)) != 0 {
t.Fatalf("Banks do not have right Herfindahl Index %v\n", hIndex)
}
fmt.Println("Passed TestHerfindahl")
}
func TestBadAudit(t *testing.T) {
s := SetupTest(2, 1, 500)
defer FinishTest(s)
if len(s.L.Transactions) != 3 {
t.Errorf("Ledger Process() didn't work")
}
// Reach in and change the bank's answer
tmp := big.NewInt(2)
var tx *Transaction
for _, v := range s.B[1].transactions {
tx = v
break
}
tx.value.Set(tmp)
s.B[1].ValueCache.Set(tmp)
bank_val, _ := s.B[1].answerSum()
audit_val, x := s.A.computeSum(1)
if x == true {
t.Fatalf("Auditing should have failed %v %v", bank_val, audit_val)
}
if audit_val.Cmp(tmp) != 0 {
t.Fatalf("Bad audit value, should be 2 auditor: %v\n", audit_val)
}
if audit_val.Cmp(bank_val) < 0 {
t.Fatalf("Mismatched answers to sum for bank 1, should be 2 auditor: %v, bank: %v\n",
audit_val, bank_val)
}
fmt.Printf("Passed TestBadAudit\n")
}
func TestThreeBankAudit(t *testing.T) {
nb := 3
ntx := 2
initialAmount := 500
s := SetupTest(nb, ntx, int64(initialAmount))
defer FinishTest(s)
if len(s.L.Transactions) != (ntx + nb) {
t.Errorf("Ledger Process() didn't work")
}
for i := 0; i < nb; i++ {
if len(s.B[i].local_ledger.Transactions) != ntx+nb {
t.Fatalf("Bank %v has not gotten all %v transactions. Has %v", i, nb+ntx, len(s.B[i].local_ledger.Transactions))
}
}
audit_val, x := s.A.computeSum(0)
if x != true {
for i := 0; i < nb; i++ {
s.B[i].print_transactions()
}
t.Fatalf("Auditing failed %v", audit_val)
}
fmt.Printf("Passed TestThreeBankAudit\n")
}
func benchmarkClearSum(rows int, b *testing.B) {
s := SetupTest(10, rows, 500)
defer FinishTest(s)
b.ResetTimer()
for i := 0; i < b.N; i++ {
audit := i % 10
s.A.computeClearSum(audit)
}
}
func BenchmarkClearSum1000x(b *testing.B) { benchmarkClearSum(1000, b) }
func BenchmarkClearSum10000(b *testing.B) { benchmarkClearSum(10000, b) }
func benchmarkEncSum(rows int, b *testing.B) {
s := SetupTest(10, rows, 500)
defer FinishTest(s)
b.ResetTimer()
for i := 0; i < b.N; i++ {
audit := i % 10
s.A.computeSum(audit)
}
}
func BenchmarkEncSum100x(b *testing.B) { benchmarkEncSum(100, b) }
func BenchmarkEncSum1000x(b *testing.B) { benchmarkEncSum(1000, b) }
func xBenchmarkEncSum10000(b *testing.B) { benchmarkEncSum(10000, b) }