This repository has been archived by the owner on Jun 10, 2020. It is now read-only.
forked from kilic/bls12-381
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kyber_g1.go
136 lines (111 loc) · 2.6 KB
/
kyber_g1.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
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
package bls
import (
"crypto/cipher"
"encoding/hex"
"io"
"github.com/drand/kyber"
"github.com/drand/kyber/group/mod"
)
// KyberG1 is a kyber.Point holding a G1 point on BLS12-381 curve
type KyberG1 struct {
p *PointG1
}
func nullKyberG1() *KyberG1 {
var p PointG1
return newKyberG1(&p)
}
func newKyberG1(p *PointG1) *KyberG1 {
return &KyberG1{p: p}
}
func (k *KyberG1) Equal(k2 kyber.Point) bool {
return NewG1().Equal(k.p, k2.(*KyberG1).p)
}
func (k *KyberG1) Null() kyber.Point {
return newKyberG1(NewG1().Zero())
}
func (k *KyberG1) Base() kyber.Point {
return newKyberG1(NewG1().One())
}
func (k *KyberG1) Pick(rand cipher.Stream) kyber.Point {
//panic("not implemented")
var dst, src [32]byte
rand.XORKeyStream(dst[:], src[:])
return k.Hash(dst[:])
}
func (k *KyberG1) Set(q kyber.Point) kyber.Point {
k.p.Set(q.(*KyberG1).p)
return k
}
func (k *KyberG1) Clone() kyber.Point {
var p PointG1
p.Set(k.p)
return newKyberG1(&p)
}
func (k *KyberG1) EmbedLen() int {
panic("bls12-381: unsupported operation")
}
func (k *KyberG1) Embed(data []byte, rand cipher.Stream) kyber.Point {
panic("bls12-381: unsupported operation")
}
func (k *KyberG1) Data() ([]byte, error) {
panic("bls12-381: unsupported operation")
}
func (k *KyberG1) Add(a, b kyber.Point) kyber.Point {
aa := a.(*KyberG1)
bb := b.(*KyberG1)
NewG1().Add(k.p, aa.p, bb.p)
return k
}
func (k *KyberG1) Sub(a, b kyber.Point) kyber.Point {
aa := a.(*KyberG1)
bb := b.(*KyberG1)
NewG1().Sub(k.p, aa.p, bb.p)
return k
}
func (k *KyberG1) Neg(a kyber.Point) kyber.Point {
aa := a.(*KyberG1)
NewG1().Neg(k.p, aa.p)
return k
}
func (k *KyberG1) Mul(s kyber.Scalar, q kyber.Point) kyber.Point {
if q == nil {
q = nullKyberG1().Base()
}
NewG1().MulScalar(k.p, q.(*KyberG1).p, &s.(*mod.Int).V)
return k
}
func (k *KyberG1) MarshalBinary() ([]byte, error) {
return NewG1().ToCompressed(k.p), nil
}
func (k *KyberG1) UnmarshalBinary(buff []byte) error {
var err error
k.p, err = NewG1().FromCompressed(buff)
return err
}
func (k *KyberG1) MarshalTo(w io.Writer) (int, error) {
buf, err := k.MarshalBinary()
if err != nil {
return 0, err
}
return w.Write(buf)
}
func (k *KyberG1) UnmarshalFrom(r io.Reader) (int, error) {
buf := make([]byte, k.MarshalSize())
n, err := io.ReadFull(r, buf)
if err != nil {
return n, err
}
return n, k.UnmarshalBinary(buf)
}
func (k *KyberG1) MarshalSize() int {
return 48
}
func (k *KyberG1) String() string {
b, _ := k.MarshalBinary()
return "bls12-381.G1: " + hex.EncodeToString(b)
}
func (k *KyberG1) Hash(m []byte) kyber.Point {
p, _ := NewG1().HashToCurve(m, Domain)
k.p = p
return k
}