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.

94 lines
1.9 KiB

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