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