aboutsummaryrefslogtreecommitdiffstats
path: root/go/main.go
blob: 6c51b06f9b4c7862be0d337888d9ad9c9590a251 (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main

import "fmt"
import "./bls"
import "runtime"
import "time"

func verifyTrue(b bool) {
    if !b {
        fmt.Println("ERR")
    }
}
func testRecoverSecretKey() {
    fmt.Println("testRecoverSecretKey")
    k := 3000
    sec := bls.NewSecretKey()
    sec.Init()
    fmt.Println("sec =", sec)

    // make master secret key
    msk := sec.GetMasterSecretKey(k)

    n := k
    secVec := make([]*bls.SecretKey, n)
    idVec := make([]*bls.Id, n)
    for i := 0; i < n; i++ {
        idVec[i] = bls.NewId()
        idVec[i].Set([]uint64{1, 2, 3, uint64(i)})
        secVec[i] = bls.NewSecretKey()
        secVec[i].Set(msk, idVec[i])
    }
    // recover sec2 from secVec and idVec
    sec2 := bls.NewSecretKey()
    sec2.Recover(secVec, idVec)
    fmt.Println("sec2=", sec2)
}

func testSign() {
    m := "testSign"
    fmt.Println(m)

    sec0 := bls.NewSecretKey()
    sec0.Init()
    pub0 := sec0.GetPublicKey()
    s0 := sec0.Sign(m)
    verifyTrue(s0.Verify(pub0, m))

    k := 3
    msk := sec0.GetMasterSecretKey(k)
    mpk := bls.GetMasterPublicKey(msk)

    idTbl := []uint64{3, 5, 193, 22, 15}
    n := len(idTbl)

    secVec := make([]*bls.SecretKey, n)
    pubVec := make([]*bls.PublicKey, n)
    signVec := make([]*bls.Sign, n)
    idVec := make([]*bls.Id, n)

    for i := 0; i < n; i++ {
        idVec[i] = bls.NewId()
        idVec[i].Set([]uint64{idTbl[i], 0, 0, 0})
        fmt.Printf("idVec[%d]=%s\n", i, idVec[i].String())

        secVec[i] = bls.NewSecretKey()
        secVec[i].Set(msk, idVec[i])

        pubVec[i] = bls.NewPublicKey()
        pubVec[i].Set(mpk, idVec[i])
        fmt.Printf("pubVec[%d]=%s\n", i, pubVec[i].String())

        verifyTrue(pubVec[i].String() == secVec[i].GetPublicKey().String())

        signVec[i] = secVec[i].Sign(m)
        verifyTrue(signVec[i].Verify(pubVec[i], m))
    }
    sec1 := bls.NewSecretKey()
    sec1.Recover(secVec, idVec)
    verifyTrue(sec0.String() == sec1.String())
    pub1 := bls.NewPublicKey()
    pub1.Recover(pubVec, idVec)
    verifyTrue(pub0.String() == pub1.String())
    s1 := bls.NewSign()
    s1.Recover(signVec, idVec)
    verifyTrue(s0.String() == s1.String())
}

func testAdd() {
    fmt.Println("testAdd")
    sec1 := bls.NewSecretKey()
    sec2 := bls.NewSecretKey()
    sec1.Init()
    sec2.Init()

    pub1 := sec1.GetPublicKey()
    pub2 := sec2.GetPublicKey()

    m := "test test"
    sign1 := sec1.Sign(m)
    sign2 := sec2.Sign(m)

    fmt.Println("sign1    :", sign1)
    sign1.Add(sign2)
    fmt.Println("sign1 add:", sign1)
    pub1.Add(pub2)
    verifyTrue(sign1.Verify(pub1, m))
}

func testPop() {
    fmt.Println("testPop")
    sec := bls.NewSecretKey()
    sec.Init()
    pop := sec.GetPop()
    verifyTrue(pop.VerifyPop(sec.GetPublicKey()))
    sec.Init()
    verifyTrue(!pop.VerifyPop(sec.GetPublicKey()))
}
func main() {
    fmt.Println("init")
    bls.Init()
    {
        id := bls.NewId()
        id.Set([]uint64{4, 3, 2, 1})
        fmt.Println("id :", id)
        id2 := bls.NewId()
        id2.SetStr(id.String())
        fmt.Println("id2:", id2)
    }
    {
        sec := bls.NewSecretKey()
        sec.SetArray([]uint64{1, 2, 3, 4})
        fmt.Println("sec=", sec)
    }

    fmt.Println("create secret key")
    m := "this is a bls sample for go"
    sec := bls.NewSecretKey()
    sec.Init()
    fmt.Println("sec:", sec)
    fmt.Println("create public key")
    pub := sec.GetPublicKey()
    fmt.Println("pub:", pub)
    sign := sec.Sign(m)
    fmt.Println("sign:", sign)
    verifyTrue(sign.Verify(pub, m))

    // How to make array of SecretKey
    {
        sec := make([]bls.SecretKey, 3)
        for i := 0; i < len(sec); i++ {
            sec[i] = *bls.NewSecretKey()
            sec[i].Init()
            fmt.Println("sec=", sec[i].String())
        }
    }
    testRecoverSecretKey()
    testAdd()
    testSign()
    testPop()

    // put memory status
    runtime.GC()
    time.Sleep(2 * time.Second)
    var mem runtime.MemStats
    runtime.ReadMemStats(&mem)
    fmt.Println("mem=", mem)
}