-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.go
71 lines (66 loc) · 1.47 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
/*
cd bls
make minimised_static
*/
package main
import (
"crypto/rand"
"fmt"
"github.com/herumi/bls/ffi/go/bls"
)
type SeqRead struct {
}
func (self *SeqRead) Read(buf []byte) (int, error) {
n := len(buf)
for i := 0; i < n; i++ {
buf[i] = byte(i)
}
return n, nil
}
func testReadRand() {
s1 := new(SeqRead)
bls.SetRandFunc(s1)
var sec bls.SecretKey
sec.SetByCSPRNG()
buf := sec.GetLittleEndian()
fmt.Printf("1. buf=%x\n", buf)
for i := 0; i < len(buf); i++ {
if buf[i] != byte(i) {
fmt.Printf("err %d\n", i)
}
}
bls.SetRandFunc(rand.Reader)
sec.SetByCSPRNG()
buf = sec.GetLittleEndian()
fmt.Printf("2. (cr.Read) buf=%x\n", buf)
bls.SetRandFunc(nil)
sec.SetByCSPRNG()
buf = sec.GetLittleEndian()
fmt.Printf("3. (cr.Read) buf=%x\n", buf)
}
func main() {
bls.Init(bls.BLS12_381)
var sec bls.SecretKey
sec.SetByCSPRNG()
fmt.Printf("sec:%s\n", sec.SerializeToHexStr())
pub := sec.GetPublicKey()
fmt.Printf("pub:%s\n", pub.SerializeToHexStr())
msgTbl := []string{"abc", "def", "123"}
n := len(msgTbl)
sigVec := make([]*bls.Sign, n)
for i := 0; i < n; i++ {
m := msgTbl[i]
sigVec[i] = sec.Sign(m)
fmt.Printf("%d. sign(%s)=%s\n", i, m, sigVec[i].SerializeToHexStr())
}
agg := sigVec[0]
for i := 1; i < n; i++ {
agg.Add(sigVec[i])
}
hashPt := bls.HashAndMapToSignature([]byte(msgTbl[0]))
for i := 1; i < n; i++ {
hashPt.Add(bls.HashAndMapToSignature([]byte(msgTbl[i])))
}
fmt.Printf("verify %t\n", bls.VerifyPairing(agg, hashPt, pub))
testReadRand()
}