-
Notifications
You must be signed in to change notification settings - Fork 3
/
bilinear_map.go
134 lines (116 loc) · 3.06 KB
/
bilinear_map.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
package crypto
import (
"errors"
"io"
"math/big"
"sync"
)
//go:generate mockgen -destination ./mock/vc.go -package crypto -source ./bilinear_map.go
// Position position in Pairing
type Position int
// Position in Pairing
const (
G1 Position = 1 << iota
G2
GT
)
// FieldElement in Montgomery From
type FieldElement interface {
UnmarshalJSON([]byte) error
MarshalJSON() ([]byte, error)
Add(a1, a2 FieldElement) FieldElement
Double(FieldElement) FieldElement
Sub(a1, a2 FieldElement) FieldElement
Neg(FieldElement) FieldElement
Mul(a1, a2 FieldElement) FieldElement
Square(FieldElement) FieldElement
Div(a1, a2 FieldElement) FieldElement
Inv(FieldElement) FieldElement
Exp(FieldElement, []byte) FieldElement
Equal(FieldElement) bool
IsZero() bool
IsOne() bool
IsNeg() bool
Set(FieldElement) FieldElement
SetOne() FieldElement
SetZero() FieldElement
SetInt64(int64) FieldElement
SetUint64(uint64) FieldElement
SetRandom(io.Reader) FieldElement
// From sets self to v (regular form) and returns self (Montgomery form)
From(rat *big.Int) FieldElement
//Regular append regular bytes to 'in', keep 'z' unchanged
Regular(in []byte) []byte
//FromRegular interprets 'content' as the bytes of a big-endian unsigned integer,
// sets z to that value (in Montgomery form), and returns z.
FromRegular(content []byte) FieldElement
//MontBytes set and return 'res' with bytes in Montgomery form
MontBytes(res []byte) []byte
//GetModule set 'b' to 21888242871839275222246405745257275088548364400416034343698204186575808495617
GetModule(*big.Int)
String() string
//Copy get a clone
Copy() FieldElement
}
// Point elliptic point
type Point interface {
Marshaller
Add(Point, Point) Point
Set(Point) Point
Double(Point) Point
//Neg neg
Neg(Point) Point
ScalarMult(Point, *big.Int) Point //scalar is at Z+
ScalarBaseMult(*big.Int) Point
//GetPosition get position
GetPosition() Position
GetPairing() Pairing
SetInfinity()
IsInfinity() bool
}
// ErrFFT not support fft
var ErrFFT = errors.New("not support fft")
// AlgebraicSys algebra system
type AlgebraicSys interface {
Marshaller
GetModule() *big.Int
Name() string
NewScalar() FieldElement //fr
//NewField() FieldElement //fp
PutScalar(FieldElement)
GetRootOfUnity(uint64) (FieldElement, uint64, error)
}
// Pairing pairing of elliptic
type Pairing interface {
AlgebraicSys
Pair([]Point, []Point) Point
PairCheck(P []Point, Q []Point) bool
IsOnCurve(Point) error
//GetBase never change result's value for GetBase, GetModule and GetOlder!
GetBase(Position) Point
NewPoint(Position) Point
BatchScalarMultiplicationG1(scalars []*big.Int, ret []Point)
BatchScalarMultiplicationG2(scalars []*big.Int, ret []Point)
}
// Marshaller marshal and unmarshal
type Marshaller interface {
Marshal() []byte
Unmarshal([]byte) ([]byte, error)
}
var bigPool = sync.Pool{
New: func() interface{} {
return new(big.Int)
},
}
// GetBigInt get *big.Int
func GetBigInt() *big.Int {
return bigPool.Get().(*big.Int)
}
// PutBigInt put *big.Int
func PutBigInt(in *big.Int) {
if in == nil {
return
}
in.SetInt64(0)
bigPool.Put(in)
}