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.

3068 lines
73 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. // Copyright 2013 The Go Authors. All rights reserved.
  17. // Use of this source code is governed by a BSD-style
  18. // license that can be found in the LICENSE-BSD file.
  19. // Most of this is from the golang x/crypto package
  20. // Package edwards25519 implements operations in GF(2**255-19) and on an
  21. // Edwards curve that is isomorphic to curve25519. See
  22. // http://ed25519.cr.yp.to/.
  23. // move this file out of this package and use x/crypto
  24. package ringct
  25. // This code is a port of the public domain, "ref10" implementation of ed25519
  26. // from SUPERCOP.
  27. // FieldElement represents an element of the field GF(2^255 - 19). An element
  28. // t, entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
  29. // t[3]+2^102 t[4]+...+2^230 t[9]. Bounds on each t[i] vary depending on
  30. // context.
  31. type FieldElement [10]int32
  32. var FeMa = FieldElement{-486662, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* -A */
  33. var FeMa2 = FieldElement{-12721188, -3529, 0, 0, 0, 0, 0, 0, 0, 0} /* -A^2 */
  34. var FeFffb1 = FieldElement{-31702527, -2466483, -26106795, -12203692, -12169197, -321052, 14850977, -10296299, -16929438, -407568} /* sqrt(-2 * A * (A + 2)) */
  35. var FeFffb2 = FieldElement{8166131, -6741800, -17040804, 3154616, 21461005, 1466302, -30876704, -6368709, 10503587, -13363080} /* sqrt(2 * A * (A + 2)) */
  36. var FeFffb3 = FieldElement{-13620103, 14639558, 4532995, 7679154, 16815101, -15883539, -22863840, -14813421, 13716513, -6477756} /* sqrt(-sqrt(-1) * A * (A + 2)) */
  37. var FeFffb4 = FieldElement{-21786234, -12173074, 21573800, 4524538, -4645904, 16204591, 8012863, -8444712, 3212926, 6885324} /* sqrt(sqrt(-1) * A * (A + 2)) */
  38. var FeSqrtM1 = FieldElement{-32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482} /* sqrt(-1) */
  39. var zero FieldElement
  40. var one FieldElement
  41. func init() {
  42. one[0] = 1
  43. }
  44. func (f *FieldElement) Zero() {
  45. copy(f[:], zero[:])
  46. }
  47. func (f *FieldElement) One() {
  48. copy(f[:], one[:])
  49. }
  50. func FeAdd(dst, a, b *FieldElement) {
  51. dst[0] = a[0] + b[0]
  52. dst[1] = a[1] + b[1]
  53. dst[2] = a[2] + b[2]
  54. dst[3] = a[3] + b[3]
  55. dst[4] = a[4] + b[4]
  56. dst[5] = a[5] + b[5]
  57. dst[6] = a[6] + b[6]
  58. dst[7] = a[7] + b[7]
  59. dst[8] = a[8] + b[8]
  60. dst[9] = a[9] + b[9]
  61. }
  62. func FeSub(dst, a, b *FieldElement) {
  63. dst[0] = a[0] - b[0]
  64. dst[1] = a[1] - b[1]
  65. dst[2] = a[2] - b[2]
  66. dst[3] = a[3] - b[3]
  67. dst[4] = a[4] - b[4]
  68. dst[5] = a[5] - b[5]
  69. dst[6] = a[6] - b[6]
  70. dst[7] = a[7] - b[7]
  71. dst[8] = a[8] - b[8]
  72. dst[9] = a[9] - b[9]
  73. }
  74. func FeCopy(dst, src *FieldElement) {
  75. copy(dst[:], src[:])
  76. }
  77. // Replace (f,g) with (g,g) if b == 1;
  78. // replace (f,g) with (f,g) if b == 0.
  79. //
  80. // Preconditions: b in {0,1}.
  81. func FeCMove(f, g *FieldElement, b int32) {
  82. b = -b
  83. f[0] ^= b & (f[0] ^ g[0])
  84. f[1] ^= b & (f[1] ^ g[1])
  85. f[2] ^= b & (f[2] ^ g[2])
  86. f[3] ^= b & (f[3] ^ g[3])
  87. f[4] ^= b & (f[4] ^ g[4])
  88. f[5] ^= b & (f[5] ^ g[5])
  89. f[6] ^= b & (f[6] ^ g[6])
  90. f[7] ^= b & (f[7] ^ g[7])
  91. f[8] ^= b & (f[8] ^ g[8])
  92. f[9] ^= b & (f[9] ^ g[9])
  93. }
  94. func load3(in []byte) (result int64) {
  95. result = int64(in[0]) | (int64(in[1]) << 8) | (int64(in[2]) << 16)
  96. return
  97. }
  98. func load4(in []byte) (result int64) {
  99. result = int64(in[0]) | (int64(in[1]) << 8) | (int64(in[2]) << 16) | (int64(in[3]) << 24)
  100. return
  101. }
  102. func FeFromBytes(dst *FieldElement, src *Key) {
  103. h0 := load4(src[:])
  104. h1 := load3(src[4:]) << 6
  105. h2 := load3(src[7:]) << 5
  106. h3 := load3(src[10:]) << 3
  107. h4 := load3(src[13:]) << 2
  108. h5 := load4(src[16:])
  109. h6 := load3(src[20:]) << 7
  110. h7 := load3(src[23:]) << 5
  111. h8 := load3(src[26:]) << 4
  112. h9 := (load3(src[29:]) & 8388607) << 2
  113. FeCombine(dst, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9)
  114. }
  115. // FeToBytes marshals h to s.
  116. // Preconditions:
  117. // |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
  118. //
  119. // Write p=2^255-19; q=floor(h/p).
  120. // Basic claim: q = floor(2^(-255)(h + 19 2^(-25)h9 + 2^(-1))).
  121. //
  122. // Proof:
  123. // Have |h|<=p so |q|<=1 so |19^2 2^(-255) q|<1/4.
  124. // Also have |h-2^230 h9|<2^230 so |19 2^(-255)(h-2^230 h9)|<1/4.
  125. //
  126. // Write y=2^(-1)-19^2 2^(-255)q-19 2^(-255)(h-2^230 h9).
  127. // Then 0<y<1.
  128. //
  129. // Write r=h-pq.
  130. // Have 0<=r<=p-1=2^255-20.
  131. // Thus 0<=r+19(2^-255)r<r+19(2^-255)2^255<=2^255-1.
  132. //
  133. // Write x=r+19(2^-255)r+y.
  134. // Then 0<x<2^255 so floor(2^(-255)x) = 0 so floor(q+2^(-255)x) = q.
  135. //
  136. // Have q+2^(-255)x = 2^(-255)(h + 19 2^(-25) h9 + 2^(-1))
  137. // so floor(2^(-255)(h + 19 2^(-25) h9 + 2^(-1))) = q.
  138. func FeToBytes(s *Key, h *FieldElement) {
  139. var carry [10]int32
  140. q := (19*h[9] + (1 << 24)) >> 25
  141. q = (h[0] + q) >> 26
  142. q = (h[1] + q) >> 25
  143. q = (h[2] + q) >> 26
  144. q = (h[3] + q) >> 25
  145. q = (h[4] + q) >> 26
  146. q = (h[5] + q) >> 25
  147. q = (h[6] + q) >> 26
  148. q = (h[7] + q) >> 25
  149. q = (h[8] + q) >> 26
  150. q = (h[9] + q) >> 25
  151. // Goal: Output h-(2^255-19)q, which is between 0 and 2^255-20.
  152. h[0] += 19 * q
  153. // Goal: Output h-2^255 q, which is between 0 and 2^255-20.
  154. carry[0] = h[0] >> 26
  155. h[1] += carry[0]
  156. h[0] -= carry[0] << 26
  157. carry[1] = h[1] >> 25
  158. h[2] += carry[1]
  159. h[1] -= carry[1] << 25
  160. carry[2] = h[2] >> 26
  161. h[3] += carry[2]
  162. h[2] -= carry[2] << 26
  163. carry[3] = h[3] >> 25
  164. h[4] += carry[3]
  165. h[3] -= carry[3] << 25
  166. carry[4] = h[4] >> 26
  167. h[5] += carry[4]
  168. h[4] -= carry[4] << 26
  169. carry[5] = h[5] >> 25
  170. h[6] += carry[5]
  171. h[5] -= carry[5] << 25
  172. carry[6] = h[6] >> 26
  173. h[7] += carry[6]
  174. h[6] -= carry[6] << 26
  175. carry[7] = h[7] >> 25
  176. h[8] += carry[7]
  177. h[7] -= carry[7] << 25
  178. carry[8] = h[8] >> 26
  179. h[9] += carry[8]
  180. h[8] -= carry[8] << 26
  181. carry[9] = h[9] >> 25
  182. h[9] -= carry[9] << 25
  183. // h10 = carry9
  184. // Goal: Output h[0]+...+2^255 h10-2^255 q, which is between 0 and 2^255-20.
  185. // Have h[0]+...+2^230 h[9] between 0 and 2^255-1;
  186. // evidently 2^255 h10-2^255 q = 0.
  187. // Goal: Output h[0]+...+2^230 h[9].
  188. s[0] = byte(h[0] >> 0)
  189. s[1] = byte(h[0] >> 8)
  190. s[2] = byte(h[0] >> 16)
  191. s[3] = byte((h[0] >> 24) | (h[1] << 2))
  192. s[4] = byte(h[1] >> 6)
  193. s[5] = byte(h[1] >> 14)
  194. s[6] = byte((h[1] >> 22) | (h[2] << 3))
  195. s[7] = byte(h[2] >> 5)
  196. s[8] = byte(h[2] >> 13)
  197. s[9] = byte((h[2] >> 21) | (h[3] << 5))
  198. s[10] = byte(h[3] >> 3)
  199. s[11] = byte(h[3] >> 11)
  200. s[12] = byte((h[3] >> 19) | (h[4] << 6))
  201. s[13] = byte(h[4] >> 2)
  202. s[14] = byte(h[4] >> 10)
  203. s[15] = byte(h[4] >> 18)
  204. s[16] = byte(h[5] >> 0)
  205. s[17] = byte(h[5] >> 8)
  206. s[18] = byte(h[5] >> 16)
  207. s[19] = byte((h[5] >> 24) | (h[6] << 1))
  208. s[20] = byte(h[6] >> 7)
  209. s[21] = byte(h[6] >> 15)
  210. s[22] = byte((h[6] >> 23) | (h[7] << 3))
  211. s[23] = byte(h[7] >> 5)
  212. s[24] = byte(h[7] >> 13)
  213. s[25] = byte((h[7] >> 21) | (h[8] << 4))
  214. s[26] = byte(h[8] >> 4)
  215. s[27] = byte(h[8] >> 12)
  216. s[28] = byte((h[8] >> 20) | (h[9] << 6))
  217. s[29] = byte(h[9] >> 2)
  218. s[30] = byte(h[9] >> 10)
  219. s[31] = byte(h[9] >> 18)
  220. }
  221. func (f *FieldElement) IsNegative() byte {
  222. var s Key
  223. FeToBytes(&s, f)
  224. return s[0] & 1
  225. }
  226. func (f *FieldElement) IsNonZero() int32 {
  227. var s Key
  228. FeToBytes(&s, f)
  229. var x uint8
  230. for _, b := range s {
  231. x |= b
  232. }
  233. x |= x >> 4
  234. x |= x >> 2
  235. x |= x >> 1
  236. return int32(x & 1)
  237. }
  238. // FeNeg sets h = -f
  239. //
  240. // Preconditions:
  241. // |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
  242. //
  243. // Postconditions:
  244. // |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
  245. func FeNeg(h, f *FieldElement) {
  246. h[0] = -f[0]
  247. h[1] = -f[1]
  248. h[2] = -f[2]
  249. h[3] = -f[3]
  250. h[4] = -f[4]
  251. h[5] = -f[5]
  252. h[6] = -f[6]
  253. h[7] = -f[7]
  254. h[8] = -f[8]
  255. h[9] = -f[9]
  256. }
  257. func FeCombine(h *FieldElement, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 int64) {
  258. var c0, c1, c2, c3, c4, c5, c6, c7, c8, c9 int64
  259. /*
  260. |h0| <= (1.1*1.1*2^52*(1+19+19+19+19)+1.1*1.1*2^50*(38+38+38+38+38))
  261. i.e. |h0| <= 1.2*2^59; narrower ranges for h2, h4, h6, h8
  262. |h1| <= (1.1*1.1*2^51*(1+1+19+19+19+19+19+19+19+19))
  263. i.e. |h1| <= 1.5*2^58; narrower ranges for h3, h5, h7, h9
  264. */
  265. c0 = (h0 + (1 << 25)) >> 26
  266. h1 += c0
  267. h0 -= c0 << 26
  268. c4 = (h4 + (1 << 25)) >> 26
  269. h5 += c4
  270. h4 -= c4 << 26
  271. /* |h0| <= 2^25 */
  272. /* |h4| <= 2^25 */
  273. /* |h1| <= 1.51*2^58 */
  274. /* |h5| <= 1.51*2^58 */
  275. c1 = (h1 + (1 << 24)) >> 25
  276. h2 += c1
  277. h1 -= c1 << 25
  278. c5 = (h5 + (1 << 24)) >> 25
  279. h6 += c5
  280. h5 -= c5 << 25
  281. /* |h1| <= 2^24; from now on fits into int32 */
  282. /* |h5| <= 2^24; from now on fits into int32 */
  283. /* |h2| <= 1.21*2^59 */
  284. /* |h6| <= 1.21*2^59 */
  285. c2 = (h2 + (1 << 25)) >> 26
  286. h3 += c2
  287. h2 -= c2 << 26
  288. c6 = (h6 + (1 << 25)) >> 26
  289. h7 += c6
  290. h6 -= c6 << 26
  291. /* |h2| <= 2^25; from now on fits into int32 unchanged */
  292. /* |h6| <= 2^25; from now on fits into int32 unchanged */
  293. /* |h3| <= 1.51*2^58 */
  294. /* |h7| <= 1.51*2^58 */
  295. c3 = (h3 + (1 << 24)) >> 25
  296. h4 += c3
  297. h3 -= c3 << 25
  298. c7 = (h7 + (1 << 24)) >> 25
  299. h8 += c7
  300. h7 -= c7 << 25
  301. /* |h3| <= 2^24; from now on fits into int32 unchanged */
  302. /* |h7| <= 2^24; from now on fits into int32 unchanged */
  303. /* |h4| <= 1.52*2^33 */
  304. /* |h8| <= 1.52*2^33 */
  305. c4 = (h4 + (1 << 25)) >> 26
  306. h5 += c4
  307. h4 -= c4 << 26
  308. c8 = (h8 + (1 << 25)) >> 26
  309. h9 += c8
  310. h8 -= c8 << 26
  311. /* |h4| <= 2^25; from now on fits into int32 unchanged */
  312. /* |h8| <= 2^25; from now on fits into int32 unchanged */
  313. /* |h5| <= 1.01*2^24 */
  314. /* |h9| <= 1.51*2^58 */
  315. c9 = (h9 + (1 << 24)) >> 25
  316. h0 += c9 * 19
  317. h9 -= c9 << 25
  318. /* |h9| <= 2^24; from now on fits into int32 unchanged */
  319. /* |h0| <= 1.8*2^37 */
  320. c0 = (h0 + (1 << 25)) >> 26
  321. h1 += c0
  322. h0 -= c0 << 26
  323. /* |h0| <= 2^25; from now on fits into int32 unchanged */
  324. /* |h1| <= 1.01*2^24 */
  325. h[0] = int32(h0)
  326. h[1] = int32(h1)
  327. h[2] = int32(h2)
  328. h[3] = int32(h3)
  329. h[4] = int32(h4)
  330. h[5] = int32(h5)
  331. h[6] = int32(h6)
  332. h[7] = int32(h7)
  333. h[8] = int32(h8)
  334. h[9] = int32(h9)
  335. }
  336. // FeMul calculates h = f * g
  337. // Can overlap h with f or g.
  338. //
  339. // Preconditions:
  340. // |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
  341. // |g| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
  342. //
  343. // Postconditions:
  344. // |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
  345. //
  346. // Notes on implementation strategy:
  347. //
  348. // Using schoolbook multiplication.
  349. // Karatsuba would save a little in some cost models.
  350. //
  351. // Most multiplications by 2 and 19 are 32-bit precomputations;
  352. // cheaper than 64-bit postcomputations.
  353. //
  354. // There is one remaining multiplication by 19 in the carry chain;
  355. // one *19 precomputation can be merged into this,
  356. // but the resulting data flow is considerably less clean.
  357. //
  358. // There are 12 carries below.
  359. // 10 of them are 2-way parallelizable and vectorizable.
  360. // Can get away with 11 carries, but then data flow is much deeper.
  361. //
  362. // With tighter constraints on inputs can squeeze carries into int32.
  363. func FeMul(h, f, g *FieldElement) {
  364. f0 := int64(f[0])
  365. f1 := int64(f[1])
  366. f2 := int64(f[2])
  367. f3 := int64(f[3])
  368. f4 := int64(f[4])
  369. f5 := int64(f[5])
  370. f6 := int64(f[6])
  371. f7 := int64(f[7])
  372. f8 := int64(f[8])
  373. f9 := int64(f[9])
  374. f1_2 := int64(2 * f[1])
  375. f3_2 := int64(2 * f[3])
  376. f5_2 := int64(2 * f[5])
  377. f7_2 := int64(2 * f[7])
  378. f9_2 := int64(2 * f[9])
  379. g0 := int64(g[0])
  380. g1 := int64(g[1])
  381. g2 := int64(g[2])
  382. g3 := int64(g[3])
  383. g4 := int64(g[4])
  384. g5 := int64(g[5])
  385. g6 := int64(g[6])
  386. g7 := int64(g[7])
  387. g8 := int64(g[8])
  388. g9 := int64(g[9])
  389. g1_19 := int64(19 * g[1]) /* 1.4*2^29 */
  390. g2_19 := int64(19 * g[2]) /* 1.4*2^30; still ok */
  391. g3_19 := int64(19 * g[3])
  392. g4_19 := int64(19 * g[4])
  393. g5_19 := int64(19 * g[5])
  394. g6_19 := int64(19 * g[6])
  395. g7_19 := int64(19 * g[7])
  396. g8_19 := int64(19 * g[8])
  397. g9_19 := int64(19 * g[9])
  398. 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
  399. 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
  400. 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
  401. 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
  402. 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
  403. 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
  404. 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
  405. h7 := f0*g7 + f1*g6 + f2*g5 + f3*g4 + f4*g3 + f5*g2 + f6*g1 + f7*g0 + f8*g9_19 + f9*g8_19
  406. 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
  407. h9 := f0*g9 + f1*g8 + f2*g7 + f3*g6 + f4*g5 + f5*g4 + f6*g3 + f7*g2 + f8*g1 + f9*g0
  408. FeCombine(h, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9)
  409. }
  410. func feSquare(f *FieldElement) (h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 int64) {
  411. f0 := int64(f[0])
  412. f1 := int64(f[1])
  413. f2 := int64(f[2])
  414. f3 := int64(f[3])
  415. f4 := int64(f[4])
  416. f5 := int64(f[5])
  417. f6 := int64(f[6])
  418. f7 := int64(f[7])
  419. f8 := int64(f[8])
  420. f9 := int64(f[9])
  421. f0_2 := int64(2 * f[0])
  422. f1_2 := int64(2 * f[1])
  423. f2_2 := int64(2 * f[2])
  424. f3_2 := int64(2 * f[3])
  425. f4_2 := int64(2 * f[4])
  426. f5_2 := int64(2 * f[5])
  427. f6_2 := int64(2 * f[6])
  428. f7_2 := int64(2 * f[7])
  429. f5_38 := 38 * f5 // 1.31*2^30
  430. f6_19 := 19 * f6 // 1.31*2^30
  431. f7_38 := 38 * f7 // 1.31*2^30
  432. f8_19 := 19 * f8 // 1.31*2^30
  433. f9_38 := 38 * f9 // 1.31*2^30
  434. h0 = f0*f0 + f1_2*f9_38 + f2_2*f8_19 + f3_2*f7_38 + f4_2*f6_19 + f5*f5_38
  435. h1 = f0_2*f1 + f2*f9_38 + f3_2*f8_19 + f4*f7_38 + f5_2*f6_19
  436. h2 = f0_2*f2 + f1_2*f1 + f3_2*f9_38 + f4_2*f8_19 + f5_2*f7_38 + f6*f6_19
  437. h3 = f0_2*f3 + f1_2*f2 + f4*f9_38 + f5_2*f8_19 + f6*f7_38
  438. h4 = f0_2*f4 + f1_2*f3_2 + f2*f2 + f5_2*f9_38 + f6_2*f8_19 + f7*f7_38
  439. h5 = f0_2*f5 + f1_2*f4 + f2_2*f3 + f6*f9_38 + f7_2*f8_19
  440. h6 = f0_2*f6 + f1_2*f5_2 + f2_2*f4 + f3_2*f3 + f7_2*f9_38 + f8*f8_19
  441. h7 = f0_2*f7 + f1_2*f6 + f2_2*f5 + f3_2*f4 + f8*f9_38
  442. h8 = f0_2*f8 + f1_2*f7_2 + f2_2*f6 + f3_2*f5_2 + f4*f4 + f9*f9_38
  443. h9 = f0_2*f9 + f1_2*f8 + f2_2*f7 + f3_2*f6 + f4_2*f5
  444. return
  445. }
  446. // FeSquare calculates h = f*f. Can overlap h with f.
  447. //
  448. // Preconditions:
  449. // |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc.
  450. //
  451. // Postconditions:
  452. // |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc.
  453. func FeSquare(h, f *FieldElement) {
  454. h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 := feSquare(f)
  455. FeCombine(h, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9)
  456. }
  457. // FeSquare2 sets h = 2 * f * f
  458. //
  459. // Can overlap h with f.
  460. //
  461. // Preconditions:
  462. // |f| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc.
  463. //
  464. // Postconditions:
  465. // |h| bounded by 1.01*2^25,1.01*2^24,1.01*2^25,1.01*2^24,etc.
  466. // See fe_mul.c for discussion of implementation strategy.
  467. func FeSquare2(h, f *FieldElement) {
  468. h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 := feSquare(f)
  469. h0 += h0
  470. h1 += h1
  471. h2 += h2
  472. h3 += h3
  473. h4 += h4
  474. h5 += h5
  475. h6 += h6
  476. h7 += h7
  477. h8 += h8
  478. h9 += h9
  479. FeCombine(h, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9)
  480. }
  481. func FeInvert(out, z *FieldElement) {
  482. var t0, t1, t2, t3 FieldElement
  483. var i int
  484. FeSquare(&t0, z) // 2^1
  485. FeSquare(&t1, &t0) // 2^2
  486. for i = 1; i < 2; i++ { // 2^3
  487. FeSquare(&t1, &t1)
  488. }
  489. FeMul(&t1, z, &t1) // 2^3 + 2^0
  490. FeMul(&t0, &t0, &t1) // 2^3 + 2^1 + 2^0
  491. FeSquare(&t2, &t0) // 2^4 + 2^2 + 2^1
  492. FeMul(&t1, &t1, &t2) // 2^4 + 2^3 + 2^2 + 2^1 + 2^0
  493. FeSquare(&t2, &t1) // 5,4,3,2,1
  494. for i = 1; i < 5; i++ { // 9,8,7,6,5
  495. FeSquare(&t2, &t2)
  496. }
  497. FeMul(&t1, &t2, &t1) // 9,8,7,6,5,4,3,2,1,0
  498. FeSquare(&t2, &t1) // 10..1
  499. for i = 1; i < 10; i++ { // 19..10
  500. FeSquare(&t2, &t2)
  501. }
  502. FeMul(&t2, &t2, &t1) // 19..0
  503. FeSquare(&t3, &t2) // 20..1
  504. for i = 1; i < 20; i++ { // 39..20
  505. FeSquare(&t3, &t3)
  506. }
  507. FeMul(&t2, &t3, &t2) // 39..0
  508. FeSquare(&t2, &t2) // 40..1
  509. for i = 1; i < 10; i++ { // 49..10
  510. FeSquare(&t2, &t2)
  511. }
  512. FeMul(&t1, &t2, &t1) // 49..0
  513. FeSquare(&t2, &t1) // 50..1
  514. for i = 1; i < 50; i++ { // 99..50
  515. FeSquare(&t2, &t2)
  516. }
  517. FeMul(&t2, &t2, &t1) // 99..0
  518. FeSquare(&t3, &t2) // 100..1
  519. for i = 1; i < 100; i++ { // 199..100
  520. FeSquare(&t3, &t3)
  521. }
  522. FeMul(&t2, &t3, &t2) // 199..0
  523. FeSquare(&t2, &t2) // 200..1
  524. for i = 1; i < 50; i++ { // 249..50
  525. FeSquare(&t2, &t2)
  526. }
  527. FeMul(&t1, &t2, &t1) // 249..0
  528. FeSquare(&t1, &t1) // 250..1
  529. for i = 1; i < 5; i++ { // 254..5
  530. FeSquare(&t1, &t1)
  531. }
  532. FeMul(out, &t1, &t0) // 254..5,3,1,0
  533. }
  534. func fePow22523(out, z *FieldElement) {
  535. var t0, t1, t2 FieldElement
  536. var i int
  537. FeSquare(&t0, z)
  538. for i = 1; i < 1; i++ {
  539. FeSquare(&t0, &t0)
  540. }
  541. FeSquare(&t1, &t0)
  542. for i = 1; i < 2; i++ {
  543. FeSquare(&t1, &t1)
  544. }
  545. FeMul(&t1, z, &t1)
  546. FeMul(&t0, &t0, &t1)
  547. FeSquare(&t0, &t0)
  548. for i = 1; i < 1; i++ {
  549. FeSquare(&t0, &t0)
  550. }
  551. FeMul(&t0, &t1, &t0)
  552. FeSquare(&t1, &t0)
  553. for i = 1; i < 5; i++ {
  554. FeSquare(&t1, &t1)
  555. }
  556. FeMul(&t0, &t1, &t0)
  557. FeSquare(&t1, &t0)
  558. for i = 1; i < 10; i++ {
  559. FeSquare(&t1, &t1)
  560. }
  561. FeMul(&t1, &t1, &t0)
  562. FeSquare(&t2, &t1)
  563. for i = 1; i < 20; i++ {
  564. FeSquare(&t2, &t2)
  565. }
  566. FeMul(&t1, &t2, &t1)
  567. FeSquare(&t1, &t1)
  568. for i = 1; i < 10; i++ {
  569. FeSquare(&t1, &t1)
  570. }
  571. FeMul(&t0, &t1, &t0)
  572. FeSquare(&t1, &t0)
  573. for i = 1; i < 50; i++ {
  574. FeSquare(&t1, &t1)
  575. }
  576. FeMul(&t1, &t1, &t0)
  577. FeSquare(&t2, &t1)
  578. for i = 1; i < 100; i++ {
  579. FeSquare(&t2, &t2)
  580. }
  581. FeMul(&t1, &t2, &t1)
  582. FeSquare(&t1, &t1)
  583. for i = 1; i < 50; i++ {
  584. FeSquare(&t1, &t1)
  585. }
  586. FeMul(&t0, &t1, &t0)
  587. FeSquare(&t0, &t0)
  588. for i = 1; i < 2; i++ {
  589. FeSquare(&t0, &t0)
  590. }
  591. FeMul(out, &t0, z)
  592. }
  593. func FeDivPowM1(out, u, v *FieldElement) {
  594. var v3, uv7, t0 FieldElement
  595. FeSquare(&v3, v)
  596. FeMul(&v3, &v3, v) /* v3 = v^3 */
  597. FeSquare(&uv7, &v3)
  598. FeMul(&uv7, &uv7, v)
  599. FeMul(&uv7, &uv7, u) /* uv7 = uv^7 */
  600. fePow22523(&t0, &uv7)
  601. /* t0 = (uv^7)^((q-5)/8) */
  602. FeMul(&t0, &t0, &v3)
  603. FeMul(out, &t0, u) /* u^(m+1)v^(-(m+1)) */
  604. }
  605. // Group elements are members of the elliptic curve -x^2 + y^2 = 1 + d * x^2 *
  606. // y^2 where d = -121665/121666.
  607. //
  608. // Several representations are used:
  609. // ProjectiveGroupElement: (X:Y:Z) satisfying x=X/Z, y=Y/Z
  610. // ExtendedGroupElement: (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT
  611. // CompletedGroupElement: ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
  612. // PreComputedGroupElement: (y+x,y-x,2dxy)
  613. type ProjectiveGroupElement struct {
  614. X, Y, Z FieldElement
  615. }
  616. type ExtendedGroupElement struct {
  617. X, Y, Z, T FieldElement
  618. }
  619. type CompletedGroupElement struct {
  620. X, Y, Z, T FieldElement
  621. }
  622. type PreComputedGroupElement struct {
  623. yPlusX, yMinusX, xy2d FieldElement
  624. }
  625. type CachedGroupElement struct {
  626. yPlusX, yMinusX, Z, T2d FieldElement
  627. }
  628. func (p *ProjectiveGroupElement) Zero() {
  629. p.X.Zero()
  630. p.Y.One()
  631. p.Z.One()
  632. }
  633. func (p *ProjectiveGroupElement) Double(r *CompletedGroupElement) {
  634. var t0 FieldElement
  635. FeSquare(&r.X, &p.X)
  636. FeSquare(&r.Z, &p.Y)
  637. FeSquare2(&r.T, &p.Z)
  638. FeAdd(&r.Y, &p.X, &p.Y)
  639. FeSquare(&t0, &r.Y)
  640. FeAdd(&r.Y, &r.Z, &r.X)
  641. FeSub(&r.Z, &r.Z, &r.X)
  642. FeSub(&r.X, &t0, &r.Y)
  643. FeSub(&r.T, &r.T, &r.Z)
  644. }
  645. func (p *ProjectiveGroupElement) ToBytes(s *Key) {
  646. var recip, x, y FieldElement
  647. FeInvert(&recip, &p.Z)
  648. FeMul(&x, &p.X, &recip)
  649. FeMul(&y, &p.Y, &recip)
  650. FeToBytes(s, &y)
  651. s[31] ^= x.IsNegative() << 7
  652. }
  653. func (p *ProjectiveGroupElement) FromBytes(s *Key) {
  654. h0 := load4(s[:])
  655. h1 := load3(s[4:]) << 6
  656. h2 := load3(s[7:]) << 5
  657. h3 := load3(s[10:]) << 3
  658. h4 := load3(s[13:]) << 2
  659. h5 := load4(s[16:])
  660. h6 := load3(s[20:]) << 7
  661. h7 := load3(s[23:]) << 5
  662. h8 := load3(s[26:]) << 4
  663. h9 := load3(s[29:]) << 2
  664. var carry [10]int64
  665. carry[9] = (h9 + int64(1<<24)) >> 25
  666. h0 += carry[9] * 19
  667. h9 -= carry[9] << 25
  668. carry[1] = (h1 + int64(1<<24)) >> 25
  669. h2 += carry[1]
  670. h1 -= carry[1] << 25
  671. carry[3] = (h3 + int64(1<<24)) >> 25
  672. h4 += carry[3]
  673. h3 -= carry[3] << 25
  674. carry[5] = (h5 + int64(1<<24)) >> 25
  675. h6 += carry[5]
  676. h5 -= carry[5] << 25
  677. carry[7] = (h7 + int64(1<<24)) >> 25
  678. h8 += carry[7]
  679. h7 -= carry[7] << 25
  680. carry[0] = (h0 + int64(1<<25)) >> 26
  681. h1 += carry[0]
  682. h0 -= carry[0] << 26
  683. carry[2] = (h2 + int64(1<<25)) >> 26
  684. h3 += carry[2]
  685. h2 -= carry[2] << 26
  686. carry[4] = (h4 + int64(1<<25)) >> 26
  687. h5 += carry[4]
  688. h4 -= carry[4] << 26
  689. carry[6] = (h6 + int64(1<<25)) >> 26
  690. h7 += carry[6]
  691. h6 -= carry[6] << 26
  692. carry[8] = (h8 + int64(1<<25)) >> 26
  693. h9 += carry[8]
  694. h8 -= carry[8] << 26
  695. var u, v, w, x, y, z FieldElement
  696. u[0] = int32(h0)
  697. u[1] = int32(h1)
  698. u[2] = int32(h2)
  699. u[3] = int32(h3)
  700. u[4] = int32(h4)
  701. u[5] = int32(h5)
  702. u[6] = int32(h6)
  703. u[7] = int32(h7)
  704. u[8] = int32(h8)
  705. u[9] = int32(h9)
  706. FeSquare2(&v, &u) /* 2 * u^2 */
  707. w.One()
  708. FeAdd(&w, &v, &w) /* w = 2 * u^2 + 1 */
  709. FeSquare(&x, &w) /* w^2 */
  710. FeMul(&y, &FeMa2, &v) /* -2 * A^2 * u^2 */
  711. FeAdd(&x, &x, &y) /* x = w^2 - 2 * A^2 * u^2 */
  712. FeDivPowM1(&p.X, &w, &x) /* (w / x)^(m + 1) */
  713. FeSquare(&y, &p.X)
  714. FeMul(&x, &y, &x)
  715. FeSub(&y, &w, &x)
  716. FeCopy(&z, &FeMa)
  717. isNegative := false
  718. var sign byte
  719. if y.IsNonZero() != 0 {
  720. FeAdd(&y, &w, &x)
  721. if y.IsNonZero() != 0 {
  722. isNegative = true
  723. } else {
  724. FeMul(&p.X, &p.X, &FeFffb1)
  725. }
  726. } else {
  727. FeMul(&p.X, &p.X, &FeFffb2)
  728. }
  729. if isNegative {
  730. FeMul(&x, &x, &FeSqrtM1)
  731. FeSub(&y, &w, &x)
  732. if y.IsNonZero() != 0 {
  733. FeAdd(&y, &w, &x)
  734. FeMul(&p.X, &p.X, &FeFffb3)
  735. } else {
  736. FeMul(&p.X, &p.X, &FeFffb4)
  737. }
  738. /* p.X = sqrt(A * (A + 2) * w / x) */
  739. /* z = -A */
  740. sign = 1
  741. } else {
  742. FeMul(&p.X, &p.X, &u) /* u * sqrt(2 * A * (A + 2) * w / x) */
  743. FeMul(&z, &z, &v) /* -2 * A * u^2 */
  744. sign = 0
  745. }
  746. if p.X.IsNegative() != sign {
  747. FeNeg(&p.X, &p.X)
  748. }
  749. FeAdd(&p.Z, &z, &w)
  750. FeSub(&p.Y, &z, &w)
  751. FeMul(&p.X, &p.X, &p.Z)
  752. }
  753. func (p *ExtendedGroupElement) Zero() {
  754. p.X.Zero()
  755. p.Y.One()
  756. p.Z.One()
  757. p.T.Zero()
  758. }
  759. func (p *ExtendedGroupElement) Double(r *CompletedGroupElement) {
  760. var q ProjectiveGroupElement
  761. p.ToProjective(&q)
  762. q.Double(r)
  763. }
  764. func (p *ExtendedGroupElement) ToCached(r *CachedGroupElement) {
  765. FeAdd(&r.yPlusX, &p.Y, &p.X)
  766. FeSub(&r.yMinusX, &p.Y, &p.X)
  767. FeCopy(&r.Z, &p.Z)
  768. FeMul(&r.T2d, &p.T, &d2)
  769. }
  770. func (p *ExtendedGroupElement) ToProjective(r *ProjectiveGroupElement) {
  771. FeCopy(&r.X, &p.X)
  772. FeCopy(&r.Y, &p.Y)
  773. FeCopy(&r.Z, &p.Z)
  774. }
  775. func (p *ExtendedGroupElement) ToBytes(s *Key) {
  776. var recip, x, y FieldElement
  777. FeInvert(&recip, &p.Z)
  778. FeMul(&x, &p.X, &recip)
  779. FeMul(&y, &p.Y, &recip)
  780. FeToBytes(s, &y)
  781. s[31] ^= x.IsNegative() << 7
  782. }
  783. func (p *ExtendedGroupElement) FromBytes(s *Key) bool {
  784. var u, v, v3, vxx, check FieldElement
  785. FeFromBytes(&p.Y, s)
  786. p.Z.One()
  787. FeSquare(&u, &p.Y)
  788. FeMul(&v, &u, &d)
  789. FeSub(&u, &u, &p.Z) // y = y^2-1
  790. FeAdd(&v, &v, &p.Z) // v = dy^2+1
  791. FeSquare(&v3, &v)
  792. FeMul(&v3, &v3, &v) // v3 = v^3
  793. FeSquare(&p.X, &v3)
  794. FeMul(&p.X, &p.X, &v)
  795. FeMul(&p.X, &p.X, &u) // x = uv^7
  796. fePow22523(&p.X, &p.X) // x = (uv^7)^((q-5)/8)
  797. FeMul(&p.X, &p.X, &v3)
  798. FeMul(&p.X, &p.X, &u) // x = uv^3(uv^7)^((q-5)/8)
  799. var tmpX, tmp2 Key
  800. FeSquare(&vxx, &p.X)
  801. FeMul(&vxx, &vxx, &v)
  802. FeSub(&check, &vxx, &u) // vx^2-u
  803. if check.IsNonZero() == 1 {
  804. FeAdd(&check, &vxx, &u) // vx^2+u
  805. if check.IsNonZero() == 1 {
  806. return false
  807. }
  808. FeMul(&p.X, &p.X, &SqrtM1)
  809. FeToBytes(&tmpX, &p.X)
  810. for i, v := range tmpX {
  811. tmp2[31-i] = v
  812. }
  813. }
  814. if p.X.IsNegative() != (s[31] >> 7) {
  815. FeNeg(&p.X, &p.X)
  816. }
  817. FeMul(&p.T, &p.X, &p.Y)
  818. return true
  819. }
  820. func (p *CompletedGroupElement) ToProjective(r *ProjectiveGroupElement) {
  821. FeMul(&r.X, &p.X, &p.T)
  822. FeMul(&r.Y, &p.Y, &p.Z)
  823. FeMul(&r.Z, &p.Z, &p.T)
  824. }
  825. func (p *CompletedGroupElement) ToExtended(r *ExtendedGroupElement) {
  826. FeMul(&r.X, &p.X, &p.T)
  827. FeMul(&r.Y, &p.Y, &p.Z)
  828. FeMul(&r.Z, &p.Z, &p.T)
  829. FeMul(&r.T, &p.X, &p.Y)
  830. }
  831. func (p *PreComputedGroupElement) Zero() {
  832. p.yPlusX.One()
  833. p.yMinusX.One()
  834. p.xy2d.Zero()
  835. }
  836. func (c *CachedGroupElement) Zero() {
  837. c.yPlusX.One()
  838. c.yMinusX.One()
  839. c.Z.One()
  840. c.T2d.Zero()
  841. }
  842. func geAdd(r *CompletedGroupElement, p *ExtendedGroupElement, q *CachedGroupElement) {
  843. var t0 FieldElement
  844. FeAdd(&r.X, &p.Y, &p.X)
  845. FeSub(&r.Y, &p.Y, &p.X)
  846. FeMul(&r.Z, &r.X, &q.yPlusX)
  847. FeMul(&r.Y, &r.Y, &q.yMinusX)
  848. FeMul(&r.T, &q.T2d, &p.T)
  849. FeMul(&r.X, &p.Z, &q.Z)
  850. FeAdd(&t0, &r.X, &r.X)
  851. FeSub(&r.X, &r.Z, &r.Y)
  852. FeAdd(&r.Y, &r.Z, &r.Y)
  853. FeAdd(&r.Z, &t0, &r.T)
  854. FeSub(&r.T, &t0, &r.T)
  855. }
  856. func geSub(r *CompletedGroupElement, p *ExtendedGroupElement, q *CachedGroupElement) {
  857. var t0 FieldElement
  858. FeAdd(&r.X, &p.Y, &p.X)
  859. FeSub(&r.Y, &p.Y, &p.X)
  860. FeMul(&r.Z, &r.X, &q.yMinusX)
  861. FeMul(&r.Y, &r.Y, &q.yPlusX)
  862. FeMul(&r.T, &q.T2d, &p.T)
  863. FeMul(&r.X, &p.Z, &q.Z)
  864. FeAdd(&t0, &r.X, &r.X)
  865. FeSub(&r.X, &r.Z, &r.Y)
  866. FeAdd(&r.Y, &r.Z, &r.Y)
  867. FeSub(&r.Z, &t0, &r.T)
  868. FeAdd(&r.T, &t0, &r.T)
  869. }
  870. func geMixedAdd(r *CompletedGroupElement, p *ExtendedGroupElement, q *PreComputedGroupElement) {
  871. var t0 FieldElement
  872. FeAdd(&r.X, &p.Y, &p.X)
  873. FeSub(&r.Y, &p.Y, &p.X)
  874. FeMul(&r.Z, &r.X, &q.yPlusX)
  875. FeMul(&r.Y, &r.Y, &q.yMinusX)
  876. FeMul(&r.T, &q.xy2d, &p.T)
  877. FeAdd(&t0, &p.Z, &p.Z)
  878. FeSub(&r.X, &r.Z, &r.Y)
  879. FeAdd(&r.Y, &r.Z, &r.Y)
  880. FeAdd(&r.Z, &t0, &r.T)
  881. FeSub(&r.T, &t0, &r.T)
  882. }
  883. func geMixedSub(r *CompletedGroupElement, p *ExtendedGroupElement, q *PreComputedGroupElement) {
  884. var t0 FieldElement
  885. FeAdd(&r.X, &p.Y, &p.X)
  886. FeSub(&r.Y, &p.Y, &p.X)
  887. FeMul(&r.Z, &r.X, &q.yMinusX)
  888. FeMul(&r.Y, &r.Y, &q.yPlusX)
  889. FeMul(&r.T, &q.xy2d, &p.T)
  890. FeAdd(&t0, &p.Z, &p.Z)
  891. FeSub(&r.X, &r.Z, &r.Y)
  892. FeAdd(&r.Y, &r.Z, &r.Y)
  893. FeSub(&r.Z, &t0, &r.T)
  894. FeAdd(&r.T, &t0, &r.T)
  895. }
  896. // r = 8 * t
  897. func GeMul8(r *CompletedGroupElement, t *ProjectiveGroupElement) {
  898. var u ProjectiveGroupElement
  899. t.Double(r)
  900. r.ToProjective(&u)
  901. u.Double(r)
  902. r.ToProjective(&u)
  903. u.Double(r)
  904. }
  905. // caches s into an array of CachedGroupElements for scalar multiplication later
  906. func GePrecompute(r *[8]CachedGroupElement, s *ExtendedGroupElement) {
  907. var t CompletedGroupElement
  908. var s2, u ExtendedGroupElement
  909. s.ToCached(&r[0])
  910. s.Double(&t)
  911. t.ToExtended(&s2)
  912. for i := 0; i < 7; i++ {
  913. geAdd(&t, &s2, &r[i])
  914. t.ToExtended(&u)
  915. u.ToCached(&r[i+1])
  916. }
  917. }
  918. func slide(r *[256]int8, a *Key) {
  919. for i := range r {
  920. r[i] = int8(1 & (a[i>>3] >> uint(i&7)))
  921. }
  922. for i := range r {
  923. if r[i] != 0 {
  924. for b := 1; b <= 6 && i+b < 256; b++ {
  925. if r[i+b] != 0 {
  926. if r[i]+(r[i+b]<<uint(b)) <= 15 {
  927. r[i] += r[i+b] << uint(b)
  928. r[i+b] = 0
  929. } else if r[i]-(r[i+b]<<uint(b)) >= -15 {
  930. r[i] -= r[i+b] << uint(b)
  931. for k := i + b; k < 256; k++ {
  932. if r[k] == 0 {
  933. r[k] = 1
  934. break
  935. }
  936. r[k] = 0
  937. }
  938. } else {
  939. break
  940. }
  941. }
  942. }
  943. }
  944. }
  945. }
  946. // GeDoubleScalarMultVartime sets r = a*A + b*B
  947. // where a = a[0]+256*a[1]+...+256^31 a[31].
  948. // and b = b[0]+256*b[1]+...+256^31 b[31].
  949. // B is the Ed25519 base point (x,4/5) with x positive.
  950. func GeDoubleScalarMultVartime(r *ProjectiveGroupElement, a *Key, A *ExtendedGroupElement, b *Key) {
  951. var aSlide, bSlide [256]int8
  952. var Ai [8]CachedGroupElement // A,3A,5A,7A,9A,11A,13A,15A
  953. var t CompletedGroupElement
  954. var u ExtendedGroupElement
  955. var i int
  956. slide(&aSlide, a)
  957. slide(&bSlide, b)
  958. GePrecompute(&Ai, A)
  959. r.Zero()
  960. for i = 255; i >= 0; i-- {
  961. if aSlide[i] != 0 || bSlide[i] != 0 {
  962. break
  963. }
  964. }
  965. for ; i >= 0; i-- {
  966. r.Double(&t)
  967. if aSlide[i] > 0 {
  968. t.ToExtended(&u)
  969. geAdd(&t, &u, &Ai[aSlide[i]/2])
  970. } else if aSlide[i] < 0 {
  971. t.ToExtended(&u)
  972. geSub(&t, &u, &Ai[(-aSlide[i])/2])
  973. }
  974. if bSlide[i] > 0 {
  975. t.ToExtended(&u)
  976. geMixedAdd(&t, &u, &bi[bSlide[i]/2])
  977. } else if bSlide[i] < 0 {
  978. t.ToExtended(&u)
  979. geMixedSub(&t, &u, &bi[(-bSlide[i])/2])
  980. }
  981. t.ToProjective(r)
  982. }
  983. }
  984. // sets r = a*A + b*B
  985. // where Bi is the [8]CachedGroupElement consisting of
  986. // B,3B,5B,7B,9B,11B,13B,15B
  987. func GeDoubleScalarMultPrecompVartime(r *ProjectiveGroupElement, a *Key, A *ExtendedGroupElement, b *Key, Bi *[8]CachedGroupElement) {
  988. var aSlide, bSlide [256]int8
  989. var Ai [8]CachedGroupElement // A,3A,5A,7A,9A,11A,13A,15A
  990. var t CompletedGroupElement
  991. var u ExtendedGroupElement
  992. var i int
  993. slide(&aSlide, a)
  994. slide(&bSlide, b)
  995. GePrecompute(&Ai, A)
  996. r.Zero()
  997. for i = 255; i >= 0; i-- {
  998. if aSlide[i] != 0 || bSlide[i] != 0 {
  999. break
  1000. }
  1001. }
  1002. for ; i >= 0; i-- {
  1003. r.Double(&t)
  1004. if aSlide[i] > 0 {
  1005. t.ToExtended(&u)
  1006. geAdd(&t, &u, &Ai[aSlide[i]/2])
  1007. } else if aSlide[i] < 0 {
  1008. t.ToExtended(&u)
  1009. geSub(&t, &u, &Ai[(-aSlide[i])/2])
  1010. }
  1011. if bSlide[i] > 0 {
  1012. t.ToExtended(&u)
  1013. geAdd(&t, &u, &Bi[bSlide[i]/2])
  1014. } else if bSlide[i] < 0 {
  1015. t.ToExtended(&u)
  1016. geSub(&t, &u, &Bi[(-bSlide[i])/2])
  1017. }
  1018. t.ToProjective(r)
  1019. }
  1020. return
  1021. }
  1022. // equal returns 1 if b == c and 0 otherwise.
  1023. func equal(b, c int32) int32 {
  1024. x := uint32(b ^ c)
  1025. x--
  1026. return int32(x >> 31)
  1027. }
  1028. // negative returns 1 if b < 0 and 0 otherwise.
  1029. func negative(b int32) int32 {
  1030. return (b >> 31) & 1
  1031. }
  1032. func CachedGroupElementCMove(t, u *CachedGroupElement, b int32) {
  1033. FeCMove(&t.yPlusX, &u.yPlusX, b)
  1034. FeCMove(&t.yMinusX, &u.yMinusX, b)
  1035. FeCMove(&t.Z, &u.Z, b)
  1036. FeCMove(&t.T2d, &u.T2d, b)
  1037. }
  1038. func PreComputedGroupElementCMove(t, u *PreComputedGroupElement, b int32) {
  1039. FeCMove(&t.yPlusX, &u.yPlusX, b)
  1040. FeCMove(&t.yMinusX, &u.yMinusX, b)
  1041. FeCMove(&t.xy2d, &u.xy2d, b)
  1042. }
  1043. func selectPoint(t *PreComputedGroupElement, pos int32, b int32) {
  1044. var minusT PreComputedGroupElement
  1045. bNegative := negative(b)
  1046. bAbs := b - (((-bNegative) & b) << 1)
  1047. t.Zero()
  1048. for i := int32(0); i < 8; i++ {
  1049. PreComputedGroupElementCMove(t, &base[pos][i], equal(bAbs, i+1))
  1050. }
  1051. FeCopy(&minusT.yPlusX, &t.yMinusX)
  1052. FeCopy(&minusT.yMinusX, &t.yPlusX)
  1053. FeNeg(&minusT.xy2d, &t.xy2d)
  1054. PreComputedGroupElementCMove(t, &minusT, bNegative)
  1055. }
  1056. // GeScalarMult computes h = a*A, where
  1057. // a = a[0]+256*a[1]+...+256^31 a[31]
  1058. // A is a point on the curve
  1059. //
  1060. // Preconditions:
  1061. // a[31] <= 127
  1062. func GeScalarMult(r *ProjectiveGroupElement, a *Key, A *ExtendedGroupElement) {
  1063. var e [64]int32
  1064. var carry, carry2 int32
  1065. for i := 0; i < 31; i++ {
  1066. carry += int32(a[i]) /* 0..256 */
  1067. carry2 = (carry + 8) >> 4 /* 0..16 */
  1068. e[2*i] = carry - (carry2 << 4) /* -8..7 */
  1069. carry = (carry2 + 8) >> 4 /* 0..1 */
  1070. e[2*i+1] = carry2 - (carry << 4) /* -8..7 */
  1071. }
  1072. carry += int32(a[31]) /* 0..128 */
  1073. carry2 = (carry + 8) >> 4 /* 0..8 */
  1074. e[62] = carry - (carry2 << 4) /* -8..7 */
  1075. e[63] = carry2 /* 0..8 */
  1076. var Ai [8]CachedGroupElement // A,2A,3A,4A,5A,6A,7A,8A
  1077. t := new(CompletedGroupElement)
  1078. u := new(ExtendedGroupElement)
  1079. A.ToCached(&Ai[0])
  1080. for i := 0; i < 7; i++ {
  1081. geAdd(t, A, &Ai[i])
  1082. t.ToExtended(u)
  1083. u.ToCached(&Ai[i+1])
  1084. }
  1085. r.Zero()
  1086. cur := new(CachedGroupElement)
  1087. minusCur := new(CachedGroupElement)
  1088. for i := 63; i >= 0; i-- {
  1089. b := e[i]
  1090. bNegative := negative(b)
  1091. bAbs := b - (((-bNegative) & b) << 1)
  1092. r.Double(t)
  1093. t.ToProjective(r)
  1094. r.Double(t)
  1095. t.ToProjective(r)
  1096. r.Double(t)
  1097. t.ToProjective(r)
  1098. r.Double(t)
  1099. t.ToExtended(u)
  1100. cur.Zero()
  1101. for j := int32(0); j < 8; j++ {
  1102. CachedGroupElementCMove(cur, &Ai[j], equal(bAbs, j+1))
  1103. }
  1104. FeCopy(&minusCur.yPlusX, &cur.yMinusX)
  1105. FeCopy(&minusCur.yMinusX, &cur.yPlusX)
  1106. FeCopy(&minusCur.Z, &cur.Z)
  1107. FeNeg(&minusCur.T2d, &cur.T2d)
  1108. CachedGroupElementCMove(cur, minusCur, bNegative)
  1109. geAdd(t, u, cur)
  1110. t.ToProjective(r)
  1111. }
  1112. }
  1113. // GeScalarMultBase computes h = a*B, where
  1114. // a = a[0]+256*a[1]+...+256^31 a[31]
  1115. // B is the Ed25519 base point (x,4/5) with x positive.
  1116. //
  1117. // Preconditions:
  1118. // a[31] <= 127
  1119. func GeScalarMultBase(h *ExtendedGroupElement, a *Key) {
  1120. var e [64]int8
  1121. for i, v := range a {
  1122. e[2*i] = int8(v & 15)
  1123. e[2*i+1] = int8((v >> 4) & 15)
  1124. }
  1125. // each e[i] is between 0 and 15 and e[63] is between 0 and 7.
  1126. carry := int8(0)
  1127. for i := 0; i < 63; i++ {
  1128. e[i] += carry
  1129. carry = (e[i] + 8) >> 4
  1130. e[i] -= carry << 4
  1131. }
  1132. e[63] += carry
  1133. // each e[i] is between -8 and 8.
  1134. h.Zero()
  1135. var t PreComputedGroupElement
  1136. var r CompletedGroupElement
  1137. for i := int32(1); i < 64; i += 2 {
  1138. selectPoint(&t, i/2, int32(e[i]))
  1139. geMixedAdd(&r, h, &t)
  1140. r.ToExtended(h)
  1141. }
  1142. var s ProjectiveGroupElement
  1143. h.Double(&r)
  1144. r.ToProjective(&s)
  1145. s.Double(&r)
  1146. r.ToProjective(&s)
  1147. s.Double(&r)
  1148. r.ToProjective(&s)
  1149. s.Double(&r)
  1150. r.ToExtended(h)
  1151. for i := int32(0); i < 64; i += 2 {
  1152. selectPoint(&t, i/2, int32(e[i]))
  1153. geMixedAdd(&r, h, &t)
  1154. r.ToExtended(h)
  1155. }
  1156. }
  1157. func ScAdd(s, a, b *Key) {
  1158. a0 := 2097151 & load3(a[:])
  1159. a1 := 2097151 & (load4(a[2:]) >> 5)
  1160. a2 := 2097151 & (load3(a[5:]) >> 2)
  1161. a3 := 2097151 & (load4(a[7:]) >> 7)
  1162. a4 := 2097151 & (load4(a[10:]) >> 4)
  1163. a5 := 2097151 & (load3(a[13:]) >> 1)
  1164. a6 := 2097151 & (load4(a[15:]) >> 6)
  1165. a7 := 2097151 & (load3(a[18:]) >> 3)
  1166. a8 := 2097151 & load3(a[21:])
  1167. a9 := 2097151 & (load4(a[23:]) >> 5)
  1168. a10 := 2097151 & (load3(a[26:]) >> 2)
  1169. a11 := (load4(a[28:]) >> 7)
  1170. b0 := 2097151 & load3(b[:])
  1171. b1 := 2097151 & (load4(b[2:]) >> 5)
  1172. b2 := 2097151 & (load3(b[5:]) >> 2)
  1173. b3 := 2097151 & (load4(b[7:]) >> 7)
  1174. b4 := 2097151 & (load4(b[10:]) >> 4)
  1175. b5 := 2097151 & (load3(b[13:]) >> 1)
  1176. b6 := 2097151 & (load4(b[15:]) >> 6)
  1177. b7 := 2097151 & (load3(b[18:]) >> 3)
  1178. b8 := 2097151 & load3(b[21:])
  1179. b9 := 2097151 & (load4(b[23:]) >> 5)
  1180. b10 := 2097151 & (load3(b[26:]) >> 2)
  1181. b11 := (load4(b[28:]) >> 7)
  1182. s0 := a0 + b0
  1183. s1 := a1 + b1
  1184. s2 := a2 + b2
  1185. s3 := a3 + b3
  1186. s4 := a4 + b4
  1187. s5 := a5 + b5
  1188. s6 := a6 + b6
  1189. s7 := a7 + b7
  1190. s8 := a8 + b8
  1191. s9 := a9 + b9
  1192. s10 := a10 + b10
  1193. s11 := a11 + b11
  1194. s12 := int64(0)
  1195. var carry [12]int64
  1196. carry[0] = (s0 + (1 << 20)) >> 21
  1197. s1 += carry[0]
  1198. s0 -= carry[0] << 21
  1199. carry[2] = (s2 + (1 << 20)) >> 21
  1200. s3 += carry[2]
  1201. s2 -= carry[2] << 21
  1202. carry[4] = (s4 + (1 << 20)) >> 21
  1203. s5 += carry[4]
  1204. s4 -= carry[4] << 21
  1205. carry[6] = (s6 + (1 << 20)) >> 21
  1206. s7 += carry[6]
  1207. s6 -= carry[6] << 21
  1208. carry[8] = (s8 + (1 << 20)) >> 21
  1209. s9 += carry[8]
  1210. s8 -= carry[8] << 21
  1211. carry[10] = (s10 + (1 << 20)) >> 21
  1212. s11 += carry[10]
  1213. s10 -= carry[10] << 21
  1214. carry[1] = (s1 + (1 << 20)) >> 21
  1215. s2 += carry[1]
  1216. s1 -= carry[1] << 21
  1217. carry[3] = (s3 + (1 << 20)) >> 21
  1218. s4 += carry[3]
  1219. s3 -= carry[3] << 21
  1220. carry[5] = (s5 + (1 << 20)) >> 21
  1221. s6 += carry[5]
  1222. s5 -= carry[5] << 21
  1223. carry[7] = (s7 + (1 << 20)) >> 21
  1224. s8 += carry[7]
  1225. s7 -= carry[7] << 21
  1226. carry[9] = (s9 + (1 << 20)) >> 21
  1227. s10 += carry[9]
  1228. s9 -= carry[9] << 21
  1229. carry[11] = (s11 + (1 << 20)) >> 21
  1230. s12 += carry[11]
  1231. s11 -= carry[11] << 21
  1232. s0 += s12 * 666643
  1233. s1 += s12 * 470296
  1234. s2 += s12 * 654183
  1235. s3 -= s12 * 997805
  1236. s4 += s12 * 136657
  1237. s5 -= s12 * 683901
  1238. s12 = 0
  1239. carry[0] = s0 >> 21
  1240. s1 += carry[0]
  1241. s0 -= carry[0] << 21
  1242. carry[1] = s1 >> 21
  1243. s2 += carry[1]
  1244. s1 -= carry[1] << 21
  1245. carry[2] = s2 >> 21
  1246. s3 += carry[2]
  1247. s2 -= carry[2] << 21
  1248. carry[3] = s3 >> 21
  1249. s4 += carry[3]
  1250. s3 -= carry[3] << 21
  1251. carry[4] = s4 >> 21
  1252. s5 += carry[4]
  1253. s4 -= carry[4] << 21
  1254. carry[5] = s5 >> 21
  1255. s6 += carry[5]
  1256. s5 -= carry[5] << 21
  1257. carry[6] = s6 >> 21
  1258. s7 += carry[6]
  1259. s6 -= carry[6] << 21
  1260. carry[7] = s7 >> 21
  1261. s8 += carry[7]
  1262. s7 -= carry[7] << 21
  1263. carry[8] = s8 >> 21
  1264. s9 += carry[8]
  1265. s8 -= carry[8] << 21
  1266. carry[9] = s9 >> 21
  1267. s10 += carry[9]
  1268. s9 -= carry[9] << 21
  1269. carry[10] = s10 >> 21
  1270. s11 += carry[10]
  1271. s10 -= carry[10] << 21
  1272. carry[11] = s11 >> 21
  1273. s12 += carry[11]
  1274. s11 -= carry[11] << 21
  1275. s0 += s12 * 666643
  1276. s1 += s12 * 470296
  1277. s2 += s12 * 654183
  1278. s3 -= s12 * 997805
  1279. s4 += s12 * 136657
  1280. s5 -= s12 * 683901
  1281. carry[0] = s0 >> 21
  1282. s1 += carry[0]
  1283. s0 -= carry[0] << 21
  1284. carry[1] = s1 >> 21
  1285. s2 += carry[1]
  1286. s1 -= carry[1] << 21
  1287. carry[2] = s2 >> 21
  1288. s3 += carry[2]
  1289. s2 -= carry[2] << 21
  1290. carry[3] = s3 >> 21
  1291. s4 += carry[3]
  1292. s3 -= carry[3] << 21
  1293. carry[4] = s4 >> 21
  1294. s5 += carry[4]
  1295. s4 -= carry[4] << 21
  1296. carry[5] = s5 >> 21
  1297. s6 += carry[5]
  1298. s5 -= carry[5] << 21
  1299. carry[6] = s6 >> 21
  1300. s7 += carry[6]
  1301. s6 -= carry[6] << 21
  1302. carry[7] = s7 >> 21
  1303. s8 += carry[7]
  1304. s7 -= carry[7] << 21
  1305. carry[8] = s8 >> 21
  1306. s9 += carry[8]
  1307. s8 -= carry[8] << 21
  1308. carry[9] = s9 >> 21
  1309. s10 += carry[9]
  1310. s9 -= carry[9] << 21
  1311. carry[10] = s10 >> 21
  1312. s11 += carry[10]
  1313. s10 -= carry[10] << 21
  1314. s[0] = byte(s0 >> 0)
  1315. s[1] = byte(s0 >> 8)
  1316. s[2] = byte((s0 >> 16) | (s1 << 5))
  1317. s[3] = byte(s1 >> 3)
  1318. s[4] = byte(s1 >> 11)
  1319. s[5] = byte((s1 >> 19) | (s2 << 2))
  1320. s[6] = byte(s2 >> 6)
  1321. s[7] = byte((s2 >> 14) | (s3 << 7))
  1322. s[8] = byte(s3 >> 1)
  1323. s[9] = byte(s3 >> 9)
  1324. s[10] = byte((s3 >> 17) | (s4 << 4))
  1325. s[11] = byte(s4 >> 4)
  1326. s[12] = byte(s4 >> 12)
  1327. s[13] = byte((s4 >> 20) | (s5 << 1))
  1328. s[14] = byte(s5 >> 7)
  1329. s[15] = byte((s5 >> 15) | (s6 << 6))
  1330. s[16] = byte(s6 >> 2)
  1331. s[17] = byte(s6 >> 10)
  1332. s[18] = byte((s6 >> 18) | (s7 << 3))
  1333. s[19] = byte(s7 >> 5)
  1334. s[20] = byte(s7 >> 13)
  1335. s[21] = byte(s8 >> 0)
  1336. s[22] = byte(s8 >> 8)
  1337. s[23] = byte((s8 >> 16) | (s9 << 5))
  1338. s[24] = byte(s9 >> 3)
  1339. s[25] = byte(s9 >> 11)
  1340. s[26] = byte((s9 >> 19) | (s10 << 2))
  1341. s[27] = byte(s10 >> 6)
  1342. s[28] = byte((s10 >> 14) | (s11 << 7))
  1343. s[29] = byte(s11 >> 1)
  1344. s[30] = byte(s11 >> 9)
  1345. s[31] = byte(s11 >> 17)
  1346. }
  1347. func ScSub(s, a, b *Key) {
  1348. a0 := 2097151 & load3(a[:])
  1349. a1 := 2097151 & (load4(a[2:]) >> 5)
  1350. a2 := 2097151 & (load3(a[5:]) >> 2)
  1351. a3 := 2097151 & (load4(a[7:]) >> 7)
  1352. a4 := 2097151 & (load4(a[10:]) >> 4)
  1353. a5 := 2097151 & (load3(a[13:]) >> 1)
  1354. a6 := 2097151 & (load4(a[15:]) >> 6)
  1355. a7 := 2097151 & (load3(a[18:]) >> 3)
  1356. a8 := 2097151 & load3(a[21:])
  1357. a9 := 2097151 & (load4(a[23:]) >> 5)
  1358. a10 := 2097151 & (load3(a[26:]) >> 2)
  1359. a11 := (load4(a[28:]) >> 7)
  1360. b0 := 2097151 & load3(b[:])
  1361. b1 := 2097151 & (load4(b[2:]) >> 5)
  1362. b2 := 2097151 & (load3(b[5:]) >> 2)
  1363. b3 := 2097151 & (load4(b[7:]) >> 7)
  1364. b4 := 2097151 & (load4(b[10:]) >> 4)
  1365. b5 := 2097151 & (load3(b[13:]) >> 1)
  1366. b6 := 2097151 & (load4(b[15:]) >> 6)
  1367. b7 := 2097151 & (load3(b[18:]) >> 3)
  1368. b8 := 2097151 & load3(b[21:])
  1369. b9 := 2097151 & (load4(b[23:]) >> 5)
  1370. b10 := 2097151 & (load3(b[26:]) >> 2)
  1371. b11 := (load4(b[28:]) >> 7)
  1372. s0 := a0 - b0
  1373. s1 := a1 - b1
  1374. s2 := a2 - b2
  1375. s3 := a3 - b3
  1376. s4 := a4 - b4
  1377. s5 := a5 - b5
  1378. s6 := a6 - b6
  1379. s7 := a7 - b7
  1380. s8 := a8 - b8
  1381. s9 := a9 - b9
  1382. s10 := a10 - b10
  1383. s11 := a11 - b11
  1384. s12 := int64(0)
  1385. var carry [12]int64
  1386. carry[0] = (s0 + (1 << 20)) >> 21
  1387. s1 += carry[0]
  1388. s0 -= carry[0] << 21
  1389. carry[2] = (s2 + (1 << 20)) >> 21
  1390. s3 += carry[2]
  1391. s2 -= carry[2] << 21
  1392. carry[4] = (s4 + (1 << 20)) >> 21
  1393. s5 += carry[4]
  1394. s4 -= carry[4] << 21
  1395. carry[6] = (s6 + (1 << 20)) >> 21
  1396. s7 += carry[6]
  1397. s6 -= carry[6] << 21
  1398. carry[8] = (s8 + (1 << 20)) >> 21
  1399. s9 += carry[8]
  1400. s8 -= carry[8] << 21
  1401. carry[10] = (s10 + (1 << 20)) >> 21
  1402. s11 += carry[10]
  1403. s10 -= carry[10] << 21
  1404. carry[1] = (s1 + (1 << 20)) >> 21
  1405. s2 += carry[1]
  1406. s1 -= carry[1] << 21
  1407. carry[3] = (s3 + (1 << 20)) >> 21
  1408. s4 += carry[3]
  1409. s3 -= carry[3] << 21
  1410. carry[5] = (s5 + (1 << 20)) >> 21
  1411. s6 += carry[5]
  1412. s5 -= carry[5] << 21
  1413. carry[7] = (s7 + (1 << 20)) >> 21
  1414. s8 += carry[7]
  1415. s7 -= carry[7] << 21
  1416. carry[9] = (s9 + (1 << 20)) >> 21
  1417. s10 += carry[9]
  1418. s9 -= carry[9] << 21
  1419. carry[11] = (s11 + (1 << 20)) >> 21
  1420. s12 += carry[11]
  1421. s11 -= carry[11] << 21
  1422. s0 += s12 * 666643
  1423. s1 += s12 * 470296
  1424. s2 += s12 * 654183
  1425. s3 -= s12 * 997805
  1426. s4 += s12 * 136657
  1427. s5 -= s12 * 683901
  1428. s12 = 0
  1429. carry[0] = s0 >> 21
  1430. s1 += carry[0]
  1431. s0 -= carry[0] << 21
  1432. carry[1] = s1 >> 21
  1433. s2 += carry[1]
  1434. s1 -= carry[1] << 21
  1435. carry[2] = s2 >> 21
  1436. s3 += carry[2]
  1437. s2 -= carry[2] << 21
  1438. carry[3] = s3 >> 21
  1439. s4 += carry[3]
  1440. s3 -= carry[3] << 21
  1441. carry[4] = s4 >> 21
  1442. s5 += carry[4]
  1443. s4 -= carry[4] << 21
  1444. carry[5] = s5 >> 21
  1445. s6 += carry[5]
  1446. s5 -= carry[5] << 21
  1447. carry[6] = s6 >> 21
  1448. s7 += carry[6]
  1449. s6 -= carry[6] << 21
  1450. carry[7] = s7 >> 21
  1451. s8 += carry[7]
  1452. s7 -= carry[7] << 21
  1453. carry[8] = s8 >> 21
  1454. s9 += carry[8]
  1455. s8 -= carry[8] << 21
  1456. carry[9] = s9 >> 21
  1457. s10 += carry[9]
  1458. s9 -= carry[9] << 21
  1459. carry[10] = s10 >> 21
  1460. s11 += carry[10]
  1461. s10 -= carry[10] << 21
  1462. carry[11] = s11 >> 21
  1463. s12 += carry[11]
  1464. s11 -= carry[11] << 21
  1465. s0 += s12 * 666643
  1466. s1 += s12 * 470296
  1467. s2 += s12 * 654183
  1468. s3 -= s12 * 997805
  1469. s4 += s12 * 136657
  1470. s5 -= s12 * 683901
  1471. carry[0] = s0 >> 21
  1472. s1 += carry[0]
  1473. s0 -= carry[0] << 21
  1474. carry[1] = s1 >> 21
  1475. s2 += carry[1]
  1476. s1 -= carry[1] << 21
  1477. carry[2] = s2 >> 21
  1478. s3 += carry[2]
  1479. s2 -= carry[2] << 21
  1480. carry[3] = s3 >> 21
  1481. s4 += carry[3]
  1482. s3 -= carry[3] << 21
  1483. carry[4] = s4 >> 21
  1484. s5 += carry[4]
  1485. s4 -= carry[4] << 21
  1486. carry[5] = s5 >> 21
  1487. s6 += carry[5]
  1488. s5 -= carry[5] << 21
  1489. carry[6] = s6 >> 21
  1490. s7 += carry[6]
  1491. s6 -= carry[6] << 21
  1492. carry[7] = s7 >> 21
  1493. s8 += carry[7]
  1494. s7 -= carry[7] << 21
  1495. carry[8] = s8 >> 21
  1496. s9 += carry[8]
  1497. s8 -= carry[8] << 21
  1498. carry[9] = s9 >> 21
  1499. s10 += carry[9]
  1500. s9 -= carry[9] << 21
  1501. carry[10] = s10 >> 21
  1502. s11 += carry[10]
  1503. s10 -= carry[10] << 21
  1504. s[0] = byte(s0 >> 0)
  1505. s[1] = byte(s0 >> 8)
  1506. s[2] = byte((s0 >> 16) | (s1 << 5))
  1507. s[3] = byte(s1 >> 3)
  1508. s[4] = byte(s1 >> 11)
  1509. s[5] = byte((s1 >> 19) | (s2 << 2))
  1510. s[6] = byte(s2 >> 6)
  1511. s[7] = byte((s2 >> 14) | (s3 << 7))
  1512. s[8] = byte(s3 >> 1)
  1513. s[9] = byte(s3 >> 9)
  1514. s[10] = byte((s3 >> 17) | (s4 << 4))
  1515. s[11] = byte(s4 >> 4)
  1516. s[12] = byte(s4 >> 12)
  1517. s[13] = byte((s4 >> 20) | (s5 << 1))
  1518. s[14] = byte(s5 >> 7)
  1519. s[15] = byte((s5 >> 15) | (s6 << 6))
  1520. s[16] = byte(s6 >> 2)
  1521. s[17] = byte(s6 >> 10)
  1522. s[18] = byte((s6 >> 18) | (s7 << 3))
  1523. s[19] = byte(s7 >> 5)
  1524. s[20] = byte(s7 >> 13)
  1525. s[21] = byte(s8 >> 0)
  1526. s[22] = byte(s8 >> 8)
  1527. s[23] = byte((s8 >> 16) | (s9 << 5))
  1528. s[24] = byte(s9 >> 3)
  1529. s[25] = byte(s9 >> 11)
  1530. s[26] = byte((s9 >> 19) | (s10 << 2))
  1531. s[27] = byte(s10 >> 6)
  1532. s[28] = byte((s10 >> 14) | (s11 << 7))
  1533. s[29] = byte(s11 >> 1)
  1534. s[30] = byte(s11 >> 9)
  1535. s[31] = byte(s11 >> 17)
  1536. }
  1537. func signum(a int64) int64 {
  1538. return a>>63 - ((-a) >> 63)
  1539. }
  1540. func ScValid(s *Key) bool {
  1541. s0 := load4(s[:])
  1542. s1 := load4(s[4:])
  1543. s2 := load4(s[8:])
  1544. s3 := load4(s[12:])
  1545. s4 := load4(s[16:])
  1546. s5 := load4(s[20:])
  1547. s6 := load4(s[24:])
  1548. s7 := load4(s[28:])
  1549. return (signum(1559614444-s0)+(signum(1477600026-s1)<<1)+(signum(2734136534-s2)<<2)+(signum(350157278-s3)<<3)+(signum(-s4)<<4)+(signum(-s5)<<5)+(signum(-s6)<<6)+(signum(268435456-s7)<<7))>>8 == 0
  1550. }
  1551. func ScIsZero(s *Key) bool {
  1552. return ((int(s[0]|s[1]|s[2]|s[3]|s[4]|s[5]|s[6]|s[7]|s[8]|
  1553. s[9]|s[10]|s[11]|s[12]|s[13]|s[14]|s[15]|s[16]|s[17]|
  1554. s[18]|s[19]|s[20]|s[21]|s[22]|s[23]|s[24]|s[25]|s[26]|
  1555. s[27]|s[28]|s[29]|s[30]|s[31])-1)>>8)+1 == 0
  1556. }
  1557. // The scalars are GF(2^252 + 27742317777372353535851937790883648493).
  1558. // Input:
  1559. // a[0]+256*a[1]+...+256^31*a[31] = a
  1560. // b[0]+256*b[1]+...+256^31*b[31] = b
  1561. // c[0]+256*c[1]+...+256^31*c[31] = c
  1562. //
  1563. // Output:
  1564. // s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l
  1565. // where l = 2^252 + 27742317777372353535851937790883648493.
  1566. func ScMulAdd(s, a, b, c *Key) {
  1567. a0 := 2097151 & load3(a[:])
  1568. a1 := 2097151 & (load4(a[2:]) >> 5)
  1569. a2 := 2097151 & (load3(a[5:]) >> 2)
  1570. a3 := 2097151 & (load4(a[7:]) >> 7)
  1571. a4 := 2097151 & (load4(a[10:]) >> 4)
  1572. a5 := 2097151 & (load3(a[13:]) >> 1)
  1573. a6 := 2097151 & (load4(a[15:]) >> 6)
  1574. a7 := 2097151 & (load3(a[18:]) >> 3)
  1575. a8 := 2097151 & load3(a[21:])
  1576. a9 := 2097151 & (load4(a[23:]) >> 5)
  1577. a10 := 2097151 & (load3(a[26:]) >> 2)
  1578. a11 := (load4(a[28:]) >> 7)
  1579. b0 := 2097151 & load3(b[:])
  1580. b1 := 2097151 & (load4(b[2:]) >> 5)
  1581. b2 := 2097151 & (load3(b[5:]) >> 2)
  1582. b3 := 2097151 & (load4(b[7:]) >> 7)
  1583. b4 := 2097151 & (load4(b[10:]) >> 4)
  1584. b5 := 2097151 & (load3(b[13:]) >> 1)
  1585. b6 := 2097151 & (load4(b[15:]) >> 6)
  1586. b7 := 2097151 & (load3(b[18:]) >> 3)
  1587. b8 := 2097151 & load3(b[21:])
  1588. b9 := 2097151 & (load4(b[23:]) >> 5)
  1589. b10 := 2097151 & (load3(b[26:]) >> 2)
  1590. b11 := (load4(b[28:]) >> 7)
  1591. c0 := 2097151 & load3(c[:])
  1592. c1 := 2097151 & (load4(c[2:]) >> 5)
  1593. c2 := 2097151 & (load3(c[5:]) >> 2)
  1594. c3 := 2097151 & (load4(c[7:]) >> 7)
  1595. c4 := 2097151 & (load4(c[10:]) >> 4)
  1596. c5 := 2097151 & (load3(c[13:]) >> 1)
  1597. c6 := 2097151 & (load4(c[15:]) >> 6)
  1598. c7 := 2097151 & (load3(c[18:]) >> 3)
  1599. c8 := 2097151 & load3(c[21:])
  1600. c9 := 2097151 & (load4(c[23:]) >> 5)
  1601. c10 := 2097151 & (load3(c[26:]) >> 2)
  1602. c11 := (load4(c[28:]) >> 7)
  1603. var carry [23]int64
  1604. s0 := c0 + a0*b0
  1605. s1 := c1 + a0*b1 + a1*b0
  1606. s2 := c2 + a0*b2 + a1*b1 + a2*b0
  1607. s3 := c3 + a0*b3 + a1*b2 + a2*b1 + a3*b0
  1608. s4 := c4 + a0*b4 + a1*b3 + a2*b2 + a3*b1 + a4*b0
  1609. s5 := c5 + a0*b5 + a1*b4 + a2*b3 + a3*b2 + a4*b1 + a5*b0
  1610. s6 := c6 + a0*b6 + a1*b5 + a2*b4 + a3*b3 + a4*b2 + a5*b1 + a6*b0
  1611. s7 := c7 + a0*b7 + a1*b6 + a2*b5 + a3*b4 + a4*b3 + a5*b2 + a6*b1 + a7*b0
  1612. s8 := c8 + a0*b8 + a1*b7 + a2*b6 + a3*b5 + a4*b4 + a5*b3 + a6*b2 + a7*b1 + a8*b0
  1613. s9 := c9 + a0*b9 + a1*b8 + a2*b7 + a3*b6 + a4*b5 + a5*b4 + a6*b3 + a7*b2 + a8*b1 + a9*b0
  1614. s10 := c10 + a0*b10 + a1*b9 + a2*b8 + a3*b7 + a4*b6 + a5*b5 + a6*b4 + a7*b3 + a8*b2 + a9*b1 + a10*b0
  1615. 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
  1616. s12 := a1*b11 + a2*b10 + a3*b9 + a4*b8 + a5*b7 + a6*b6 + a7*b5 + a8*b4 + a9*b3 + a10*b2 + a11*b1
  1617. s13 := a2*b11 + a3*b10 + a4*b9 + a5*b8 + a6*b7 + a7*b6 + a8*b5 + a9*b4 + a10*b3 + a11*b2
  1618. s14 := a3*b11 + a4*b10 + a5*b9 + a6*b8 + a7*b7 + a8*b6 + a9*b5 + a10*b4 + a11*b3
  1619. s15 := a4*b11 + a5*b10 + a6*b9 + a7*b8 + a8*b7 + a9*b6 + a10*b5 + a11*b4
  1620. s16 := a5*b11 + a6*b10 + a7*b9 + a8*b8 + a9*b7 + a10*b6 + a11*b5
  1621. s17 := a6*b11 + a7*b10 + a8*b9 + a9*b8 + a10*b7 + a11*b6
  1622. s18 := a7*b11 + a8*b10 + a9*b9 + a10*b8 + a11*b7
  1623. s19 := a8*b11 + a9*b10 + a10*b9 + a11*b8
  1624. s20 := a9*b11 + a10*b10 + a11*b9
  1625. s21 := a10*b11 + a11*b10
  1626. s22 := a11 * b11
  1627. s23 := int64(0)
  1628. carry[0] = (s0 + (1 << 20)) >> 21
  1629. s1 += carry[0]
  1630. s0 -= carry[0] << 21
  1631. carry[2] = (s2 + (1 << 20)) >> 21
  1632. s3 += carry[2]
  1633. s2 -= carry[2] << 21
  1634. carry[4] = (s4 + (1 << 20)) >> 21
  1635. s5 += carry[4]
  1636. s4 -= carry[4] << 21
  1637. carry[6] = (s6 + (1 << 20)) >> 21
  1638. s7 += carry[6]
  1639. s6 -= carry[6] << 21
  1640. carry[8] = (s8 + (1 << 20)) >> 21
  1641. s9 += carry[8]
  1642. s8 -= carry[8] << 21
  1643. carry[10] = (s10 + (1 << 20)) >> 21
  1644. s11 += carry[10]
  1645. s10 -= carry[10] << 21
  1646. carry[12] = (s12 + (1 << 20)) >> 21
  1647. s13 += carry[12]
  1648. s12 -= carry[12] << 21
  1649. carry[14] = (s14 + (1 << 20)) >> 21
  1650. s15 += carry[14]
  1651. s14 -= carry[14] << 21
  1652. carry[16] = (s16 + (1 << 20)) >> 21
  1653. s17 += carry[16]
  1654. s16 -= carry[16] << 21
  1655. carry[18] = (s18 + (1 << 20)) >> 21
  1656. s19 += carry[18]
  1657. s18 -= carry[18] << 21
  1658. carry[20] = (s20 + (1 << 20)) >> 21
  1659. s21 += carry[20]
  1660. s20 -= carry[20] << 21
  1661. carry[22] = (s22 + (1 << 20)) >> 21
  1662. s23 += carry[22]
  1663. s22 -= carry[22] << 21
  1664. carry[1] = (s1 + (1 << 20)) >> 21
  1665. s2 += carry[1]
  1666. s1 -= carry[1] << 21
  1667. carry[3] = (s3 + (1 << 20)) >> 21
  1668. s4 += carry[3]
  1669. s3 -= carry[3] << 21
  1670. carry[5] = (s5 + (1 << 20)) >> 21
  1671. s6 += carry[5]
  1672. s5 -= carry[5] << 21
  1673. carry[7] = (s7 + (1 << 20)) >> 21
  1674. s8 += carry[7]
  1675. s7 -= carry[7] << 21
  1676. carry[9] = (s9 + (1 << 20)) >> 21
  1677. s10 += carry[9]
  1678. s9 -= carry[9] << 21
  1679. carry[11] = (s11 + (1 << 20)) >> 21
  1680. s12 += carry[11]
  1681. s11 -= carry[11] << 21
  1682. carry[13] = (s13 + (1 << 20)) >> 21
  1683. s14 += carry[13]
  1684. s13 -= carry[13] << 21
  1685. carry[15] = (s15 + (1 << 20)) >> 21
  1686. s16 += carry[15]
  1687. s15 -= carry[15] << 21
  1688. carry[17] = (s17 + (1 << 20)) >> 21
  1689. s18 += carry[17]
  1690. s17 -= carry[17] << 21
  1691. carry[19] = (s19 + (1 << 20)) >> 21
  1692. s20 += carry[19]
  1693. s19 -= carry[19] << 21
  1694. carry[21] = (s21 + (1 << 20)) >> 21
  1695. s22 += carry[21]
  1696. s21 -= carry[21] << 21
  1697. s11 += s23 * 666643
  1698. s12 += s23 * 470296
  1699. s13 += s23 * 654183
  1700. s14 -= s23 * 997805
  1701. s15 += s23 * 136657
  1702. s16 -= s23 * 683901
  1703. s23 = 0
  1704. s10 += s22 * 666643
  1705. s11 += s22 * 470296
  1706. s12 += s22 * 654183
  1707. s13 -= s22 * 997805
  1708. s14 += s22 * 136657
  1709. s15 -= s22 * 683901
  1710. s22 = 0
  1711. s9 += s21 * 666643
  1712. s10 += s21 * 470296
  1713. s11 += s21 * 654183
  1714. s12 -= s21 * 997805
  1715. s13 += s21 * 136657
  1716. s14 -= s21 * 683901
  1717. s21 = 0
  1718. s8 += s20 * 666643
  1719. s9 += s20 * 470296
  1720. s10 += s20 * 654183
  1721. s11 -= s20 * 997805
  1722. s12 += s20 * 136657
  1723. s13 -= s20 * 683901
  1724. s20 = 0
  1725. s7 += s19 * 666643
  1726. s8 += s19 * 470296
  1727. s9 += s19 * 654183
  1728. s10 -= s19 * 997805
  1729. s11 += s19 * 136657
  1730. s12 -= s19 * 683901
  1731. s19 = 0
  1732. s6 += s18 * 666643
  1733. s7 += s18 * 470296
  1734. s8 += s18 * 654183
  1735. s9 -= s18 * 997805
  1736. s10 += s18 * 136657
  1737. s11 -= s18 * 683901
  1738. s18 = 0
  1739. carry[6] = (s6 + (1 << 20)) >> 21
  1740. s7 += carry[6]
  1741. s6 -= carry[6] << 21
  1742. carry[8] = (s8 + (1 << 20)) >> 21
  1743. s9 += carry[8]
  1744. s8 -= carry[8] << 21
  1745. carry[10] = (s10 + (1 << 20)) >> 21
  1746. s11 += carry[10]
  1747. s10 -= carry[10] << 21
  1748. carry[12] = (s12 + (1 << 20)) >> 21
  1749. s13 += carry[12]
  1750. s12 -= carry[12] << 21
  1751. carry[14] = (s14 + (1 << 20)) >> 21
  1752. s15 += carry[14]
  1753. s14 -= carry[14] << 21
  1754. carry[16] = (s16 + (1 << 20)) >> 21
  1755. s17 += carry[16]
  1756. s16 -= carry[16] << 21
  1757. carry[7] = (s7 + (1 << 20)) >> 21
  1758. s8 += carry[7]
  1759. s7 -= carry[7] << 21
  1760. carry[9] = (s9 + (1 << 20)) >> 21
  1761. s10 += carry[9]
  1762. s9 -= carry[9] << 21
  1763. carry[11] = (s11 + (1 << 20)) >> 21
  1764. s12 += carry[11]
  1765. s11 -= carry[11] << 21
  1766. carry[13] = (s13 + (1 << 20)) >> 21
  1767. s14 += carry[13]
  1768. s13 -= carry[13] << 21
  1769. carry[15] = (s15 + (1 << 20)) >> 21
  1770. s16 += carry[15]
  1771. s15 -= carry[15] << 21
  1772. s5 += s17 * 666643
  1773. s6 += s17 * 470296
  1774. s7 += s17 * 654183
  1775. s8 -= s17 * 997805
  1776. s9 += s17 * 136657
  1777. s10 -= s17 * 683901
  1778. s17 = 0
  1779. s4 += s16 * 666643
  1780. s5 += s16 * 470296
  1781. s6 += s16 * 654183
  1782. s7 -= s16 * 997805
  1783. s8 += s16 * 136657
  1784. s9 -= s16 * 683901
  1785. s16 = 0
  1786. s3 += s15 * 666643
  1787. s4 += s15 * 470296
  1788. s5 += s15 * 654183
  1789. s6 -= s15 * 997805
  1790. s7 += s15 * 136657
  1791. s8 -= s15 * 683901
  1792. s15 = 0
  1793. s2 += s14 * 666643
  1794. s3 += s14 * 470296
  1795. s4 += s14 * 654183
  1796. s5 -= s14 * 997805
  1797. s6 += s14 * 136657
  1798. s7 -= s14 * 683901
  1799. s14 = 0
  1800. s1 += s13 * 666643
  1801. s2 += s13 * 470296
  1802. s3 += s13 * 654183
  1803. s4 -= s13 * 997805
  1804. s5 += s13 * 136657
  1805. s6 -= s13 * 683901
  1806. s13 = 0
  1807. s0 += s12 * 666643
  1808. s1 += s12 * 470296
  1809. s2 += s12 * 654183
  1810. s3 -= s12 * 997805
  1811. s4 += s12 * 136657
  1812. s5 -= s12 * 683901
  1813. s12 = 0
  1814. carry[0] = (s0 + (1 << 20)) >> 21
  1815. s1 += carry[0]
  1816. s0 -= carry[0] << 21
  1817. carry[2] = (s2 + (1 << 20)) >> 21
  1818. s3 += carry[2]
  1819. s2 -= carry[2] << 21
  1820. carry[4] = (s4 + (1 << 20)) >> 21
  1821. s5 += carry[4]
  1822. s4 -= carry[4] << 21
  1823. carry[6] = (s6 + (1 << 20)) >> 21
  1824. s7 += carry[6]
  1825. s6 -= carry[6] << 21
  1826. carry[8] = (s8 + (1 << 20)) >> 21
  1827. s9 += carry[8]
  1828. s8 -= carry[8] << 21
  1829. carry[10] = (s10 + (1 << 20)) >> 21
  1830. s11 += carry[10]
  1831. s10 -= carry[10] << 21
  1832. carry[1] = (s1 + (1 << 20)) >> 21
  1833. s2 += carry[1]
  1834. s1 -= carry[1] << 21
  1835. carry[3] = (s3 + (1 << 20)) >> 21
  1836. s4 += carry[3]
  1837. s3 -= carry[3] << 21
  1838. carry[5] = (s5 + (1 << 20)) >> 21
  1839. s6 += carry[5]
  1840. s5 -= carry[5] << 21
  1841. carry[7] = (s7 + (1 << 20)) >> 21
  1842. s8 += carry[7]
  1843. s7 -= carry[7] << 21
  1844. carry[9] = (s9 + (1 << 20)) >> 21
  1845. s10 += carry[9]
  1846. s9 -= carry[9] << 21
  1847. carry[11] = (s11 + (1 << 20)) >> 21
  1848. s12 += carry[11]
  1849. s11 -= carry[11] << 21
  1850. s0 += s12 * 666643
  1851. s1 += s12 * 470296
  1852. s2 += s12 * 654183
  1853. s3 -= s12 * 997805
  1854. s4 += s12 * 136657
  1855. s5 -= s12 * 683901
  1856. s12 = 0
  1857. carry[0] = s0 >> 21
  1858. s1 += carry[0]
  1859. s0 -= carry[0] << 21
  1860. carry[1] = s1 >> 21
  1861. s2 += carry[1]
  1862. s1 -= carry[1] << 21
  1863. carry[2] = s2 >> 21
  1864. s3 += carry[2]
  1865. s2 -= carry[2] << 21
  1866. carry[3] = s3 >> 21
  1867. s4 += carry[3]
  1868. s3 -= carry[3] << 21
  1869. carry[4] = s4 >> 21
  1870. s5 += carry[4]
  1871. s4 -= carry[4] << 21
  1872. carry[5] = s5 >> 21
  1873. s6 += carry[5]
  1874. s5 -= carry[5] << 21
  1875. carry[6] = s6 >> 21
  1876. s7 += carry[6]
  1877. s6 -= carry[6] << 21
  1878. carry[7] = s7 >> 21
  1879. s8 += carry[7]
  1880. s7 -= carry[7] << 21
  1881. carry[8] = s8 >> 21
  1882. s9 += carry[8]
  1883. s8 -= carry[8] << 21
  1884. carry[9] = s9 >> 21
  1885. s10 += carry[9]
  1886. s9 -= carry[9] << 21
  1887. carry[10] = s10 >> 21
  1888. s11 += carry[10]
  1889. s10 -= carry[10] << 21
  1890. carry[11] = s11 >> 21
  1891. s12 += carry[11]
  1892. s11 -= carry[11] << 21
  1893. s0 += s12 * 666643
  1894. s1 += s12 * 470296
  1895. s2 += s12 * 654183
  1896. s3 -= s12 * 997805
  1897. s4 += s12 * 136657
  1898. s5 -= s12 * 683901
  1899. s12 = 0
  1900. carry[0] = s0 >> 21
  1901. s1 += carry[0]
  1902. s0 -= carry[0] << 21
  1903. carry[1] = s1 >> 21
  1904. s2 += carry[1]
  1905. s1 -= carry[1] << 21
  1906. carry[2] = s2 >> 21
  1907. s3 += carry[2]
  1908. s2 -= carry[2] << 21
  1909. carry[3] = s3 >> 21
  1910. s4 += carry[3]
  1911. s3 -= carry[3] << 21
  1912. carry[4] = s4 >> 21
  1913. s5 += carry[4]
  1914. s4 -= carry[4] << 21
  1915. carry[5] = s5 >> 21
  1916. s6 += carry[5]
  1917. s5 -= carry[5] << 21
  1918. carry[6] = s6 >> 21
  1919. s7 += carry[6]
  1920. s6 -= carry[6] << 21
  1921. carry[7] = s7 >> 21
  1922. s8 += carry[7]
  1923. s7 -= carry[7] << 21
  1924. carry[8] = s8 >> 21
  1925. s9 += carry[8]
  1926. s8 -= carry[8] << 21
  1927. carry[9] = s9 >> 21
  1928. s10 += carry[9]
  1929. s9 -= carry[9] << 21
  1930. carry[10] = s10 >> 21
  1931. s11 += carry[10]
  1932. s10 -= carry[10] << 21
  1933. s[0] = byte(s0 >> 0)
  1934. s[1] = byte(s0 >> 8)
  1935. s[2] = byte((s0 >> 16) | (s1 << 5))
  1936. s[3] = byte(s1 >> 3)
  1937. s[4] = byte(s1 >> 11)
  1938. s[5] = byte((s1 >> 19) | (s2 << 2))
  1939. s[6] = byte(s2 >> 6)
  1940. s[7] = byte((s2 >> 14) | (s3 << 7))
  1941. s[8] = byte(s3 >> 1)
  1942. s[9] = byte(s3 >> 9)
  1943. s[10] = byte((s3 >> 17) | (s4 << 4))
  1944. s[11] = byte(s4 >> 4)
  1945. s[12] = byte(s4 >> 12)
  1946. s[13] = byte((s4 >> 20) | (s5 << 1))
  1947. s[14] = byte(s5 >> 7)
  1948. s[15] = byte((s5 >> 15) | (s6 << 6))
  1949. s[16] = byte(s6 >> 2)
  1950. s[17] = byte(s6 >> 10)
  1951. s[18] = byte((s6 >> 18) | (s7 << 3))
  1952. s[19] = byte(s7 >> 5)
  1953. s[20] = byte(s7 >> 13)
  1954. s[21] = byte(s8 >> 0)
  1955. s[22] = byte(s8 >> 8)
  1956. s[23] = byte((s8 >> 16) | (s9 << 5))
  1957. s[24] = byte(s9 >> 3)
  1958. s[25] = byte(s9 >> 11)
  1959. s[26] = byte((s9 >> 19) | (s10 << 2))
  1960. s[27] = byte(s10 >> 6)
  1961. s[28] = byte((s10 >> 14) | (s11 << 7))
  1962. s[29] = byte(s11 >> 1)
  1963. s[30] = byte(s11 >> 9)
  1964. s[31] = byte(s11 >> 17)
  1965. }
  1966. // Input:
  1967. // a[0]+256*a[1]+...+256^31*a[31] = a
  1968. // b[0]+256*b[1]+...+256^31*b[31] = b
  1969. // c[0]+256*c[1]+...+256^31*c[31] = c
  1970. //
  1971. // Output:
  1972. // s[0]+256*s[1]+...+256^31*s[31] = (c-ab) mod l
  1973. // where l = 2^252 + 27742317777372353535851937790883648493.
  1974. func ScMulSub(s, a, b, c *Key) {
  1975. a0 := 2097151 & load3(a[:])
  1976. a1 := 2097151 & (load4(a[2:]) >> 5)
  1977. a2 := 2097151 & (load3(a[5:]) >> 2)
  1978. a3 := 2097151 & (load4(a[7:]) >> 7)
  1979. a4 := 2097151 & (load4(a[10:]) >> 4)
  1980. a5 := 2097151 & (load3(a[13:]) >> 1)
  1981. a6 := 2097151 & (load4(a[15:]) >> 6)
  1982. a7 := 2097151 & (load3(a[18:]) >> 3)
  1983. a8 := 2097151 & load3(a[21:])
  1984. a9 := 2097151 & (load4(a[23:]) >> 5)
  1985. a10 := 2097151 & (load3(a[26:]) >> 2)
  1986. a11 := (load4(a[28:]) >> 7)
  1987. b0 := 2097151 & load3(b[:])
  1988. b1 := 2097151 & (load4(b[2:]) >> 5)
  1989. b2 := 2097151 & (load3(b[5:]) >> 2)
  1990. b3 := 2097151 & (load4(b[7:]) >> 7)
  1991. b4 := 2097151 & (load4(b[10:]) >> 4)
  1992. b5 := 2097151 & (load3(b[13:]) >> 1)
  1993. b6 := 2097151 & (load4(b[15:]) >> 6)
  1994. b7 := 2097151 & (load3(b[18:]) >> 3)
  1995. b8 := 2097151 & load3(b[21:])
  1996. b9 := 2097151 & (load4(b[23:]) >> 5)
  1997. b10 := 2097151 & (load3(b[26:]) >> 2)
  1998. b11 := (load4(b[28:]) >> 7)
  1999. c0 := 2097151 & load3(c[:])
  2000. c1 := 2097151 & (load4(c[2:]) >> 5)
  2001. c2 := 2097151 & (load3(c[5:]) >> 2)
  2002. c3 := 2097151 & (load4(c[7:]) >> 7)
  2003. c4 := 2097151 & (load4(c[10:]) >> 4)
  2004. c5 := 2097151 & (load3(c[13:]) >> 1)
  2005. c6 := 2097151 & (load4(c[15:]) >> 6)
  2006. c7 := 2097151 & (load3(c[18:]) >> 3)
  2007. c8 := 2097151 & load3(c[21:])
  2008. c9 := 2097151 & (load4(c[23:]) >> 5)
  2009. c10 := 2097151 & (load3(c[26:]) >> 2)
  2010. c11 := (load4(c[28:]) >> 7)
  2011. var carry [23]int64
  2012. s0 := c0 - a0*b0
  2013. s1 := c1 - a0*b1 - a1*b0
  2014. s2 := c2 - a0*b2 - a1*b1 - a2*b0
  2015. s3 := c3 - a0*b3 - a1*b2 - a2*b1 - a3*b0
  2016. s4 := c4 - a0*b4 - a1*b3 - a2*b2 - a3*b1 - a4*b0
  2017. s5 := c5 - a0*b5 - a1*b4 - a2*b3 - a3*b2 - a4*b1 - a5*b0
  2018. s6 := c6 - a0*b6 - a1*b5 - a2*b4 - a3*b3 - a4*b2 - a5*b1 - a6*b0
  2019. s7 := c7 - a0*b7 - a1*b6 - a2*b5 - a3*b4 - a4*b3 - a5*b2 - a6*b1 - a7*b0
  2020. s8 := c8 - a0*b8 - a1*b7 - a2*b6 - a3*b5 - a4*b4 - a5*b3 - a6*b2 - a7*b1 - a8*b0
  2021. s9 := c9 - a0*b9 - a1*b8 - a2*b7 - a3*b6 - a4*b5 - a5*b4 - a6*b3 - a7*b2 - a8*b1 - a9*b0
  2022. s10 := c10 - a0*b10 - a1*b9 - a2*b8 - a3*b7 - a4*b6 - a5*b5 - a6*b4 - a7*b3 - a8*b2 - a9*b1 - a10*b0
  2023. 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
  2024. s12 := -a1*b11 - a2*b10 - a3*b9 - a4*b8 - a5*b7 - a6*b6 - a7*b5 - a8*b4 - a9*b3 - a10*b2 - a11*b1
  2025. s13 := -a2*b11 - a3*b10 - a4*b9 - a5*b8 - a6*b7 - a7*b6 - a8*b5 - a9*b4 - a10*b3 - a11*b2
  2026. s14 := -a3*b11 - a4*b10 - a5*b9 - a6*b8 - a7*b7 - a8*b6 - a9*b5 - a10*b4 - a11*b3
  2027. s15 := -a4*b11 - a5*b10 - a6*b9 - a7*b8 - a8*b7 - a9*b6 - a10*b5 - a11*b4
  2028. s16 := -a5*b11 - a6*b10 - a7*b9 - a8*b8 - a9*b7 - a10*b6 - a11*b5
  2029. s17 := -a6*b11 - a7*b10 - a8*b9 - a9*b8 - a10*b7 - a11*b6
  2030. s18 := -a7*b11 - a8*b10 - a9*b9 - a10*b8 - a11*b7
  2031. s19 := -a8*b11 - a9*b10 - a10*b9 - a11*b8
  2032. s20 := -a9*b11 - a10*b10 - a11*b9
  2033. s21 := -a10*b11 - a11*b10
  2034. s22 := -a11 * b11
  2035. s23 := int64(0)
  2036. carry[0] = (s0 + (1 << 20)) >> 21
  2037. s1 += carry[0]
  2038. s0 -= carry[0] << 21
  2039. carry[2] = (s2 + (1 << 20)) >> 21
  2040. s3 += carry[2]
  2041. s2 -= carry[2] << 21
  2042. carry[4] = (s4 + (1 << 20)) >> 21
  2043. s5 += carry[4]
  2044. s4 -= carry[4] << 21
  2045. carry[6] = (s6 + (1 << 20)) >> 21
  2046. s7 += carry[6]
  2047. s6 -= carry[6] << 21
  2048. carry[8] = (s8 + (1 << 20)) >> 21
  2049. s9 += carry[8]
  2050. s8 -= carry[8] << 21
  2051. carry[10] = (s10 + (1 << 20)) >> 21
  2052. s11 += carry[10]
  2053. s10 -= carry[10] << 21
  2054. carry[12] = (s12 + (1 << 20)) >> 21
  2055. s13 += carry[12]
  2056. s12 -= carry[12] << 21
  2057. carry[14] = (s14 + (1 << 20)) >> 21
  2058. s15 += carry[14]
  2059. s14 -= carry[14] << 21
  2060. carry[16] = (s16 + (1 << 20)) >> 21
  2061. s17 += carry[16]
  2062. s16 -= carry[16] << 21
  2063. carry[18] = (s18 + (1 << 20)) >> 21
  2064. s19 += carry[18]
  2065. s18 -= carry[18] << 21
  2066. carry[20] = (s20 + (1 << 20)) >> 21
  2067. s21 += carry[20]
  2068. s20 -= carry[20] << 21
  2069. carry[22] = (s22 + (1 << 20)) >> 21
  2070. s23 += carry[22]
  2071. s22 -= carry[22] << 21
  2072. carry[1] = (s1 + (1 << 20)) >> 21
  2073. s2 += carry[1]
  2074. s1 -= carry[1] << 21
  2075. carry[3] = (s3 + (1 << 20)) >> 21
  2076. s4 += carry[3]
  2077. s3 -= carry[3] << 21
  2078. carry[5] = (s5 + (1 << 20)) >> 21
  2079. s6 += carry[5]
  2080. s5 -= carry[5] << 21
  2081. carry[7] = (s7 + (1 << 20)) >> 21
  2082. s8 += carry[7]
  2083. s7 -= carry[7] << 21
  2084. carry[9] = (s9 + (1 << 20)) >> 21
  2085. s10 += carry[9]
  2086. s9 -= carry[9] << 21
  2087. carry[11] = (s11 + (1 << 20)) >> 21
  2088. s12 += carry[11]
  2089. s11 -= carry[11] << 21
  2090. carry[13] = (s13 + (1 << 20)) >> 21
  2091. s14 += carry[13]
  2092. s13 -= carry[13] << 21
  2093. carry[15] = (s15 + (1 << 20)) >> 21
  2094. s16 += carry[15]
  2095. s15 -= carry[15] << 21
  2096. carry[17] = (s17 + (1 << 20)) >> 21
  2097. s18 += carry[17]
  2098. s17 -= carry[17] << 21
  2099. carry[19] = (s19 + (1 << 20)) >> 21
  2100. s20 += carry[19]
  2101. s19 -= carry[19] << 21
  2102. carry[21] = (s21 + (1 << 20)) >> 21
  2103. s22 += carry[21]
  2104. s21 -= carry[21] << 21
  2105. s11 += s23 * 666643
  2106. s12 += s23 * 470296
  2107. s13 += s23 * 654183
  2108. s14 -= s23 * 997805
  2109. s15 += s23 * 136657
  2110. s16 -= s23 * 683901
  2111. s23 = 0
  2112. s10 += s22 * 666643
  2113. s11 += s22 * 470296
  2114. s12 += s22 * 654183
  2115. s13 -= s22 * 997805
  2116. s14 += s22 * 136657
  2117. s15 -= s22 * 683901
  2118. s22 = 0
  2119. s9 += s21 * 666643
  2120. s10 += s21 * 470296
  2121. s11 += s21 * 654183
  2122. s12 -= s21 * 997805
  2123. s13 += s21 * 136657
  2124. s14 -= s21 * 683901
  2125. s21 = 0
  2126. s8 += s20 * 666643
  2127. s9 += s20 * 470296
  2128. s10 += s20 * 654183
  2129. s11 -= s20 * 997805
  2130. s12 += s20 * 136657
  2131. s13 -= s20 * 683901
  2132. s20 = 0
  2133. s7 += s19 * 666643
  2134. s8 += s19 * 470296
  2135. s9 += s19 * 654183
  2136. s10 -= s19 * 997805
  2137. s11 += s19 * 136657
  2138. s12 -= s19 * 683901
  2139. s19 = 0
  2140. s6 += s18 * 666643
  2141. s7 += s18 * 470296
  2142. s8 += s18 * 654183
  2143. s9 -= s18 * 997805
  2144. s10 += s18 * 136657
  2145. s11 -= s18 * 683901
  2146. s18 = 0
  2147. carry[6] = (s6 + (1 << 20)) >> 21
  2148. s7 += carry[6]
  2149. s6 -= carry[6] << 21
  2150. carry[8] = (s8 + (1 << 20)) >> 21
  2151. s9 += carry[8]
  2152. s8 -= carry[8] << 21
  2153. carry[10] = (s10 + (1 << 20)) >> 21
  2154. s11 += carry[10]
  2155. s10 -= carry[10] << 21
  2156. carry[12] = (s12 + (1 << 20)) >> 21
  2157. s13 += carry[12]
  2158. s12 -= carry[12] << 21
  2159. carry[14] = (s14 + (1 << 20)) >> 21
  2160. s15 += carry[14]
  2161. s14 -= carry[14] << 21
  2162. carry[16] = (s16 + (1 << 20)) >> 21
  2163. s17 += carry[16]
  2164. s16 -= carry[16] << 21
  2165. carry[7] = (s7 + (1 << 20)) >> 21
  2166. s8 += carry[7]
  2167. s7 -= carry[7] << 21
  2168. carry[9] = (s9 + (1 << 20)) >> 21
  2169. s10 += carry[9]
  2170. s9 -= carry[9] << 21
  2171. carry[11] = (s11 + (1 << 20)) >> 21
  2172. s12 += carry[11]
  2173. s11 -= carry[11] << 21
  2174. carry[13] = (s13 + (1 << 20)) >> 21
  2175. s14 += carry[13]
  2176. s13 -= carry[13] << 21
  2177. carry[15] = (s15 + (1 << 20)) >> 21
  2178. s16 += carry[15]
  2179. s15 -= carry[15] << 21
  2180. s5 += s17 * 666643
  2181. s6 += s17 * 470296
  2182. s7 += s17 * 654183
  2183. s8 -= s17 * 997805
  2184. s9 += s17 * 136657
  2185. s10 -= s17 * 683901
  2186. s17 = 0
  2187. s4 += s16 * 666643
  2188. s5 += s16 * 470296
  2189. s6 += s16 * 654183
  2190. s7 -= s16 * 997805
  2191. s8 += s16 * 136657
  2192. s9 -= s16 * 683901
  2193. s16 = 0
  2194. s3 += s15 * 666643
  2195. s4 += s15 * 470296
  2196. s5 += s15 * 654183
  2197. s6 -= s15 * 997805
  2198. s7 += s15 * 136657
  2199. s8 -= s15 * 683901
  2200. s15 = 0
  2201. s2 += s14 * 666643
  2202. s3 += s14 * 470296
  2203. s4 += s14 * 654183
  2204. s5 -= s14 * 997805
  2205. s6 += s14 * 136657
  2206. s7 -= s14 * 683901
  2207. s14 = 0
  2208. s1 += s13 * 666643
  2209. s2 += s13 * 470296
  2210. s3 += s13 * 654183
  2211. s4 -= s13 * 997805
  2212. s5 += s13 * 136657
  2213. s6 -= s13 * 683901
  2214. s13 = 0
  2215. s0 += s12 * 666643
  2216. s1 += s12 * 470296
  2217. s2 += s12 * 654183
  2218. s3 -= s12 * 997805
  2219. s4 += s12 * 136657
  2220. s5 -= s12 * 683901
  2221. s12 = 0
  2222. carry[0] = (s0 + (1 << 20)) >> 21
  2223. s1 += carry[0]
  2224. s0 -= carry[0] << 21
  2225. carry[2] = (s2 + (1 << 20)) >> 21
  2226. s3 += carry[2]
  2227. s2 -= carry[2] << 21
  2228. carry[4] = (s4 + (1 << 20)) >> 21
  2229. s5 += carry[4]
  2230. s4 -= carry[4] << 21
  2231. carry[6] = (s6 + (1 << 20)) >> 21
  2232. s7 += carry[6]
  2233. s6 -= carry[6] << 21
  2234. carry[8] = (s8 + (1 << 20)) >> 21
  2235. s9 += carry[8]
  2236. s8 -= carry[8] << 21
  2237. carry[10] = (s10 + (1 << 20)) >> 21
  2238. s11 += carry[10]
  2239. s10 -= carry[10] << 21
  2240. carry[1] = (s1 + (1 << 20)) >> 21
  2241. s2 += carry[1]
  2242. s1 -= carry[1] << 21
  2243. carry[3] = (s3 + (1 << 20)) >> 21
  2244. s4 += carry[3]
  2245. s3 -= carry[3] << 21
  2246. carry[5] = (s5 + (1 << 20)) >> 21
  2247. s6 += carry[5]
  2248. s5 -= carry[5] << 21
  2249. carry[7] = (s7 + (1 << 20)) >> 21
  2250. s8 += carry[7]
  2251. s7 -= carry[7] << 21
  2252. carry[9] = (s9 + (1 << 20)) >> 21
  2253. s10 += carry[9]
  2254. s9 -= carry[9] << 21
  2255. carry[11] = (s11 + (1 << 20)) >> 21
  2256. s12 += carry[11]
  2257. s11 -= carry[11] << 21
  2258. s0 += s12 * 666643
  2259. s1 += s12 * 470296
  2260. s2 += s12 * 654183
  2261. s3 -= s12 * 997805
  2262. s4 += s12 * 136657
  2263. s5 -= s12 * 683901
  2264. s12 = 0
  2265. carry[0] = s0 >> 21
  2266. s1 += carry[0]
  2267. s0 -= carry[0] << 21
  2268. carry[1] = s1 >> 21
  2269. s2 += carry[1]
  2270. s1 -= carry[1] << 21
  2271. carry[2] = s2 >> 21
  2272. s3 += carry[2]
  2273. s2 -= carry[2] << 21
  2274. carry[3] = s3 >> 21
  2275. s4 += carry[3]
  2276. s3 -= carry[3] << 21
  2277. carry[4] = s4 >> 21
  2278. s5 += carry[4]
  2279. s4 -= carry[4] << 21
  2280. carry[5] = s5 >> 21
  2281. s6 += carry[5]
  2282. s5 -= carry[5] << 21
  2283. carry[6] = s6 >> 21
  2284. s7 += carry[6]
  2285. s6 -= carry[6] << 21
  2286. carry[7] = s7 >> 21
  2287. s8 += carry[7]
  2288. s7 -= carry[7] << 21
  2289. carry[8] = s8 >> 21
  2290. s9 += carry[8]
  2291. s8 -= carry[8] << 21
  2292. carry[9] = s9 >> 21
  2293. s10 += carry[9]
  2294. s9 -= carry[9] << 21
  2295. carry[10] = s10 >> 21
  2296. s11 += carry[10]
  2297. s10 -= carry[10] << 21
  2298. carry[11] = s11 >> 21
  2299. s12 += carry[11]
  2300. s11 -= carry[11] << 21
  2301. s0 += s12 * 666643
  2302. s1 += s12 * 470296
  2303. s2 += s12 * 654183
  2304. s3 -= s12 * 997805
  2305. s4 += s12 * 136657
  2306. s5 -= s12 * 683901
  2307. s12 = 0
  2308. carry[0] = s0 >> 21
  2309. s1 += carry[0]
  2310. s0 -= carry[0] << 21
  2311. carry[1] = s1 >> 21
  2312. s2 += carry[1]
  2313. s1 -= carry[1] << 21
  2314. carry[2] = s2 >> 21
  2315. s3 += carry[2]
  2316. s2 -= carry[2] << 21
  2317. carry[3] = s3 >> 21
  2318. s4 += carry[3]
  2319. s3 -= carry[3] << 21
  2320. carry[4] = s4 >> 21
  2321. s5 += carry[4]
  2322. s4 -= carry[4] << 21
  2323. carry[5] = s5 >> 21
  2324. s6 += carry[5]
  2325. s5 -= carry[5] << 21
  2326. carry[6] = s6 >> 21
  2327. s7 += carry[6]
  2328. s6 -= carry[6] << 21
  2329. carry[7] = s7 >> 21
  2330. s8 += carry[7]
  2331. s7 -= carry[7] << 21
  2332. carry[8] = s8 >> 21
  2333. s9 += carry[8]
  2334. s8 -= carry[8] << 21
  2335. carry[9] = s9 >> 21
  2336. s10 += carry[9]
  2337. s9 -= carry[9] << 21
  2338. carry[10] = s10 >> 21
  2339. s11 += carry[10]
  2340. s10 -= carry[10] << 21
  2341. s[0] = byte(s0 >> 0)
  2342. s[1] = byte(s0 >> 8)
  2343. s[2] = byte((s0 >> 16) | (s1 << 5))
  2344. s[3] = byte(s1 >> 3)
  2345. s[4] = byte(s1 >> 11)
  2346. s[5] = byte((s1 >> 19) | (s2 << 2))
  2347. s[6] = byte(s2 >> 6)
  2348. s[7] = byte((s2 >> 14) | (s3 << 7))
  2349. s[8] = byte(s3 >> 1)
  2350. s[9] = byte(s3 >> 9)
  2351. s[10] = byte((s3 >> 17) | (s4 << 4))
  2352. s[11] = byte(s4 >> 4)
  2353. s[12] = byte(s4 >> 12)
  2354. s[13] = byte((s4 >> 20) | (s5 << 1))
  2355. s[14] = byte(s5 >> 7)
  2356. s[15] = byte((s5 >> 15) | (s6 << 6))
  2357. s[16] = byte(s6 >> 2)
  2358. s[17] = byte(s6 >> 10)
  2359. s[18] = byte((s6 >> 18) | (s7 << 3))
  2360. s[19] = byte(s7 >> 5)
  2361. s[20] = byte(s7 >> 13)
  2362. s[21] = byte(s8 >> 0)
  2363. s[22] = byte(s8 >> 8)
  2364. s[23] = byte((s8 >> 16) | (s9 << 5))
  2365. s[24] = byte(s9 >> 3)
  2366. s[25] = byte(s9 >> 11)
  2367. s[26] = byte((s9 >> 19) | (s10 << 2))
  2368. s[27] = byte(s10 >> 6)
  2369. s[28] = byte((s10 >> 14) | (s11 << 7))
  2370. s[29] = byte(s11 >> 1)
  2371. s[30] = byte(s11 >> 9)
  2372. s[31] = byte(s11 >> 17)
  2373. }
  2374. // Input:
  2375. // s[0]+256*s[1]+...+256^63*s[63] = s
  2376. //
  2377. // Output:
  2378. // s[0]+256*s[1]+...+256^31*s[31] = s mod l
  2379. // where l = 2^252 + 27742317777372353535851937790883648493.
  2380. func ScReduce(out *Key, s *[64]byte) {
  2381. s0 := 2097151 & load3(s[:])
  2382. s1 := 2097151 & (load4(s[2:]) >> 5)
  2383. s2 := 2097151 & (load3(s[5:]) >> 2)
  2384. s3 := 2097151 & (load4(s[7:]) >> 7)
  2385. s4 := 2097151 & (load4(s[10:]) >> 4)
  2386. s5 := 2097151 & (load3(s[13:]) >> 1)
  2387. s6 := 2097151 & (load4(s[15:]) >> 6)
  2388. s7 := 2097151 & (load3(s[18:]) >> 3)
  2389. s8 := 2097151 & load3(s[21:])
  2390. s9 := 2097151 & (load4(s[23:]) >> 5)
  2391. s10 := 2097151 & (load3(s[26:]) >> 2)
  2392. s11 := 2097151 & (load4(s[28:]) >> 7)
  2393. s12 := 2097151 & (load4(s[31:]) >> 4)
  2394. s13 := 2097151 & (load3(s[34:]) >> 1)
  2395. s14 := 2097151 & (load4(s[36:]) >> 6)
  2396. s15 := 2097151 & (load3(s[39:]) >> 3)
  2397. s16 := 2097151 & load3(s[42:])
  2398. s17 := 2097151 & (load4(s[44:]) >> 5)
  2399. s18 := 2097151 & (load3(s[47:]) >> 2)
  2400. s19 := 2097151 & (load4(s[49:]) >> 7)
  2401. s20 := 2097151 & (load4(s[52:]) >> 4)
  2402. s21 := 2097151 & (load3(s[55:]) >> 1)
  2403. s22 := 2097151 & (load4(s[57:]) >> 6)
  2404. s23 := (load4(s[60:]) >> 3)
  2405. s11 += s23 * 666643
  2406. s12 += s23 * 470296
  2407. s13 += s23 * 654183
  2408. s14 -= s23 * 997805
  2409. s15 += s23 * 136657
  2410. s16 -= s23 * 683901
  2411. s23 = 0
  2412. s10 += s22 * 666643
  2413. s11 += s22 * 470296
  2414. s12 += s22 * 654183
  2415. s13 -= s22 * 997805
  2416. s14 += s22 * 136657
  2417. s15 -= s22 * 683901
  2418. s22 = 0
  2419. s9 += s21 * 666643
  2420. s10 += s21 * 470296
  2421. s11 += s21 * 654183
  2422. s12 -= s21 * 997805
  2423. s13 += s21 * 136657
  2424. s14 -= s21 * 683901
  2425. s21 = 0
  2426. s8 += s20 * 666643
  2427. s9 += s20 * 470296
  2428. s10 += s20 * 654183
  2429. s11 -= s20 * 997805
  2430. s12 += s20 * 136657
  2431. s13 -= s20 * 683901
  2432. s20 = 0
  2433. s7 += s19 * 666643
  2434. s8 += s19 * 470296
  2435. s9 += s19 * 654183
  2436. s10 -= s19 * 997805
  2437. s11 += s19 * 136657
  2438. s12 -= s19 * 683901
  2439. s19 = 0
  2440. s6 += s18 * 666643
  2441. s7 += s18 * 470296
  2442. s8 += s18 * 654183
  2443. s9 -= s18 * 997805
  2444. s10 += s18 * 136657
  2445. s11 -= s18 * 683901
  2446. s18 = 0
  2447. var carry [17]int64
  2448. carry[6] = (s6 + (1 << 20)) >> 21
  2449. s7 += carry[6]
  2450. s6 -= carry[6] << 21
  2451. carry[8] = (s8 + (1 << 20)) >> 21
  2452. s9 += carry[8]
  2453. s8 -= carry[8] << 21
  2454. carry[10] = (s10 + (1 << 20)) >> 21
  2455. s11 += carry[10]
  2456. s10 -= carry[10] << 21
  2457. carry[12] = (s12 + (1 << 20)) >> 21
  2458. s13 += carry[12]
  2459. s12 -= carry[12] << 21
  2460. carry[14] = (s14 + (1 << 20)) >> 21
  2461. s15 += carry[14]
  2462. s14 -= carry[14] << 21
  2463. carry[16] = (s16 + (1 << 20)) >> 21
  2464. s17 += carry[16]
  2465. s16 -= carry[16] << 21
  2466. carry[7] = (s7 + (1 << 20)) >> 21
  2467. s8 += carry[7]
  2468. s7 -= carry[7] << 21
  2469. carry[9] = (s9 + (1 << 20)) >> 21
  2470. s10 += carry[9]
  2471. s9 -= carry[9] << 21
  2472. carry[11] = (s11 + (1 << 20)) >> 21
  2473. s12 += carry[11]
  2474. s11 -= carry[11] << 21
  2475. carry[13] = (s13 + (1 << 20)) >> 21
  2476. s14 += carry[13]
  2477. s13 -= carry[13] << 21
  2478. carry[15] = (s15 + (1 << 20)) >> 21
  2479. s16 += carry[15]
  2480. s15 -= carry[15] << 21
  2481. s5 += s17 * 666643
  2482. s6 += s17 * 470296
  2483. s7 += s17 * 654183
  2484. s8 -= s17 * 997805
  2485. s9 += s17 * 136657
  2486. s10 -= s17 * 683901
  2487. s17 = 0
  2488. s4 += s16 * 666643
  2489. s5 += s16 * 470296
  2490. s6 += s16 * 654183
  2491. s7 -= s16 * 997805
  2492. s8 += s16 * 136657
  2493. s9 -= s16 * 683901
  2494. s16 = 0
  2495. s3 += s15 * 666643
  2496. s4 += s15 * 470296
  2497. s5 += s15 * 654183
  2498. s6 -= s15 * 997805
  2499. s7 += s15 * 136657
  2500. s8 -= s15 * 683901
  2501. s15 = 0
  2502. s2 += s14 * 666643
  2503. s3 += s14 * 470296
  2504. s4 += s14 * 654183
  2505. s5 -= s14 * 997805
  2506. s6 += s14 * 136657
  2507. s7 -= s14 * 683901
  2508. s14 = 0
  2509. s1 += s13 * 666643
  2510. s2 += s13 * 470296
  2511. s3 += s13 * 654183
  2512. s4 -= s13 * 997805
  2513. s5 += s13 * 136657
  2514. s6 -= s13 * 683901
  2515. s13 = 0
  2516. s0 += s12 * 666643
  2517. s1 += s12 * 470296
  2518. s2 += s12 * 654183
  2519. s3 -= s12 * 997805
  2520. s4 += s12 * 136657
  2521. s5 -= s12 * 683901
  2522. s12 = 0
  2523. carry[0] = (s0 + (1 << 20)) >> 21
  2524. s1 += carry[0]
  2525. s0 -= carry[0] << 21
  2526. carry[2] = (s2 + (1 << 20)) >> 21
  2527. s3 += carry[2]
  2528. s2 -= carry[2] << 21
  2529. carry[4] = (s4 + (1 << 20)) >> 21
  2530. s5 += carry[4]
  2531. s4 -= carry[4] << 21
  2532. carry[6] = (s6 + (1 << 20)) >> 21
  2533. s7 += carry[6]
  2534. s6 -= carry[6] << 21
  2535. carry[8] = (s8 + (1 << 20)) >> 21
  2536. s9 += carry[8]
  2537. s8 -= carry[8] << 21
  2538. carry[10] = (s10 + (1 << 20)) >> 21
  2539. s11 += carry[10]
  2540. s10 -= carry[10] << 21
  2541. carry[1] = (s1 + (1 << 20)) >> 21
  2542. s2 += carry[1]
  2543. s1 -= carry[1] << 21
  2544. carry[3] = (s3 + (1 << 20)) >> 21
  2545. s4 += carry[3]
  2546. s3 -= carry[3] << 21
  2547. carry[5] = (s5 + (1 << 20)) >> 21
  2548. s6 += carry[5]
  2549. s5 -= carry[5] << 21
  2550. carry[7] = (s7 + (1 << 20)) >> 21
  2551. s8 += carry[7]
  2552. s7 -= carry[7] << 21
  2553. carry[9] = (s9 + (1 << 20)) >> 21
  2554. s10 += carry[9]
  2555. s9 -= carry[9] << 21
  2556. carry[11] = (s11 + (1 << 20)) >> 21
  2557. s12 += carry[11]
  2558. s11 -= carry[11] << 21
  2559. s0 += s12 * 666643
  2560. s1 += s12 * 470296
  2561. s2 += s12 * 654183
  2562. s3 -= s12 * 997805
  2563. s4 += s12 * 136657
  2564. s5 -= s12 * 683901
  2565. s12 = 0
  2566. carry[0] = s0 >> 21
  2567. s1 += carry[0]
  2568. s0 -= carry[0] << 21
  2569. carry[1] = s1 >> 21
  2570. s2 += carry[1]
  2571. s1 -= carry[1] << 21
  2572. carry[2] = s2 >> 21
  2573. s3 += carry[2]
  2574. s2 -= carry[2] << 21
  2575. carry[3] = s3 >> 21
  2576. s4 += carry[3]
  2577. s3 -= carry[3] << 21
  2578. carry[4] = s4 >> 21
  2579. s5 += carry[4]
  2580. s4 -= carry[4] << 21
  2581. carry[5] = s5 >> 21
  2582. s6 += carry[5]
  2583. s5 -= carry[5] << 21
  2584. carry[6] = s6 >> 21
  2585. s7 += carry[6]
  2586. s6 -= carry[6] << 21
  2587. carry[7] = s7 >> 21
  2588. s8 += carry[7]
  2589. s7 -= carry[7] << 21
  2590. carry[8] = s8 >> 21
  2591. s9 += carry[8]
  2592. s8 -= carry[8] << 21
  2593. carry[9] = s9 >> 21
  2594. s10 += carry[9]
  2595. s9 -= carry[9] << 21
  2596. carry[10] = s10 >> 21
  2597. s11 += carry[10]
  2598. s10 -= carry[10] << 21
  2599. carry[11] = s11 >> 21
  2600. s12 += carry[11]
  2601. s11 -= carry[11] << 21
  2602. s0 += s12 * 666643
  2603. s1 += s12 * 470296
  2604. s2 += s12 * 654183
  2605. s3 -= s12 * 997805
  2606. s4 += s12 * 136657
  2607. s5 -= s12 * 683901
  2608. s12 = 0
  2609. carry[0] = s0 >> 21
  2610. s1 += carry[0]
  2611. s0 -= carry[0] << 21
  2612. carry[1] = s1 >> 21
  2613. s2 += carry[1]
  2614. s1 -= carry[1] << 21
  2615. carry[2] = s2 >> 21
  2616. s3 += carry[2]
  2617. s2 -= carry[2] << 21
  2618. carry[3] = s3 >> 21
  2619. s4 += carry[3]
  2620. s3 -= carry[3] << 21
  2621. carry[4] = s4 >> 21
  2622. s5 += carry[4]
  2623. s4 -= carry[4] << 21
  2624. carry[5] = s5 >> 21
  2625. s6 += carry[5]
  2626. s5 -= carry[5] << 21
  2627. carry[6] = s6 >> 21
  2628. s7 += carry[6]
  2629. s6 -= carry[6] << 21
  2630. carry[7] = s7 >> 21
  2631. s8 += carry[7]
  2632. s7 -= carry[7] << 21
  2633. carry[8] = s8 >> 21
  2634. s9 += carry[8]
  2635. s8 -= carry[8] << 21
  2636. carry[9] = s9 >> 21
  2637. s10 += carry[9]
  2638. s9 -= carry[9] << 21
  2639. carry[10] = s10 >> 21
  2640. s11 += carry[10]
  2641. s10 -= carry[10] << 21
  2642. out[0] = byte(s0 >> 0)
  2643. out[1] = byte(s0 >> 8)
  2644. out[2] = byte((s0 >> 16) | (s1 << 5))
  2645. out[3] = byte(s1 >> 3)
  2646. out[4] = byte(s1 >> 11)
  2647. out[5] = byte((s1 >> 19) | (s2 << 2))
  2648. out[6] = byte(s2 >> 6)
  2649. out[7] = byte((s2 >> 14) | (s3 << 7))
  2650. out[8] = byte(s3 >> 1)
  2651. out[9] = byte(s3 >> 9)
  2652. out[10] = byte((s3 >> 17) | (s4 << 4))
  2653. out[11] = byte(s4 >> 4)
  2654. out[12] = byte(s4 >> 12)
  2655. out[13] = byte((s4 >> 20) | (s5 << 1))
  2656. out[14] = byte(s5 >> 7)
  2657. out[15] = byte((s5 >> 15) | (s6 << 6))
  2658. out[16] = byte(s6 >> 2)
  2659. out[17] = byte(s6 >> 10)
  2660. out[18] = byte((s6 >> 18) | (s7 << 3))
  2661. out[19] = byte(s7 >> 5)
  2662. out[20] = byte(s7 >> 13)
  2663. out[21] = byte(s8 >> 0)
  2664. out[22] = byte(s8 >> 8)
  2665. out[23] = byte((s8 >> 16) | (s9 << 5))
  2666. out[24] = byte(s9 >> 3)
  2667. out[25] = byte(s9 >> 11)
  2668. out[26] = byte((s9 >> 19) | (s10 << 2))
  2669. out[27] = byte(s10 >> 6)
  2670. out[28] = byte((s10 >> 14) | (s11 << 7))
  2671. out[29] = byte(s11 >> 1)
  2672. out[30] = byte(s11 >> 9)
  2673. out[31] = byte(s11 >> 17)
  2674. }
  2675. func ScReduce32(s *Key) {
  2676. s0 := 2097151 & load3(s[:])
  2677. s1 := 2097151 & (load4(s[2:]) >> 5)
  2678. s2 := 2097151 & (load3(s[5:]) >> 2)
  2679. s3 := 2097151 & (load4(s[7:]) >> 7)
  2680. s4 := 2097151 & (load4(s[10:]) >> 4)
  2681. s5 := 2097151 & (load3(s[13:]) >> 1)
  2682. s6 := 2097151 & (load4(s[15:]) >> 6)
  2683. s7 := 2097151 & (load3(s[18:]) >> 3)
  2684. s8 := 2097151 & load3(s[21:])
  2685. s9 := 2097151 & (load4(s[23:]) >> 5)
  2686. s10 := 2097151 & (load3(s[26:]) >> 2)
  2687. s11 := (load4(s[28:]) >> 7)
  2688. s12 := int64(0)
  2689. var carry [12]int64
  2690. carry[0] = (s0 + (1 << 20)) >> 21
  2691. s1 += carry[0]
  2692. s0 -= carry[0] << 21
  2693. carry[2] = (s2 + (1 << 20)) >> 21
  2694. s3 += carry[2]
  2695. s2 -= carry[2] << 21
  2696. carry[4] = (s4 + (1 << 20)) >> 21
  2697. s5 += carry[4]
  2698. s4 -= carry[4] << 21
  2699. carry[6] = (s6 + (1 << 20)) >> 21
  2700. s7 += carry[6]
  2701. s6 -= carry[6] << 21
  2702. carry[8] = (s8 + (1 << 20)) >> 21
  2703. s9 += carry[8]
  2704. s8 -= carry[8] << 21
  2705. carry[10] = (s10 + (1 << 20)) >> 21
  2706. s11 += carry[10]
  2707. s10 -= carry[10] << 21
  2708. carry[1] = (s1 + (1 << 20)) >> 21
  2709. s2 += carry[1]
  2710. s1 -= carry[1] << 21
  2711. carry[3] = (s3 + (1 << 20)) >> 21
  2712. s4 += carry[3]
  2713. s3 -= carry[3] << 21
  2714. carry[5] = (s5 + (1 << 20)) >> 21
  2715. s6 += carry[5]
  2716. s5 -= carry[5] << 21
  2717. carry[7] = (s7 + (1 << 20)) >> 21
  2718. s8 += carry[7]
  2719. s7 -= carry[7] << 21
  2720. carry[9] = (s9 + (1 << 20)) >> 21
  2721. s10 += carry[9]
  2722. s9 -= carry[9] << 21
  2723. carry[11] = (s11 + (1 << 20)) >> 21
  2724. s12 += carry[11]
  2725. s11 -= carry[11] << 21
  2726. s0 += s12 * 666643
  2727. s1 += s12 * 470296
  2728. s2 += s12 * 654183
  2729. s3 -= s12 * 997805
  2730. s4 += s12 * 136657
  2731. s5 -= s12 * 683901
  2732. s12 = 0
  2733. carry[0] = s0 >> 21
  2734. s1 += carry[0]
  2735. s0 -= carry[0] << 21
  2736. carry[1] = s1 >> 21
  2737. s2 += carry[1]
  2738. s1 -= carry[1] << 21
  2739. carry[2] = s2 >> 21
  2740. s3 += carry[2]
  2741. s2 -= carry[2] << 21
  2742. carry[3] = s3 >> 21
  2743. s4 += carry[3]
  2744. s3 -= carry[3] << 21
  2745. carry[4] = s4 >> 21
  2746. s5 += carry[4]
  2747. s4 -= carry[4] << 21
  2748. carry[5] = s5 >> 21
  2749. s6 += carry[5]
  2750. s5 -= carry[5] << 21
  2751. carry[6] = s6 >> 21
  2752. s7 += carry[6]
  2753. s6 -= carry[6] << 21
  2754. carry[7] = s7 >> 21
  2755. s8 += carry[7]
  2756. s7 -= carry[7] << 21
  2757. carry[8] = s8 >> 21
  2758. s9 += carry[8]
  2759. s8 -= carry[8] << 21
  2760. carry[9] = s9 >> 21
  2761. s10 += carry[9]
  2762. s9 -= carry[9] << 21
  2763. carry[10] = s10 >> 21
  2764. s11 += carry[10]
  2765. s10 -= carry[10] << 21
  2766. carry[11] = s11 >> 21
  2767. s12 += carry[11]
  2768. s11 -= carry[11] << 21
  2769. s0 += s12 * 666643
  2770. s1 += s12 * 470296
  2771. s2 += s12 * 654183
  2772. s3 -= s12 * 997805
  2773. s4 += s12 * 136657
  2774. s5 -= s12 * 683901
  2775. carry[0] = s0 >> 21
  2776. s1 += carry[0]
  2777. s0 -= carry[0] << 21
  2778. carry[1] = s1 >> 21
  2779. s2 += carry[1]
  2780. s1 -= carry[1] << 21
  2781. carry[2] = s2 >> 21
  2782. s3 += carry[2]
  2783. s2 -= carry[2] << 21
  2784. carry[3] = s3 >> 21
  2785. s4 += carry[3]
  2786. s3 -= carry[3] << 21
  2787. carry[4] = s4 >> 21
  2788. s5 += carry[4]
  2789. s4 -= carry[4] << 21
  2790. carry[5] = s5 >> 21
  2791. s6 += carry[5]
  2792. s5 -= carry[5] << 21
  2793. carry[6] = s6 >> 21
  2794. s7 += carry[6]
  2795. s6 -= carry[6] << 21
  2796. carry[7] = s7 >> 21
  2797. s8 += carry[7]
  2798. s7 -= carry[7] << 21
  2799. carry[8] = s8 >> 21
  2800. s9 += carry[8]
  2801. s8 -= carry[8] << 21
  2802. carry[9] = s9 >> 21
  2803. s10 += carry[9]
  2804. s9 -= carry[9] << 21
  2805. carry[10] = s10 >> 21
  2806. s11 += carry[10]
  2807. s10 -= carry[10] << 21
  2808. s[0] = byte(s0 >> 0)
  2809. s[1] = byte(s0 >> 8)
  2810. s[2] = byte((s0 >> 16) | (s1 << 5))
  2811. s[3] = byte(s1 >> 3)
  2812. s[4] = byte(s1 >> 11)
  2813. s[5] = byte((s1 >> 19) | (s2 << 2))
  2814. s[6] = byte(s2 >> 6)
  2815. s[7] = byte((s2 >> 14) | (s3 << 7))
  2816. s[8] = byte(s3 >> 1)
  2817. s[9] = byte(s3 >> 9)
  2818. s[10] = byte((s3 >> 17) | (s4 << 4))
  2819. s[11] = byte(s4 >> 4)
  2820. s[12] = byte(s4 >> 12)
  2821. s[13] = byte((s4 >> 20) | (s5 << 1))
  2822. s[14] = byte(s5 >> 7)
  2823. s[15] = byte((s5 >> 15) | (s6 << 6))
  2824. s[16] = byte(s6 >> 2)
  2825. s[17] = byte(s6 >> 10)
  2826. s[18] = byte((s6 >> 18) | (s7 << 3))
  2827. s[19] = byte(s7 >> 5)
  2828. s[20] = byte(s7 >> 13)
  2829. s[21] = byte(s8 >> 0)
  2830. s[22] = byte(s8 >> 8)
  2831. s[23] = byte((s8 >> 16) | (s9 << 5))
  2832. s[24] = byte(s9 >> 3)
  2833. s[25] = byte(s9 >> 11)
  2834. s[26] = byte((s9 >> 19) | (s10 << 2))
  2835. s[27] = byte(s10 >> 6)
  2836. s[28] = byte((s10 >> 14) | (s11 << 7))
  2837. s[29] = byte(s11 >> 1)
  2838. s[30] = byte(s11 >> 9)
  2839. s[31] = byte(s11 >> 17)
  2840. }