You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.5 KiB

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