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.

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