-
Notifications
You must be signed in to change notification settings - Fork 4
/
verifier_test.go
85 lines (78 loc) · 2.2 KB
/
verifier_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
package main
import (
"crypto/sha256"
"errors"
"fmt"
"github.com/Evanesco-Labs/go-evanesco/consensus/ethash"
"github.com/Evanesco-Labs/go-evanesco/core"
"github.com/Evanesco-Labs/go-evanesco/core/rawdb"
"github.com/Evanesco-Labs/go-evanesco/core/types"
"github.com/Evanesco-Labs/go-evanesco/params"
"github.com/Evanesco-Labs/go-evanesco/zkpminer/problem"
"github.com/stretchr/testify/assert"
"runtime"
"testing"
"time"
)
var (
PkPath = "../provekeytest.txt"
VkPath = "../verifykeytest.txt"
)
func GenerateTestChain(cnt int) (*core.HeaderChain, error) {
testdb := rawdb.NewMemoryDatabase()
gspec := &core.Genesis{Config: params.TestChainConfig}
genesis := gspec.MustCommit(testdb)
hc, err := core.NewHeaderChain(testdb, params.TestChainConfig, ethash.NewFaker(), func() bool { return false })
if err != nil {
return nil, err
}
hc.SetGenesis(genesis.Header())
blocks, _ := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), testdb, 100, nil)
headerChain := make([]*types.Header, 0)
for _, block := range blocks {
headerChain = append(headerChain, block.Header())
}
_, err = hc.InsertHeaderChain(headerChain, time.Now())
if err != nil {
return nil, err
}
return hc, nil
}
func TestNewProverVerifier(t *testing.T) {
hc, err := GenerateTestChain(100)
if err != nil {
panic(err)
}
runtime.GOMAXPROCS(1)
t1 := time.Now()
prover, err := problem.NewProblemProver(PkPath)
t2 := time.Now()
if err != nil {
t.Fatal(err)
}
msg := "test evanesco"
hash := sha256.New()
hash.Write([]byte(msg))
preimage := hash.Sum(nil)
mimcHash, proof := prover.Prove(preimage)
t3 := time.Now()
getHeader := func(height uint64) (*types.Header, error) {
header := hc.GetHeaderByNumber(height)
if header == nil {
return nil, errors.New("no header")
}
return header, nil
}
verifier, err := problem.NewProblemVerifier(VkPath, 100, 80, getHeader)
if err != nil {
t.Fatal(err)
}
t4 := time.Now()
result := verifier.VerifyZKP(preimage, mimcHash, proof)
t5 := time.Now()
fmt.Println("new prover:", t2.Sub(t1).String())
fmt.Println("prove:", t3.Sub(t2).String())
fmt.Println("new verifier:", t4.Sub(t3).String())
fmt.Println("verify:", t5.Sub(t4).String())
assert.Equal(t, true, result)
}