Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

433 рядки
14 KiB

  1. package p751toolbox
  2. //------------------------------------------------------------------------------
  3. // Extension Field
  4. //------------------------------------------------------------------------------
  5. // Represents an element of the extension field F_{p^2}.
  6. type ExtensionFieldElement struct {
  7. // This field element is in Montgomery form, so that the value `A` is
  8. // represented by `aR mod p`.
  9. A Fp751Element
  10. // This field element is in Montgomery form, so that the value `B` is
  11. // represented by `bR mod p`.
  12. B Fp751Element
  13. }
  14. var zeroExtensionField = ExtensionFieldElement{
  15. A: Fp751Element{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
  16. B: Fp751Element{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
  17. }
  18. var oneExtensionField = ExtensionFieldElement{
  19. A: Fp751Element{0x249ad, 0x0, 0x0, 0x0, 0x0, 0x8310000000000000, 0x5527b1e4375c6c66, 0x697797bf3f4f24d0, 0xc89db7b2ac5c4e2e, 0x4ca4b439d2076956, 0x10f7926c7512c7e9, 0x2d5b24bce5e2},
  20. B: Fp751Element{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
  21. }
  22. // 2*p751
  23. var p751x2 = Fp751Element{
  24. 0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
  25. 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xDD5FFFFFFFFFFFFF,
  26. 0xC7D92D0A93F0F151, 0xB52B363427EF98ED, 0x109D30CFADD7D0ED,
  27. 0x0AC56A08B964AE90, 0x1C25213F2F75B8CD, 0x0000DFCBAA83EE38}
  28. // p751
  29. var p751 = Fp751Element{
  30. 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
  31. 0xffffffffffffffff, 0xffffffffffffffff, 0xeeafffffffffffff,
  32. 0xe3ec968549f878a8, 0xda959b1a13f7cc76, 0x084e9867d6ebe876,
  33. 0x8562b5045cb25748, 0x0e12909f97badc66, 0x00006fe5d541f71c}
  34. // p751 + 1
  35. var p751p1 = Fp751Element{
  36. 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  37. 0x0000000000000000, 0x0000000000000000, 0xeeb0000000000000,
  38. 0xe3ec968549f878a8, 0xda959b1a13f7cc76, 0x084e9867d6ebe876,
  39. 0x8562b5045cb25748, 0x0e12909f97badc66, 0x00006fe5d541f71c}
  40. // Set dest = 0.
  41. //
  42. // Returns dest to allow chaining operations.
  43. func (dest *ExtensionFieldElement) Zero() *ExtensionFieldElement {
  44. *dest = zeroExtensionField
  45. return dest
  46. }
  47. // Set dest = 1.
  48. //
  49. // Returns dest to allow chaining operations.
  50. func (dest *ExtensionFieldElement) One() *ExtensionFieldElement {
  51. *dest = oneExtensionField
  52. return dest
  53. }
  54. // Set dest = lhs * rhs.
  55. //
  56. // Allowed to overlap lhs or rhs with dest.
  57. //
  58. // Returns dest to allow chaining operations.
  59. func (dest *ExtensionFieldElement) Mul(lhs, rhs *ExtensionFieldElement) *ExtensionFieldElement {
  60. // Let (a,b,c,d) = (lhs.a,lhs.b,rhs.a,rhs.b).
  61. a := &lhs.A
  62. b := &lhs.B
  63. c := &rhs.A
  64. d := &rhs.B
  65. // We want to compute
  66. //
  67. // (a + bi)*(c + di) = (a*c - b*d) + (a*d + b*c)i
  68. //
  69. // Use Karatsuba's trick: note that
  70. //
  71. // (b - a)*(c - d) = (b*c + a*d) - a*c - b*d
  72. //
  73. // so (a*d + b*c) = (b-a)*(c-d) + a*c + b*d.
  74. var ac, bd fp751X2
  75. fp751Mul(&ac, a, c) // = a*c*R*R
  76. fp751Mul(&bd, b, d) // = b*d*R*R
  77. var b_minus_a, c_minus_d Fp751Element
  78. fp751SubReduced(&b_minus_a, b, a) // = (b-a)*R
  79. fp751SubReduced(&c_minus_d, c, d) // = (c-d)*R
  80. var ad_plus_bc fp751X2
  81. fp751Mul(&ad_plus_bc, &b_minus_a, &c_minus_d) // = (b-a)*(c-d)*R*R
  82. fp751X2AddLazy(&ad_plus_bc, &ad_plus_bc, &ac) // = ((b-a)*(c-d) + a*c)*R*R
  83. fp751X2AddLazy(&ad_plus_bc, &ad_plus_bc, &bd) // = ((b-a)*(c-d) + a*c + b*d)*R*R
  84. fp751MontgomeryReduce(&dest.B, &ad_plus_bc) // = (a*d + b*c)*R mod p
  85. var ac_minus_bd fp751X2
  86. fp751X2SubLazy(&ac_minus_bd, &ac, &bd) // = (a*c - b*d)*R*R
  87. fp751MontgomeryReduce(&dest.A, &ac_minus_bd) // = (a*c - b*d)*R mod p
  88. return dest
  89. }
  90. // Set dest = 1/x
  91. //
  92. // Allowed to overlap dest with x.
  93. //
  94. // Returns dest to allow chaining operations.
  95. func (dest *ExtensionFieldElement) Inv(x *ExtensionFieldElement) *ExtensionFieldElement {
  96. a := &x.A
  97. b := &x.B
  98. // We want to compute
  99. //
  100. // 1 1 (a - bi) (a - bi)
  101. // -------- = -------- -------- = -----------
  102. // (a + bi) (a + bi) (a - bi) (a^2 + b^2)
  103. //
  104. // Letting c = 1/(a^2 + b^2), this is
  105. //
  106. // 1/(a+bi) = a*c - b*ci.
  107. var asq_plus_bsq PrimeFieldElement
  108. var asq, bsq fp751X2
  109. fp751Mul(&asq, a, a) // = a*a*R*R
  110. fp751Mul(&bsq, b, b) // = b*b*R*R
  111. fp751X2AddLazy(&asq, &asq, &bsq) // = (a^2 + b^2)*R*R
  112. fp751MontgomeryReduce(&asq_plus_bsq.A, &asq) // = (a^2 + b^2)*R mod p
  113. // Now asq_plus_bsq = a^2 + b^2
  114. var asq_plus_bsq_inv PrimeFieldElement
  115. asq_plus_bsq_inv.Inv(&asq_plus_bsq)
  116. c := &asq_plus_bsq_inv.A
  117. var ac fp751X2
  118. fp751Mul(&ac, a, c)
  119. fp751MontgomeryReduce(&dest.A, &ac)
  120. var minus_b Fp751Element
  121. fp751SubReduced(&minus_b, &minus_b, b)
  122. var minus_bc fp751X2
  123. fp751Mul(&minus_bc, &minus_b, c)
  124. fp751MontgomeryReduce(&dest.B, &minus_bc)
  125. return dest
  126. }
  127. // Set (y1, y2, y3) = (1/x1, 1/x2, 1/x3).
  128. //
  129. // All xi, yi must be distinct.
  130. func ExtensionFieldBatch3Inv(x1, x2, x3, y1, y2, y3 *ExtensionFieldElement) {
  131. var x1x2, t ExtensionFieldElement
  132. x1x2.Mul(x1, x2) // x1*x2
  133. t.Mul(&x1x2, x3).Inv(&t) // 1/(x1*x2*x3)
  134. y1.Mul(&t, x2).Mul(y1, x3) // 1/x1
  135. y2.Mul(&t, x1).Mul(y2, x3) // 1/x2
  136. y3.Mul(&t, &x1x2) // 1/x3
  137. }
  138. // Set dest = x * x
  139. //
  140. // Allowed to overlap dest with x.
  141. //
  142. // Returns dest to allow chaining operations.
  143. func (dest *ExtensionFieldElement) Square(x *ExtensionFieldElement) *ExtensionFieldElement {
  144. a := &x.A
  145. b := &x.B
  146. // We want to compute
  147. //
  148. // (a + bi)*(a + bi) = (a^2 - b^2) + 2abi.
  149. var a2, a_plus_b, a_minus_b Fp751Element
  150. fp751AddReduced(&a2, a, a) // = a*R + a*R = 2*a*R
  151. fp751AddReduced(&a_plus_b, a, b) // = a*R + b*R = (a+b)*R
  152. fp751SubReduced(&a_minus_b, a, b) // = a*R - b*R = (a-b)*R
  153. var asq_minus_bsq, ab2 fp751X2
  154. fp751Mul(&asq_minus_bsq, &a_plus_b, &a_minus_b) // = (a+b)*(a-b)*R*R = (a^2 - b^2)*R*R
  155. fp751Mul(&ab2, &a2, b) // = 2*a*b*R*R
  156. fp751MontgomeryReduce(&dest.A, &asq_minus_bsq) // = (a^2 - b^2)*R mod p
  157. fp751MontgomeryReduce(&dest.B, &ab2) // = 2*a*b*R mod p
  158. return dest
  159. }
  160. // Set dest = lhs + rhs.
  161. //
  162. // Allowed to overlap lhs or rhs with dest.
  163. //
  164. // Returns dest to allow chaining operations.
  165. func (dest *ExtensionFieldElement) Add(lhs, rhs *ExtensionFieldElement) *ExtensionFieldElement {
  166. fp751AddReduced(&dest.A, &lhs.A, &rhs.A)
  167. fp751AddReduced(&dest.B, &lhs.B, &rhs.B)
  168. return dest
  169. }
  170. // Set dest = lhs - rhs.
  171. //
  172. // Allowed to overlap lhs or rhs with dest.
  173. //
  174. // Returns dest to allow chaining operations.
  175. func (dest *ExtensionFieldElement) Sub(lhs, rhs *ExtensionFieldElement) *ExtensionFieldElement {
  176. fp751SubReduced(&dest.A, &lhs.A, &rhs.A)
  177. fp751SubReduced(&dest.B, &lhs.B, &rhs.B)
  178. return dest
  179. }
  180. // If choice = 1u8, set (x,y) = (y,x). If choice = 0u8, set (x,y) = (x,y).
  181. //
  182. // Returns dest to allow chaining operations.
  183. func ExtensionFieldConditionalSwap(x, y *ExtensionFieldElement, choice uint8) {
  184. fp751ConditionalSwap(&x.A, &y.A, choice)
  185. fp751ConditionalSwap(&x.B, &y.B, choice)
  186. }
  187. // Returns true if lhs = rhs. Takes variable time.
  188. func (lhs *ExtensionFieldElement) VartimeEq(rhs *ExtensionFieldElement) bool {
  189. return lhs.A.vartimeEq(rhs.A) && lhs.B.vartimeEq(rhs.B)
  190. }
  191. // Convert the input to wire format.
  192. //
  193. // The output byte slice must be at least 188 bytes long.
  194. func (x *ExtensionFieldElement) ToBytes(output []byte) {
  195. if len(output) < 188 {
  196. panic("output byte slice too short, need 188 bytes")
  197. }
  198. x.A.toBytesFromMontgomeryForm(output[0:94])
  199. x.B.toBytesFromMontgomeryForm(output[94:188])
  200. }
  201. // Read 188 bytes into the given ExtensionFieldElement.
  202. //
  203. // It is an error to call this function if the input byte slice is less than 188 bytes long.
  204. func (x *ExtensionFieldElement) FromBytes(input []byte) {
  205. if len(input) < 188 {
  206. panic("input byte slice too short, need 188 bytes")
  207. }
  208. x.A.montgomeryFormFromBytes(input[:94])
  209. x.B.montgomeryFormFromBytes(input[94:188])
  210. }
  211. //------------------------------------------------------------------------------
  212. // Prime Field
  213. //------------------------------------------------------------------------------
  214. // Represents an element of the prime field F_p.
  215. type PrimeFieldElement struct {
  216. // This field element is in Montgomery form, so that the value `A` is
  217. // represented by `aR mod p`.
  218. A Fp751Element
  219. }
  220. var zeroPrimeField = PrimeFieldElement{
  221. A: Fp751Element{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
  222. }
  223. var onePrimeField = PrimeFieldElement{
  224. A: Fp751Element{0x249ad, 0x0, 0x0, 0x0, 0x0, 0x8310000000000000, 0x5527b1e4375c6c66, 0x697797bf3f4f24d0, 0xc89db7b2ac5c4e2e, 0x4ca4b439d2076956, 0x10f7926c7512c7e9, 0x2d5b24bce5e2},
  225. }
  226. // Set dest = lhs * rhs.
  227. //
  228. // Allowed to overlap lhs or rhs with dest.
  229. //
  230. // Returns dest to allow chaining operations.
  231. func (dest *PrimeFieldElement) Mul(lhs, rhs *PrimeFieldElement) *PrimeFieldElement {
  232. a := &lhs.A // = a*R
  233. b := &rhs.A // = b*R
  234. var ab fp751X2
  235. fp751Mul(&ab, a, b) // = a*b*R*R
  236. fp751MontgomeryReduce(&dest.A, &ab) // = a*b*R mod p
  237. return dest
  238. }
  239. // Set dest = x^(2^k), for k >= 1, by repeated squarings.
  240. //
  241. // Allowed to overlap x with dest.
  242. //
  243. // Returns dest to allow chaining operations.
  244. func (dest *PrimeFieldElement) Pow2k(x *PrimeFieldElement, k uint8) *PrimeFieldElement {
  245. dest.Mul(x, x)
  246. for i := uint8(1); i < k; i++ {
  247. dest.Mul(dest, dest)
  248. }
  249. return dest
  250. }
  251. // Set dest = 1/x.
  252. //
  253. // Allowed to overlap x with dest.
  254. //
  255. // Returns dest to allow chaining operations.
  256. func (dest *PrimeFieldElement) Inv(x *PrimeFieldElement) *PrimeFieldElement {
  257. tmp_x := *x // Copy x in case dest == x
  258. dest.Mul(x, x) // dest = x^2
  259. dest.P34(dest) // dest = (x^2)^((p-3)/4) = x^((p-3)/2)
  260. dest.Mul(dest, dest) // dest = x^(p-3)
  261. dest.Mul(dest, &tmp_x) // dest = x^(p-2)
  262. return dest
  263. }
  264. // Set dest = x^((p-3)/4). If x is square, this is 1/sqrt(x).
  265. //
  266. // Allowed to overlap x with dest.
  267. //
  268. // Returns dest to allow chaining operations.
  269. func (dest *PrimeFieldElement) P34(x *PrimeFieldElement) *PrimeFieldElement {
  270. // Sliding-window strategy computed with Sage, awk, sed, and tr.
  271. //
  272. // This performs sum(powStrategy) = 744 squarings and len(mulStrategy)
  273. // = 137 multiplications, in addition to 1 squaring and 15
  274. // multiplications to build a lookup table.
  275. //
  276. // In total this is 745 squarings, 152 multiplications. Since squaring
  277. // is not implemented for the prime field, this is 897 multiplications
  278. // in total.
  279. 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}
  280. 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}
  281. initialMul := uint8(27)
  282. // Build a lookup table of odd multiples of x.
  283. lookup := [16]PrimeFieldElement{}
  284. xx := &PrimeFieldElement{}
  285. xx.Mul(x, x) // Set xx = x^2
  286. lookup[0] = *x
  287. for i := 1; i < 16; i++ {
  288. lookup[i].Mul(&lookup[i-1], xx)
  289. }
  290. // Now lookup = {x, x^3, x^5, ... }
  291. // so that lookup[i] = x^{2*i + 1}
  292. // so that lookup[k/2] = x^k, for odd k
  293. *dest = lookup[initialMul/2]
  294. for i := uint8(0); i < 137; i++ {
  295. dest.Pow2k(dest, powStrategy[i])
  296. dest.Mul(dest, &lookup[mulStrategy[i]/2])
  297. }
  298. return dest
  299. }
  300. //------------------------------------------------------------------------------
  301. // Internals
  302. //------------------------------------------------------------------------------
  303. const fp751NumWords = 12
  304. // (2^768)^2 mod p
  305. // This can't be a constant because Go doesn't allow array constants, so try
  306. // not to modify it.
  307. var montgomeryRsq = Fp751Element{2535603850726686808, 15780896088201250090, 6788776303855402382, 17585428585582356230, 5274503137951975249, 2266259624764636289, 11695651972693921304, 13072885652150159301, 4908312795585420432, 6229583484603254826, 488927695601805643, 72213483953973}
  308. // Internal representation of an element of the base field F_p.
  309. //
  310. // This type is distinct from PrimeFieldElement in that no particular meaning
  311. // is assigned to the representation -- it could represent an element in
  312. // Montgomery form, or not. Tracking the meaning of the field element is left
  313. // to higher types.
  314. type Fp751Element [fp751NumWords]uint64
  315. // Represents an intermediate product of two elements of the base field F_p.
  316. type fp751X2 [2 * fp751NumWords]uint64
  317. func (x Fp751Element) vartimeEq(y Fp751Element) bool {
  318. fp751StrongReduce(&x)
  319. fp751StrongReduce(&y)
  320. eq := true
  321. for i := 0; i < fp751NumWords; i++ {
  322. eq = (x[i] == y[i]) && eq
  323. }
  324. return eq
  325. }
  326. // Read an Fp751Element from little-endian bytes and convert to Montgomery form.
  327. //
  328. // The input byte slice must be at least 94 bytes long.
  329. func (x *Fp751Element) montgomeryFormFromBytes(input []byte) {
  330. if len(input) < 94 {
  331. panic("input byte slice too short")
  332. }
  333. var a Fp751Element
  334. for i := 0; i < 94; i++ {
  335. // set i = j*8 + k
  336. j := i / 8
  337. k := uint64(i % 8)
  338. a[j] |= uint64(input[i]) << (8 * k)
  339. }
  340. var aRR fp751X2
  341. fp751Mul(&aRR, &a, &montgomeryRsq) // = a*R*R
  342. fp751MontgomeryReduce(x, &aRR) // = a*R mod p
  343. }
  344. // Given an Fp751Element in Montgomery form, convert to little-endian bytes.
  345. //
  346. // The output byte slice must be at least 94 bytes long.
  347. func (x *Fp751Element) toBytesFromMontgomeryForm(output []byte) {
  348. if len(output) < 94 {
  349. panic("output byte slice too short")
  350. }
  351. var a Fp751Element
  352. var aR fp751X2
  353. copy(aR[:], x[:]) // = a*R
  354. fp751MontgomeryReduce(&a, &aR) // = a mod p in [0, 2p)
  355. fp751StrongReduce(&a) // = a mod p in [0, p)
  356. // 8*12 = 96, but we drop the last two bytes since p is 751 < 752=94*8 bits.
  357. for i := 0; i < 94; i++ {
  358. // set i = j*8 + k
  359. j := i / 8
  360. k := uint64(i % 8)
  361. // Need parens because Go's operator precedence would interpret
  362. // a[j] >> 8*k as (a[j] >> 8) * k
  363. output[i] = byte(a[j] >> (8 * k))
  364. }
  365. }