-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (102 loc) · 3.02 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
package main
import (
"fmt"
"github.com/twingdev/go-libzk13/zkp"
"log"
"math/big"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"runtime/pprof"
)
func main() {
runtime.GOMAXPROCS(7)
// Start a goroutine with an HTTP server for runtime profiling
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
// Enable CPU profiling
f, err := os.Create("cpu.prof")
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
// Run tests
err = runTests()
if err != nil {
log.Fatalf("Error running tests: %v", err)
}
// Memory profiling
mf, err := os.Create("mem.prof")
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer mf.Close()
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(mf); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
func runTests() error {
// Test different prime lengths
for _, bits := range []int{512, 1024, 2048, 2048 + 32} {
fmt.Printf("Testing with prime length: %d bits\n", bits)
zk13 := zkp.NewZK13("shared secret", bits) // Adjust NewZK13 to accept prime length as an argument
nonce := zk13.GenerateNonce() // Generate a nonce for replay attack protection
proof, err := zk13.Prover(nonce)
if err != nil {
return fmt.Errorf("error generating proof: %v", err)
}
isValid := zk13.Verifier(proof)
fmt.Printf("Verification with %d bits prime: %v\n", bits, isValid)
}
// Run timing attack test
zk13 := zkp.NewZK13("shared secret", 2048) // Use a fixed prime length for timing attack test
nonce := zk13.GenerateNonce() // Generate a nonce for replay attack protection
proof, err := zk13.Prover(nonce)
if err != nil {
return fmt.Errorf("error generating proof: %v", err)
}
isValid := zk13.Verifier(proof)
if !isValid {
return fmt.Errorf("proof should be valid")
}
// Modify the proof and verify that it is invalid
proof.R.Add(proof.R, big.NewInt(1))
isValid = zk13.Verifier(proof)
if isValid {
return fmt.Errorf("proof should be invalid")
}
// Modify the nonce and verify that the proof is invalid
proof.Nonce.Add(proof.Nonce, big.NewInt(1))
isValid = zk13.Verifier(proof)
if isValid {
return fmt.Errorf("proof should be invalid")
}
// Run replay attack test
zk13 = zkp.NewZK13("shared secret", 2048) // Use a fixed prime length for replay attack test
nonce = zk13.GenerateNonce() // Generate a nonce for replay attack protection
proof, err = zk13.Prover(nonce)
if err != nil {
return fmt.Errorf("error generating proof: %v", err)
}
isValid = zk13.Verifier(proof)
if !isValid {
return fmt.Errorf("proof should be valid")
}
// Use the same nonce to generate another proof
proof2, err := zk13.Prover(nonce)
if err != nil {
return fmt.Errorf("error generating proof: %v", err)
}
isValid = zk13.Verifier(proof2)
if isValid {
return fmt.Errorf("proof should be invalid")
}
return nil
}