forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g1.go
64 lines (52 loc) · 1.83 KB
/
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
package sw_bls12381
import (
"fmt"
bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381"
fr_bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
"github.com/airchains-network/gnark/frontend"
"github.com/airchains-network/gnark/std/algebra/emulated/sw_emulated"
"github.com/airchains-network/gnark/std/math/emulated"
)
// G1Affine is the point in G1. It is an alias to the generic emulated affine
// point.
type G1Affine = sw_emulated.AffinePoint[BaseField]
// Scalar is the scalar in the groups. It is an alias to the emulated element
// defined over the scalar field of the groups.
type Scalar = emulated.Element[ScalarField]
// NewG1Affine allocates a witness from the native G1 element and returns it.
func NewG1Affine(v bls12381.G1Affine) G1Affine {
return G1Affine{
X: emulated.ValueOf[BaseField](v.X),
Y: emulated.ValueOf[BaseField](v.Y),
}
}
type G1 struct {
curveF *emulated.Field[BaseField]
w *emulated.Element[BaseField]
}
func NewG1(api frontend.API) (*G1, error) {
ba, err := emulated.NewField[BaseField](api)
if err != nil {
return nil, fmt.Errorf("new base api: %w", err)
}
w := emulated.ValueOf[BaseField]("4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436")
return &G1{
curveF: ba,
w: &w,
}, nil
}
func (g1 *G1) phi(q *G1Affine) *G1Affine {
x := g1.curveF.Mul(&q.X, g1.w)
return &G1Affine{
X: *x,
Y: q.Y,
}
}
// NewScalar allocates a witness from the native scalar and returns it.
func NewScalar(v fr_bls12381.Element) Scalar {
return emulated.ValueOf[ScalarField](v)
}
// ScalarField is the [emulated.FieldParams] impelementation of the curve scalar field.
type ScalarField = emulated.BLS12381Fr
// BaseField is the [emulated.FieldParams] impelementation of the curve base field.
type BaseField = emulated.BLS12381Fp