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
|
package nizk
import (
"testing"
. "kesim.org/seal/common"
)
func TestStage2Simple1(t *testing.T) {
id := Curve.RandomScalar()
for _, lost := range []bool{true, false} {
b1 := NewBit(id, !lost)
c1 := b1.StageCommit()
r1, _ := b1.RevealStage1(c1.X)
// Because the first index is a junction, any subsequent
// combination of Bits must verify with 'lost' set to true
// in the RevealStage2 calls.
for _, s := range [][2]bool{
{false, false},
{true, false},
{false, true},
{true, true},
} {
b2 := NewBit(id, s[0])
b3 := NewBit(id, s[1])
b4 := NewBit(id, s[1]) // same as b3
c2 := b2.StageCommit()
c3 := b3.StageCommit()
c4 := b4.StageCommit()
r2, p2 := b2.RevealStage2(lost, b1, c1.X, c2.X, c3.X)
if !b2.Commitment.VerifyStage2(c1, c2, r1, r2, p2) {
t.Fatalf("failed to verify b2: %t b3: %t bc2/b1", s[0], s[1])
}
r3, p3 := b3.RevealStage2(lost, b1, c1.X, c2.X, c3.X)
if !b3.Commitment.VerifyStage2(c1, c3, r1, r3, p3) {
t.Fatalf("failed to verify b1: %t b3: %t bc3/b1", s[0], s[1])
}
r4, p4 := b4.RevealStage2(lost, b1, c1.X, c2.X, c3.X, c4.X)
if !b4.Commitment.VerifyStage2(c1, c4, r1, r4, p4) {
t.Fatalf("failed to verify b1: %t b4: %t bc4/b1", s[0], s[1])
}
}
}
}
func uint2bits(bid int) [4]*Bit {
id := Curve.RandomScalar()
return [4]*Bit{
NewBit(id, (bid>>3)&1 != 0),
NewBit(id, (bid>>2)&1 != 0),
NewBit(id, (bid>>1)&1 != 0),
NewBit(id, (bid>>0)&1 != 0),
}
}
var Id = Curve.Identity()
func TestStage2Complex(t *testing.T) {
bid1 := 0b0101
bid2 := 0b0010
t.Logf("testing bid1: %04b vs. bid2: %04b", bid1, bid2)
bits1 := uint2bits(bid1)
bits2 := uint2bits(bid2)
lost1 := false
lost2 := false
if len(bits1) != len(bits2) || len(bits1) != 4 {
t.Fatalf("oops")
}
instage1 := true
junction := -1
result := 0
for c := 0; c < len(bits1); c++ {
b1 := bits1[c]
b2 := bits2[c]
c1 := b1.StageCommit()
c2 := b2.StageCommit()
if instage1 {
t.Logf("Testing bit b1[%d] = %t vs b2[%d] = %t", c, b1.IsSet(), c, b2.IsSet())
r1, p1 := b1.RevealStage1(c1.X, c2.X)
r2, p2 := b2.RevealStage1(c1.X, c2.X)
if !b1.Commitment.VerifyStage1(c1, r1, p1) {
t.Fatalf("b1 commitment failed to verify in stage1")
}
if !b2.Commitment.VerifyStage1(c2, r2, p2) {
t.Fatalf("b2 commitment failed to verify in stage1")
}
Z := Curve.Product(r1.Z, r2.Z)
if !Id.Equal(Z) {
t.Logf("Aha! Z[%d] != Id, switch to stage2", c)
junction = c
instage1 = false
if !lost1 && !b1.IsSet() {
t.Logf("setting lost1 to true")
lost1 = true
}
if !lost2 && !b2.IsSet() {
t.Logf("setting lost2 to true")
lost2 = true
}
result |= 1 << (3 - c)
} else {
t.Logf("Z[%d] == Id, staying in stage1", c)
}
} else {
t.Logf("Testing bit b1[%d]∧lost1 = %t vs b2[%d]∧lost2 = %t", c, b1.IsSet() && !lost1, c, b2.IsSet() && !lost2)
bj1 := bits1[junction]
bj2 := bits2[junction]
r1, p1 := b1.RevealStage2(lost1, bj1, c1.X, c2.X)
r2, p2 := b2.RevealStage2(lost2, bj2, c1.X, c2.X)
if !b1.Commitment.VerifyStage2(bj1.StageCommitment, c1, bj1.StageReveal, r1, p1) {
t.Fatalf("b1 commitment failed to verify in stage1")
}
if !b2.Commitment.VerifyStage2(bj2.StageCommitment, c2, bj2.StageReveal, r2, p2) {
t.Fatalf("b2 commitment failed to verify in stage1")
}
Z := Curve.Product(r1.Z, r2.Z)
if !Id.Equal(Z) {
t.Logf("Aha! Z[%d] != Id, new junction!", c)
junction = c
if !lost1 && !b1.IsSet() {
t.Logf("setting lost1 to true")
lost1 = true
}
if !lost2 && !b2.IsSet() {
t.Logf("setting lost2 to true")
lost2 = true
}
result |= 1 << (3 - c)
}
}
}
if result != bid1 {
t.Fatalf("wrong result: %04b", result)
}
}
func TestFromPaper(t *testing.T) {
bid1 := 0b01010
bid2 := 0b01001
bid3 := 0b00111
t.Logf("testing\n\tbits1: %04b\n\tbits2: %04b\n\tbits3: %04b", bid1, bid2, bid3)
}
|