本站源代码
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.

1772 lines
41KB

  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package edwards25519
  5. // This code is a port of the public domain, “ref10” implementation of ed25519
  6. // from SUPERCOP.
  7. // FieldElement represents an element of the field GF(2^255 - 19). An element
  8. // t, entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
  9. // t[3]+2^102 t[4]+...+2^230 t[9]. Bounds on each t[i] vary depending on
  10. // context.
  11. type FieldElement [10]int32
  12. var zero FieldElement
  13. func FeZero(fe *FieldElement) {
  14. copy(fe[:], zero[:])
  15. }
  16. func FeOne(fe *FieldElement) {
  17. FeZero(fe)
  18. fe[0] = 1
  19. }
  20. func FeAdd(dst, a, b *FieldElement) {
  21. dst[0] = a[0] + b[0]
  22. dst[1] = a[1] + b[1]
  23. dst[2] = a[2] + b[2]
  24. dst[3] = a[3] + b[3]
  25. dst[4] = a[4] + b[4]
  26. dst[5] = a[5] + b[5]
  27. dst[6] = a[6] + b[6]
  28. dst[7] = a[7] + b[7]
  29. dst[8] = a[8] + b[8]
  30. dst[9] = a[9] + b[9]
  31. }
  32. func FeSub(dst, a, b *FieldElement) {
  33. dst[0] = a[0] - b[0]
  34. dst[1] = a[1] - b[1]
  35. dst[2] = a[2] - b[2]
  36. dst[3] = a[3] - b[3]
  37. dst[4] = a[4] - b[4]
  38. dst[5] = a[5] - b[5]
  39. dst[6] = a[6] - b[6]
  40. dst[7] = a[7] - b[7]
  41. dst[8] = a[8] - b[8]
  42. dst[9] = a[9] - b[9]
  43. }
  44. func FeCopy(dst, src *FieldElement) {
  45. copy(dst[:], src[:])
  46. }
  47. // Replace (f,g) with (g,g) if b == 1;
  48. // replace (f,g) with (f,g) if b == 0.
  49. //
  50. // Preconditions: b in {0,1}.
  51. func FeCMove(f, g *FieldElement, b int32) {
  52. b = -b
  53. f[0] ^= b & (f[0] ^ g[0])
  54. f[1] ^= b & (f[1] ^ g[1])
  55. f[2] ^= b & (f[2] ^ g[2])
  56. f[3] ^= b & (f[3] ^ g[3])
  57. f[4] ^= b & (f[4] ^ g[4])
  58. f[5] ^= b & (f[5] ^ g[5])
  59. f[6] ^= b & (f[6] ^ g[6])
  60. f[7] ^= b & (f[7] ^ g[7])
  61. f[8] ^= b & (f[8] ^ g[8])
  62. f[9] ^= b & (f[9] ^ g[9])
  63. }
  64. func load3(in []byte) int64 {
  65. var r int64
  66. r = int64(in[0])
  67. r |= int64(in[1]) << 8
  68. r |= int64(in[2]) << 16
  69. return r
  70. }
  71. func load4(in []byte) int64 {
  72. var r int64
  73. r = int64(in[0])
  74. r |= int64(in[1]) << 8
  75. r |= int64(in[2]) << 16
  76. r |= int64(in[3]) << 24
  77. return r
  78. }
  79. func FeFromBytes(dst *FieldElement, src *[32]byte) {
  80. h0 := load4(src[:])
  81. h1 := load3(src[4:]) << 6
  82. h2 := load3(src[7:]) << 5
  83. h3 := load3(src[10:]) << 3
  84. h4 := load3(src[13:]) << 2
  85. h5 := load4(src[16:])
  86. h6 := load3(src[20:]) << 7
  87. h7 := load3(src[23:]) << 5
  88. h8 := load3(src[26:]) << 4
  89. h9 := (load3(src[29:]) & 8388607) << 2
  90. FeCombine(dst, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9)
  91. }
  92. // FeToBytes marshals h to s.
  93. // Preconditions:
  94. // |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
  95. //
  96. // Write p=2^255-19; q=floor(h/p).
  97. // Basic claim: q = floor(2^(-255)(h + 19 2^(-25)h9 + 2^(-1))).
  98. //
  99. // Proof:
  100. // Have |h|<=p so |q|<=1 so |19^2 2^(-255) q|<1/4.
  101. // Also have |h-2^230 h9|<2^230 so |19 2^(-255)(h-2^230 h9)|<1/4.
  102. //
  103. // Write y=2^(-1)-19^2 2^(-255)q-19 2^(-255)(h-2^230 h9).
  104. // Then 0<y<1.
  105. //
  106. // Write r=h-pq.
  107. // Have 0<=r<=p-1=2^255-20.
  108. // Thus 0<=r+19(2^-255)r<r+19(2^-255)2^255<=2^255-1.
  109. //
  110. // Write x=r+19(2^-255)r+y.
  111. // Then 0<x<2^255 so floor(2^(-255)x) = 0 so floor(q+2^(-255)x) = q.
  112. //
  113. // Have q+2^(-255)x = 2^(-255)(h + 19 2^(-25) h9 + 2^(-1))
  114. // so floor(2^(-255)(h + 19 2^(-25) h9 + 2^(-1))) = q.
  115. func FeToBytes(s *[32]byte, h *FieldElement) {
  116. var carry [10]int32
  117. q := (19*h[9] + (1 << 24)) >> 25
  118. q = (h[0] + q) >> 26
  119. q = (h[1] + q) >> 25
  120. q = (h[2] + q) >> 26
  121. q = (h[3] + q) >> 25
  122. q = (h[4] + q) >> 26
  123. q = (h[5] + q) >> 25
  124. q = (h[6] + q) >> 26
  125. q = (h[7] + q) >> 25
  126. q = (h[8] + q) >> 26
  127. q = (h[9] + q) >> 25
  128. // Goal: Output h-(2^255-19)q, which is between 0 and 2^255-20.
  129. h[0] += 19 * q
  130. // Goal: Output h-2^255 q, which is between 0 and 2^255-20.
  131. carry[0] = h[0] >> 26
  132. h[1] += carry[0]
  133. h[0] -= carry[0] << 26
  134. carry[1] = h[1] >> 25
  135. h[2] += carry[1]
  136. h[1] -= carry[1] << 25
  137. carry[2] = h[2] >> 26
  138. h[3] += carry[2]
  139. h[2] -= carry[2] << 26
  140. carry[3] = h[3] >> 25
  141. h[4] += carry[3]
  142. h[3] -= carry[3] << 25
  143. carry[4] = h[4] >> 26
  144. h[5] += carry[4]
  145. h[4] -= carry[4] << 26
  146. carry[5] = h[5] >> 25
  147. h[6] += carry[5]
  148. h[5] -= carry[5] << 25
  149. carry[6] = h[6] >> 26
  150. h[7] += carry[6]
  151. h[6] -= carry[6] << 26
  152. carry[7] = h[7] >> 25
  153. h[8] += carry[7]
  154. h[7] -= carry[7] << 25
  155. carry[8] = h[8] >> 26
  156. h[9] += carry[8]
  157. h[8] -= carry[8] << 26
  158. carry[9] = h[9] >> 25
  159. h[9] -= carry[9] << 25
  160. // h10 = carry9
  161. // Goal: Output h[0]+...+2^255 h10-2^255 q, which is between 0 and 2^255-20.
  162. // Have h[0]+...+2^230 h[9] between 0 and 2^255-1;
  163. // evidently 2^255 h10-2^255 q = 0.
  164. // Goal: Output h[0]+...+2^230 h[9].
  165. s[0] = byte(h[0] >> 0)
  166. s[1] = byte(h[0] >> 8)
  167. s[2] = byte(h[0] >> 16)
  168. s[3] = byte((h[0] >> 24) | (h[1] << 2))
  169. s[4] = byte(h[1] >> 6)
  170. s[5] = byte(h[1] >> 14)
  171. s[6] = byte((h[1] >> 22) | (h[2] << 3))
  172. s[7] = byte(h[2] >> 5)
  173. s[8] = byte(h[2] >> 13)
  174. s[9] = byte((h[2] >> 21) | (h[3] << 5))
  175. s[10] = byte(h[3] >> 3)
  176. s[11] = byte(h[3] >> 11)
  177. s[12] = byte((h[3] >> 19) | (h[4] << 6))
  178. s[13] = byte(h[4] >> 2)
  179. s[14] = byte(h[4] >> 10)
  180. s[15] = byte(h[4] >> 18)
  181. s[16] = byte(h[5] >> 0)
  182. s[17] = byte(h[5] >> 8)
  183. s[18] = byte(h[5] >> 16)
  184. s[19] = byte((h[5] >> 24) | (h[6] << 1))
  185. s[20] = byte(h[6] >> 7)
  186. s[21] = byte(h[6] >> 15)
  187. s[22] = byte((h[6] >> 23) | (h[7] << 3))
  188. s[23] = byte(h[7] >> 5)
  189. s[24] = byte(h[7] >> 13)
  190. s[25] = byte((h[7] >> 21) | (h[8] << 4))
  191. s[26] = byte(h[8] >> 4)
  192. s[27] = byte(h[8] >> 12)
  193. s[28] = byte((h[8] >> 20) | (h[9] << 6))
  194. s[29] = byte(h[9] >> 2)
  195. s[30] = byte(h[9] >> 10)
  196. s[31] = byte(h[9] >> 18)
  197. }
  198. func FeIsNegative(f *FieldElement) byte {
  199. var s [32]byte
  200. FeToBytes(&s, f)
  201. return s[0] & 1
  202. }
  203. func FeIsNonZero(f *FieldElement) int32 {
  204. var s [32]byte
  205. FeToBytes(&s, f)
  206. var x uint8
  207. for _, b := range s {
  208. x |= b
  209. }
  210. x |= x >> 4
  211. x |= x >> 2
  212. x |= x >> 1
  213. return int32(x & 1)
  214. }
  215. // FeNeg sets h = -f
  216. //
  217. // Preconditions:
  218. // |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
  219. //
  220. // Postconditions:
  221. // |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
  222. func FeNeg(h, f *FieldElement) {
  223. h[0] = -f[0]
  224. h[1] = -f[1]
  225. h[2] = -f[2]
  226. h[3] = -f[3]
  227. h[4] = -f[4]
  228. h[5] = -f[5]
  229. h[6] = -f[6]
  230. h[7] = -f[7]
  231. h[8] = -f[8]
  232. h[9] = -f[9]
  233. }
  234. func FeCombine(h *FieldElement, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 int64) {
  235. var c0, c1, c2, c3, c4, c5, c6, c7, c8, c9 int64
  236. /*
  237. |h0| <= (1.1*1.1*2^52*(1+19+19+19+19)+1.1*1.1*2^50*(38+38+38+38+38))
  238. i.e. |h0| <= 1.2*2^59; narrower ranges for h2, h4, h6, h8
  239. |h1| <= (1.1*1.1*2^51*(1+1+19+19+19+19+19+19+19+19))
  240. i.e. |h1| <= 1.5*2^58; narrower ranges for h3, h5, h7, h9
  241. */
  242. c0 = (h0 + (1 << 25)) >> 26
  243. h1 += c0
  244. h0 -= c0 << 26
  245. c4 = (h4 + (1 << 25)) >> 26
  246. h5 += c4
  247. h4 -= c4 << 26
  248. /* |h0| <= 2^25 */
  249. /* |h4| <= 2^25 */
  250. /* |h1| <= 1.51*2^58 */
  251. /* |h5| <= 1.51*2^58 */
  252. c1 = (h1 + (1 << 24)) >> 25
  253. h2 += c1
  254. h1 -= c1 << 25
  255. c5 = (h5 + (1 << 24)) >> 25
  256. h6 += c5
  257. h5 -= c5 << 25
  258. /* |h1| <= 2^24; from now on fits into int32 */
  259. /* |h5| <= 2^24; from now on fits into int32 */
  260. /* |h2| <= 1.21*2^59 */
  261. /* |h6| <= 1.21*2^59 */
  262. c2 = (h2 + (1 << 25)) >> 26
  263. h3 += c2
  264. h2 -= c2 << 26
  265. c6 = (h6 + (1 << 25)) >> 26
  266. h7 += c6
  267. h6 -= c6 << 26
  268. /* |h2| <= 2^25; from now on fits into int32 unchanged */
  269. /* |h6| <= 2^25; from now on fits into int32 unchanged */
  270. /* |h3| <= 1.51*2^58 */
  271. /* |h7| <= 1.51*2^58 */
  272. c3 = (h3 + (1 << 24)) >> 25
  273. h4 += c3
  274. h3 -= c3 << 25
  275. c7 = (h7 + (1 << 24)) >> 25
  276. h8 += c7
  277. h7 -= c7 << 25
  278. /* |h3| <= 2^24; from now on fits into int32 unchanged */
  279. /* |h7| <= 2^24; from now on fits into int32 unchanged */
  280. /* |h4| <= 1.52*2^33 */
  281. /* |h8| <= 1.52*2^33 */
  282. c4 = (h4 + (1 << 25)) >> 26
  283. h5 += c4
  284. h4 -= c4 << 26
  285. c8 = (h8 + (1 << 25)) >> 26
  286. h9 += c8
  287. h8 -= c8 << 26
  288. /* |h4| <= 2^25; from now on fits into int32 unchanged */
  289. /* |h8| <= 2^25; from now on fits into int32 unchanged */
  290. /* |h5| <= 1.01*2^24 */
  291. /* |h9| <= 1.51*2^58 */
  292. c9 = (h9 + (1 << 24)) >> 25
  293. h0 += c9 * 19
  294. h9 -= c9 << 25
  295. /* |h9| <= 2^24; from now on fits into int32 unchanged */
  296. /* |h0| <= 1.8*2^37 */
  297. c0 = (h0 + (1 << 25)) >> 26
  298. h1 += c0
  299. h0 -= c0 << 26
  300. /* |h0| <= 2^25; from now on fits into int32 unchanged */
  301. /* |h1| <= 1.01*2^24 */
  302. h[0] = int32(h0)
  303. h[1] = int32(h1)
  304. h[2] = int32(h2)
  305. h[3] = int32(h3)
  306. h[4] = int32(h4)
  307. h[5] = int32(h5)
  308. h[6] = int32(h6)
  309. h[7] = int32(h7)
  310. h[8] = int32(h8)
  311. h[9] = int32(h9)
  312. }
  313. // FeMul calculates h = f * g
  314. // Can overlap h with f or g.
  315. //
  316. // Preconditions:
  317. // |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
  318. // |g| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
  319. //
  320. // Postconditions:
  321. // |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
  322. //
  323. // Notes on implementation strategy:
  324. //
  325. // Using schoolbook multiplication.
  326. // Karatsuba would save a little in some cost models.
  327. //
  328. // Most multiplications by 2 and 19 are 32-bit precomputations;
  329. // cheaper than 64-bit postcomputations.
  330. //
  331. // There is one remaining multiplication by 19 in the carry chain;
  332. // one *19 precomputation can be merged into this,
  333. // but the resulting data flow is considerably less clean.
  334. //
  335. // There are 12 carries below.
  336. // 10 of them are 2-way parallelizable and vectorizable.
  337. // Can get away with 11 carries, but then data flow is much deeper.
  338. //
  339. // With tighter constraints on inputs, can squeeze carries into int32.
  340. func FeMul(h, f, g *FieldElement) {
  341. f0 := int64(f[0])
  342. f1 := int64(f[1])
  343. f2 := int64(f[2])
  344. f3 := int64(f[3])
  345. f4 := int64(f[4])
  346. f5 := int64(f[5])
  347. f6 := int64(f[6])
  348. f7 := int64(f[7])
  349. f8 := int64(f[8])
  350. f9 := int64(f[9])
  351. f1_2 := int64(2 * f[1])
  352. f3_2 := int64(2 * f[3])
  353. f5_2 := int64(2 * f[5])
  354. f7_2 := int64(2 * f[7])
  355. f9_2 := int64(2 * f[9])
  356. g0 := int64(g[0])
  357. g1 := int64(g[1])
  358. g2 := int64(g[2])
  359. g3 := int64(g[3])
  360. g4 := int64(g[4])
  361. g5 := int64(g[5])
  362. g6 := int64(g[6])
  363. g7 := int64(g[7])
  364. g8 := int64(g[8])
  365. g9 := int64(g[9])
  366. g1_19 := int64(19 * g[1]) /* 1.4*2^29 */
  367. g2_19 := int64(19 * g[2]) /* 1.4*2^30; still ok */
  368. g3_19 := int64(19 * g[3])
  369. g4_19 := int64(19 * g[4])
  370. g5_19 := int64(19 * g[5])
  371. g6_19 := int64(19 * g[6])
  372. g7_19 := int64(19 * g[7])
  373. g8_19 := int64(19 * g[8])
  374. g9_19 := int64(19 * g[9])
  375. h0 := f0*g0 + f1_2*g9_19 + f2*g8_19 + f3_2*g7_19 + f4*g6_19 + f5_2*g5_19 + f6*g4_19 + f7_2*g3_19 + f8*g2_19 + f9_2*g1_19
  376. h1 := f0*g1 + f1*g0 + f2*g9_19 + f3*g8_19 + f4*g7_19 + f5*g6_19 + f6*g5_19 + f7*g4_19 + f8*g3_19 + f9*g2_19
  377. h2 := f0*g2 + f1_2*g1 + f2*g0 + f3_2*g9_19 + f4*g8_19 + f5_2*g7_19 + f6*g6_19 + f7_2*g5_19 + f8*g4_19 + f9_2*g3_19
  378. h3 := f0*g3 + f1*g2 + f2*g1 + f3*g0 + f4*g9_19 + f5*g8_19 + f6*g7_19 + f7*g6_19 + f8*g5_19 + f9*g4_19
  379. h4 := f0*g4 + f1_2*g3 + f2*g2 + f3_2*g1 + f4*g0 + f5_2*g9_19 + f6*g8_19 + f7_2*g7_19 + f8*g6_19 + f9_2*g5_19
  380. h5 := f0*g5 + f1*g4 + f2*g3 + f3*g2 + f4*g1 + f5*g0 + f6*g9_19 + f7*g8_19 + f8*g7_19 + f9*g6_19
  381. h6 := f0*g6 + f1_2*g5 + f2*g4 + f3_2*g3 + f4*g2 + f5_2*g1 + f6*g0 + f7_2*g9_19 + f8*g8_19 + f9_2*g7_19
  382. h7 := f0*g7 + f1*g6 + f2*g5 + f3*g4 + f4*g3 + f5*g2 + f6*g1 + f7*g0 + f8*g9_19 + f9*g8_19
  383. h8 := f0*g8 + f1_2*g7 + f2*g6 + f3_2*g5 + f4*g4 + f5_2*g3 + f6*g2 + f7_2*g1 + f8*g0 + f9_2*g9_19
  384. h9 := f0*g9 + f1*g8 + f2*g7 + f3*g6 + f4*g5 + f5*g4 + f6*g3 + f7*g2 + f8*g1 + f9*g0
  385. FeCombine(h, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9)
  386. }
  387. func feSquare(f *FieldElement) (h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 int64) {
  388. f0 := int64(f[0])
  389. f1 := int64(f[1])
  390. f2 := int64(f[2])
  391. f3 := int64(f[3])
  392. f4 := int64(f[4])
  393. f5 := int64(f[5])
  394. f6 := int64(f[6])
  395. f7 := int64(f[7])
  396. f8 := int64(f[8])
  397. f9 := int64(f[9])
  398. f0_2 := int64(2 * f[0])
  399. f1_2 := int64(2 * f[1])
  400. f2_2 := int64(2 * f[2])
  401. f3_2 := int64(2 * f[3])
  402. f4_2 := int64(2 * f[4])
  403. f5_2 := int64(2 * f[5])
  404. f6_2 := int64(2 * f[6])
  405. f7_2 := int64(2 * f[7])
  406. f5_38 := 38 * f5 // 1.31*2^30
  407. f6_19 := 19 * f6 // 1.31*2^30
  408. f7_38 := 38 * f7 // 1.31*2^30
  409. f8_19 := 19 * f8 // 1.31*2^30
  410. f9_38 := 38 * f9 // 1.31*2^30
  411. h0 = f0*f0 + f1_2*f9_38 + f2_2*f8_19 + f3_2*f7_38 + f4_2*f6_19 + f5*f5_38
  412. h1 = f0_2*f1 + f2*f9_38 + f3_2*f8_19 + f4*f7_38 + f5_2*f6_19
  413. h2 = f0_2*f2 + f1_2*f1 + f3_2*f9_38 + f4_2*f8_19 + f5_2*f7_38 + f6*f6_19
  414. h3 = f0_2*f3 + f1_2*f2 + f4*f9_38 + f5_2*f8_19 + f6*f7_38
  415. h4 = f0_2*f4 + f1_2*f3_2 + f2*f2 + f5_2*f9_38 + f6_2*f8_19 + f7*f7_38
  416. h5 = f0_2*f5 + f1_2*f4 + f2_2*f3 + f6*f9_38 + f7_2*f8_19
  417. h6 = f0_2*f6 + f1_2*f5_2 + f2_2*f4 + f3_2*f3 + f7_2*f9_38 + f8*f8_19
  418. h7 = f0_2*f7 + f1_2*f6 + f2_2*f5 + f3_2*f4 + f8*f9_38
  419. h8 = f0_2*f8 + f1_2*f7_2 + f2_2*f6 + f3_2*f5_2 + f4*f4 + f9*f9_38
  420. h9 = f0_2*f9 + f1_2*f8 + f2_2*f7 + f3_2*f6 + f4_2*f5
  421. return
  422. }
  423. // FeSquare calculates h = f*f. Can overlap h with f.
  424. //
  425. // Preconditions:
  426. // |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
  427. //
  428. // Postconditions:
  429. // |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
  430. func FeSquare(h, f *FieldElement) {
  431. h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 := feSquare(f)
  432. FeCombine(h, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9)
  433. }
  434. // FeSquare2 sets h = 2 * f * f
  435. //
  436. // Can overlap h with f.
  437. //
  438. // Preconditions:
  439. // |f| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc.
  440. //
  441. // Postconditions:
  442. // |h| bounded by 1.01*2^25,1.01*2^24,1.01*2^25,1.01*2^24,etc.
  443. // See fe_mul.c for discussion of implementation strategy.
  444. func FeSquare2(h, f *FieldElement) {
  445. h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 := feSquare(f)
  446. h0 += h0
  447. h1 += h1
  448. h2 += h2
  449. h3 += h3
  450. h4 += h4
  451. h5 += h5
  452. h6 += h6
  453. h7 += h7
  454. h8 += h8
  455. h9 += h9
  456. FeCombine(h, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9)
  457. }
  458. func FeInvert(out, z *FieldElement) {
  459. var t0, t1, t2, t3 FieldElement
  460. var i int
  461. FeSquare(&t0, z) // 2^1
  462. FeSquare(&t1, &t0) // 2^2
  463. for i = 1; i < 2; i++ { // 2^3
  464. FeSquare(&t1, &t1)
  465. }
  466. FeMul(&t1, z, &t1) // 2^3 + 2^0
  467. FeMul(&t0, &t0, &t1) // 2^3 + 2^1 + 2^0
  468. FeSquare(&t2, &t0) // 2^4 + 2^2 + 2^1
  469. FeMul(&t1, &t1, &t2) // 2^4 + 2^3 + 2^2 + 2^1 + 2^0
  470. FeSquare(&t2, &t1) // 5,4,3,2,1
  471. for i = 1; i < 5; i++ { // 9,8,7,6,5
  472. FeSquare(&t2, &t2)
  473. }
  474. FeMul(&t1, &t2, &t1) // 9,8,7,6,5,4,3,2,1,0
  475. FeSquare(&t2, &t1) // 10..1
  476. for i = 1; i < 10; i++ { // 19..10
  477. FeSquare(&t2, &t2)
  478. }
  479. FeMul(&t2, &t2, &t1) // 19..0
  480. FeSquare(&t3, &t2) // 20..1
  481. for i = 1; i < 20; i++ { // 39..20
  482. FeSquare(&t3, &t3)
  483. }
  484. FeMul(&t2, &t3, &t2) // 39..0
  485. FeSquare(&t2, &t2) // 40..1
  486. for i = 1; i < 10; i++ { // 49..10
  487. FeSquare(&t2, &t2)
  488. }
  489. FeMul(&t1, &t2, &t1) // 49..0
  490. FeSquare(&t2, &t1) // 50..1
  491. for i = 1; i < 50; i++ { // 99..50
  492. FeSquare(&t2, &t2)
  493. }
  494. FeMul(&t2, &t2, &t1) // 99..0
  495. FeSquare(&t3, &t2) // 100..1
  496. for i = 1; i < 100; i++ { // 199..100
  497. FeSquare(&t3, &t3)
  498. }
  499. FeMul(&t2, &t3, &t2) // 199..0
  500. FeSquare(&t2, &t2) // 200..1
  501. for i = 1; i < 50; i++ { // 249..50
  502. FeSquare(&t2, &t2)
  503. }
  504. FeMul(&t1, &t2, &t1) // 249..0
  505. FeSquare(&t1, &t1) // 250..1
  506. for i = 1; i < 5; i++ { // 254..5
  507. FeSquare(&t1, &t1)
  508. }
  509. FeMul(out, &t1, &t0) // 254..5,3,1,0
  510. }
  511. func fePow22523(out, z *FieldElement) {
  512. var t0, t1, t2 FieldElement
  513. var i int
  514. FeSquare(&t0, z)
  515. for i = 1; i < 1; i++ {
  516. FeSquare(&t0, &t0)
  517. }
  518. FeSquare(&t1, &t0)
  519. for i = 1; i < 2; i++ {
  520. FeSquare(&t1, &t1)
  521. }
  522. FeMul(&t1, z, &t1)
  523. FeMul(&t0, &t0, &t1)
  524. FeSquare(&t0, &t0)
  525. for i = 1; i < 1; i++ {
  526. FeSquare(&t0, &t0)
  527. }
  528. FeMul(&t0, &t1, &t0)
  529. FeSquare(&t1, &t0)
  530. for i = 1; i < 5; i++ {
  531. FeSquare(&t1, &t1)
  532. }
  533. FeMul(&t0, &t1, &t0)
  534. FeSquare(&t1, &t0)
  535. for i = 1; i < 10; i++ {
  536. FeSquare(&t1, &t1)
  537. }
  538. FeMul(&t1, &t1, &t0)
  539. FeSquare(&t2, &t1)
  540. for i = 1; i < 20; i++ {
  541. FeSquare(&t2, &t2)
  542. }
  543. FeMul(&t1, &t2, &t1)
  544. FeSquare(&t1, &t1)
  545. for i = 1; i < 10; i++ {
  546. FeSquare(&t1, &t1)
  547. }
  548. FeMul(&t0, &t1, &t0)
  549. FeSquare(&t1, &t0)
  550. for i = 1; i < 50; i++ {
  551. FeSquare(&t1, &t1)
  552. }
  553. FeMul(&t1, &t1, &t0)
  554. FeSquare(&t2, &t1)
  555. for i = 1; i < 100; i++ {
  556. FeSquare(&t2, &t2)
  557. }
  558. FeMul(&t1, &t2, &t1)
  559. FeSquare(&t1, &t1)
  560. for i = 1; i < 50; i++ {
  561. FeSquare(&t1, &t1)
  562. }
  563. FeMul(&t0, &t1, &t0)
  564. FeSquare(&t0, &t0)
  565. for i = 1; i < 2; i++ {
  566. FeSquare(&t0, &t0)
  567. }
  568. FeMul(out, &t0, z)
  569. }
  570. // Group elements are members of the elliptic curve -x^2 + y^2 = 1 + d * x^2 *
  571. // y^2 where d = -121665/121666.
  572. //
  573. // Several representations are used:
  574. // ProjectiveGroupElement: (X:Y:Z) satisfying x=X/Z, y=Y/Z
  575. // ExtendedGroupElement: (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT
  576. // CompletedGroupElement: ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
  577. // PreComputedGroupElement: (y+x,y-x,2dxy)
  578. type ProjectiveGroupElement struct {
  579. X, Y, Z FieldElement
  580. }
  581. type ExtendedGroupElement struct {
  582. X, Y, Z, T FieldElement
  583. }
  584. type CompletedGroupElement struct {
  585. X, Y, Z, T FieldElement
  586. }
  587. type PreComputedGroupElement struct {
  588. yPlusX, yMinusX, xy2d FieldElement
  589. }
  590. type CachedGroupElement struct {
  591. yPlusX, yMinusX, Z, T2d FieldElement
  592. }
  593. func (p *ProjectiveGroupElement) Zero() {
  594. FeZero(&p.X)
  595. FeOne(&p.Y)
  596. FeOne(&p.Z)
  597. }
  598. func (p *ProjectiveGroupElement) Double(r *CompletedGroupElement) {
  599. var t0 FieldElement
  600. FeSquare(&r.X, &p.X)
  601. FeSquare(&r.Z, &p.Y)
  602. FeSquare2(&r.T, &p.Z)
  603. FeAdd(&r.Y, &p.X, &p.Y)
  604. FeSquare(&t0, &r.Y)
  605. FeAdd(&r.Y, &r.Z, &r.X)
  606. FeSub(&r.Z, &r.Z, &r.X)
  607. FeSub(&r.X, &t0, &r.Y)
  608. FeSub(&r.T, &r.T, &r.Z)
  609. }
  610. func (p *ProjectiveGroupElement) ToBytes(s *[32]byte) {
  611. var recip, x, y FieldElement
  612. FeInvert(&recip, &p.Z)
  613. FeMul(&x, &p.X, &recip)
  614. FeMul(&y, &p.Y, &recip)
  615. FeToBytes(s, &y)
  616. s[31] ^= FeIsNegative(&x) << 7
  617. }
  618. func (p *ExtendedGroupElement) Zero() {
  619. FeZero(&p.X)
  620. FeOne(&p.Y)
  621. FeOne(&p.Z)
  622. FeZero(&p.T)
  623. }
  624. func (p *ExtendedGroupElement) Double(r *CompletedGroupElement) {
  625. var q ProjectiveGroupElement
  626. p.ToProjective(&q)
  627. q.Double(r)
  628. }
  629. func (p *ExtendedGroupElement) ToCached(r *CachedGroupElement) {
  630. FeAdd(&r.yPlusX, &p.Y, &p.X)
  631. FeSub(&r.yMinusX, &p.Y, &p.X)
  632. FeCopy(&r.Z, &p.Z)
  633. FeMul(&r.T2d, &p.T, &d2)
  634. }
  635. func (p *ExtendedGroupElement) ToProjective(r *ProjectiveGroupElement) {
  636. FeCopy(&r.X, &p.X)
  637. FeCopy(&r.Y, &p.Y)
  638. FeCopy(&r.Z, &p.Z)
  639. }
  640. func (p *ExtendedGroupElement) ToBytes(s *[32]byte) {
  641. var recip, x, y FieldElement
  642. FeInvert(&recip, &p.Z)
  643. FeMul(&x, &p.X, &recip)
  644. FeMul(&y, &p.Y, &recip)
  645. FeToBytes(s, &y)
  646. s[31] ^= FeIsNegative(&x) << 7
  647. }
  648. func (p *ExtendedGroupElement) FromBytes(s *[32]byte) bool {
  649. var u, v, v3, vxx, check FieldElement
  650. FeFromBytes(&p.Y, s)
  651. FeOne(&p.Z)
  652. FeSquare(&u, &p.Y)
  653. FeMul(&v, &u, &d)
  654. FeSub(&u, &u, &p.Z) // y = y^2-1
  655. FeAdd(&v, &v, &p.Z) // v = dy^2+1
  656. FeSquare(&v3, &v)
  657. FeMul(&v3, &v3, &v) // v3 = v^3
  658. FeSquare(&p.X, &v3)
  659. FeMul(&p.X, &p.X, &v)
  660. FeMul(&p.X, &p.X, &u) // x = uv^7
  661. fePow22523(&p.X, &p.X) // x = (uv^7)^((q-5)/8)
  662. FeMul(&p.X, &p.X, &v3)
  663. FeMul(&p.X, &p.X, &u) // x = uv^3(uv^7)^((q-5)/8)
  664. var tmpX, tmp2 [32]byte
  665. FeSquare(&vxx, &p.X)
  666. FeMul(&vxx, &vxx, &v)
  667. FeSub(&check, &vxx, &u) // vx^2-u
  668. if FeIsNonZero(&check) == 1 {
  669. FeAdd(&check, &vxx, &u) // vx^2+u
  670. if FeIsNonZero(&check) == 1 {
  671. return false
  672. }
  673. FeMul(&p.X, &p.X, &SqrtM1)
  674. FeToBytes(&tmpX, &p.X)
  675. for i, v := range tmpX {
  676. tmp2[31-i] = v
  677. }
  678. }
  679. if FeIsNegative(&p.X) != (s[31] >> 7) {
  680. FeNeg(&p.X, &p.X)
  681. }
  682. FeMul(&p.T, &p.X, &p.Y)
  683. return true
  684. }
  685. func (p *CompletedGroupElement) ToProjective(r *ProjectiveGroupElement) {
  686. FeMul(&r.X, &p.X, &p.T)
  687. FeMul(&r.Y, &p.Y, &p.Z)
  688. FeMul(&r.Z, &p.Z, &p.T)
  689. }
  690. func (p *CompletedGroupElement) ToExtended(r *ExtendedGroupElement) {
  691. FeMul(&r.X, &p.X, &p.T)
  692. FeMul(&r.Y, &p.Y, &p.Z)
  693. FeMul(&r.Z, &p.Z, &p.T)
  694. FeMul(&r.T, &p.X, &p.Y)
  695. }
  696. func (p *PreComputedGroupElement) Zero() {
  697. FeOne(&p.yPlusX)
  698. FeOne(&p.yMinusX)
  699. FeZero(&p.xy2d)
  700. }
  701. func geAdd(r *CompletedGroupElement, p *ExtendedGroupElement, q *CachedGroupElement) {
  702. var t0 FieldElement
  703. FeAdd(&r.X, &p.Y, &p.X)
  704. FeSub(&r.Y, &p.Y, &p.X)
  705. FeMul(&r.Z, &r.X, &q.yPlusX)
  706. FeMul(&r.Y, &r.Y, &q.yMinusX)
  707. FeMul(&r.T, &q.T2d, &p.T)
  708. FeMul(&r.X, &p.Z, &q.Z)
  709. FeAdd(&t0, &r.X, &r.X)
  710. FeSub(&r.X, &r.Z, &r.Y)
  711. FeAdd(&r.Y, &r.Z, &r.Y)
  712. FeAdd(&r.Z, &t0, &r.T)
  713. FeSub(&r.T, &t0, &r.T)
  714. }
  715. func geSub(r *CompletedGroupElement, p *ExtendedGroupElement, q *CachedGroupElement) {
  716. var t0 FieldElement
  717. FeAdd(&r.X, &p.Y, &p.X)
  718. FeSub(&r.Y, &p.Y, &p.X)
  719. FeMul(&r.Z, &r.X, &q.yMinusX)
  720. FeMul(&r.Y, &r.Y, &q.yPlusX)
  721. FeMul(&r.T, &q.T2d, &p.T)
  722. FeMul(&r.X, &p.Z, &q.Z)
  723. FeAdd(&t0, &r.X, &r.X)
  724. FeSub(&r.X, &r.Z, &r.Y)
  725. FeAdd(&r.Y, &r.Z, &r.Y)
  726. FeSub(&r.Z, &t0, &r.T)
  727. FeAdd(&r.T, &t0, &r.T)
  728. }
  729. func geMixedAdd(r *CompletedGroupElement, p *ExtendedGroupElement, q *PreComputedGroupElement) {
  730. var t0 FieldElement
  731. FeAdd(&r.X, &p.Y, &p.X)
  732. FeSub(&r.Y, &p.Y, &p.X)
  733. FeMul(&r.Z, &r.X, &q.yPlusX)
  734. FeMul(&r.Y, &r.Y, &q.yMinusX)
  735. FeMul(&r.T, &q.xy2d, &p.T)
  736. FeAdd(&t0, &p.Z, &p.Z)
  737. FeSub(&r.X, &r.Z, &r.Y)
  738. FeAdd(&r.Y, &r.Z, &r.Y)
  739. FeAdd(&r.Z, &t0, &r.T)
  740. FeSub(&r.T, &t0, &r.T)
  741. }
  742. func geMixedSub(r *CompletedGroupElement, p *ExtendedGroupElement, q *PreComputedGroupElement) {
  743. var t0 FieldElement
  744. FeAdd(&r.X, &p.Y, &p.X)
  745. FeSub(&r.Y, &p.Y, &p.X)
  746. FeMul(&r.Z, &r.X, &q.yMinusX)
  747. FeMul(&r.Y, &r.Y, &q.yPlusX)
  748. FeMul(&r.T, &q.xy2d, &p.T)
  749. FeAdd(&t0, &p.Z, &p.Z)
  750. FeSub(&r.X, &r.Z, &r.Y)
  751. FeAdd(&r.Y, &r.Z, &r.Y)
  752. FeSub(&r.Z, &t0, &r.T)
  753. FeAdd(&r.T, &t0, &r.T)
  754. }
  755. func slide(r *[256]int8, a *[32]byte) {
  756. for i := range r {
  757. r[i] = int8(1 & (a[i>>3] >> uint(i&7)))
  758. }
  759. for i := range r {
  760. if r[i] != 0 {
  761. for b := 1; b <= 6 && i+b < 256; b++ {
  762. if r[i+b] != 0 {
  763. if r[i]+(r[i+b]<<uint(b)) <= 15 {
  764. r[i] += r[i+b] << uint(b)
  765. r[i+b] = 0
  766. } else if r[i]-(r[i+b]<<uint(b)) >= -15 {
  767. r[i] -= r[i+b] << uint(b)
  768. for k := i + b; k < 256; k++ {
  769. if r[k] == 0 {
  770. r[k] = 1
  771. break
  772. }
  773. r[k] = 0
  774. }
  775. } else {
  776. break
  777. }
  778. }
  779. }
  780. }
  781. }
  782. }
  783. // GeDoubleScalarMultVartime sets r = a*A + b*B
  784. // where a = a[0]+256*a[1]+...+256^31 a[31].
  785. // and b = b[0]+256*b[1]+...+256^31 b[31].
  786. // B is the Ed25519 base point (x,4/5) with x positive.
  787. func GeDoubleScalarMultVartime(r *ProjectiveGroupElement, a *[32]byte, A *ExtendedGroupElement, b *[32]byte) {
  788. var aSlide, bSlide [256]int8
  789. var Ai [8]CachedGroupElement // A,3A,5A,7A,9A,11A,13A,15A
  790. var t CompletedGroupElement
  791. var u, A2 ExtendedGroupElement
  792. var i int
  793. slide(&aSlide, a)
  794. slide(&bSlide, b)
  795. A.ToCached(&Ai[0])
  796. A.Double(&t)
  797. t.ToExtended(&A2)
  798. for i := 0; i < 7; i++ {
  799. geAdd(&t, &A2, &Ai[i])
  800. t.ToExtended(&u)
  801. u.ToCached(&Ai[i+1])
  802. }
  803. r.Zero()
  804. for i = 255; i >= 0; i-- {
  805. if aSlide[i] != 0 || bSlide[i] != 0 {
  806. break
  807. }
  808. }
  809. for ; i >= 0; i-- {
  810. r.Double(&t)
  811. if aSlide[i] > 0 {
  812. t.ToExtended(&u)
  813. geAdd(&t, &u, &Ai[aSlide[i]/2])
  814. } else if aSlide[i] < 0 {
  815. t.ToExtended(&u)
  816. geSub(&t, &u, &Ai[(-aSlide[i])/2])
  817. }
  818. if bSlide[i] > 0 {
  819. t.ToExtended(&u)
  820. geMixedAdd(&t, &u, &bi[bSlide[i]/2])
  821. } else if bSlide[i] < 0 {
  822. t.ToExtended(&u)
  823. geMixedSub(&t, &u, &bi[(-bSlide[i])/2])
  824. }
  825. t.ToProjective(r)
  826. }
  827. }
  828. // equal returns 1 if b == c and 0 otherwise, assuming that b and c are
  829. // non-negative.
  830. func equal(b, c int32) int32 {
  831. x := uint32(b ^ c)
  832. x--
  833. return int32(x >> 31)
  834. }
  835. // negative returns 1 if b < 0 and 0 otherwise.
  836. func negative(b int32) int32 {
  837. return (b >> 31) & 1
  838. }
  839. func PreComputedGroupElementCMove(t, u *PreComputedGroupElement, b int32) {
  840. FeCMove(&t.yPlusX, &u.yPlusX, b)
  841. FeCMove(&t.yMinusX, &u.yMinusX, b)
  842. FeCMove(&t.xy2d, &u.xy2d, b)
  843. }
  844. func selectPoint(t *PreComputedGroupElement, pos int32, b int32) {
  845. var minusT PreComputedGroupElement
  846. bNegative := negative(b)
  847. bAbs := b - (((-bNegative) & b) << 1)
  848. t.Zero()
  849. for i := int32(0); i < 8; i++ {
  850. PreComputedGroupElementCMove(t, &base[pos][i], equal(bAbs, i+1))
  851. }
  852. FeCopy(&minusT.yPlusX, &t.yMinusX)
  853. FeCopy(&minusT.yMinusX, &t.yPlusX)
  854. FeNeg(&minusT.xy2d, &t.xy2d)
  855. PreComputedGroupElementCMove(t, &minusT, bNegative)
  856. }
  857. // GeScalarMultBase computes h = a*B, where
  858. // a = a[0]+256*a[1]+...+256^31 a[31]
  859. // B is the Ed25519 base point (x,4/5) with x positive.
  860. //
  861. // Preconditions:
  862. // a[31] <= 127
  863. func GeScalarMultBase(h *ExtendedGroupElement, a *[32]byte) {
  864. var e [64]int8
  865. for i, v := range a {
  866. e[2*i] = int8(v & 15)
  867. e[2*i+1] = int8((v >> 4) & 15)
  868. }
  869. // each e[i] is between 0 and 15 and e[63] is between 0 and 7.
  870. carry := int8(0)
  871. for i := 0; i < 63; i++ {
  872. e[i] += carry
  873. carry = (e[i] + 8) >> 4
  874. e[i] -= carry << 4
  875. }
  876. e[63] += carry
  877. // each e[i] is between -8 and 8.
  878. h.Zero()
  879. var t PreComputedGroupElement
  880. var r CompletedGroupElement
  881. for i := int32(1); i < 64; i += 2 {
  882. selectPoint(&t, i/2, int32(e[i]))
  883. geMixedAdd(&r, h, &t)
  884. r.ToExtended(h)
  885. }
  886. var s ProjectiveGroupElement
  887. h.Double(&r)
  888. r.ToProjective(&s)
  889. s.Double(&r)
  890. r.ToProjective(&s)
  891. s.Double(&r)
  892. r.ToProjective(&s)
  893. s.Double(&r)
  894. r.ToExtended(h)
  895. for i := int32(0); i < 64; i += 2 {
  896. selectPoint(&t, i/2, int32(e[i]))
  897. geMixedAdd(&r, h, &t)
  898. r.ToExtended(h)
  899. }
  900. }
  901. // The scalars are GF(2^252 + 27742317777372353535851937790883648493).
  902. // Input:
  903. // a[0]+256*a[1]+...+256^31*a[31] = a
  904. // b[0]+256*b[1]+...+256^31*b[31] = b
  905. // c[0]+256*c[1]+...+256^31*c[31] = c
  906. //
  907. // Output:
  908. // s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l
  909. // where l = 2^252 + 27742317777372353535851937790883648493.
  910. func ScMulAdd(s, a, b, c *[32]byte) {
  911. a0 := 2097151 & load3(a[:])
  912. a1 := 2097151 & (load4(a[2:]) >> 5)
  913. a2 := 2097151 & (load3(a[5:]) >> 2)
  914. a3 := 2097151 & (load4(a[7:]) >> 7)
  915. a4 := 2097151 & (load4(a[10:]) >> 4)
  916. a5 := 2097151 & (load3(a[13:]) >> 1)
  917. a6 := 2097151 & (load4(a[15:]) >> 6)
  918. a7 := 2097151 & (load3(a[18:]) >> 3)
  919. a8 := 2097151 & load3(a[21:])
  920. a9 := 2097151 & (load4(a[23:]) >> 5)
  921. a10 := 2097151 & (load3(a[26:]) >> 2)
  922. a11 := (load4(a[28:]) >> 7)
  923. b0 := 2097151 & load3(b[:])
  924. b1 := 2097151 & (load4(b[2:]) >> 5)
  925. b2 := 2097151 & (load3(b[5:]) >> 2)
  926. b3 := 2097151 & (load4(b[7:]) >> 7)
  927. b4 := 2097151 & (load4(b[10:]) >> 4)
  928. b5 := 2097151 & (load3(b[13:]) >> 1)
  929. b6 := 2097151 & (load4(b[15:]) >> 6)
  930. b7 := 2097151 & (load3(b[18:]) >> 3)
  931. b8 := 2097151 & load3(b[21:])
  932. b9 := 2097151 & (load4(b[23:]) >> 5)
  933. b10 := 2097151 & (load3(b[26:]) >> 2)
  934. b11 := (load4(b[28:]) >> 7)
  935. c0 := 2097151 & load3(c[:])
  936. c1 := 2097151 & (load4(c[2:]) >> 5)
  937. c2 := 2097151 & (load3(c[5:]) >> 2)
  938. c3 := 2097151 & (load4(c[7:]) >> 7)
  939. c4 := 2097151 & (load4(c[10:]) >> 4)
  940. c5 := 2097151 & (load3(c[13:]) >> 1)
  941. c6 := 2097151 & (load4(c[15:]) >> 6)
  942. c7 := 2097151 & (load3(c[18:]) >> 3)
  943. c8 := 2097151 & load3(c[21:])
  944. c9 := 2097151 & (load4(c[23:]) >> 5)
  945. c10 := 2097151 & (load3(c[26:]) >> 2)
  946. c11 := (load4(c[28:]) >> 7)
  947. var carry [23]int64
  948. s0 := c0 + a0*b0
  949. s1 := c1 + a0*b1 + a1*b0
  950. s2 := c2 + a0*b2 + a1*b1 + a2*b0
  951. s3 := c3 + a0*b3 + a1*b2 + a2*b1 + a3*b0
  952. s4 := c4 + a0*b4 + a1*b3 + a2*b2 + a3*b1 + a4*b0
  953. s5 := c5 + a0*b5 + a1*b4 + a2*b3 + a3*b2 + a4*b1 + a5*b0
  954. s6 := c6 + a0*b6 + a1*b5 + a2*b4 + a3*b3 + a4*b2 + a5*b1 + a6*b0
  955. s7 := c7 + a0*b7 + a1*b6 + a2*b5 + a3*b4 + a4*b3 + a5*b2 + a6*b1 + a7*b0
  956. s8 := c8 + a0*b8 + a1*b7 + a2*b6 + a3*b5 + a4*b4 + a5*b3 + a6*b2 + a7*b1 + a8*b0
  957. s9 := c9 + a0*b9 + a1*b8 + a2*b7 + a3*b6 + a4*b5 + a5*b4 + a6*b3 + a7*b2 + a8*b1 + a9*b0
  958. s10 := c10 + a0*b10 + a1*b9 + a2*b8 + a3*b7 + a4*b6 + a5*b5 + a6*b4 + a7*b3 + a8*b2 + a9*b1 + a10*b0
  959. s11 := c11 + a0*b11 + a1*b10 + a2*b9 + a3*b8 + a4*b7 + a5*b6 + a6*b5 + a7*b4 + a8*b3 + a9*b2 + a10*b1 + a11*b0
  960. s12 := a1*b11 + a2*b10 + a3*b9 + a4*b8 + a5*b7 + a6*b6 + a7*b5 + a8*b4 + a9*b3 + a10*b2 + a11*b1
  961. s13 := a2*b11 + a3*b10 + a4*b9 + a5*b8 + a6*b7 + a7*b6 + a8*b5 + a9*b4 + a10*b3 + a11*b2
  962. s14 := a3*b11 + a4*b10 + a5*b9 + a6*b8 + a7*b7 + a8*b6 + a9*b5 + a10*b4 + a11*b3
  963. s15 := a4*b11 + a5*b10 + a6*b9 + a7*b8 + a8*b7 + a9*b6 + a10*b5 + a11*b4
  964. s16 := a5*b11 + a6*b10 + a7*b9 + a8*b8 + a9*b7 + a10*b6 + a11*b5
  965. s17 := a6*b11 + a7*b10 + a8*b9 + a9*b8 + a10*b7 + a11*b6
  966. s18 := a7*b11 + a8*b10 + a9*b9 + a10*b8 + a11*b7
  967. s19 := a8*b11 + a9*b10 + a10*b9 + a11*b8
  968. s20 := a9*b11 + a10*b10 + a11*b9
  969. s21 := a10*b11 + a11*b10
  970. s22 := a11 * b11
  971. s23 := int64(0)
  972. carry[0] = (s0 + (1 << 20)) >> 21
  973. s1 += carry[0]
  974. s0 -= carry[0] << 21
  975. carry[2] = (s2 + (1 << 20)) >> 21
  976. s3 += carry[2]
  977. s2 -= carry[2] << 21
  978. carry[4] = (s4 + (1 << 20)) >> 21
  979. s5 += carry[4]
  980. s4 -= carry[4] << 21
  981. carry[6] = (s6 + (1 << 20)) >> 21
  982. s7 += carry[6]
  983. s6 -= carry[6] << 21
  984. carry[8] = (s8 + (1 << 20)) >> 21
  985. s9 += carry[8]
  986. s8 -= carry[8] << 21
  987. carry[10] = (s10 + (1 << 20)) >> 21
  988. s11 += carry[10]
  989. s10 -= carry[10] << 21
  990. carry[12] = (s12 + (1 << 20)) >> 21
  991. s13 += carry[12]
  992. s12 -= carry[12] << 21
  993. carry[14] = (s14 + (1 << 20)) >> 21
  994. s15 += carry[14]
  995. s14 -= carry[14] << 21
  996. carry[16] = (s16 + (1 << 20)) >> 21
  997. s17 += carry[16]
  998. s16 -= carry[16] << 21
  999. carry[18] = (s18 + (1 << 20)) >> 21
  1000. s19 += carry[18]
  1001. s18 -= carry[18] << 21
  1002. carry[20] = (s20 + (1 << 20)) >> 21
  1003. s21 += carry[20]
  1004. s20 -= carry[20] << 21
  1005. carry[22] = (s22 + (1 << 20)) >> 21
  1006. s23 += carry[22]
  1007. s22 -= carry[22] << 21
  1008. carry[1] = (s1 + (1 << 20)) >> 21
  1009. s2 += carry[1]
  1010. s1 -= carry[1] << 21
  1011. carry[3] = (s3 + (1 << 20)) >> 21
  1012. s4 += carry[3]
  1013. s3 -= carry[3] << 21
  1014. carry[5] = (s5 + (1 << 20)) >> 21
  1015. s6 += carry[5]
  1016. s5 -= carry[5] << 21
  1017. carry[7] = (s7 + (1 << 20)) >> 21
  1018. s8 += carry[7]
  1019. s7 -= carry[7] << 21
  1020. carry[9] = (s9 + (1 << 20)) >> 21
  1021. s10 += carry[9]
  1022. s9 -= carry[9] << 21
  1023. carry[11] = (s11 + (1 << 20)) >> 21
  1024. s12 += carry[11]
  1025. s11 -= carry[11] << 21
  1026. carry[13] = (s13 + (1 << 20)) >> 21
  1027. s14 += carry[13]
  1028. s13 -= carry[13] << 21
  1029. carry[15] = (s15 + (1 << 20)) >> 21
  1030. s16 += carry[15]
  1031. s15 -= carry[15] << 21
  1032. carry[17] = (s17 + (1 << 20)) >> 21
  1033. s18 += carry[17]
  1034. s17 -= carry[17] << 21
  1035. carry[19] = (s19 + (1 << 20)) >> 21
  1036. s20 += carry[19]
  1037. s19 -= carry[19] << 21
  1038. carry[21] = (s21 + (1 << 20)) >> 21
  1039. s22 += carry[21]
  1040. s21 -= carry[21] << 21
  1041. s11 += s23 * 666643
  1042. s12 += s23 * 470296
  1043. s13 += s23 * 654183
  1044. s14 -= s23 * 997805
  1045. s15 += s23 * 136657
  1046. s16 -= s23 * 683901
  1047. s23 = 0
  1048. s10 += s22 * 666643
  1049. s11 += s22 * 470296
  1050. s12 += s22 * 654183
  1051. s13 -= s22 * 997805
  1052. s14 += s22 * 136657
  1053. s15 -= s22 * 683901
  1054. s22 = 0
  1055. s9 += s21 * 666643
  1056. s10 += s21 * 470296
  1057. s11 += s21 * 654183
  1058. s12 -= s21 * 997805
  1059. s13 += s21 * 136657
  1060. s14 -= s21 * 683901
  1061. s21 = 0
  1062. s8 += s20 * 666643
  1063. s9 += s20 * 470296
  1064. s10 += s20 * 654183
  1065. s11 -= s20 * 997805
  1066. s12 += s20 * 136657
  1067. s13 -= s20 * 683901
  1068. s20 = 0
  1069. s7 += s19 * 666643
  1070. s8 += s19 * 470296
  1071. s9 += s19 * 654183
  1072. s10 -= s19 * 997805
  1073. s11 += s19 * 136657
  1074. s12 -= s19 * 683901
  1075. s19 = 0
  1076. s6 += s18 * 666643
  1077. s7 += s18 * 470296
  1078. s8 += s18 * 654183
  1079. s9 -= s18 * 997805
  1080. s10 += s18 * 136657
  1081. s11 -= s18 * 683901
  1082. s18 = 0
  1083. carry[6] = (s6 + (1 << 20)) >> 21
  1084. s7 += carry[6]
  1085. s6 -= carry[6] << 21
  1086. carry[8] = (s8 + (1 << 20)) >> 21
  1087. s9 += carry[8]
  1088. s8 -= carry[8] << 21
  1089. carry[10] = (s10 + (1 << 20)) >> 21
  1090. s11 += carry[10]
  1091. s10 -= carry[10] << 21
  1092. carry[12] = (s12 + (1 << 20)) >> 21
  1093. s13 += carry[12]
  1094. s12 -= carry[12] << 21
  1095. carry[14] = (s14 + (1 << 20)) >> 21
  1096. s15 += carry[14]
  1097. s14 -= carry[14] << 21
  1098. carry[16] = (s16 + (1 << 20)) >> 21
  1099. s17 += carry[16]
  1100. s16 -= carry[16] << 21
  1101. carry[7] = (s7 + (1 << 20)) >> 21
  1102. s8 += carry[7]
  1103. s7 -= carry[7] << 21
  1104. carry[9] = (s9 + (1 << 20)) >> 21
  1105. s10 += carry[9]
  1106. s9 -= carry[9] << 21
  1107. carry[11] = (s11 + (1 << 20)) >> 21
  1108. s12 += carry[11]
  1109. s11 -= carry[11] << 21
  1110. carry[13] = (s13 + (1 << 20)) >> 21
  1111. s14 += carry[13]
  1112. s13 -= carry[13] << 21
  1113. carry[15] = (s15 + (1 << 20)) >> 21
  1114. s16 += carry[15]
  1115. s15 -= carry[15] << 21
  1116. s5 += s17 * 666643
  1117. s6 += s17 * 470296
  1118. s7 += s17 * 654183
  1119. s8 -= s17 * 997805
  1120. s9 += s17 * 136657
  1121. s10 -= s17 * 683901
  1122. s17 = 0
  1123. s4 += s16 * 666643
  1124. s5 += s16 * 470296
  1125. s6 += s16 * 654183
  1126. s7 -= s16 * 997805
  1127. s8 += s16 * 136657
  1128. s9 -= s16 * 683901
  1129. s16 = 0
  1130. s3 += s15 * 666643
  1131. s4 += s15 * 470296
  1132. s5 += s15 * 654183
  1133. s6 -= s15 * 997805
  1134. s7 += s15 * 136657
  1135. s8 -= s15 * 683901
  1136. s15 = 0
  1137. s2 += s14 * 666643
  1138. s3 += s14 * 470296
  1139. s4 += s14 * 654183
  1140. s5 -= s14 * 997805
  1141. s6 += s14 * 136657
  1142. s7 -= s14 * 683901
  1143. s14 = 0
  1144. s1 += s13 * 666643
  1145. s2 += s13 * 470296
  1146. s3 += s13 * 654183
  1147. s4 -= s13 * 997805
  1148. s5 += s13 * 136657
  1149. s6 -= s13 * 683901
  1150. s13 = 0
  1151. s0 += s12 * 666643
  1152. s1 += s12 * 470296
  1153. s2 += s12 * 654183
  1154. s3 -= s12 * 997805
  1155. s4 += s12 * 136657
  1156. s5 -= s12 * 683901
  1157. s12 = 0
  1158. carry[0] = (s0 + (1 << 20)) >> 21
  1159. s1 += carry[0]
  1160. s0 -= carry[0] << 21
  1161. carry[2] = (s2 + (1 << 20)) >> 21
  1162. s3 += carry[2]
  1163. s2 -= carry[2] << 21
  1164. carry[4] = (s4 + (1 << 20)) >> 21
  1165. s5 += carry[4]
  1166. s4 -= carry[4] << 21
  1167. carry[6] = (s6 + (1 << 20)) >> 21
  1168. s7 += carry[6]
  1169. s6 -= carry[6] << 21
  1170. carry[8] = (s8 + (1 << 20)) >> 21
  1171. s9 += carry[8]
  1172. s8 -= carry[8] << 21
  1173. carry[10] = (s10 + (1 << 20)) >> 21
  1174. s11 += carry[10]
  1175. s10 -= carry[10] << 21
  1176. carry[1] = (s1 + (1 << 20)) >> 21
  1177. s2 += carry[1]
  1178. s1 -= carry[1] << 21
  1179. carry[3] = (s3 + (1 << 20)) >> 21
  1180. s4 += carry[3]
  1181. s3 -= carry[3] << 21
  1182. carry[5] = (s5 + (1 << 20)) >> 21
  1183. s6 += carry[5]
  1184. s5 -= carry[5] << 21
  1185. carry[7] = (s7 + (1 << 20)) >> 21
  1186. s8 += carry[7]
  1187. s7 -= carry[7] << 21
  1188. carry[9] = (s9 + (1 << 20)) >> 21
  1189. s10 += carry[9]
  1190. s9 -= carry[9] << 21
  1191. carry[11] = (s11 + (1 << 20)) >> 21
  1192. s12 += carry[11]
  1193. s11 -= carry[11] << 21
  1194. s0 += s12 * 666643
  1195. s1 += s12 * 470296
  1196. s2 += s12 * 654183
  1197. s3 -= s12 * 997805
  1198. s4 += s12 * 136657
  1199. s5 -= s12 * 683901
  1200. s12 = 0
  1201. carry[0] = s0 >> 21
  1202. s1 += carry[0]
  1203. s0 -= carry[0] << 21
  1204. carry[1] = s1 >> 21
  1205. s2 += carry[1]
  1206. s1 -= carry[1] << 21
  1207. carry[2] = s2 >> 21
  1208. s3 += carry[2]
  1209. s2 -= carry[2] << 21
  1210. carry[3] = s3 >> 21
  1211. s4 += carry[3]
  1212. s3 -= carry[3] << 21
  1213. carry[4] = s4 >> 21
  1214. s5 += carry[4]
  1215. s4 -= carry[4] << 21
  1216. carry[5] = s5 >> 21
  1217. s6 += carry[5]
  1218. s5 -= carry[5] << 21
  1219. carry[6] = s6 >> 21
  1220. s7 += carry[6]
  1221. s6 -= carry[6] << 21
  1222. carry[7] = s7 >> 21
  1223. s8 += carry[7]
  1224. s7 -= carry[7] << 21
  1225. carry[8] = s8 >> 21
  1226. s9 += carry[8]
  1227. s8 -= carry[8] << 21
  1228. carry[9] = s9 >> 21
  1229. s10 += carry[9]
  1230. s9 -= carry[9] << 21
  1231. carry[10] = s10 >> 21
  1232. s11 += carry[10]
  1233. s10 -= carry[10] << 21
  1234. carry[11] = s11 >> 21
  1235. s12 += carry[11]
  1236. s11 -= carry[11] << 21
  1237. s0 += s12 * 666643
  1238. s1 += s12 * 470296
  1239. s2 += s12 * 654183
  1240. s3 -= s12 * 997805
  1241. s4 += s12 * 136657
  1242. s5 -= s12 * 683901
  1243. s12 = 0
  1244. carry[0] = s0 >> 21
  1245. s1 += carry[0]
  1246. s0 -= carry[0] << 21
  1247. carry[1] = s1 >> 21
  1248. s2 += carry[1]
  1249. s1 -= carry[1] << 21
  1250. carry[2] = s2 >> 21
  1251. s3 += carry[2]
  1252. s2 -= carry[2] << 21
  1253. carry[3] = s3 >> 21
  1254. s4 += carry[3]
  1255. s3 -= carry[3] << 21
  1256. carry[4] = s4 >> 21
  1257. s5 += carry[4]
  1258. s4 -= carry[4] << 21
  1259. carry[5] = s5 >> 21
  1260. s6 += carry[5]
  1261. s5 -= carry[5] << 21
  1262. carry[6] = s6 >> 21
  1263. s7 += carry[6]
  1264. s6 -= carry[6] << 21
  1265. carry[7] = s7 >> 21
  1266. s8 += carry[7]
  1267. s7 -= carry[7] << 21
  1268. carry[8] = s8 >> 21
  1269. s9 += carry[8]
  1270. s8 -= carry[8] << 21
  1271. carry[9] = s9 >> 21
  1272. s10 += carry[9]
  1273. s9 -= carry[9] << 21
  1274. carry[10] = s10 >> 21
  1275. s11 += carry[10]
  1276. s10 -= carry[10] << 21
  1277. s[0] = byte(s0 >> 0)
  1278. s[1] = byte(s0 >> 8)
  1279. s[2] = byte((s0 >> 16) | (s1 << 5))
  1280. s[3] = byte(s1 >> 3)
  1281. s[4] = byte(s1 >> 11)
  1282. s[5] = byte((s1 >> 19) | (s2 << 2))
  1283. s[6] = byte(s2 >> 6)
  1284. s[7] = byte((s2 >> 14) | (s3 << 7))
  1285. s[8] = byte(s3 >> 1)
  1286. s[9] = byte(s3 >> 9)
  1287. s[10] = byte((s3 >> 17) | (s4 << 4))
  1288. s[11] = byte(s4 >> 4)
  1289. s[12] = byte(s4 >> 12)
  1290. s[13] = byte((s4 >> 20) | (s5 << 1))
  1291. s[14] = byte(s5 >> 7)
  1292. s[15] = byte((s5 >> 15) | (s6 << 6))
  1293. s[16] = byte(s6 >> 2)
  1294. s[17] = byte(s6 >> 10)
  1295. s[18] = byte((s6 >> 18) | (s7 << 3))
  1296. s[19] = byte(s7 >> 5)
  1297. s[20] = byte(s7 >> 13)
  1298. s[21] = byte(s8 >> 0)
  1299. s[22] = byte(s8 >> 8)
  1300. s[23] = byte((s8 >> 16) | (s9 << 5))
  1301. s[24] = byte(s9 >> 3)
  1302. s[25] = byte(s9 >> 11)
  1303. s[26] = byte((s9 >> 19) | (s10 << 2))
  1304. s[27] = byte(s10 >> 6)
  1305. s[28] = byte((s10 >> 14) | (s11 << 7))
  1306. s[29] = byte(s11 >> 1)
  1307. s[30] = byte(s11 >> 9)
  1308. s[31] = byte(s11 >> 17)
  1309. }
  1310. // Input:
  1311. // s[0]+256*s[1]+...+256^63*s[63] = s
  1312. //
  1313. // Output:
  1314. // s[0]+256*s[1]+...+256^31*s[31] = s mod l
  1315. // where l = 2^252 + 27742317777372353535851937790883648493.
  1316. func ScReduce(out *[32]byte, s *[64]byte) {
  1317. s0 := 2097151 & load3(s[:])
  1318. s1 := 2097151 & (load4(s[2:]) >> 5)
  1319. s2 := 2097151 & (load3(s[5:]) >> 2)
  1320. s3 := 2097151 & (load4(s[7:]) >> 7)
  1321. s4 := 2097151 & (load4(s[10:]) >> 4)
  1322. s5 := 2097151 & (load3(s[13:]) >> 1)
  1323. s6 := 2097151 & (load4(s[15:]) >> 6)
  1324. s7 := 2097151 & (load3(s[18:]) >> 3)
  1325. s8 := 2097151 & load3(s[21:])
  1326. s9 := 2097151 & (load4(s[23:]) >> 5)
  1327. s10 := 2097151 & (load3(s[26:]) >> 2)
  1328. s11 := 2097151 & (load4(s[28:]) >> 7)
  1329. s12 := 2097151 & (load4(s[31:]) >> 4)
  1330. s13 := 2097151 & (load3(s[34:]) >> 1)
  1331. s14 := 2097151 & (load4(s[36:]) >> 6)
  1332. s15 := 2097151 & (load3(s[39:]) >> 3)
  1333. s16 := 2097151 & load3(s[42:])
  1334. s17 := 2097151 & (load4(s[44:]) >> 5)
  1335. s18 := 2097151 & (load3(s[47:]) >> 2)
  1336. s19 := 2097151 & (load4(s[49:]) >> 7)
  1337. s20 := 2097151 & (load4(s[52:]) >> 4)
  1338. s21 := 2097151 & (load3(s[55:]) >> 1)
  1339. s22 := 2097151 & (load4(s[57:]) >> 6)
  1340. s23 := (load4(s[60:]) >> 3)
  1341. s11 += s23 * 666643
  1342. s12 += s23 * 470296
  1343. s13 += s23 * 654183
  1344. s14 -= s23 * 997805
  1345. s15 += s23 * 136657
  1346. s16 -= s23 * 683901
  1347. s23 = 0
  1348. s10 += s22 * 666643
  1349. s11 += s22 * 470296
  1350. s12 += s22 * 654183
  1351. s13 -= s22 * 997805
  1352. s14 += s22 * 136657
  1353. s15 -= s22 * 683901
  1354. s22 = 0
  1355. s9 += s21 * 666643
  1356. s10 += s21 * 470296
  1357. s11 += s21 * 654183
  1358. s12 -= s21 * 997805
  1359. s13 += s21 * 136657
  1360. s14 -= s21 * 683901
  1361. s21 = 0
  1362. s8 += s20 * 666643
  1363. s9 += s20 * 470296
  1364. s10 += s20 * 654183
  1365. s11 -= s20 * 997805
  1366. s12 += s20 * 136657
  1367. s13 -= s20 * 683901
  1368. s20 = 0
  1369. s7 += s19 * 666643
  1370. s8 += s19 * 470296
  1371. s9 += s19 * 654183
  1372. s10 -= s19 * 997805
  1373. s11 += s19 * 136657
  1374. s12 -= s19 * 683901
  1375. s19 = 0
  1376. s6 += s18 * 666643
  1377. s7 += s18 * 470296
  1378. s8 += s18 * 654183
  1379. s9 -= s18 * 997805
  1380. s10 += s18 * 136657
  1381. s11 -= s18 * 683901
  1382. s18 = 0
  1383. var carry [17]int64
  1384. carry[6] = (s6 + (1 << 20)) >> 21
  1385. s7 += carry[6]
  1386. s6 -= carry[6] << 21
  1387. carry[8] = (s8 + (1 << 20)) >> 21
  1388. s9 += carry[8]
  1389. s8 -= carry[8] << 21
  1390. carry[10] = (s10 + (1 << 20)) >> 21
  1391. s11 += carry[10]
  1392. s10 -= carry[10] << 21
  1393. carry[12] = (s12 + (1 << 20)) >> 21
  1394. s13 += carry[12]
  1395. s12 -= carry[12] << 21
  1396. carry[14] = (s14 + (1 << 20)) >> 21
  1397. s15 += carry[14]
  1398. s14 -= carry[14] << 21
  1399. carry[16] = (s16 + (1 << 20)) >> 21
  1400. s17 += carry[16]
  1401. s16 -= carry[16] << 21
  1402. carry[7] = (s7 + (1 << 20)) >> 21
  1403. s8 += carry[7]
  1404. s7 -= carry[7] << 21
  1405. carry[9] = (s9 + (1 << 20)) >> 21
  1406. s10 += carry[9]
  1407. s9 -= carry[9] << 21
  1408. carry[11] = (s11 + (1 << 20)) >> 21
  1409. s12 += carry[11]
  1410. s11 -= carry[11] << 21
  1411. carry[13] = (s13 + (1 << 20)) >> 21
  1412. s14 += carry[13]
  1413. s13 -= carry[13] << 21
  1414. carry[15] = (s15 + (1 << 20)) >> 21
  1415. s16 += carry[15]
  1416. s15 -= carry[15] << 21
  1417. s5 += s17 * 666643
  1418. s6 += s17 * 470296
  1419. s7 += s17 * 654183
  1420. s8 -= s17 * 997805
  1421. s9 += s17 * 136657
  1422. s10 -= s17 * 683901
  1423. s17 = 0
  1424. s4 += s16 * 666643
  1425. s5 += s16 * 470296
  1426. s6 += s16 * 654183
  1427. s7 -= s16 * 997805
  1428. s8 += s16 * 136657
  1429. s9 -= s16 * 683901
  1430. s16 = 0
  1431. s3 += s15 * 666643
  1432. s4 += s15 * 470296
  1433. s5 += s15 * 654183
  1434. s6 -= s15 * 997805
  1435. s7 += s15 * 136657
  1436. s8 -= s15 * 683901
  1437. s15 = 0
  1438. s2 += s14 * 666643
  1439. s3 += s14 * 470296
  1440. s4 += s14 * 654183
  1441. s5 -= s14 * 997805
  1442. s6 += s14 * 136657
  1443. s7 -= s14 * 683901
  1444. s14 = 0
  1445. s1 += s13 * 666643
  1446. s2 += s13 * 470296
  1447. s3 += s13 * 654183
  1448. s4 -= s13 * 997805
  1449. s5 += s13 * 136657
  1450. s6 -= s13 * 683901
  1451. s13 = 0
  1452. s0 += s12 * 666643
  1453. s1 += s12 * 470296
  1454. s2 += s12 * 654183
  1455. s3 -= s12 * 997805
  1456. s4 += s12 * 136657
  1457. s5 -= s12 * 683901
  1458. s12 = 0
  1459. carry[0] = (s0 + (1 << 20)) >> 21
  1460. s1 += carry[0]
  1461. s0 -= carry[0] << 21
  1462. carry[2] = (s2 + (1 << 20)) >> 21
  1463. s3 += carry[2]
  1464. s2 -= carry[2] << 21
  1465. carry[4] = (s4 + (1 << 20)) >> 21
  1466. s5 += carry[4]
  1467. s4 -= carry[4] << 21
  1468. carry[6] = (s6 + (1 << 20)) >> 21
  1469. s7 += carry[6]
  1470. s6 -= carry[6] << 21
  1471. carry[8] = (s8 + (1 << 20)) >> 21
  1472. s9 += carry[8]
  1473. s8 -= carry[8] << 21
  1474. carry[10] = (s10 + (1 << 20)) >> 21
  1475. s11 += carry[10]
  1476. s10 -= carry[10] << 21
  1477. carry[1] = (s1 + (1 << 20)) >> 21
  1478. s2 += carry[1]
  1479. s1 -= carry[1] << 21
  1480. carry[3] = (s3 + (1 << 20)) >> 21
  1481. s4 += carry[3]
  1482. s3 -= carry[3] << 21
  1483. carry[5] = (s5 + (1 << 20)) >> 21
  1484. s6 += carry[5]
  1485. s5 -= carry[5] << 21
  1486. carry[7] = (s7 + (1 << 20)) >> 21
  1487. s8 += carry[7]
  1488. s7 -= carry[7] << 21
  1489. carry[9] = (s9 + (1 << 20)) >> 21
  1490. s10 += carry[9]
  1491. s9 -= carry[9] << 21
  1492. carry[11] = (s11 + (1 << 20)) >> 21
  1493. s12 += carry[11]
  1494. s11 -= carry[11] << 21
  1495. s0 += s12 * 666643
  1496. s1 += s12 * 470296
  1497. s2 += s12 * 654183
  1498. s3 -= s12 * 997805
  1499. s4 += s12 * 136657
  1500. s5 -= s12 * 683901
  1501. s12 = 0
  1502. carry[0] = s0 >> 21
  1503. s1 += carry[0]
  1504. s0 -= carry[0] << 21
  1505. carry[1] = s1 >> 21
  1506. s2 += carry[1]
  1507. s1 -= carry[1] << 21
  1508. carry[2] = s2 >> 21
  1509. s3 += carry[2]
  1510. s2 -= carry[2] << 21
  1511. carry[3] = s3 >> 21
  1512. s4 += carry[3]
  1513. s3 -= carry[3] << 21
  1514. carry[4] = s4 >> 21
  1515. s5 += carry[4]
  1516. s4 -= carry[4] << 21
  1517. carry[5] = s5 >> 21
  1518. s6 += carry[5]
  1519. s5 -= carry[5] << 21
  1520. carry[6] = s6 >> 21
  1521. s7 += carry[6]
  1522. s6 -= carry[6] << 21
  1523. carry[7] = s7 >> 21
  1524. s8 += carry[7]
  1525. s7 -= carry[7] << 21
  1526. carry[8] = s8 >> 21
  1527. s9 += carry[8]
  1528. s8 -= carry[8] << 21
  1529. carry[9] = s9 >> 21
  1530. s10 += carry[9]
  1531. s9 -= carry[9] << 21
  1532. carry[10] = s10 >> 21
  1533. s11 += carry[10]
  1534. s10 -= carry[10] << 21
  1535. carry[11] = s11 >> 21
  1536. s12 += carry[11]
  1537. s11 -= carry[11] << 21
  1538. s0 += s12 * 666643
  1539. s1 += s12 * 470296
  1540. s2 += s12 * 654183
  1541. s3 -= s12 * 997805
  1542. s4 += s12 * 136657
  1543. s5 -= s12 * 683901
  1544. s12 = 0
  1545. carry[0] = s0 >> 21
  1546. s1 += carry[0]
  1547. s0 -= carry[0] << 21
  1548. carry[1] = s1 >> 21
  1549. s2 += carry[1]
  1550. s1 -= carry[1] << 21
  1551. carry[2] = s2 >> 21
  1552. s3 += carry[2]
  1553. s2 -= carry[2] << 21
  1554. carry[3] = s3 >> 21
  1555. s4 += carry[3]
  1556. s3 -= carry[3] << 21
  1557. carry[4] = s4 >> 21
  1558. s5 += carry[4]
  1559. s4 -= carry[4] << 21
  1560. carry[5] = s5 >> 21
  1561. s6 += carry[5]
  1562. s5 -= carry[5] << 21
  1563. carry[6] = s6 >> 21
  1564. s7 += carry[6]
  1565. s6 -= carry[6] << 21
  1566. carry[7] = s7 >> 21
  1567. s8 += carry[7]
  1568. s7 -= carry[7] << 21
  1569. carry[8] = s8 >> 21
  1570. s9 += carry[8]
  1571. s8 -= carry[8] << 21
  1572. carry[9] = s9 >> 21
  1573. s10 += carry[9]
  1574. s9 -= carry[9] << 21
  1575. carry[10] = s10 >> 21
  1576. s11 += carry[10]
  1577. s10 -= carry[10] << 21
  1578. out[0] = byte(s0 >> 0)
  1579. out[1] = byte(s0 >> 8)
  1580. out[2] = byte((s0 >> 16) | (s1 << 5))
  1581. out[3] = byte(s1 >> 3)
  1582. out[4] = byte(s1 >> 11)
  1583. out[5] = byte((s1 >> 19) | (s2 << 2))
  1584. out[6] = byte(s2 >> 6)
  1585. out[7] = byte((s2 >> 14) | (s3 << 7))
  1586. out[8] = byte(s3 >> 1)
  1587. out[9] = byte(s3 >> 9)
  1588. out[10] = byte((s3 >> 17) | (s4 << 4))
  1589. out[11] = byte(s4 >> 4)
  1590. out[12] = byte(s4 >> 12)
  1591. out[13] = byte((s4 >> 20) | (s5 << 1))
  1592. out[14] = byte(s5 >> 7)
  1593. out[15] = byte((s5 >> 15) | (s6 << 6))
  1594. out[16] = byte(s6 >> 2)
  1595. out[17] = byte(s6 >> 10)
  1596. out[18] = byte((s6 >> 18) | (s7 << 3))
  1597. out[19] = byte(s7 >> 5)
  1598. out[20] = byte(s7 >> 13)
  1599. out[21] = byte(s8 >> 0)
  1600. out[22] = byte(s8 >> 8)
  1601. out[23] = byte((s8 >> 16) | (s9 << 5))
  1602. out[24] = byte(s9 >> 3)
  1603. out[25] = byte(s9 >> 11)
  1604. out[26] = byte((s9 >> 19) | (s10 << 2))
  1605. out[27] = byte(s10 >> 6)
  1606. out[28] = byte((s10 >> 14) | (s11 << 7))
  1607. out[29] = byte(s11 >> 1)
  1608. out[30] = byte(s11 >> 9)
  1609. out[31] = byte(s11 >> 17)
  1610. }
上海开阖软件有限公司 沪ICP备12045867号-1