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.

171 lines
3.7 KiB

  1. package fields
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "math/big"
  6. )
  7. // Fq is the Z field over modulus Q
  8. type Fq struct {
  9. Q *big.Int // Q
  10. }
  11. // NewFq generates a new Fq
  12. func NewFq(q *big.Int) Fq {
  13. return Fq{
  14. q,
  15. }
  16. }
  17. // Zero returns a Zero value on the Fq
  18. func (fq Fq) Zero() *big.Int {
  19. return big.NewInt(int64(0))
  20. }
  21. // One returns a One value on the Fq
  22. func (fq Fq) One() *big.Int {
  23. return big.NewInt(int64(1))
  24. }
  25. // Add performs an addition on the Fq
  26. func (fq Fq) Add(a, b *big.Int) *big.Int {
  27. r := new(big.Int).Add(a, b)
  28. return new(big.Int).Mod(r, fq.Q)
  29. }
  30. // Double performs a doubling on the Fq
  31. func (fq Fq) Double(a *big.Int) *big.Int {
  32. r := new(big.Int).Add(a, a)
  33. return new(big.Int).Mod(r, fq.Q)
  34. }
  35. // Sub performs a subtraction on the Fq
  36. func (fq Fq) Sub(a, b *big.Int) *big.Int {
  37. r := new(big.Int).Sub(a, b)
  38. return new(big.Int).Mod(r, fq.Q)
  39. }
  40. // Neg performs a negation on the Fq
  41. func (fq Fq) Neg(a *big.Int) *big.Int {
  42. m := new(big.Int).Neg(a)
  43. return new(big.Int).Mod(m, fq.Q)
  44. }
  45. // Mul performs a multiplication on the Fq
  46. func (fq Fq) Mul(a, b *big.Int) *big.Int {
  47. m := new(big.Int).Mul(a, b)
  48. return new(big.Int).Mod(m, fq.Q)
  49. }
  50. func (fq Fq) MulScalar(base, e *big.Int) *big.Int {
  51. return fq.Mul(base, e)
  52. }
  53. // Inverse returns the inverse on the Fq
  54. func (fq Fq) Inverse(a *big.Int) *big.Int {
  55. return new(big.Int).ModInverse(a, fq.Q)
  56. // q := bigCopy(fq.Q)
  57. // t := big.NewInt(int64(0))
  58. // r := fq.Q
  59. // newt := big.NewInt(int64(0))
  60. // newr := fq.Affine(a)
  61. // for !bytes.Equal(newr.Bytes(), big.NewInt(int64(0)).Bytes()) {
  62. // q := new(big.Int).Div(bigCopy(r), bigCopy(newr))
  63. //
  64. // t = bigCopy(newt)
  65. // newt = fq.Sub(t, fq.Mul(q, newt))
  66. //
  67. // r = bigCopy(newr)
  68. // newr = fq.Sub(r, fq.Mul(q, newr))
  69. // }
  70. // if t.Cmp(big.NewInt(0)) == -1 { // t< 0
  71. // t = fq.Add(t, q)
  72. // }
  73. // return t
  74. }
  75. // Div performs the division over the finite field
  76. func (fq Fq) Div(a, b *big.Int) *big.Int {
  77. d := fq.Mul(a, fq.Inverse(b))
  78. return new(big.Int).Mod(d, fq.Q)
  79. }
  80. // Square performs a square operation on the Fq
  81. func (fq Fq) Square(a *big.Int) *big.Int {
  82. m := new(big.Int).Mul(a, a)
  83. return new(big.Int).Mod(m, fq.Q)
  84. }
  85. // Exp performs the exponential over Fq
  86. func (fq Fq) Exp(base *big.Int, e *big.Int) *big.Int {
  87. res := fq.One()
  88. rem := fq.Copy(e)
  89. exp := base
  90. for !bytes.Equal(rem.Bytes(), big.NewInt(int64(0)).Bytes()) {
  91. if BigIsOdd(rem) {
  92. res = fq.Mul(res, exp)
  93. }
  94. exp = fq.Square(exp)
  95. rem = new(big.Int).Rsh(rem, 1)
  96. }
  97. return res
  98. }
  99. func (fq Fq) Rand() (*big.Int, error) {
  100. // twoexp := new(big.Int).Exp(big.NewInt(2), big.NewInt(int64(maxbits)), nil)
  101. // max := new(big.Int).Sub(twoexp, big.NewInt(1))
  102. maxbits := fq.Q.BitLen()
  103. b := make([]byte, (maxbits/8)-1)
  104. _, err := rand.Read(b)
  105. if err != nil {
  106. return nil, err
  107. }
  108. r := new(big.Int).SetBytes(b)
  109. rq := new(big.Int).Mod(r, fq.Q)
  110. // r over q, nil
  111. return rq, nil
  112. }
  113. func (fq Fq) IsZero(a *big.Int) bool {
  114. return bytes.Equal(a.Bytes(), fq.Zero().Bytes())
  115. }
  116. func (fq Fq) Copy(a *big.Int) *big.Int {
  117. return new(big.Int).SetBytes(a.Bytes())
  118. }
  119. func (fq Fq) Affine(a *big.Int) *big.Int {
  120. nq := fq.Neg(fq.Q)
  121. aux := a
  122. if aux.Cmp(big.NewInt(int64(0))) == -1 { // negative value
  123. if aux.Cmp(nq) != 1 { // aux less or equal nq
  124. aux = new(big.Int).Mod(aux, fq.Q)
  125. }
  126. if aux.Cmp(big.NewInt(int64(0))) == -1 { // negative value
  127. aux = new(big.Int).Add(aux, fq.Q)
  128. }
  129. } else {
  130. if aux.Cmp(fq.Q) != -1 { // aux greater or equal nq
  131. aux = new(big.Int).Mod(aux, fq.Q)
  132. }
  133. }
  134. return aux
  135. }
  136. func (fq Fq) Equal(a, b *big.Int) bool {
  137. aAff := fq.Affine(a)
  138. bAff := fq.Affine(b)
  139. return bytes.Equal(aAff.Bytes(), bAff.Bytes())
  140. }
  141. func BigIsOdd(n *big.Int) bool {
  142. one := big.NewInt(int64(1))
  143. and := new(big.Int).And(n, one)
  144. return bytes.Equal(and.Bytes(), big.NewInt(int64(1)).Bytes())
  145. }