forked from harmony-one/vdf
/
vdf.go
75 lines (61 loc) · 2.08 KB
/
vdf.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
package vdf_go
import ("os/exec"
"fmt")
// VDF is the struct holding necessary state for a hash chain delay function.
type VDF struct {
difficulty int
input [32]byte
output [516]byte
outputChan chan [516]byte
finished bool
}
//size of long integers in quadratic function group
const sizeInBits = 2048
// New create a new instance of VDF.
func New(difficulty int, input [32]byte) *VDF {
return &VDF{
difficulty: difficulty,
input: input,
outputChan: make(chan [516]byte),
}
}
// GetOutputChannel returns the vdf output channel.
// VDF output consists of 258 bytes of serialized Y and 258 bytes of serialized Proof
func (vdf *VDF) GetOutputChannel() chan [516]byte {
return vdf.outputChan
}
// Execute runs the VDF until it's finished and put the result into output channel.
// currently on i7-6700K, it takes about 14 seconds when iteration is set to 10000
func (vdf *VDF) Execute() {
vdf.finished = false
cmdOutput, err := exec.Command("vdf-cli", string(vdf.input[:]), string(vdf.difficulty), "-l ",string(sizeInBits)).Output()
//yBuf, proofBuf := GenerateVDF(vdf.input[:], vdf.difficulty, sizeInBits)
fmt.Printf("%s", err)
//f//mt.Printf("%s", cmdOutput)
copy(vdf.output[:], cmdOutput)
//copy(vdf.output[258:], proofBuf)
go func() {
vdf.outputChan <- vdf.output
}()
vdf.finished = true
}
// Verify runs the verification of generated proof
// currently on i7-6700K, verification takes about 350 ms
func (vdf *VDF) Verify(proof [516]byte) bool {
cmdOutput, err := exec.Command("vdf-cli", string(vdf.input[:]), string(vdf.difficulty), string(proof[:]), "-l ",string(sizeInBits)).Output()
fmt.Printf("%s", cmdOutput)
fmt.Printf("%s", err)
if (string(cmdOutput) == "Proof is valid"){
return true
}
return false
//return VerifyVDF(vdf.input[:], proof[:], vdf.difficulty, sizeInBits)
}
// IsFinished returns whether the vdf execution is finished or not.
func (vdf *VDF) IsFinished() bool {
return vdf.finished
}
// GetOutput returns the vdf output, which can be bytes of 0s is the vdf is not finished.
func (vdf *VDF) GetOutput() [516]byte {
return vdf.output
}