-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
51 lines (39 loc) · 904 Bytes
/
util.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
package tibe
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"github.com/cloudflare/circl/ecc/bls12381"
)
const macSize = 32
func mac(data, secret []byte) []byte {
h := hmac.New(sha256.New, secret)
h.Write(data)
return h.Sum(nil)
}
func randomScalar() (*bls12381.Scalar, error) {
r := &bls12381.Scalar{}
if err := r.Random(rand.Reader); err != nil {
return nil, err
}
return r, nil
}
func hashGt(gt *bls12381.Gt) ([]byte, error) {
bts, err := gt.MarshalBinary()
if err != nil {
return nil, err
}
hsh := sha512.Sum512_256(bts)
return hsh[:], nil
}
func g2Hash(id []byte) *bls12381.G2 {
tHsh := &bls12381.G2{}
tHsh.Hash(id, []byte("g2hash"))
return tHsh
}
func isValidSignature(msg []byte, sig *bls12381.G2, pubkey *bls12381.G1) bool {
l := bls12381.Pair(pubkey, g2Hash(msg))
r := bls12381.Pair(bls12381.G1Generator(), sig)
return l.IsEqual(r)
}