-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
sample.go
71 lines (65 loc) · 1.58 KB
/
sample.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
package main
import (
"encoding/hex"
"fmt"
"github.com/herumi/bls-eth-go-binary/bls"
)
func sample1() {
fmt.Printf("sample1\n")
var sec bls.SecretKey
sec.SetByCSPRNG()
msg := []byte("abc")
pub := sec.GetPublicKey()
sig := sec.SignByte(msg)
fmt.Printf("verify=%v\n", sig.VerifyByte(pub, msg))
}
func sample2() {
fmt.Printf("sample2\n")
var sec bls.SecretKey
sec.SetByCSPRNG()
fmt.Printf("sec:%s\n", sec.SerializeToHexStr())
pub := sec.GetPublicKey()
fmt.Printf("1.pub:%s\n", pub.SerializeToHexStr())
fmt.Printf("1.pub x=%x\n", pub)
var P *bls.G1 = bls.CastFromPublicKey(pub)
bls.G1Normalize(P, P)
fmt.Printf("2.pub:%s\n", pub.SerializeToHexStr())
fmt.Printf("2.pub x=%x\n", pub)
fmt.Printf("P.X=%x\n", P.X.Serialize())
fmt.Printf("P.Y=%x\n", P.Y.Serialize())
fmt.Printf("P.Z=%x\n", P.Z.Serialize())
}
func sample3() {
fmt.Printf("sample3\n")
var sec bls.SecretKey
b := make([]byte, 64)
for i := 0; i < len(b); i++ {
b[i] = 0xff
}
err := sec.SetLittleEndianMod(b)
if err != nil {
fmt.Printf("err")
return
}
fmt.Printf("sec=%x\n", sec.Serialize())
}
func sample4() {
fmt.Printf("sample4\n")
var sec bls.SecretKey
secByte, _ := hex.DecodeString("4aac41b5cb665b93e031faa751944b1f14d77cb17322403cba8df1d6e4541a4d")
sec.Deserialize(secByte)
msg := []byte("message to be signed.")
fmt.Printf("sec:%x\n", sec.Serialize())
pub := sec.GetPublicKey()
fmt.Printf("pub:%x\n", pub.Serialize())
sig := sec.SignByte(msg)
fmt.Printf("sig:%x\n", sig.Serialize())
}
func main() {
bls.Init(bls.BLS12_381)
bls.SetETHmode(bls.EthModeDraft07)
sample1()
sample2()
sample3()
sample4()
}