-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpublic_key.go
49 lines (41 loc) · 979 Bytes
/
public_key.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
package bls
import (
"encoding/hex"
bls12381 "github.com/kilic/bls12-381"
"math/big"
)
type PublicKey struct {
value *bls12381.PointG1
}
func NewPublicKey(data []byte) (PublicKey, error) {
value, err := bls12381.NewG1().FromCompressed(data)
if err != nil {
return PublicKey{}, err
}
return PublicKey{
value: value,
}, nil
}
// FingerPrint 生成指纹
func (key PublicKey) FingerPrint() string {
return new(big.Int).SetBytes(Hash256(bls12381.NewG1().ToCompressed(key.value))[:4]).String()
}
// Bytes 转成bytes
func (key PublicKey) Bytes() []byte {
return bls12381.NewG1().ToCompressed(key.value)
}
// Hex 转成hex string
func (key PublicKey) Hex() string {
return "0x" + hex.EncodeToString(key.Bytes())
}
// G1 获取G1
func (key PublicKey) G1() *bls12381.PointG1 {
return key.value
}
// Add 组合公钥
func (key PublicKey) Add(pk PublicKey) PublicKey {
g1 := bls12381.NewG1()
return PublicKey{
value: g1.Add(g1.New(), key.value, pk.G1()),
}
}