Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

255 Zeilen
8.0 KiB

  1. package p751
  2. import . "github.com/cloudflare/p751sidh/internal/isogeny"
  3. // 2*p751
  4. var ()
  5. //------------------------------------------------------------------------------
  6. // Implementtaion of FieldOperations
  7. //------------------------------------------------------------------------------
  8. // Implements FieldOps
  9. type fp751Ops struct{}
  10. func FieldOperations() FieldOps {
  11. return &fp751Ops{}
  12. }
  13. func (fp751Ops) Add(dest, lhs, rhs *Fp2Element) {
  14. fp751AddReduced(&dest.A, &lhs.A, &rhs.A)
  15. fp751AddReduced(&dest.B, &lhs.B, &rhs.B)
  16. }
  17. func (fp751Ops) Sub(dest, lhs, rhs *Fp2Element) {
  18. fp751SubReduced(&dest.A, &lhs.A, &rhs.A)
  19. fp751SubReduced(&dest.B, &lhs.B, &rhs.B)
  20. }
  21. func (fp751Ops) Mul(dest, lhs, rhs *Fp2Element) {
  22. // Let (a,b,c,d) = (lhs.a,lhs.b,rhs.a,rhs.b).
  23. a := &lhs.A
  24. b := &lhs.B
  25. c := &rhs.A
  26. d := &rhs.B
  27. // We want to compute
  28. //
  29. // (a + bi)*(c + di) = (a*c - b*d) + (a*d + b*c)i
  30. //
  31. // Use Karatsuba's trick: note that
  32. //
  33. // (b - a)*(c - d) = (b*c + a*d) - a*c - b*d
  34. //
  35. // so (a*d + b*c) = (b-a)*(c-d) + a*c + b*d.
  36. var ac, bd FpElementX2
  37. fp751Mul(&ac, a, c) // = a*c*R*R
  38. fp751Mul(&bd, b, d) // = b*d*R*R
  39. var b_minus_a, c_minus_d FpElement
  40. fp751SubReduced(&b_minus_a, b, a) // = (b-a)*R
  41. fp751SubReduced(&c_minus_d, c, d) // = (c-d)*R
  42. var ad_plus_bc FpElementX2
  43. fp751Mul(&ad_plus_bc, &b_minus_a, &c_minus_d) // = (b-a)*(c-d)*R*R
  44. fp751X2AddLazy(&ad_plus_bc, &ad_plus_bc, &ac) // = ((b-a)*(c-d) + a*c)*R*R
  45. fp751X2AddLazy(&ad_plus_bc, &ad_plus_bc, &bd) // = ((b-a)*(c-d) + a*c + b*d)*R*R
  46. fp751MontgomeryReduce(&dest.B, &ad_plus_bc) // = (a*d + b*c)*R mod p
  47. var ac_minus_bd FpElementX2
  48. fp751X2SubLazy(&ac_minus_bd, &ac, &bd) // = (a*c - b*d)*R*R
  49. fp751MontgomeryReduce(&dest.A, &ac_minus_bd) // = (a*c - b*d)*R mod p
  50. }
  51. func (fp751Ops) Square(dest, x *Fp2Element) {
  52. a := &x.A
  53. b := &x.B
  54. // We want to compute
  55. //
  56. // (a + bi)*(a + bi) = (a^2 - b^2) + 2abi.
  57. var a2, a_plus_b, a_minus_b FpElement
  58. fp751AddReduced(&a2, a, a) // = a*R + a*R = 2*a*R
  59. fp751AddReduced(&a_plus_b, a, b) // = a*R + b*R = (a+b)*R
  60. fp751SubReduced(&a_minus_b, a, b) // = a*R - b*R = (a-b)*R
  61. var asq_minus_bsq, ab2 FpElementX2
  62. fp751Mul(&asq_minus_bsq, &a_plus_b, &a_minus_b) // = (a+b)*(a-b)*R*R = (a^2 - b^2)*R*R
  63. fp751Mul(&ab2, &a2, b) // = 2*a*b*R*R
  64. fp751MontgomeryReduce(&dest.A, &asq_minus_bsq) // = (a^2 - b^2)*R mod p
  65. fp751MontgomeryReduce(&dest.B, &ab2) // = 2*a*b*R mod p
  66. }
  67. // Set dest = 1/x
  68. //
  69. // Allowed to overlap dest with x.
  70. //
  71. // Returns dest to allow chaining operations.
  72. func (fp751Ops) Inv(dest, x *Fp2Element) {
  73. a := &x.A
  74. b := &x.B
  75. // We want to compute
  76. //
  77. // 1 1 (a - bi) (a - bi)
  78. // -------- = -------- -------- = -----------
  79. // (a + bi) (a + bi) (a - bi) (a^2 + b^2)
  80. //
  81. // Letting c = 1/(a^2 + b^2), this is
  82. //
  83. // 1/(a+bi) = a*c - b*ci.
  84. var asq_plus_bsq primeFieldElement
  85. var asq, bsq FpElementX2
  86. fp751Mul(&asq, a, a) // = a*a*R*R
  87. fp751Mul(&bsq, b, b) // = b*b*R*R
  88. fp751X2AddLazy(&asq, &asq, &bsq) // = (a^2 + b^2)*R*R
  89. fp751MontgomeryReduce(&asq_plus_bsq.A, &asq) // = (a^2 + b^2)*R mod p
  90. // Now asq_plus_bsq = a^2 + b^2
  91. // Invert asq_plus_bsq
  92. inv := asq_plus_bsq
  93. inv.Mul(&asq_plus_bsq, &asq_plus_bsq)
  94. inv.P34(&inv)
  95. inv.Mul(&inv, &inv)
  96. inv.Mul(&inv, &asq_plus_bsq)
  97. var ac FpElementX2
  98. fp751Mul(&ac, a, &inv.A)
  99. fp751MontgomeryReduce(&dest.A, &ac)
  100. var minus_b FpElement
  101. fp751SubReduced(&minus_b, &minus_b, b)
  102. var minus_bc FpElementX2
  103. fp751Mul(&minus_bc, &minus_b, &inv.A)
  104. fp751MontgomeryReduce(&dest.B, &minus_bc)
  105. }
  106. // In case choice == 1, performs following swap in constant time:
  107. // xPx <-> xQx
  108. // xPz <-> xQz
  109. // Otherwise returns xPx, xPz, xQx, xQz unchanged
  110. func (fp751Ops) CondSwap(xPx, xPz, xQx, xQz *Fp2Element, choice uint8) {
  111. fp751ConditionalSwap(&xPx.A, &xQx.A, choice)
  112. fp751ConditionalSwap(&xPx.B, &xQx.B, choice)
  113. fp751ConditionalSwap(&xPz.A, &xQz.A, choice)
  114. fp751ConditionalSwap(&xPz.B, &xQz.B, choice)
  115. }
  116. // Converts values in x.A and x.B to Montgomery domain
  117. // x.A = x.A * R mod p
  118. // x.B = x.B * R mod p
  119. func (fp751Ops) ToMontgomery(x *Fp2Element) {
  120. var aRR FpElementX2
  121. // convert to montgomery domain
  122. fp751Mul(&aRR, &x.A, &p751R2) // = a*R*R
  123. fp751MontgomeryReduce(&x.A, &aRR) // = a*R mod p
  124. fp751Mul(&aRR, &x.B, &p751R2)
  125. fp751MontgomeryReduce(&x.B, &aRR)
  126. }
  127. // Converts values in x.A and x.B from Montgomery domain
  128. // a = x.A mod p
  129. // b = x.B mod p
  130. //
  131. // After returning from the call x is not modified.
  132. func (fp751Ops) FromMontgomery(x *Fp2Element, out *Fp2Element) {
  133. var aR FpElementX2
  134. // convert from montgomery domain
  135. copy(aR[:], x.A[:])
  136. fp751MontgomeryReduce(&out.A, &aR) // = a mod p in [0, 2p)
  137. fp751StrongReduce(&out.A) // = a mod p in [0, p)
  138. for i := range aR {
  139. aR[i] = 0
  140. }
  141. copy(aR[:], x.B[:])
  142. fp751MontgomeryReduce(&out.B, &aR)
  143. fp751StrongReduce(&out.B)
  144. }
  145. //------------------------------------------------------------------------------
  146. // Prime Field
  147. //------------------------------------------------------------------------------
  148. // Represents an element of the prime field F_p in Montgomery domain
  149. type primeFieldElement struct {
  150. // The value `A`is represented by `aR mod p`.
  151. A FpElement
  152. }
  153. // Set dest = lhs * rhs.
  154. //
  155. // Allowed to overlap lhs or rhs with dest.
  156. //
  157. // Returns dest to allow chaining operations.
  158. func (dest *primeFieldElement) Mul(lhs, rhs *primeFieldElement) *primeFieldElement {
  159. a := &lhs.A // = a*R
  160. b := &rhs.A // = b*R
  161. var ab FpElementX2
  162. fp751Mul(&ab, a, b) // = a*b*R*R
  163. fp751MontgomeryReduce(&dest.A, &ab) // = a*b*R mod p
  164. return dest
  165. }
  166. // Set dest = x^(2^k), for k >= 1, by repeated squarings.
  167. //
  168. // Allowed to overlap x with dest.
  169. //
  170. // Returns dest to allow chaining operations.
  171. func (dest *primeFieldElement) Pow2k(x *primeFieldElement, k uint8) *primeFieldElement {
  172. dest.Mul(x, x)
  173. for i := uint8(1); i < k; i++ {
  174. dest.Mul(dest, dest)
  175. }
  176. return dest
  177. }
  178. // Set dest = x^((p-3)/4). If x is square, this is 1/sqrt(x).
  179. //
  180. // Allowed to overlap x with dest.
  181. //
  182. // Returns dest to allow chaining operations.
  183. func (dest *primeFieldElement) P34(x *primeFieldElement) *primeFieldElement {
  184. // Sliding-window strategy computed with Sage, awk, sed, and tr.
  185. //
  186. // This performs sum(powStrategy) = 744 squarings and len(mulStrategy)
  187. // = 137 multiplications, in addition to 1 squaring and 15
  188. // multiplications to build a lookup table.
  189. //
  190. // In total this is 745 squarings, 152 multiplications. Since squaring
  191. // is not implemented for the prime field, this is 897 multiplications
  192. // in total.
  193. powStrategy := [137]uint8{5, 7, 6, 2, 10, 4, 6, 9, 8, 5, 9, 4, 7, 5, 5, 4, 8, 3, 9, 5, 5, 4, 10, 4, 6, 6, 6, 5, 8, 9, 3, 4, 9, 4, 5, 6, 6, 2, 9, 4, 5, 5, 5, 7, 7, 9, 4, 6, 4, 8, 5, 8, 6, 6, 2, 9, 7, 4, 8, 8, 8, 4, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2}
  194. mulStrategy := [137]uint8{31, 23, 21, 1, 31, 7, 7, 7, 9, 9, 19, 15, 23, 23, 11, 7, 25, 5, 21, 17, 11, 5, 17, 7, 11, 9, 23, 9, 1, 19, 5, 3, 25, 15, 11, 29, 31, 1, 29, 11, 13, 9, 11, 27, 13, 19, 15, 31, 3, 29, 23, 31, 25, 11, 1, 21, 19, 15, 15, 21, 29, 13, 23, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 3}
  195. initialMul := uint8(27)
  196. // Build a lookup table of odd multiples of x.
  197. lookup := [16]primeFieldElement{}
  198. xx := &primeFieldElement{}
  199. xx.Mul(x, x) // Set xx = x^2
  200. lookup[0] = *x
  201. for i := 1; i < 16; i++ {
  202. lookup[i].Mul(&lookup[i-1], xx)
  203. }
  204. // Now lookup = {x, x^3, x^5, ... }
  205. // so that lookup[i] = x^{2*i + 1}
  206. // so that lookup[k/2] = x^k, for odd k
  207. *dest = lookup[initialMul/2]
  208. for i := uint8(0); i < 137; i++ {
  209. dest.Pow2k(dest, powStrategy[i])
  210. dest.Mul(dest, &lookup[mulStrategy[i]/2])
  211. }
  212. return dest
  213. }