/
types.go
143 lines (121 loc) · 2.22 KB
/
types.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
137
138
139
140
141
142
143
package bls
/*
#include <mcl/curve_type.h>
*/
import "C"
import (
"fmt"
"sync"
)
//noinspection GoSnakeCaseUsage,GoNameStartsWithPackageName
const (
// BN160
BN160 = Curve(C.MCL_BN160)
// BN254 -- 254 bit curve
BN254 = Curve(C.MCL_BN254)
// BN_SNARK1
BN_SNARK1 = Curve(C.MCL_BN_SNARK1)
// BN381_1 -- 382 bit curve 1
BN381_1 = Curve(C.MCL_BN381_1)
// BN381_2 -- 382 bit curve 2
BN381_2 = Curve(C.MCL_BN381_2)
// BLS12_381
BLS12_381 = Curve(C.MCL_BLS12_381)
)
var (
current Curve = -1
currentMutex sync.RWMutex
)
func CurrentCurve() Curve {
currentMutex.RLock()
defer currentMutex.RUnlock()
return current
}
type Curve int8
func (curve Curve) Init() {
curve.Run(nil)
}
func (curve Curve) Run(fun func()) {
if !curve.IsValid() {
panic(fmt.Sprintf("invalid curve `%d`", curve))
}
currentMutex.RLock()
rlock := true
defer func() {
if rlock {
currentMutex.RUnlock()
}
}()
if current != curve {
currentMutex.RUnlock()
rlock = false
currentMutex.Lock()
defer currentMutex.Unlock()
if current != curve {
err := initCurve(curve)
if err != nil {
panic(err)
}
current = curve
}
}
if fun != nil {
fun()
}
}
func (curve Curve) IsValid() bool {
switch curve {
case BN160, BN254, BN_SNARK1, BN381_1, BN381_2, BLS12_381:
return true
default:
return false
}
}
func (curve Curve) SecretKeyLength() int {
switch curve {
case BN254, BN_SNARK1, BLS12_381:
return 32
case BN381_1, BN381_2:
return 48
default:
panic(fmt.Sprintf("invalid curve `%d`", curve))
}
}
func (curve Curve) PublicKeyLength() int {
switch curve {
case BN254, BN_SNARK1:
return 64
case BN381_1, BN381_2, BLS12_381:
return 96
default:
panic(fmt.Sprintf("invalid curve `%d`", curve))
}
}
func (curve Curve) SignLength() int {
switch curve {
case BN254, BN_SNARK1:
return 32
case BN381_1, BN381_2, BLS12_381:
return 48
default:
panic(fmt.Sprintf("invalid curve `%d`", curve))
}
}
func (curve Curve) String() string {
switch curve {
case BN160:
return "BN160"
case BN254:
return "BN254"
case BN_SNARK1:
return "BN_SNARK1"
case BN381_1:
return "BN381_1"
case BN381_2:
return "BN381_2"
case BLS12_381:
return "BLS12_381"
default:
return fmt.Sprintf("UNKNOWN(%d)", curve)
}
}