-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
290 lines (252 loc) · 8.96 KB
/
main.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// scriptless-script-dlc-demo project main.go
package main
import (
"fmt"
"crypto/rand"
"math/big"
"github.com/btcsuite/btcd/btcec"
)
func main() {
// DLC
fmt.Println("DLC")
fmt.Println()
// Alice public and private key is
// Pa = xaG
xa, Pa := KeyGen()
// Bob public and private key is
// Pb = xbG
xb, Pb := KeyGen()
fmt.Printf("Alice public key : Pa %x\n", Pa.SerializeCompressed())
fmt.Printf("Bob public key : Pb %x\n", Pb.SerializeCompressed())
// Fund
// Alice and Bob each send 1 BTC to Fund.
// Fund address is P .
// c = Hash(Pa || Pb)
c := Hash(Pa.SerializeCompressed(), Pb.SerializeCompressed())
// μa = Hash(c || 0x01)
mua := Hash(c.Bytes(), []byte{0x01})
// μb = Hash(c || 0x02)
mub := Hash(c.Bytes(), []byte{0x02})
// P = μaPa + μbPb
P := SumPubs(MulPub(mua, Pa), MulPub(mub, Pb))
fmt.Println("P = μaPa + μbPb")
fmt.Printf("Fund public key : P %x\n", P.SerializeCompressed())
fmt.Println()
// Oracle
// Olivia(Oracle) announces m] or my after n days.
// Olivia(Oracle) public and private key is
// Po = xoG
xo, Po := KeyGen()
// Contract key for after n days is
// Rn = knG
kn, Rn := KeyGen()
// Message is
// m : {mx , my}
mx := []byte("x")
my := []byte("y")
// Olivia publish Po, Rn and m : {mx , my} .
fmt.Printf("Oracle public key : Po %x\n", Po.SerializeCompressed())
fmt.Printf("Contract key : Rn %x\n", Rn.SerializeCompressed())
fmt.Printf("Message mx : %x\n", mx)
fmt.Printf("Message my : %x\n", my)
fmt.Println()
// Contract
// Alice and Bob contract.
// If Olivia announces mx, then Alice get 1.5 BTC and Bob get 0.5 BTC.
// If Olivia announces my, then Alice get 0.5 BTC and Bob get 1.5 BTC.
// transaction
// Make all transactions.
// This case is two.
tmpl := "Input [0]: Fund\nOutput[0]: A -> %.1f BTC\nOutput[1]: B -> %.1f BTC"
// This transaction is tx1.
// Input [0]: Fund
// Output[0]: A -> 1.5 BTC
// Output[1]: B -> 0.5 BTC
tx1 := []byte(fmt.Sprintf(tmpl, 1.5, 0.5))
// This transaction is tx2.
// Input [0]: Fund
// Output[0]: A -> 0.5 BTC
// Output[1]: B -> 1.5 BTC
tx2 := []byte(fmt.Sprintf(tmpl, 0.5, 1.5))
fmt.Println("Contract")
fmt.Printf("- tx1 -\n%s\n", tx1)
fmt.Printf("- tx2 -\n%s\n", tx2)
fmt.Println()
// random point
// Alice and Bob make random points for the number of transactions.
// This case is two. ( tx1 and tx2 )
fmt.Println("Step1")
fmt.Println("Alice creates random points and send hash value to Bob.")
// Rax = raxG
rax, Rax := KeyGen()
// Ray = rayG
ray, Ray := KeyGen()
// hRa = Hash(Rax || Ray)
hRa := Hash(Rax.SerializeCompressed(), Ray.SerializeCompressed())
fmt.Printf("Rax : %x\n", Rax.SerializeCompressed())
fmt.Printf("Ray : %x\n", Ray.SerializeCompressed())
fmt.Printf("Alice -- hRa --> Bob : %x\n", hRa)
fmt.Println()
// Bob creates random points and send hash value to Alice.
fmt.Println("Bob creates random points and send hash value to Alice.")
// Rbx = rbxG
rbx, Rbx := KeyGen()
// Rby = rbyG
rby, Rby := KeyGen()
// hRb = Hash(Rbx || Rby)
hRb := Hash(Rbx.SerializeCompressed(), Rby.SerializeCompressed())
fmt.Printf("Rbx : %x\n", Rbx.SerializeCompressed())
fmt.Printf("Rby : %x\n", Rby.SerializeCompressed())
fmt.Printf("Bob -- hRb --> Alice : %x\n", hRb)
fmt.Println()
fmt.Println("Step2")
fmt.Println("Alice sends random points to Bob.")
fmt.Printf("Alice -- Rax --> Bob : %x\n", Rax.SerializeCompressed())
fmt.Printf("Alice -- Ray --> Bob : %x\n", Ray.SerializeCompressed())
fmt.Println()
fmt.Println("Bob sends random points to Alice.")
fmt.Printf("Bob -- Rbx --> Alice : %x\n", Rbx.SerializeCompressed())
fmt.Printf("Bob -- Rby --> Alice : %x\n", Rby.SerializeCompressed())
fmt.Println()
fmt.Println("Step3")
fmt.Println("Alice checks if the hash value is equal to the random points.")
fmt.Printf("hRb =? Hash(Rbx || Rby) : %v\n", hRb.Cmp(Hash(Rbx.SerializeCompressed(), Rby.SerializeCompressed())) == 0)
fmt.Println()
fmt.Println("Bob checks if the hash value is equal to the random points.")
fmt.Printf("hRa =? Hash(Rax || Ray) : %v\n", hRa.Cmp(Hash(Rax.SerializeCompressed(), Ray.SerializeCompressed())) == 0)
fmt.Println()
fmt.Println("Alice and Bob agree Rax , Ray , Rbx and Rby.")
fmt.Println()
fmt.Println("contract point")
// Alice and Bob compute
// Cx = Rn - Hash(Rn || mx)Po
Cx := MsgKey(Rn, Po, mx)
// Cy = Rn - Hash(Rn || my)Po
Cy := MsgKey(Rn, Po, my)
fmt.Printf("Cx = Rn - Hash(Rn || mx)Po : %x\n", Cx.SerializeCompressed())
fmt.Printf("Cy = Rn - Hash(Rn || my)Po : %x\n", Cy.SerializeCompressed())
fmt.Println()
fmt.Println("pre sign")
// Alice computes
// sax = rax + Hash((Rax+Rbx+Cx) || P || tx1)μaxa
sax := Sign(rax, SumPubs(Rax, Rbx, Cx), P, tx1, Muls(mua, xa))
// say = ray + Hash((Ray+Rby+Cy) || P || tx2)μaxa
say := Sign(ray, SumPubs(Ray, Rby, Cy), P, tx2, Muls(mua, xa))
// Alice sends sax and say to Bob.
fmt.Printf("Alice -- sax --> Bob : %x\n", sax)
fmt.Printf("Alice -- say --> Bob : %x\n", say)
fmt.Println()
// Bob computes
// sbx = rbx + Hash((Rax+Rbx+Cx) || P || tx1)μbxb
sbx := Sign(rbx, SumPubs(Rax, Rbx, Cx), P, tx1, Muls(mub, xb))
// sby = rby + Hash((Ray+Rby+Cy) || P || tx2)μbxb
sby := Sign(rby, SumPubs(Ray, Rby, Cy), P, tx2, Muls(mub, xb))
// Bob sends sbx and sby to Alice.
fmt.Printf("Bob -- sbx --> Alice : %x\n", sbx)
fmt.Printf("Bob -- sby --> Alice : %x\n", sby)
fmt.Println()
// Alice checks
fmt.Println("Alice checks")
var left *btcec.PublicKey
var right *btcec.PublicKey
var ha *big.Int
// sbxG =? Rbx + Hash((Rax+Rbx+Cx) || P || tx1)μbPb
left = new(btcec.PublicKey)
left.X, left.Y = btcec.S256().ScalarBaseMult(sbx.Bytes())
ha = Hash(SumPubs(Rax, Rbx, Cx).SerializeCompressed(), P.SerializeCompressed(), tx1)
right = SumPubs(Rbx, MulPub(ha, MulPub(mub, Pb)))
fmt.Printf("sbxG =? Rbx + Hash((Rax+Rbx+Cx) || P || tx1)μbPb : %v\n", IsEqualBs(left.SerializeCompressed(), right.SerializeCompressed()))
// sbyG =? Rby + Hash((Rax+Rbx+Cx) || P || tx2)μbPb
left = new(btcec.PublicKey)
left.X, left.Y = btcec.S256().ScalarBaseMult(sby.Bytes())
ha = Hash(SumPubs(Ray, Rby, Cy).SerializeCompressed(), P.SerializeCompressed(), tx2)
right = SumPubs(Rby, MulPub(Muls(ha, mub), Pb))
fmt.Printf("sbyG =? Rby + Hash((Ray+Rby+Cy) || P || tx2)μbPb : %v\n", IsEqualBs(left.SerializeCompressed(), right.SerializeCompressed()))
// Bob checks
fmt.Println("Bob checks")
// saxG =? Rax + Hash((Rax+Rbx+Cx) || P || tx1)μaPa
left = new(btcec.PublicKey)
left.X, left.Y = btcec.S256().ScalarBaseMult(sax.Bytes())
ha = Hash(SumPubs(Rax, Rbx, Cx).SerializeCompressed(), P.SerializeCompressed(), tx1)
right = SumPubs(Rax, MulPub(ha, MulPub(mua, Pa)))
fmt.Printf("saxG =? Rax + Hash((Rax+Rbx+Cx) || P || tx1)μaPa : %v\n", IsEqualBs(left.SerializeCompressed(), right.SerializeCompressed()))
// sayG =? Ray + Hash((Rax+Rbx+Cx) || P || tx1)μaPa
left = new(btcec.PublicKey)
left.X, left.Y = btcec.S256().ScalarBaseMult(say.Bytes())
ha = Hash(SumPubs(Ray, Rby, Cy).SerializeCompressed(), P.SerializeCompressed(), tx2)
right = SumPubs(Ray, MulPub(Muls(ha, mua), Pa))
fmt.Printf("sayG =? Ray + Hash((Rax+Rbx+Cx) || P || tx2)μaPa : %v\n", IsEqualBs(left.SerializeCompressed(), right.SerializeCompressed()))
// N days ago
// Olivia computes
// sox = kn - Hash(Rn || mx)xo
sox := Commit(kn, Rn, mx, xo)
// Olivia publish sox and mx.
fmt.Println("Olivia publish sox and mx.")
fmt.Printf("message mx : %x\n", mx)
fmt.Printf("commit sox : %x\n", sox)
fmt.Println()
// Alice or Bob compute
// s = sax + sbx + sox
// R = Rax + Rbx + Cx
s := new(big.Int).Mod(new(big.Int).Add(new(big.Int).Add(sax, sbx), sox), btcec.S256().N)
R := SumPubs(Rax, Rbx, Cx)
fmt.Println("Alice or Bob send Transaction tx1 with (s,R).")
fmt.Printf("(s,R)=(%x,%x)\n", s, R.SerializeCompressed())
fmt.Println()
fmt.Println("Verify")
fmt.Println("sG =? R + H(R,P,tx1)P")
err := Verify(s, R, P, tx1)
if err != nil {
fmt.Printf("Fail : %+v\n", err)
return
}
fmt.Println("Success!")
}
// KeyGen returns a private/public key pair.
func KeyGen() (*big.Int, *btcec.PublicKey) {
x, _ := rand.Int(rand.Reader, btcec.S256().N)
pri, pub := btcec.PrivKeyFromBytes(btcec.S256(), x.Bytes())
return pri.D, pub
}
// SumPubs sum public keys.
func SumPubs(Pubs ...*btcec.PublicKey) *btcec.PublicKey {
S := new(btcec.PublicKey)
for i, P := range Pubs {
if i == 0 {
S.X, S.Y = P.X, P.Y
} else {
S.X, S.Y = btcec.S256().Add(S.X, S.Y, P.X, P.Y)
}
}
return S
}
// MulPub returns the multiplication of the public key.
func MulPub(x *big.Int, P *btcec.PublicKey) *btcec.PublicKey {
M := new(btcec.PublicKey)
M.X, M.Y = btcec.S256().ScalarMult(P.X, P.Y, x.Bytes())
return M
}
// Muls returns all multiplication mod N.
func Muls(bis ...*big.Int) *big.Int {
var m *big.Int
for _, bi := range bis {
if m == nil {
m = new(big.Int).SetBytes(bi.Bytes())
continue
}
m = new(big.Int).Mod(new(big.Int).Mul(m, bi), btcec.S256().N)
}
return m
}
// IsEqualBs returns true if they match.
func IsEqualBs(bs1, bs2 []byte) bool {
if len(bs1) != len(bs2) {
return false
}
for i, b := range bs1 {
if b != bs2[i] {
return false
}
}
return true
}