arnaucube
arnaucube/cryptofunPublic Self-hosted repository
cryptofun/bn128/fq.go
bn128/fq.goRaw
 1package bn128
 2
 3import (
 4	"bytes"
 5	"math/big"
 6)
 7
 8// Fq is the Z field over modulus Q
 9type Fq struct {
10	Q *big.Int // Q
11}
12
13// NewFq generates a new Fq
14func NewFq(q *big.Int) Fq {
15	return Fq{
16		q,
17	}
18}
19
20// Zero returns a Zero value on the Fq
21func (fq Fq) Zero() *big.Int {
22	return big.NewInt(int64(0))
23}
24
25// One returns a One value on the Fq
26func (fq Fq) One() *big.Int {
27	return big.NewInt(int64(1))
28}
29
30// Add performs an addition on the Fq
31func (fq Fq) Add(a, b *big.Int) *big.Int {
32	sum := new(big.Int).Add(a, b)
33	return new(big.Int).Mod(sum, fq.Q)
34}
35
36// Double performs a doubling on the Fq
37func (fq Fq) Double(a *big.Int) *big.Int {
38	sum := new(big.Int).Add(a, a)
39	return new(big.Int).Mod(sum, fq.Q)
40}
41
42// Sub performs a substraction on the Fq
43func (fq Fq) Sub(a, b *big.Int) *big.Int {
44	sum := new(big.Int).Sub(a, b)
45	return new(big.Int).Mod(sum, fq.Q)
46}
47
48// Neg performs a negation on the Fq
49func (fq Fq) Neg(a *big.Int) *big.Int {
50	m := new(big.Int).Neg(a)
51	return new(big.Int).Mod(m, fq.Q)
52}
53
54// Mul performs a multiplication on the Fq
55func (fq Fq) Mul(a, b *big.Int) *big.Int {
56	m := new(big.Int).Mul(a, b)
57	return new(big.Int).Mod(m, fq.Q)
58}
59
60func (fq Fq) MulScalar(base, e *big.Int) *big.Int {
61	return fq.Mul(base, e)
62}
63
64// Inverse returns the inverse on the Fq
65func (fq Fq) Inverse(a *big.Int) *big.Int {
66	return new(big.Int).ModInverse(a, fq.Q)
67}
68
69// Div performs a division on the Fq
70func (fq Fq) Div(a, b *big.Int) *big.Int {
71	// not used in fq1, method added to fit the interface
72	return a
73}
74
75// Square performs a square operation on the Fq
76func (fq Fq) Square(a *big.Int) *big.Int {
77	m := new(big.Int).Mul(a, a)
78	return new(big.Int).Mod(m, fq.Q)
79}
80
81func (fq Fq) IsZero(a *big.Int) bool {
82	return bytes.Equal(a.Bytes(), fq.Zero().Bytes())
83}
84
85func (fq Fq) Copy(a *big.Int) *big.Int {
86	return new(big.Int).SetBytes(a.Bytes())
87}
88
89func (fq Fq) Affine(a *big.Int) *big.Int {
90	return a
91}
92func (fq Fq) Equal(a, b *big.Int) bool {
93	return bytes.Equal(a.Bytes(), b.Bytes())
94}