-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
206 lines (182 loc) · 4.63 KB
/
client.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
package main
import (
"crypto"
sign "crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/json"
"flag"
"fmt"
"github.com/kwonalbert/spacemint/block"
"github.com/kwonalbert/spacemint/pos"
"github.com/kwonalbert/spacemint/util"
"golang.org/x/crypto/sha3"
"log"
"math"
"math/big"
//"net"
"net/rpc"
"time"
)
type Client struct {
//client and system params
sk crypto.Signer // signing secretkey
pk crypto.PublicKey // signing pubkey
t time.Duration // how soon we add a block
dist int // how far to look back for challenge
//round
sols chan *block.Block // others' blocks
//pos params
index int64
prover *pos.Prover
verifier *pos.Verifier
commit pos.Commitment
chain *block.BlockChain
clients []*rpc.Client
}
func NewClient(t time.Duration, dist, beta int, index int64, graph string) *Client {
sk, err := sign.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic(err)
}
pk := sk.Public()
pkBytes, err := json.Marshal(pk)
if err != nil {
panic(err)
}
prover := pos.NewProver(pkBytes, index, "Xi", graph)
commit := prover.Init()
verifier := pos.NewVerifier(pkBytes, index, beta, commit.Commit)
c := Client{
sk: sk,
pk: pk,
t: t,
dist: dist,
sols: make(chan *block.Block, 100), // nomially say 100 answers per round..
index: index,
prover: prover,
verifier: verifier,
commit: *commit,
}
return &c
}
func (c *Client) Sign(msg []byte) ([]byte, error) {
return c.sk.Sign(rand.Reader, msg, crypto.SHA3_256)
}
func (c *Client) Mine(challenge []byte) *block.PoS {
nodes := c.verifier.SelectChallenges(challenge)
hashes, parents, proofs, pProofs := c.prover.ProveSpace(nodes)
a := block.Answer{
Size: c.index,
Hashes: hashes,
Parents: parents,
Proofs: proofs,
PProofs: pProofs,
}
p := block.PoS{
Commit: c.commit,
Challenge: challenge,
Answer: a,
Quality: c.Quality(challenge, a),
}
return &p
}
// Compute quality of the answer. Also builds a verifier
// return: quality in float64
func (c *Client) Quality(challenge []byte, a block.Answer) float64 {
nodes := c.verifier.SelectChallenges(challenge)
if !c.verifier.VerifySpace(nodes, a.Hashes, a.Parents, a.Proofs, a.PProofs) {
return -1
}
all := util.Concat(a.Hashes)
answerHash := sha3.Sum256(all)
x := new(big.Float).SetInt(new(big.Int).SetBytes(answerHash[:]))
num, _ := util.Root(x, a.Size).Float64()
den := math.Exp2(float64(1<<8) / float64(a.Size))
return num / den
}
// Generate challenge from older blocks
// return: challenge for next block []byte
func (c *Client) GenerateChallenge() []byte {
var b *block.Block
var err error
if c.chain.LastBlock < c.dist {
b, err = c.chain.Read(c.chain.LastBlock)
} else {
b, err = c.chain.Read(c.chain.LastBlock - (c.dist - 1))
}
if err != nil {
panic(err)
}
bin, err := b.MarshalBinary()
if err != nil {
panic(err)
}
challenge := sha3.Sum256(bin)
return challenge[:]
}
// Runs a round of the protocol
func (c *Client) round() {
challenge := c.GenerateChallenge()
prf := c.Mine(challenge)
send := true
for {
select {
case b := <-c.sols:
// probably can't trust the others in final version..
if b.Hash.Proof.Quality > prf.Quality {
send = false
break
}
default:
break
}
}
if send {
old, err := c.chain.Read(c.chain.LastBlock)
if err != nil {
panic(err)
}
// TODO: where do transactions come from??
b := block.NewBlock(old, *prf, nil, c.sk)
for _, r := range c.clients {
err := r.Call("Client.SendBlock", b, nil)
if err != nil {
panic(err)
}
}
}
}
func main() {
idx := flag.Int("index", 1, "graph index")
name := flag.String("name", "Xi", "graph name")
dir := flag.String("file", "/media/storage/Xi", "graph location")
mode := flag.String("mode", "gen", "mode:[gen|commit]")
flag.Parse()
pk := []byte{1}
beta := 30
now := time.Now()
prover := pos.NewProver(pk, int64(*idx), *name, *dir)
if *mode == "gen" {
fmt.Printf("%d. Graph gen: %fs\n", *idx, time.Since(now).Seconds())
} else if *mode == "commit" {
now = time.Now()
prover.Init()
fmt.Printf("%d. Graph commit: %fs\n", *idx, time.Since(now).Seconds())
} else if *mode == "check" {
commit := prover.PreInit()
root := commit.Commit
verifier := pos.NewVerifier(pk, int64(*idx), beta, root)
seed := make([]byte, 64)
rand.Read(seed)
cs := verifier.SelectChallenges(seed)
now = time.Now()
hashes, parents, proofs, pProofs := prover.ProveSpace(cs)
fmt.Printf("Prove: %f\n", time.Since(now).Seconds())
now = time.Now()
if !verifier.VerifySpace(cs, hashes, parents, proofs, pProofs) {
log.Fatal("Verify space failed:", cs)
}
fmt.Printf("Verify: %f\n", time.Since(now).Seconds())
}
}