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.

3176 lines
76 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 crypto
  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. // this is equivalent to ge_fromfe_frombytes_vartime
  654. func (p *ProjectiveGroupElement) FromBytes(s *Key) {
  655. h0 := load4(s[:])
  656. h1 := load3(s[4:]) << 6
  657. h2 := load3(s[7:]) << 5
  658. h3 := load3(s[10:]) << 3
  659. h4 := load3(s[13:]) << 2
  660. h5 := load4(s[16:])
  661. h6 := load3(s[20:]) << 7
  662. h7 := load3(s[23:]) << 5
  663. h8 := load3(s[26:]) << 4
  664. h9 := load3(s[29:]) << 2
  665. var carry [10]int64
  666. carry[9] = (h9 + int64(1<<24)) >> 25
  667. h0 += carry[9] * 19
  668. h9 -= carry[9] << 25
  669. carry[1] = (h1 + int64(1<<24)) >> 25
  670. h2 += carry[1]
  671. h1 -= carry[1] << 25
  672. carry[3] = (h3 + int64(1<<24)) >> 25
  673. h4 += carry[3]
  674. h3 -= carry[3] << 25
  675. carry[5] = (h5 + int64(1<<24)) >> 25
  676. h6 += carry[5]
  677. h5 -= carry[5] << 25
  678. carry[7] = (h7 + int64(1<<24)) >> 25
  679. h8 += carry[7]
  680. h7 -= carry[7] << 25
  681. carry[0] = (h0 + int64(1<<25)) >> 26
  682. h1 += carry[0]
  683. h0 -= carry[0] << 26
  684. carry[2] = (h2 + int64(1<<25)) >> 26
  685. h3 += carry[2]
  686. h2 -= carry[2] << 26
  687. carry[4] = (h4 + int64(1<<25)) >> 26
  688. h5 += carry[4]
  689. h4 -= carry[4] << 26
  690. carry[6] = (h6 + int64(1<<25)) >> 26
  691. h7 += carry[6]
  692. h6 -= carry[6] << 26
  693. carry[8] = (h8 + int64(1<<25)) >> 26
  694. h9 += carry[8]
  695. h8 -= carry[8] << 26
  696. var u, v, w, x, y, z FieldElement
  697. u[0] = int32(h0)
  698. u[1] = int32(h1)
  699. u[2] = int32(h2)
  700. u[3] = int32(h3)
  701. u[4] = int32(h4)
  702. u[5] = int32(h5)
  703. u[6] = int32(h6)
  704. u[7] = int32(h7)
  705. u[8] = int32(h8)
  706. u[9] = int32(h9)
  707. FeSquare2(&v, &u) /* 2 * u^2 */
  708. w.One()
  709. FeAdd(&w, &v, &w) /* w = 2 * u^2 + 1 */
  710. FeSquare(&x, &w) /* w^2 */
  711. FeMul(&y, &FeMa2, &v) /* -2 * A^2 * u^2 */
  712. FeAdd(&x, &x, &y) /* x = w^2 - 2 * A^2 * u^2 */
  713. FeDivPowM1(&p.X, &w, &x) /* (w / x)^(m + 1) */
  714. FeSquare(&y, &p.X)
  715. FeMul(&x, &y, &x)
  716. FeSub(&y, &w, &x)
  717. FeCopy(&z, &FeMa)
  718. isNegative := false
  719. var sign byte
  720. if y.IsNonZero() != 0 {
  721. FeAdd(&y, &w, &x)
  722. if y.IsNonZero() != 0 {
  723. isNegative = true
  724. } else {
  725. FeMul(&p.X, &p.X, &FeFffb1)
  726. }
  727. } else {
  728. FeMul(&p.X, &p.X, &FeFffb2)
  729. }
  730. if isNegative {
  731. FeMul(&x, &x, &FeSqrtM1)
  732. FeSub(&y, &w, &x)
  733. if y.IsNonZero() != 0 {
  734. FeAdd(&y, &w, &x)
  735. FeMul(&p.X, &p.X, &FeFffb3)
  736. } else {
  737. FeMul(&p.X, &p.X, &FeFffb4)
  738. }
  739. /* p.X = sqrt(A * (A + 2) * w / x) */
  740. /* z = -A */
  741. sign = 1
  742. } else {
  743. FeMul(&p.X, &p.X, &u) /* u * sqrt(2 * A * (A + 2) * w / x) */
  744. FeMul(&z, &z, &v) /* -2 * A * u^2 */
  745. sign = 0
  746. }
  747. if p.X.IsNegative() != sign {
  748. FeNeg(&p.X, &p.X)
  749. }
  750. FeAdd(&p.Z, &z, &w)
  751. FeSub(&p.Y, &z, &w)
  752. FeMul(&p.X, &p.X, &p.Z)
  753. }
  754. func (p *ExtendedGroupElement) Zero() {
  755. p.X.Zero()
  756. p.Y.One()
  757. p.Z.One()
  758. p.T.Zero()
  759. }
  760. func (p *ExtendedGroupElement) Double(r *CompletedGroupElement) {
  761. var q ProjectiveGroupElement
  762. p.ToProjective(&q)
  763. q.Double(r)
  764. }
  765. func (p *ExtendedGroupElement) ToCached(r *CachedGroupElement) {
  766. FeAdd(&r.yPlusX, &p.Y, &p.X)
  767. FeSub(&r.yMinusX, &p.Y, &p.X)
  768. FeCopy(&r.Z, &p.Z)
  769. FeMul(&r.T2d, &p.T, &d2)
  770. }
  771. func (p *ExtendedGroupElement) ToProjective(r *ProjectiveGroupElement) {
  772. FeCopy(&r.X, &p.X)
  773. FeCopy(&r.Y, &p.Y)
  774. FeCopy(&r.Z, &p.Z)
  775. }
  776. func (p *ExtendedGroupElement) ToBytes(s *Key) {
  777. var recip, x, y FieldElement
  778. FeInvert(&recip, &p.Z)
  779. FeMul(&x, &p.X, &recip)
  780. FeMul(&y, &p.Y, &recip)
  781. FeToBytes(s, &y)
  782. s[31] ^= x.IsNegative() << 7
  783. }
  784. // used to verify whether a KEY is a point on the curve
  785. // equivalent to ge_frombytes_vartime
  786. // the original one skipped a few checks, we do them now
  787. // the original one failed a few checks so it was rewritten
  788. func (p *ExtendedGroupElement) FromBytes(s *Key) bool {
  789. /* Original implementation without checks
  790. var u, v, v3, vxx, check FieldElement
  791. FeFromBytes(&p.Y, s)
  792. p.Z.One()
  793. FeSquare(&u, &p.Y)
  794. FeMul(&v, &u, &d)
  795. FeSub(&u, &u, &p.Z) // y = y^2-1
  796. FeAdd(&v, &v, &p.Z) // v = dy^2+1
  797. FeSquare(&v3, &v)
  798. FeMul(&v3, &v3, &v) // v3 = v^3
  799. FeSquare(&p.X, &v3)
  800. FeMul(&p.X, &p.X, &v)
  801. FeMul(&p.X, &p.X, &u) // x = uv^7
  802. fePow22523(&p.X, &p.X) // x = (uv^7)^((q-5)/8)
  803. FeMul(&p.X, &p.X, &v3)
  804. FeMul(&p.X, &p.X, &u) // x = uv^3(uv^7)^((q-5)/8)
  805. var tmpX, tmp2 Key
  806. FeSquare(&vxx, &p.X)
  807. FeMul(&vxx, &vxx, &v)
  808. FeSub(&check, &vxx, &u) // vx^2-u
  809. if check.IsNonZero() == 1 {
  810. FeAdd(&check, &vxx, &u) // vx^2+u
  811. if check.IsNonZero() == 1 {
  812. return false
  813. }
  814. FeMul(&p.X, &p.X, &SqrtM1)
  815. FeToBytes(&tmpX, &p.X)
  816. for i, v := range tmpX {
  817. tmp2[31-i] = v
  818. }
  819. }
  820. if p.X.IsNegative() != (s[31] >> 7) {
  821. FeNeg(&p.X, &p.X)
  822. }
  823. FeMul(&p.T, &p.X, &p.Y)
  824. return true
  825. */
  826. var u, v, vxx, check FieldElement
  827. // expanded FeFromBytes (with canonical check)
  828. h0 := load4(s[:])
  829. h1 := load3(s[4:]) << 6
  830. h2 := load3(s[7:]) << 5
  831. h3 := load3(s[10:]) << 3
  832. h4 := load3(s[13:]) << 2
  833. h5 := load4(s[16:])
  834. h6 := load3(s[20:]) << 7
  835. h7 := load3(s[23:]) << 5
  836. h8 := load3(s[26:]) << 4
  837. h9 := (load3(s[29:]) & 8388607) << 2
  838. // Validate the number to be canonical
  839. if h9 == 33554428 && h8 == 268435440 && h7 == 536870880 && h6 == 2147483520 &&
  840. h5 == 4294967295 && h4 == 67108860 && h3 == 134217720 && h2 == 536870880 &&
  841. h1 == 1073741760 && h0 >= 4294967277 {
  842. return false
  843. }
  844. var carry [10]int64
  845. carry[9] = (h9 + 1<<24) >> 25
  846. h0 += carry[9] * 19
  847. h9 -= carry[9] << 25
  848. carry[1] = (h1 + 1<<24) >> 25
  849. h2 += carry[1]
  850. h1 -= carry[1] << 25
  851. carry[3] = (h3 + 1<<24) >> 25
  852. h4 += carry[3]
  853. h3 -= carry[3] << 25
  854. carry[5] = (h5 + 1<<24) >> 25
  855. h6 += carry[5]
  856. h5 -= carry[5] << 25
  857. carry[7] = (h7 + 1<<24) >> 25
  858. h8 += carry[7]
  859. h7 -= carry[7] << 25
  860. carry[0] = (h0 + 1<<25) >> 26
  861. h1 += carry[0]
  862. h0 -= carry[0] << 26
  863. carry[2] = (h2 + 1<<25) >> 26
  864. h3 += carry[2]
  865. h2 -= carry[2] << 26
  866. carry[4] = (h4 + 1<<25) >> 26
  867. h5 += carry[4]
  868. h4 -= carry[4] << 26
  869. carry[6] = (h6 + 1<<25) >> 26
  870. h7 += carry[6]
  871. h6 -= carry[6] << 26
  872. carry[8] = (h8 + 1<<25) >> 26
  873. h9 += carry[8]
  874. h8 -= carry[8] << 26
  875. p.Y[0] = int32(h0)
  876. p.Y[1] = int32(h1)
  877. p.Y[2] = int32(h2)
  878. p.Y[3] = int32(h3)
  879. p.Y[4] = int32(h4)
  880. p.Y[5] = int32(h5)
  881. p.Y[6] = int32(h6)
  882. p.Y[7] = int32(h7)
  883. p.Y[8] = int32(h8)
  884. p.Y[9] = int32(h9)
  885. // Finished: FeFromBytes
  886. p.Z.One() // = FeOne(&p.Z)
  887. FeSquare(&u, &p.Y)
  888. FeMul(&v, &u, &edD)
  889. FeSub(&u, &u, &p.Z) // y = y^2-1
  890. FeAdd(&v, &v, &p.Z) // v = dy^2+1
  891. FeDivPowM1(&p.X, &u, &v) // x = uv^3(uv^7)^((q-5)/8)
  892. FeSquare(&vxx, &p.X)
  893. FeMul(&vxx, &vxx, &v)
  894. FeSub(&check, &vxx, &u) // vx^2-u
  895. if check.IsNonZero() == 1 {
  896. FeAdd(&check, &vxx, &u) // vx^2+u
  897. if check.IsNonZero() == 1 {
  898. return false
  899. }
  900. FeMul(&p.X, &p.X, &edSqrtM1)
  901. }
  902. if p.X.IsNegative() != (s[31] >> 7) {
  903. // If x = 0, the sign must be positive
  904. if p.X.IsNonZero() == 0 {
  905. return false
  906. }
  907. FeNeg(&p.X, &p.X)
  908. }
  909. FeMul(&p.T, &p.X, &p.Y)
  910. return true
  911. }
  912. func (p *CompletedGroupElement) ToProjective(r *ProjectiveGroupElement) {
  913. FeMul(&r.X, &p.X, &p.T)
  914. FeMul(&r.Y, &p.Y, &p.Z)
  915. FeMul(&r.Z, &p.Z, &p.T)
  916. }
  917. func (p *CompletedGroupElement) ToExtended(r *ExtendedGroupElement) {
  918. FeMul(&r.X, &p.X, &p.T)
  919. FeMul(&r.Y, &p.Y, &p.Z)
  920. FeMul(&r.Z, &p.Z, &p.T)
  921. FeMul(&r.T, &p.X, &p.Y)
  922. }
  923. func (p *PreComputedGroupElement) Zero() {
  924. p.yPlusX.One()
  925. p.yMinusX.One()
  926. p.xy2d.Zero()
  927. }
  928. func (c *CachedGroupElement) Zero() {
  929. c.yPlusX.One()
  930. c.yMinusX.One()
  931. c.Z.One()
  932. c.T2d.Zero()
  933. }
  934. func geAdd(r *CompletedGroupElement, p *ExtendedGroupElement, q *CachedGroupElement) {
  935. var t0 FieldElement
  936. FeAdd(&r.X, &p.Y, &p.X)
  937. FeSub(&r.Y, &p.Y, &p.X)
  938. FeMul(&r.Z, &r.X, &q.yPlusX)
  939. FeMul(&r.Y, &r.Y, &q.yMinusX)
  940. FeMul(&r.T, &q.T2d, &p.T)
  941. FeMul(&r.X, &p.Z, &q.Z)
  942. FeAdd(&t0, &r.X, &r.X)
  943. FeSub(&r.X, &r.Z, &r.Y)
  944. FeAdd(&r.Y, &r.Z, &r.Y)
  945. FeAdd(&r.Z, &t0, &r.T)
  946. FeSub(&r.T, &t0, &r.T)
  947. }
  948. func geSub(r *CompletedGroupElement, p *ExtendedGroupElement, q *CachedGroupElement) {
  949. var t0 FieldElement
  950. FeAdd(&r.X, &p.Y, &p.X)
  951. FeSub(&r.Y, &p.Y, &p.X)
  952. FeMul(&r.Z, &r.X, &q.yMinusX)
  953. FeMul(&r.Y, &r.Y, &q.yPlusX)
  954. FeMul(&r.T, &q.T2d, &p.T)
  955. FeMul(&r.X, &p.Z, &q.Z)
  956. FeAdd(&t0, &r.X, &r.X)
  957. FeSub(&r.X, &r.Z, &r.Y)
  958. FeAdd(&r.Y, &r.Z, &r.Y)
  959. FeSub(&r.Z, &t0, &r.T)
  960. FeAdd(&r.T, &t0, &r.T)
  961. }
  962. func geMixedAdd(r *CompletedGroupElement, p *ExtendedGroupElement, q *PreComputedGroupElement) {
  963. var t0 FieldElement
  964. FeAdd(&r.X, &p.Y, &p.X)
  965. FeSub(&r.Y, &p.Y, &p.X)
  966. FeMul(&r.Z, &r.X, &q.yPlusX)
  967. FeMul(&r.Y, &r.Y, &q.yMinusX)
  968. FeMul(&r.T, &q.xy2d, &p.T)
  969. FeAdd(&t0, &p.Z, &p.Z)
  970. FeSub(&r.X, &r.Z, &r.Y)
  971. FeAdd(&r.Y, &r.Z, &r.Y)
  972. FeAdd(&r.Z, &t0, &r.T)
  973. FeSub(&r.T, &t0, &r.T)
  974. }
  975. func geMixedSub(r *CompletedGroupElement, p *ExtendedGroupElement, q *PreComputedGroupElement) {
  976. var t0 FieldElement
  977. FeAdd(&r.X, &p.Y, &p.X)
  978. FeSub(&r.Y, &p.Y, &p.X)
  979. FeMul(&r.Z, &r.X, &q.yMinusX)
  980. FeMul(&r.Y, &r.Y, &q.yPlusX)
  981. FeMul(&r.T, &q.xy2d, &p.T)
  982. FeAdd(&t0, &p.Z, &p.Z)
  983. FeSub(&r.X, &r.Z, &r.Y)
  984. FeAdd(&r.Y, &r.Z, &r.Y)
  985. FeSub(&r.Z, &t0, &r.T)
  986. FeAdd(&r.T, &t0, &r.T)
  987. }
  988. // r = 8 * t
  989. func GeMul8(r *CompletedGroupElement, t *ProjectiveGroupElement) {
  990. var u ProjectiveGroupElement
  991. t.Double(r)
  992. r.ToProjective(&u)
  993. u.Double(r)
  994. r.ToProjective(&u)
  995. u.Double(r)
  996. }
  997. // caches s into an array of CachedGroupElements for scalar multiplication later
  998. func GePrecompute(r *[8]CachedGroupElement, s *ExtendedGroupElement) {
  999. var t CompletedGroupElement
  1000. var s2, u ExtendedGroupElement
  1001. s.ToCached(&r[0])
  1002. s.Double(&t)
  1003. t.ToExtended(&s2)
  1004. for i := 0; i < 7; i++ {
  1005. geAdd(&t, &s2, &r[i])
  1006. t.ToExtended(&u)
  1007. u.ToCached(&r[i+1])
  1008. }
  1009. }
  1010. func slide(r *[256]int8, a *Key) {
  1011. for i := range r {
  1012. r[i] = int8(1 & (a[i>>3] >> uint(i&7)))
  1013. }
  1014. for i := range r {
  1015. if r[i] != 0 {
  1016. for b := 1; b <= 6 && i+b < 256; b++ {
  1017. if r[i+b] != 0 {
  1018. if r[i]+(r[i+b]<<uint(b)) <= 15 {
  1019. r[i] += r[i+b] << uint(b)
  1020. r[i+b] = 0
  1021. } else if r[i]-(r[i+b]<<uint(b)) >= -15 {
  1022. r[i] -= r[i+b] << uint(b)
  1023. for k := i + b; k < 256; k++ {
  1024. if r[k] == 0 {
  1025. r[k] = 1
  1026. break
  1027. }
  1028. r[k] = 0
  1029. }
  1030. } else {
  1031. break
  1032. }
  1033. }
  1034. }
  1035. }
  1036. }
  1037. }
  1038. // GeDoubleScalarMultVartime sets r = a*A + b*B
  1039. // where a = a[0]+256*a[1]+...+256^31 a[31].
  1040. // and b = b[0]+256*b[1]+...+256^31 b[31].
  1041. // B is the Ed25519 base point (x,4/5) with x positive.
  1042. func GeDoubleScalarMultVartime(r *ProjectiveGroupElement, a *Key, A *ExtendedGroupElement, b *Key) {
  1043. var aSlide, bSlide [256]int8
  1044. var Ai [8]CachedGroupElement // A,3A,5A,7A,9A,11A,13A,15A
  1045. var t CompletedGroupElement
  1046. var u ExtendedGroupElement
  1047. var i int
  1048. slide(&aSlide, a)
  1049. slide(&bSlide, b)
  1050. GePrecompute(&Ai, A)
  1051. r.Zero()
  1052. for i = 255; i >= 0; i-- {
  1053. if aSlide[i] != 0 || bSlide[i] != 0 {
  1054. break
  1055. }
  1056. }
  1057. for ; i >= 0; i-- {
  1058. r.Double(&t)
  1059. if aSlide[i] > 0 {
  1060. t.ToExtended(&u)
  1061. geAdd(&t, &u, &Ai[aSlide[i]/2])
  1062. } else if aSlide[i] < 0 {
  1063. t.ToExtended(&u)
  1064. geSub(&t, &u, &Ai[(-aSlide[i])/2])
  1065. }
  1066. if bSlide[i] > 0 {
  1067. t.ToExtended(&u)
  1068. geMixedAdd(&t, &u, &bi[bSlide[i]/2])
  1069. } else if bSlide[i] < 0 {
  1070. t.ToExtended(&u)
  1071. geMixedSub(&t, &u, &bi[(-bSlide[i])/2])
  1072. }
  1073. t.ToProjective(r)
  1074. }
  1075. }
  1076. // sets r = a*A + b*B
  1077. // where Bi is the [8]CachedGroupElement consisting of
  1078. // B,3B,5B,7B,9B,11B,13B,15B
  1079. func GeDoubleScalarMultPrecompVartime(r *ProjectiveGroupElement, a *Key, A *ExtendedGroupElement, b *Key, Bi *[8]CachedGroupElement) {
  1080. var aSlide, bSlide [256]int8
  1081. var Ai [8]CachedGroupElement // A,3A,5A,7A,9A,11A,13A,15A
  1082. var t CompletedGroupElement
  1083. var u ExtendedGroupElement
  1084. var i int
  1085. slide(&aSlide, a)
  1086. slide(&bSlide, b)
  1087. GePrecompute(&Ai, A)
  1088. r.Zero()
  1089. for i = 255; i >= 0; i-- {
  1090. if aSlide[i] != 0 || bSlide[i] != 0 {
  1091. break
  1092. }
  1093. }
  1094. for ; i >= 0; i-- {
  1095. r.Double(&t)
  1096. if aSlide[i] > 0 {
  1097. t.ToExtended(&u)
  1098. geAdd(&t, &u, &Ai[aSlide[i]/2])
  1099. } else if aSlide[i] < 0 {
  1100. t.ToExtended(&u)
  1101. geSub(&t, &u, &Ai[(-aSlide[i])/2])
  1102. }
  1103. if bSlide[i] > 0 {
  1104. t.ToExtended(&u)
  1105. geAdd(&t, &u, &Bi[bSlide[i]/2])
  1106. } else if bSlide[i] < 0 {
  1107. t.ToExtended(&u)
  1108. geSub(&t, &u, &Bi[(-bSlide[i])/2])
  1109. }
  1110. t.ToProjective(r)
  1111. }
  1112. return
  1113. }
  1114. // equal returns 1 if b == c and 0 otherwise.
  1115. func equal(b, c int32) int32 {
  1116. x := uint32(b ^ c)
  1117. x--
  1118. return int32(x >> 31)
  1119. }
  1120. // negative returns 1 if b < 0 and 0 otherwise.
  1121. func negative(b int32) int32 {
  1122. return (b >> 31) & 1
  1123. }
  1124. func CachedGroupElementCMove(t, u *CachedGroupElement, b int32) {
  1125. FeCMove(&t.yPlusX, &u.yPlusX, b)
  1126. FeCMove(&t.yMinusX, &u.yMinusX, b)
  1127. FeCMove(&t.Z, &u.Z, b)
  1128. FeCMove(&t.T2d, &u.T2d, b)
  1129. }
  1130. func PreComputedGroupElementCMove(t, u *PreComputedGroupElement, b int32) {
  1131. FeCMove(&t.yPlusX, &u.yPlusX, b)
  1132. FeCMove(&t.yMinusX, &u.yMinusX, b)
  1133. FeCMove(&t.xy2d, &u.xy2d, b)
  1134. }
  1135. func selectPoint(t *PreComputedGroupElement, pos int32, b int32) {
  1136. var minusT PreComputedGroupElement
  1137. bNegative := negative(b)
  1138. bAbs := b - (((-bNegative) & b) << 1)
  1139. t.Zero()
  1140. for i := int32(0); i < 8; i++ {
  1141. PreComputedGroupElementCMove(t, &base[pos][i], equal(bAbs, i+1))
  1142. }
  1143. FeCopy(&minusT.yPlusX, &t.yMinusX)
  1144. FeCopy(&minusT.yMinusX, &t.yPlusX)
  1145. FeNeg(&minusT.xy2d, &t.xy2d)
  1146. PreComputedGroupElementCMove(t, &minusT, bNegative)
  1147. }
  1148. // GeScalarMult computes h = a*A, where
  1149. // a = a[0]+256*a[1]+...+256^31 a[31]
  1150. // A is a point on the curve
  1151. //
  1152. // Preconditions:
  1153. // a[31] <= 127
  1154. func GeScalarMult(r *ProjectiveGroupElement, a *Key, A *ExtendedGroupElement) {
  1155. var e [64]int32
  1156. var carry, carry2 int32
  1157. for i := 0; i < 31; i++ {
  1158. carry += int32(a[i]) /* 0..256 */
  1159. carry2 = (carry + 8) >> 4 /* 0..16 */
  1160. e[2*i] = carry - (carry2 << 4) /* -8..7 */
  1161. carry = (carry2 + 8) >> 4 /* 0..1 */
  1162. e[2*i+1] = carry2 - (carry << 4) /* -8..7 */
  1163. }
  1164. carry += int32(a[31]) /* 0..128 */
  1165. carry2 = (carry + 8) >> 4 /* 0..8 */
  1166. e[62] = carry - (carry2 << 4) /* -8..7 */
  1167. e[63] = carry2 /* 0..8 */
  1168. var Ai [8]CachedGroupElement // A,2A,3A,4A,5A,6A,7A,8A
  1169. t := new(CompletedGroupElement)
  1170. u := new(ExtendedGroupElement)
  1171. A.ToCached(&Ai[0])
  1172. for i := 0; i < 7; i++ {
  1173. geAdd(t, A, &Ai[i])
  1174. t.ToExtended(u)
  1175. u.ToCached(&Ai[i+1])
  1176. }
  1177. r.Zero()
  1178. cur := new(CachedGroupElement)
  1179. minusCur := new(CachedGroupElement)
  1180. for i := 63; i >= 0; i-- {
  1181. b := e[i]
  1182. bNegative := negative(b)
  1183. bAbs := b - (((-bNegative) & b) << 1)
  1184. r.Double(t)
  1185. t.ToProjective(r)
  1186. r.Double(t)
  1187. t.ToProjective(r)
  1188. r.Double(t)
  1189. t.ToProjective(r)
  1190. r.Double(t)
  1191. t.ToExtended(u)
  1192. cur.Zero()
  1193. for j := int32(0); j < 8; j++ {
  1194. CachedGroupElementCMove(cur, &Ai[j], equal(bAbs, j+1))
  1195. }
  1196. FeCopy(&minusCur.yPlusX, &cur.yMinusX)
  1197. FeCopy(&minusCur.yMinusX, &cur.yPlusX)
  1198. FeCopy(&minusCur.Z, &cur.Z)
  1199. FeNeg(&minusCur.T2d, &cur.T2d)
  1200. CachedGroupElementCMove(cur, minusCur, bNegative)
  1201. geAdd(t, u, cur)
  1202. t.ToProjective(r)
  1203. }
  1204. }
  1205. // GeScalarMultBase computes h = a*B, where
  1206. // a = a[0]+256*a[1]+...+256^31 a[31]
  1207. // B is the Ed25519 base point (x,4/5) with x positive.
  1208. //
  1209. // Preconditions:
  1210. // a[31] <= 127
  1211. func GeScalarMultBase(h *ExtendedGroupElement, a *Key) {
  1212. var e [64]int8
  1213. for i, v := range a {
  1214. e[2*i] = int8(v & 15)
  1215. e[2*i+1] = int8((v >> 4) & 15)
  1216. }
  1217. // each e[i] is between 0 and 15 and e[63] is between 0 and 7.
  1218. carry := int8(0)
  1219. for i := 0; i < 63; i++ {
  1220. e[i] += carry
  1221. carry = (e[i] + 8) >> 4
  1222. e[i] -= carry << 4
  1223. }
  1224. e[63] += carry
  1225. // each e[i] is between -8 and 8.
  1226. h.Zero()
  1227. var t PreComputedGroupElement
  1228. var r CompletedGroupElement
  1229. for i := int32(1); i < 64; i += 2 {
  1230. selectPoint(&t, i/2, int32(e[i]))
  1231. geMixedAdd(&r, h, &t)
  1232. r.ToExtended(h)
  1233. }
  1234. var s ProjectiveGroupElement
  1235. h.Double(&r)
  1236. r.ToProjective(&s)
  1237. s.Double(&r)
  1238. r.ToProjective(&s)
  1239. s.Double(&r)
  1240. r.ToProjective(&s)
  1241. s.Double(&r)
  1242. r.ToExtended(h)
  1243. for i := int32(0); i < 64; i += 2 {
  1244. selectPoint(&t, i/2, int32(e[i]))
  1245. geMixedAdd(&r, h, &t)
  1246. r.ToExtended(h)
  1247. }
  1248. }
  1249. func ScAdd(s, a, b *Key) {
  1250. a0 := 2097151 & load3(a[:])
  1251. a1 := 2097151 & (load4(a[2:]) >> 5)
  1252. a2 := 2097151 & (load3(a[5:]) >> 2)
  1253. a3 := 2097151 & (load4(a[7:]) >> 7)
  1254. a4 := 2097151 & (load4(a[10:]) >> 4)
  1255. a5 := 2097151 & (load3(a[13:]) >> 1)
  1256. a6 := 2097151 & (load4(a[15:]) >> 6)
  1257. a7 := 2097151 & (load3(a[18:]) >> 3)
  1258. a8 := 2097151 & load3(a[21:])
  1259. a9 := 2097151 & (load4(a[23:]) >> 5)
  1260. a10 := 2097151 & (load3(a[26:]) >> 2)
  1261. a11 := (load4(a[28:]) >> 7)
  1262. b0 := 2097151 & load3(b[:])
  1263. b1 := 2097151 & (load4(b[2:]) >> 5)
  1264. b2 := 2097151 & (load3(b[5:]) >> 2)
  1265. b3 := 2097151 & (load4(b[7:]) >> 7)
  1266. b4 := 2097151 & (load4(b[10:]) >> 4)
  1267. b5 := 2097151 & (load3(b[13:]) >> 1)
  1268. b6 := 2097151 & (load4(b[15:]) >> 6)
  1269. b7 := 2097151 & (load3(b[18:]) >> 3)
  1270. b8 := 2097151 & load3(b[21:])
  1271. b9 := 2097151 & (load4(b[23:]) >> 5)
  1272. b10 := 2097151 & (load3(b[26:]) >> 2)
  1273. b11 := (load4(b[28:]) >> 7)
  1274. s0 := a0 + b0
  1275. s1 := a1 + b1
  1276. s2 := a2 + b2
  1277. s3 := a3 + b3
  1278. s4 := a4 + b4
  1279. s5 := a5 + b5
  1280. s6 := a6 + b6
  1281. s7 := a7 + b7
  1282. s8 := a8 + b8
  1283. s9 := a9 + b9
  1284. s10 := a10 + b10
  1285. s11 := a11 + b11
  1286. s12 := int64(0)
  1287. var carry [12]int64
  1288. carry[0] = (s0 + (1 << 20)) >> 21
  1289. s1 += carry[0]
  1290. s0 -= carry[0] << 21
  1291. carry[2] = (s2 + (1 << 20)) >> 21
  1292. s3 += carry[2]
  1293. s2 -= carry[2] << 21
  1294. carry[4] = (s4 + (1 << 20)) >> 21
  1295. s5 += carry[4]
  1296. s4 -= carry[4] << 21
  1297. carry[6] = (s6 + (1 << 20)) >> 21
  1298. s7 += carry[6]
  1299. s6 -= carry[6] << 21
  1300. carry[8] = (s8 + (1 << 20)) >> 21
  1301. s9 += carry[8]
  1302. s8 -= carry[8] << 21
  1303. carry[10] = (s10 + (1 << 20)) >> 21
  1304. s11 += carry[10]
  1305. s10 -= carry[10] << 21
  1306. carry[1] = (s1 + (1 << 20)) >> 21
  1307. s2 += carry[1]
  1308. s1 -= carry[1] << 21
  1309. carry[3] = (s3 + (1 << 20)) >> 21
  1310. s4 += carry[3]
  1311. s3 -= carry[3] << 21
  1312. carry[5] = (s5 + (1 << 20)) >> 21
  1313. s6 += carry[5]
  1314. s5 -= carry[5] << 21
  1315. carry[7] = (s7 + (1 << 20)) >> 21
  1316. s8 += carry[7]
  1317. s7 -= carry[7] << 21
  1318. carry[9] = (s9 + (1 << 20)) >> 21
  1319. s10 += carry[9]
  1320. s9 -= carry[9] << 21
  1321. carry[11] = (s11 + (1 << 20)) >> 21
  1322. s12 += carry[11]
  1323. s11 -= carry[11] << 21
  1324. s0 += s12 * 666643
  1325. s1 += s12 * 470296
  1326. s2 += s12 * 654183
  1327. s3 -= s12 * 997805
  1328. s4 += s12 * 136657
  1329. s5 -= s12 * 683901
  1330. s12 = 0
  1331. carry[0] = s0 >> 21
  1332. s1 += carry[0]
  1333. s0 -= carry[0] << 21
  1334. carry[1] = s1 >> 21
  1335. s2 += carry[1]
  1336. s1 -= carry[1] << 21
  1337. carry[2] = s2 >> 21
  1338. s3 += carry[2]
  1339. s2 -= carry[2] << 21
  1340. carry[3] = s3 >> 21
  1341. s4 += carry[3]
  1342. s3 -= carry[3] << 21
  1343. carry[4] = s4 >> 21
  1344. s5 += carry[4]
  1345. s4 -= carry[4] << 21
  1346. carry[5] = s5 >> 21
  1347. s6 += carry[5]
  1348. s5 -= carry[5] << 21
  1349. carry[6] = s6 >> 21
  1350. s7 += carry[6]
  1351. s6 -= carry[6] << 21
  1352. carry[7] = s7 >> 21
  1353. s8 += carry[7]
  1354. s7 -= carry[7] << 21
  1355. carry[8] = s8 >> 21
  1356. s9 += carry[8]
  1357. s8 -= carry[8] << 21
  1358. carry[9] = s9 >> 21
  1359. s10 += carry[9]
  1360. s9 -= carry[9] << 21
  1361. carry[10] = s10 >> 21
  1362. s11 += carry[10]
  1363. s10 -= carry[10] << 21
  1364. carry[11] = s11 >> 21
  1365. s12 += carry[11]
  1366. s11 -= carry[11] << 21
  1367. s0 += s12 * 666643
  1368. s1 += s12 * 470296
  1369. s2 += s12 * 654183
  1370. s3 -= s12 * 997805
  1371. s4 += s12 * 136657
  1372. s5 -= s12 * 683901
  1373. carry[0] = s0 >> 21
  1374. s1 += carry[0]
  1375. s0 -= carry[0] << 21
  1376. carry[1] = s1 >> 21
  1377. s2 += carry[1]
  1378. s1 -= carry[1] << 21
  1379. carry[2] = s2 >> 21
  1380. s3 += carry[2]
  1381. s2 -= carry[2] << 21
  1382. carry[3] = s3 >> 21
  1383. s4 += carry[3]
  1384. s3 -= carry[3] << 21
  1385. carry[4] = s4 >> 21
  1386. s5 += carry[4]
  1387. s4 -= carry[4] << 21
  1388. carry[5] = s5 >> 21
  1389. s6 += carry[5]
  1390. s5 -= carry[5] << 21
  1391. carry[6] = s6 >> 21
  1392. s7 += carry[6]
  1393. s6 -= carry[6] << 21
  1394. carry[7] = s7 >> 21
  1395. s8 += carry[7]
  1396. s7 -= carry[7] << 21
  1397. carry[8] = s8 >> 21
  1398. s9 += carry[8]
  1399. s8 -= carry[8] << 21
  1400. carry[9] = s9 >> 21
  1401. s10 += carry[9]
  1402. s9 -= carry[9] << 21
  1403. carry[10] = s10 >> 21
  1404. s11 += carry[10]
  1405. s10 -= carry[10] << 21
  1406. s[0] = byte(s0 >> 0)
  1407. s[1] = byte(s0 >> 8)
  1408. s[2] = byte((s0 >> 16) | (s1 << 5))
  1409. s[3] = byte(s1 >> 3)
  1410. s[4] = byte(s1 >> 11)
  1411. s[5] = byte((s1 >> 19) | (s2 << 2))
  1412. s[6] = byte(s2 >> 6)
  1413. s[7] = byte((s2 >> 14) | (s3 << 7))
  1414. s[8] = byte(s3 >> 1)
  1415. s[9] = byte(s3 >> 9)
  1416. s[10] = byte((s3 >> 17) | (s4 << 4))
  1417. s[11] = byte(s4 >> 4)
  1418. s[12] = byte(s4 >> 12)
  1419. s[13] = byte((s4 >> 20) | (s5 << 1))
  1420. s[14] = byte(s5 >> 7)
  1421. s[15] = byte((s5 >> 15) | (s6 << 6))
  1422. s[16] = byte(s6 >> 2)
  1423. s[17] = byte(s6 >> 10)
  1424. s[18] = byte((s6 >> 18) | (s7 << 3))
  1425. s[19] = byte(s7 >> 5)
  1426. s[20] = byte(s7 >> 13)
  1427. s[21] = byte(s8 >> 0)
  1428. s[22] = byte(s8 >> 8)
  1429. s[23] = byte((s8 >> 16) | (s9 << 5))
  1430. s[24] = byte(s9 >> 3)
  1431. s[25] = byte(s9 >> 11)
  1432. s[26] = byte((s9 >> 19) | (s10 << 2))
  1433. s[27] = byte(s10 >> 6)
  1434. s[28] = byte((s10 >> 14) | (s11 << 7))
  1435. s[29] = byte(s11 >> 1)
  1436. s[30] = byte(s11 >> 9)
  1437. s[31] = byte(s11 >> 17)
  1438. }
  1439. func ScSub(s, a, b *Key) {
  1440. a0 := 2097151 & load3(a[:])
  1441. a1 := 2097151 & (load4(a[2:]) >> 5)
  1442. a2 := 2097151 & (load3(a[5:]) >> 2)
  1443. a3 := 2097151 & (load4(a[7:]) >> 7)
  1444. a4 := 2097151 & (load4(a[10:]) >> 4)
  1445. a5 := 2097151 & (load3(a[13:]) >> 1)
  1446. a6 := 2097151 & (load4(a[15:]) >> 6)
  1447. a7 := 2097151 & (load3(a[18:]) >> 3)
  1448. a8 := 2097151 & load3(a[21:])
  1449. a9 := 2097151 & (load4(a[23:]) >> 5)
  1450. a10 := 2097151 & (load3(a[26:]) >> 2)
  1451. a11 := (load4(a[28:]) >> 7)
  1452. b0 := 2097151 & load3(b[:])
  1453. b1 := 2097151 & (load4(b[2:]) >> 5)
  1454. b2 := 2097151 & (load3(b[5:]) >> 2)
  1455. b3 := 2097151 & (load4(b[7:]) >> 7)
  1456. b4 := 2097151 & (load4(b[10:]) >> 4)
  1457. b5 := 2097151 & (load3(b[13:]) >> 1)
  1458. b6 := 2097151 & (load4(b[15:]) >> 6)
  1459. b7 := 2097151 & (load3(b[18:]) >> 3)
  1460. b8 := 2097151 & load3(b[21:])
  1461. b9 := 2097151 & (load4(b[23:]) >> 5)
  1462. b10 := 2097151 & (load3(b[26:]) >> 2)
  1463. b11 := (load4(b[28:]) >> 7)
  1464. s0 := a0 - b0
  1465. s1 := a1 - b1
  1466. s2 := a2 - b2
  1467. s3 := a3 - b3
  1468. s4 := a4 - b4
  1469. s5 := a5 - b5
  1470. s6 := a6 - b6
  1471. s7 := a7 - b7
  1472. s8 := a8 - b8
  1473. s9 := a9 - b9
  1474. s10 := a10 - b10
  1475. s11 := a11 - b11
  1476. s12 := int64(0)
  1477. var carry [12]int64
  1478. carry[0] = (s0 + (1 << 20)) >> 21
  1479. s1 += carry[0]
  1480. s0 -= carry[0] << 21
  1481. carry[2] = (s2 + (1 << 20)) >> 21
  1482. s3 += carry[2]
  1483. s2 -= carry[2] << 21
  1484. carry[4] = (s4 + (1 << 20)) >> 21
  1485. s5 += carry[4]
  1486. s4 -= carry[4] << 21
  1487. carry[6] = (s6 + (1 << 20)) >> 21
  1488. s7 += carry[6]
  1489. s6 -= carry[6] << 21
  1490. carry[8] = (s8 + (1 << 20)) >> 21
  1491. s9 += carry[8]
  1492. s8 -= carry[8] << 21
  1493. carry[10] = (s10 + (1 << 20)) >> 21
  1494. s11 += carry[10]
  1495. s10 -= carry[10] << 21
  1496. carry[1] = (s1 + (1 << 20)) >> 21
  1497. s2 += carry[1]
  1498. s1 -= carry[1] << 21
  1499. carry[3] = (s3 + (1 << 20)) >> 21
  1500. s4 += carry[3]
  1501. s3 -= carry[3] << 21
  1502. carry[5] = (s5 + (1 << 20)) >> 21
  1503. s6 += carry[5]
  1504. s5 -= carry[5] << 21
  1505. carry[7] = (s7 + (1 << 20)) >> 21
  1506. s8 += carry[7]
  1507. s7 -= carry[7] << 21
  1508. carry[9] = (s9 + (1 << 20)) >> 21
  1509. s10 += carry[9]
  1510. s9 -= carry[9] << 21
  1511. carry[11] = (s11 + (1 << 20)) >> 21
  1512. s12 += carry[11]
  1513. s11 -= carry[11] << 21
  1514. s0 += s12 * 666643
  1515. s1 += s12 * 470296
  1516. s2 += s12 * 654183
  1517. s3 -= s12 * 997805
  1518. s4 += s12 * 136657
  1519. s5 -= s12 * 683901
  1520. s12 = 0
  1521. carry[0] = s0 >> 21
  1522. s1 += carry[0]
  1523. s0 -= carry[0] << 21
  1524. carry[1] = s1 >> 21
  1525. s2 += carry[1]
  1526. s1 -= carry[1] << 21
  1527. carry[2] = s2 >> 21
  1528. s3 += carry[2]
  1529. s2 -= carry[2] << 21
  1530. carry[3] = s3 >> 21
  1531. s4 += carry[3]
  1532. s3 -= carry[3] << 21
  1533. carry[4] = s4 >> 21
  1534. s5 += carry[4]
  1535. s4 -= carry[4] << 21
  1536. carry[5] = s5 >> 21
  1537. s6 += carry[5]
  1538. s5 -= carry[5] << 21
  1539. carry[6] = s6 >> 21
  1540. s7 += carry[6]
  1541. s6 -= carry[6] << 21
  1542. carry[7] = s7 >> 21
  1543. s8 += carry[7]
  1544. s7 -= carry[7] << 21
  1545. carry[8] = s8 >> 21
  1546. s9 += carry[8]
  1547. s8 -= carry[8] << 21
  1548. carry[9] = s9 >> 21
  1549. s10 += carry[9]
  1550. s9 -= carry[9] << 21
  1551. carry[10] = s10 >> 21
  1552. s11 += carry[10]
  1553. s10 -= carry[10] << 21
  1554. carry[11] = s11 >> 21
  1555. s12 += carry[11]
  1556. s11 -= carry[11] << 21
  1557. s0 += s12 * 666643
  1558. s1 += s12 * 470296
  1559. s2 += s12 * 654183
  1560. s3 -= s12 * 997805
  1561. s4 += s12 * 136657
  1562. s5 -= s12 * 683901
  1563. carry[0] = s0 >> 21
  1564. s1 += carry[0]
  1565. s0 -= carry[0] << 21
  1566. carry[1] = s1 >> 21
  1567. s2 += carry[1]
  1568. s1 -= carry[1] << 21
  1569. carry[2] = s2 >> 21
  1570. s3 += carry[2]
  1571. s2 -= carry[2] << 21
  1572. carry[3] = s3 >> 21
  1573. s4 += carry[3]
  1574. s3 -= carry[3] << 21
  1575. carry[4] = s4 >> 21
  1576. s5 += carry[4]
  1577. s4 -= carry[4] << 21
  1578. carry[5] = s5 >> 21
  1579. s6 += carry[5]
  1580. s5 -= carry[5] << 21
  1581. carry[6] = s6 >> 21
  1582. s7 += carry[6]
  1583. s6 -= carry[6] << 21
  1584. carry[7] = s7 >> 21
  1585. s8 += carry[7]
  1586. s7 -= carry[7] << 21
  1587. carry[8] = s8 >> 21
  1588. s9 += carry[8]
  1589. s8 -= carry[8] << 21
  1590. carry[9] = s9 >> 21
  1591. s10 += carry[9]
  1592. s9 -= carry[9] << 21
  1593. carry[10] = s10 >> 21
  1594. s11 += carry[10]
  1595. s10 -= carry[10] << 21
  1596. s[0] = byte(s0 >> 0)
  1597. s[1] = byte(s0 >> 8)
  1598. s[2] = byte((s0 >> 16) | (s1 << 5))
  1599. s[3] = byte(s1 >> 3)
  1600. s[4] = byte(s1 >> 11)
  1601. s[5] = byte((s1 >> 19) | (s2 << 2))
  1602. s[6] = byte(s2 >> 6)
  1603. s[7] = byte((s2 >> 14) | (s3 << 7))
  1604. s[8] = byte(s3 >> 1)
  1605. s[9] = byte(s3 >> 9)
  1606. s[10] = byte((s3 >> 17) | (s4 << 4))
  1607. s[11] = byte(s4 >> 4)
  1608. s[12] = byte(s4 >> 12)
  1609. s[13] = byte((s4 >> 20) | (s5 << 1))
  1610. s[14] = byte(s5 >> 7)
  1611. s[15] = byte((s5 >> 15) | (s6 << 6))
  1612. s[16] = byte(s6 >> 2)
  1613. s[17] = byte(s6 >> 10)
  1614. s[18] = byte((s6 >> 18) | (s7 << 3))
  1615. s[19] = byte(s7 >> 5)
  1616. s[20] = byte(s7 >> 13)
  1617. s[21] = byte(s8 >> 0)
  1618. s[22] = byte(s8 >> 8)
  1619. s[23] = byte((s8 >> 16) | (s9 << 5))
  1620. s[24] = byte(s9 >> 3)
  1621. s[25] = byte(s9 >> 11)
  1622. s[26] = byte((s9 >> 19) | (s10 << 2))
  1623. s[27] = byte(s10 >> 6)
  1624. s[28] = byte((s10 >> 14) | (s11 << 7))
  1625. s[29] = byte(s11 >> 1)
  1626. s[30] = byte(s11 >> 9)
  1627. s[31] = byte(s11 >> 17)
  1628. }
  1629. func signum(a int64) int64 {
  1630. return a>>63 - ((-a) >> 63)
  1631. }
  1632. // equivalent to sc_check
  1633. func Sc_check(s *Key) bool {
  1634. return ScValid(s)
  1635. }
  1636. func ScValid(s *Key) bool {
  1637. s0 := load4(s[:])
  1638. s1 := load4(s[4:])
  1639. s2 := load4(s[8:])
  1640. s3 := load4(s[12:])
  1641. s4 := load4(s[16:])
  1642. s5 := load4(s[20:])
  1643. s6 := load4(s[24:])
  1644. s7 := load4(s[28:])
  1645. 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
  1646. }
  1647. func ScIsZero(s *Key) bool {
  1648. return ((int(s[0]|s[1]|s[2]|s[3]|s[4]|s[5]|s[6]|s[7]|s[8]|
  1649. s[9]|s[10]|s[11]|s[12]|s[13]|s[14]|s[15]|s[16]|s[17]|
  1650. s[18]|s[19]|s[20]|s[21]|s[22]|s[23]|s[24]|s[25]|s[26]|
  1651. s[27]|s[28]|s[29]|s[30]|s[31])-1)>>8)+1 == 0
  1652. }
  1653. // The scalars are GF(2^252 + 27742317777372353535851937790883648493).
  1654. // Input:
  1655. // a[0]+256*a[1]+...+256^31*a[31] = a
  1656. // b[0]+256*b[1]+...+256^31*b[31] = b
  1657. // c[0]+256*c[1]+...+256^31*c[31] = c
  1658. //
  1659. // Output:
  1660. // s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l
  1661. // where l = 2^252 + 27742317777372353535851937790883648493.
  1662. func ScMulAdd(s, a, b, c *Key) {
  1663. a0 := 2097151 & load3(a[:])
  1664. a1 := 2097151 & (load4(a[2:]) >> 5)
  1665. a2 := 2097151 & (load3(a[5:]) >> 2)
  1666. a3 := 2097151 & (load4(a[7:]) >> 7)
  1667. a4 := 2097151 & (load4(a[10:]) >> 4)
  1668. a5 := 2097151 & (load3(a[13:]) >> 1)
  1669. a6 := 2097151 & (load4(a[15:]) >> 6)
  1670. a7 := 2097151 & (load3(a[18:]) >> 3)
  1671. a8 := 2097151 & load3(a[21:])
  1672. a9 := 2097151 & (load4(a[23:]) >> 5)
  1673. a10 := 2097151 & (load3(a[26:]) >> 2)
  1674. a11 := (load4(a[28:]) >> 7)
  1675. b0 := 2097151 & load3(b[:])
  1676. b1 := 2097151 & (load4(b[2:]) >> 5)
  1677. b2 := 2097151 & (load3(b[5:]) >> 2)
  1678. b3 := 2097151 & (load4(b[7:]) >> 7)
  1679. b4 := 2097151 & (load4(b[10:]) >> 4)
  1680. b5 := 2097151 & (load3(b[13:]) >> 1)
  1681. b6 := 2097151 & (load4(b[15:]) >> 6)
  1682. b7 := 2097151 & (load3(b[18:]) >> 3)
  1683. b8 := 2097151 & load3(b[21:])
  1684. b9 := 2097151 & (load4(b[23:]) >> 5)
  1685. b10 := 2097151 & (load3(b[26:]) >> 2)
  1686. b11 := (load4(b[28:]) >> 7)
  1687. c0 := 2097151 & load3(c[:])
  1688. c1 := 2097151 & (load4(c[2:]) >> 5)
  1689. c2 := 2097151 & (load3(c[5:]) >> 2)
  1690. c3 := 2097151 & (load4(c[7:]) >> 7)
  1691. c4 := 2097151 & (load4(c[10:]) >> 4)
  1692. c5 := 2097151 & (load3(c[13:]) >> 1)
  1693. c6 := 2097151 & (load4(c[15:]) >> 6)
  1694. c7 := 2097151 & (load3(c[18:]) >> 3)
  1695. c8 := 2097151 & load3(c[21:])
  1696. c9 := 2097151 & (load4(c[23:]) >> 5)
  1697. c10 := 2097151 & (load3(c[26:]) >> 2)
  1698. c11 := (load4(c[28:]) >> 7)
  1699. var carry [23]int64
  1700. s0 := c0 + a0*b0
  1701. s1 := c1 + a0*b1 + a1*b0
  1702. s2 := c2 + a0*b2 + a1*b1 + a2*b0
  1703. s3 := c3 + a0*b3 + a1*b2 + a2*b1 + a3*b0
  1704. s4 := c4 + a0*b4 + a1*b3 + a2*b2 + a3*b1 + a4*b0
  1705. s5 := c5 + a0*b5 + a1*b4 + a2*b3 + a3*b2 + a4*b1 + a5*b0
  1706. s6 := c6 + a0*b6 + a1*b5 + a2*b4 + a3*b3 + a4*b2 + a5*b1 + a6*b0
  1707. s7 := c7 + a0*b7 + a1*b6 + a2*b5 + a3*b4 + a4*b3 + a5*b2 + a6*b1 + a7*b0
  1708. s8 := c8 + a0*b8 + a1*b7 + a2*b6 + a3*b5 + a4*b4 + a5*b3 + a6*b2 + a7*b1 + a8*b0
  1709. s9 := c9 + a0*b9 + a1*b8 + a2*b7 + a3*b6 + a4*b5 + a5*b4 + a6*b3 + a7*b2 + a8*b1 + a9*b0
  1710. s10 := c10 + a0*b10 + a1*b9 + a2*b8 + a3*b7 + a4*b6 + a5*b5 + a6*b4 + a7*b3 + a8*b2 + a9*b1 + a10*b0
  1711. 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
  1712. s12 := a1*b11 + a2*b10 + a3*b9 + a4*b8 + a5*b7 + a6*b6 + a7*b5 + a8*b4 + a9*b3 + a10*b2 + a11*b1
  1713. s13 := a2*b11 + a3*b10 + a4*b9 + a5*b8 + a6*b7 + a7*b6 + a8*b5 + a9*b4 + a10*b3 + a11*b2
  1714. s14 := a3*b11 + a4*b10 + a5*b9 + a6*b8 + a7*b7 + a8*b6 + a9*b5 + a10*b4 + a11*b3
  1715. s15 := a4*b11 + a5*b10 + a6*b9 + a7*b8 + a8*b7 + a9*b6 + a10*b5 + a11*b4
  1716. s16 := a5*b11 + a6*b10 + a7*b9 + a8*b8 + a9*b7 + a10*b6 + a11*b5
  1717. s17 := a6*b11 + a7*b10 + a8*b9 + a9*b8 + a10*b7 + a11*b6
  1718. s18 := a7*b11 + a8*b10 + a9*b9 + a10*b8 + a11*b7
  1719. s19 := a8*b11 + a9*b10 + a10*b9 + a11*b8
  1720. s20 := a9*b11 + a10*b10 + a11*b9
  1721. s21 := a10*b11 + a11*b10
  1722. s22 := a11 * b11
  1723. s23 := int64(0)
  1724. carry[0] = (s0 + (1 << 20)) >> 21
  1725. s1 += carry[0]
  1726. s0 -= carry[0] << 21
  1727. carry[2] = (s2 + (1 << 20)) >> 21
  1728. s3 += carry[2]
  1729. s2 -= carry[2] << 21
  1730. carry[4] = (s4 + (1 << 20)) >> 21
  1731. s5 += carry[4]
  1732. s4 -= carry[4] << 21
  1733. carry[6] = (s6 + (1 << 20)) >> 21
  1734. s7 += carry[6]
  1735. s6 -= carry[6] << 21
  1736. carry[8] = (s8 + (1 << 20)) >> 21
  1737. s9 += carry[8]
  1738. s8 -= carry[8] << 21
  1739. carry[10] = (s10 + (1 << 20)) >> 21
  1740. s11 += carry[10]
  1741. s10 -= carry[10] << 21
  1742. carry[12] = (s12 + (1 << 20)) >> 21
  1743. s13 += carry[12]
  1744. s12 -= carry[12] << 21
  1745. carry[14] = (s14 + (1 << 20)) >> 21
  1746. s15 += carry[14]
  1747. s14 -= carry[14] << 21
  1748. carry[16] = (s16 + (1 << 20)) >> 21
  1749. s17 += carry[16]
  1750. s16 -= carry[16] << 21
  1751. carry[18] = (s18 + (1 << 20)) >> 21
  1752. s19 += carry[18]
  1753. s18 -= carry[18] << 21
  1754. carry[20] = (s20 + (1 << 20)) >> 21
  1755. s21 += carry[20]
  1756. s20 -= carry[20] << 21
  1757. carry[22] = (s22 + (1 << 20)) >> 21
  1758. s23 += carry[22]
  1759. s22 -= carry[22] << 21
  1760. carry[1] = (s1 + (1 << 20)) >> 21
  1761. s2 += carry[1]
  1762. s1 -= carry[1] << 21
  1763. carry[3] = (s3 + (1 << 20)) >> 21
  1764. s4 += carry[3]
  1765. s3 -= carry[3] << 21
  1766. carry[5] = (s5 + (1 << 20)) >> 21
  1767. s6 += carry[5]
  1768. s5 -= carry[5] << 21
  1769. carry[7] = (s7 + (1 << 20)) >> 21
  1770. s8 += carry[7]
  1771. s7 -= carry[7] << 21
  1772. carry[9] = (s9 + (1 << 20)) >> 21
  1773. s10 += carry[9]
  1774. s9 -= carry[9] << 21
  1775. carry[11] = (s11 + (1 << 20)) >> 21
  1776. s12 += carry[11]
  1777. s11 -= carry[11] << 21
  1778. carry[13] = (s13 + (1 << 20)) >> 21
  1779. s14 += carry[13]
  1780. s13 -= carry[13] << 21
  1781. carry[15] = (s15 + (1 << 20)) >> 21
  1782. s16 += carry[15]
  1783. s15 -= carry[15] << 21
  1784. carry[17] = (s17 + (1 << 20)) >> 21
  1785. s18 += carry[17]
  1786. s17 -= carry[17] << 21
  1787. carry[19] = (s19 + (1 << 20)) >> 21
  1788. s20 += carry[19]
  1789. s19 -= carry[19] << 21
  1790. carry[21] = (s21 + (1 << 20)) >> 21
  1791. s22 += carry[21]
  1792. s21 -= carry[21] << 21
  1793. s11 += s23 * 666643
  1794. s12 += s23 * 470296
  1795. s13 += s23 * 654183
  1796. s14 -= s23 * 997805
  1797. s15 += s23 * 136657
  1798. s16 -= s23 * 683901
  1799. s23 = 0
  1800. s10 += s22 * 666643
  1801. s11 += s22 * 470296
  1802. s12 += s22 * 654183
  1803. s13 -= s22 * 997805
  1804. s14 += s22 * 136657
  1805. s15 -= s22 * 683901
  1806. s22 = 0
  1807. s9 += s21 * 666643
  1808. s10 += s21 * 470296
  1809. s11 += s21 * 654183
  1810. s12 -= s21 * 997805
  1811. s13 += s21 * 136657
  1812. s14 -= s21 * 683901
  1813. s21 = 0
  1814. s8 += s20 * 666643
  1815. s9 += s20 * 470296
  1816. s10 += s20 * 654183
  1817. s11 -= s20 * 997805
  1818. s12 += s20 * 136657
  1819. s13 -= s20 * 683901
  1820. s20 = 0
  1821. s7 += s19 * 666643
  1822. s8 += s19 * 470296
  1823. s9 += s19 * 654183
  1824. s10 -= s19 * 997805
  1825. s11 += s19 * 136657
  1826. s12 -= s19 * 683901
  1827. s19 = 0
  1828. s6 += s18 * 666643
  1829. s7 += s18 * 470296
  1830. s8 += s18 * 654183
  1831. s9 -= s18 * 997805
  1832. s10 += s18 * 136657
  1833. s11 -= s18 * 683901
  1834. s18 = 0
  1835. carry[6] = (s6 + (1 << 20)) >> 21
  1836. s7 += carry[6]
  1837. s6 -= carry[6] << 21
  1838. carry[8] = (s8 + (1 << 20)) >> 21
  1839. s9 += carry[8]
  1840. s8 -= carry[8] << 21
  1841. carry[10] = (s10 + (1 << 20)) >> 21
  1842. s11 += carry[10]
  1843. s10 -= carry[10] << 21
  1844. carry[12] = (s12 + (1 << 20)) >> 21
  1845. s13 += carry[12]
  1846. s12 -= carry[12] << 21
  1847. carry[14] = (s14 + (1 << 20)) >> 21
  1848. s15 += carry[14]
  1849. s14 -= carry[14] << 21
  1850. carry[16] = (s16 + (1 << 20)) >> 21
  1851. s17 += carry[16]
  1852. s16 -= carry[16] << 21
  1853. carry[7] = (s7 + (1 << 20)) >> 21
  1854. s8 += carry[7]
  1855. s7 -= carry[7] << 21
  1856. carry[9] = (s9 + (1 << 20)) >> 21
  1857. s10 += carry[9]
  1858. s9 -= carry[9] << 21
  1859. carry[11] = (s11 + (1 << 20)) >> 21
  1860. s12 += carry[11]
  1861. s11 -= carry[11] << 21
  1862. carry[13] = (s13 + (1 << 20)) >> 21
  1863. s14 += carry[13]
  1864. s13 -= carry[13] << 21
  1865. carry[15] = (s15 + (1 << 20)) >> 21
  1866. s16 += carry[15]
  1867. s15 -= carry[15] << 21
  1868. s5 += s17 * 666643
  1869. s6 += s17 * 470296
  1870. s7 += s17 * 654183
  1871. s8 -= s17 * 997805
  1872. s9 += s17 * 136657
  1873. s10 -= s17 * 683901
  1874. s17 = 0
  1875. s4 += s16 * 666643
  1876. s5 += s16 * 470296
  1877. s6 += s16 * 654183
  1878. s7 -= s16 * 997805
  1879. s8 += s16 * 136657
  1880. s9 -= s16 * 683901
  1881. s16 = 0
  1882. s3 += s15 * 666643
  1883. s4 += s15 * 470296
  1884. s5 += s15 * 654183
  1885. s6 -= s15 * 997805
  1886. s7 += s15 * 136657
  1887. s8 -= s15 * 683901
  1888. s15 = 0
  1889. s2 += s14 * 666643
  1890. s3 += s14 * 470296
  1891. s4 += s14 * 654183
  1892. s5 -= s14 * 997805
  1893. s6 += s14 * 136657
  1894. s7 -= s14 * 683901
  1895. s14 = 0
  1896. s1 += s13 * 666643
  1897. s2 += s13 * 470296
  1898. s3 += s13 * 654183
  1899. s4 -= s13 * 997805
  1900. s5 += s13 * 136657
  1901. s6 -= s13 * 683901
  1902. s13 = 0
  1903. s0 += s12 * 666643
  1904. s1 += s12 * 470296
  1905. s2 += s12 * 654183
  1906. s3 -= s12 * 997805
  1907. s4 += s12 * 136657
  1908. s5 -= s12 * 683901
  1909. s12 = 0
  1910. carry[0] = (s0 + (1 << 20)) >> 21
  1911. s1 += carry[0]
  1912. s0 -= carry[0] << 21
  1913. carry[2] = (s2 + (1 << 20)) >> 21
  1914. s3 += carry[2]
  1915. s2 -= carry[2] << 21
  1916. carry[4] = (s4 + (1 << 20)) >> 21
  1917. s5 += carry[4]
  1918. s4 -= carry[4] << 21
  1919. carry[6] = (s6 + (1 << 20)) >> 21
  1920. s7 += carry[6]
  1921. s6 -= carry[6] << 21
  1922. carry[8] = (s8 + (1 << 20)) >> 21
  1923. s9 += carry[8]
  1924. s8 -= carry[8] << 21
  1925. carry[10] = (s10 + (1 << 20)) >> 21
  1926. s11 += carry[10]
  1927. s10 -= carry[10] << 21
  1928. carry[1] = (s1 + (1 << 20)) >> 21
  1929. s2 += carry[1]
  1930. s1 -= carry[1] << 21
  1931. carry[3] = (s3 + (1 << 20)) >> 21
  1932. s4 += carry[3]
  1933. s3 -= carry[3] << 21
  1934. carry[5] = (s5 + (1 << 20)) >> 21
  1935. s6 += carry[5]
  1936. s5 -= carry[5] << 21
  1937. carry[7] = (s7 + (1 << 20)) >> 21
  1938. s8 += carry[7]
  1939. s7 -= carry[7] << 21
  1940. carry[9] = (s9 + (1 << 20)) >> 21
  1941. s10 += carry[9]
  1942. s9 -= carry[9] << 21
  1943. carry[11] = (s11 + (1 << 20)) >> 21
  1944. s12 += carry[11]
  1945. s11 -= carry[11] << 21
  1946. s0 += s12 * 666643
  1947. s1 += s12 * 470296
  1948. s2 += s12 * 654183
  1949. s3 -= s12 * 997805
  1950. s4 += s12 * 136657
  1951. s5 -= s12 * 683901
  1952. s12 = 0
  1953. carry[0] = s0 >> 21
  1954. s1 += carry[0]
  1955. s0 -= carry[0] << 21
  1956. carry[1] = s1 >> 21
  1957. s2 += carry[1]
  1958. s1 -= carry[1] << 21
  1959. carry[2] = s2 >> 21
  1960. s3 += carry[2]
  1961. s2 -= carry[2] << 21
  1962. carry[3] = s3 >> 21
  1963. s4 += carry[3]
  1964. s3 -= carry[3] << 21
  1965. carry[4] = s4 >> 21
  1966. s5 += carry[4]
  1967. s4 -= carry[4] << 21
  1968. carry[5] = s5 >> 21
  1969. s6 += carry[5]
  1970. s5 -= carry[5] << 21
  1971. carry[6] = s6 >> 21
  1972. s7 += carry[6]
  1973. s6 -= carry[6] << 21
  1974. carry[7] = s7 >> 21
  1975. s8 += carry[7]
  1976. s7 -= carry[7] << 21
  1977. carry[8] = s8 >> 21
  1978. s9 += carry[8]
  1979. s8 -= carry[8] << 21
  1980. carry[9] = s9 >> 21
  1981. s10 += carry[9]
  1982. s9 -= carry[9] << 21
  1983. carry[10] = s10 >> 21
  1984. s11 += carry[10]
  1985. s10 -= carry[10] << 21
  1986. carry[11] = s11 >> 21
  1987. s12 += carry[11]
  1988. s11 -= carry[11] << 21
  1989. s0 += s12 * 666643
  1990. s1 += s12 * 470296
  1991. s2 += s12 * 654183
  1992. s3 -= s12 * 997805
  1993. s4 += s12 * 136657
  1994. s5 -= s12 * 683901
  1995. s12 = 0
  1996. carry[0] = s0 >> 21
  1997. s1 += carry[0]
  1998. s0 -= carry[0] << 21
  1999. carry[1] = s1 >> 21
  2000. s2 += carry[1]
  2001. s1 -= carry[1] << 21
  2002. carry[2] = s2 >> 21
  2003. s3 += carry[2]
  2004. s2 -= carry[2] << 21
  2005. carry[3] = s3 >> 21
  2006. s4 += carry[3]
  2007. s3 -= carry[3] << 21
  2008. carry[4] = s4 >> 21
  2009. s5 += carry[4]
  2010. s4 -= carry[4] << 21
  2011. carry[5] = s5 >> 21
  2012. s6 += carry[5]
  2013. s5 -= carry[5] << 21
  2014. carry[6] = s6 >> 21
  2015. s7 += carry[6]
  2016. s6 -= carry[6] << 21
  2017. carry[7] = s7 >> 21
  2018. s8 += carry[7]
  2019. s7 -= carry[7] << 21
  2020. carry[8] = s8 >> 21
  2021. s9 += carry[8]
  2022. s8 -= carry[8] << 21
  2023. carry[9] = s9 >> 21
  2024. s10 += carry[9]
  2025. s9 -= carry[9] << 21
  2026. carry[10] = s10 >> 21
  2027. s11 += carry[10]
  2028. s10 -= carry[10] << 21
  2029. s[0] = byte(s0 >> 0)
  2030. s[1] = byte(s0 >> 8)
  2031. s[2] = byte((s0 >> 16) | (s1 << 5))
  2032. s[3] = byte(s1 >> 3)
  2033. s[4] = byte(s1 >> 11)
  2034. s[5] = byte((s1 >> 19) | (s2 << 2))
  2035. s[6] = byte(s2 >> 6)
  2036. s[7] = byte((s2 >> 14) | (s3 << 7))
  2037. s[8] = byte(s3 >> 1)
  2038. s[9] = byte(s3 >> 9)
  2039. s[10] = byte((s3 >> 17) | (s4 << 4))
  2040. s[11] = byte(s4 >> 4)
  2041. s[12] = byte(s4 >> 12)
  2042. s[13] = byte((s4 >> 20) | (s5 << 1))
  2043. s[14] = byte(s5 >> 7)
  2044. s[15] = byte((s5 >> 15) | (s6 << 6))
  2045. s[16] = byte(s6 >> 2)
  2046. s[17] = byte(s6 >> 10)
  2047. s[18] = byte((s6 >> 18) | (s7 << 3))
  2048. s[19] = byte(s7 >> 5)
  2049. s[20] = byte(s7 >> 13)
  2050. s[21] = byte(s8 >> 0)
  2051. s[22] = byte(s8 >> 8)
  2052. s[23] = byte((s8 >> 16) | (s9 << 5))
  2053. s[24] = byte(s9 >> 3)
  2054. s[25] = byte(s9 >> 11)
  2055. s[26] = byte((s9 >> 19) | (s10 << 2))
  2056. s[27] = byte(s10 >> 6)
  2057. s[28] = byte((s10 >> 14) | (s11 << 7))
  2058. s[29] = byte(s11 >> 1)
  2059. s[30] = byte(s11 >> 9)
  2060. s[31] = byte(s11 >> 17)
  2061. }
  2062. // Input:
  2063. // a[0]+256*a[1]+...+256^31*a[31] = a
  2064. // b[0]+256*b[1]+...+256^31*b[31] = b
  2065. // c[0]+256*c[1]+...+256^31*c[31] = c
  2066. //
  2067. // Output:
  2068. // s[0]+256*s[1]+...+256^31*s[31] = (c-ab) mod l
  2069. // where l = 2^252 + 27742317777372353535851937790883648493.
  2070. func ScMulSub(s, a, b, c *Key) {
  2071. a0 := 2097151 & load3(a[:])
  2072. a1 := 2097151 & (load4(a[2:]) >> 5)
  2073. a2 := 2097151 & (load3(a[5:]) >> 2)
  2074. a3 := 2097151 & (load4(a[7:]) >> 7)
  2075. a4 := 2097151 & (load4(a[10:]) >> 4)
  2076. a5 := 2097151 & (load3(a[13:]) >> 1)
  2077. a6 := 2097151 & (load4(a[15:]) >> 6)
  2078. a7 := 2097151 & (load3(a[18:]) >> 3)
  2079. a8 := 2097151 & load3(a[21:])
  2080. a9 := 2097151 & (load4(a[23:]) >> 5)
  2081. a10 := 2097151 & (load3(a[26:]) >> 2)
  2082. a11 := (load4(a[28:]) >> 7)
  2083. b0 := 2097151 & load3(b[:])
  2084. b1 := 2097151 & (load4(b[2:]) >> 5)
  2085. b2 := 2097151 & (load3(b[5:]) >> 2)
  2086. b3 := 2097151 & (load4(b[7:]) >> 7)
  2087. b4 := 2097151 & (load4(b[10:]) >> 4)
  2088. b5 := 2097151 & (load3(b[13:]) >> 1)
  2089. b6 := 2097151 & (load4(b[15:]) >> 6)
  2090. b7 := 2097151 & (load3(b[18:]) >> 3)
  2091. b8 := 2097151 & load3(b[21:])
  2092. b9 := 2097151 & (load4(b[23:]) >> 5)
  2093. b10 := 2097151 & (load3(b[26:]) >> 2)
  2094. b11 := (load4(b[28:]) >> 7)
  2095. c0 := 2097151 & load3(c[:])
  2096. c1 := 2097151 & (load4(c[2:]) >> 5)
  2097. c2 := 2097151 & (load3(c[5:]) >> 2)
  2098. c3 := 2097151 & (load4(c[7:]) >> 7)
  2099. c4 := 2097151 & (load4(c[10:]) >> 4)
  2100. c5 := 2097151 & (load3(c[13:]) >> 1)
  2101. c6 := 2097151 & (load4(c[15:]) >> 6)
  2102. c7 := 2097151 & (load3(c[18:]) >> 3)
  2103. c8 := 2097151 & load3(c[21:])
  2104. c9 := 2097151 & (load4(c[23:]) >> 5)
  2105. c10 := 2097151 & (load3(c[26:]) >> 2)
  2106. c11 := (load4(c[28:]) >> 7)
  2107. var carry [23]int64
  2108. s0 := c0 - a0*b0
  2109. s1 := c1 - a0*b1 - a1*b0
  2110. s2 := c2 - a0*b2 - a1*b1 - a2*b0
  2111. s3 := c3 - a0*b3 - a1*b2 - a2*b1 - a3*b0
  2112. s4 := c4 - a0*b4 - a1*b3 - a2*b2 - a3*b1 - a4*b0
  2113. s5 := c5 - a0*b5 - a1*b4 - a2*b3 - a3*b2 - a4*b1 - a5*b0
  2114. s6 := c6 - a0*b6 - a1*b5 - a2*b4 - a3*b3 - a4*b2 - a5*b1 - a6*b0
  2115. s7 := c7 - a0*b7 - a1*b6 - a2*b5 - a3*b4 - a4*b3 - a5*b2 - a6*b1 - a7*b0
  2116. s8 := c8 - a0*b8 - a1*b7 - a2*b6 - a3*b5 - a4*b4 - a5*b3 - a6*b2 - a7*b1 - a8*b0
  2117. s9 := c9 - a0*b9 - a1*b8 - a2*b7 - a3*b6 - a4*b5 - a5*b4 - a6*b3 - a7*b2 - a8*b1 - a9*b0
  2118. s10 := c10 - a0*b10 - a1*b9 - a2*b8 - a3*b7 - a4*b6 - a5*b5 - a6*b4 - a7*b3 - a8*b2 - a9*b1 - a10*b0
  2119. 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
  2120. s12 := -a1*b11 - a2*b10 - a3*b9 - a4*b8 - a5*b7 - a6*b6 - a7*b5 - a8*b4 - a9*b3 - a10*b2 - a11*b1
  2121. s13 := -a2*b11 - a3*b10 - a4*b9 - a5*b8 - a6*b7 - a7*b6 - a8*b5 - a9*b4 - a10*b3 - a11*b2
  2122. s14 := -a3*b11 - a4*b10 - a5*b9 - a6*b8 - a7*b7 - a8*b6 - a9*b5 - a10*b4 - a11*b3
  2123. s15 := -a4*b11 - a5*b10 - a6*b9 - a7*b8 - a8*b7 - a9*b6 - a10*b5 - a11*b4
  2124. s16 := -a5*b11 - a6*b10 - a7*b9 - a8*b8 - a9*b7 - a10*b6 - a11*b5
  2125. s17 := -a6*b11 - a7*b10 - a8*b9 - a9*b8 - a10*b7 - a11*b6
  2126. s18 := -a7*b11 - a8*b10 - a9*b9 - a10*b8 - a11*b7
  2127. s19 := -a8*b11 - a9*b10 - a10*b9 - a11*b8
  2128. s20 := -a9*b11 - a10*b10 - a11*b9
  2129. s21 := -a10*b11 - a11*b10
  2130. s22 := -a11 * b11
  2131. s23 := int64(0)
  2132. carry[0] = (s0 + (1 << 20)) >> 21
  2133. s1 += carry[0]
  2134. s0 -= carry[0] << 21
  2135. carry[2] = (s2 + (1 << 20)) >> 21
  2136. s3 += carry[2]
  2137. s2 -= carry[2] << 21
  2138. carry[4] = (s4 + (1 << 20)) >> 21
  2139. s5 += carry[4]
  2140. s4 -= carry[4] << 21
  2141. carry[6] = (s6 + (1 << 20)) >> 21
  2142. s7 += carry[6]
  2143. s6 -= carry[6] << 21
  2144. carry[8] = (s8 + (1 << 20)) >> 21
  2145. s9 += carry[8]
  2146. s8 -= carry[8] << 21
  2147. carry[10] = (s10 + (1 << 20)) >> 21
  2148. s11 += carry[10]
  2149. s10 -= carry[10] << 21
  2150. carry[12] = (s12 + (1 << 20)) >> 21
  2151. s13 += carry[12]
  2152. s12 -= carry[12] << 21
  2153. carry[14] = (s14 + (1 << 20)) >> 21
  2154. s15 += carry[14]
  2155. s14 -= carry[14] << 21
  2156. carry[16] = (s16 + (1 << 20)) >> 21
  2157. s17 += carry[16]
  2158. s16 -= carry[16] << 21
  2159. carry[18] = (s18 + (1 << 20)) >> 21
  2160. s19 += carry[18]
  2161. s18 -= carry[18] << 21
  2162. carry[20] = (s20 + (1 << 20)) >> 21
  2163. s21 += carry[20]
  2164. s20 -= carry[20] << 21
  2165. carry[22] = (s22 + (1 << 20)) >> 21
  2166. s23 += carry[22]
  2167. s22 -= carry[22] << 21
  2168. carry[1] = (s1 + (1 << 20)) >> 21
  2169. s2 += carry[1]
  2170. s1 -= carry[1] << 21
  2171. carry[3] = (s3 + (1 << 20)) >> 21
  2172. s4 += carry[3]
  2173. s3 -= carry[3] << 21
  2174. carry[5] = (s5 + (1 << 20)) >> 21
  2175. s6 += carry[5]
  2176. s5 -= carry[5] << 21
  2177. carry[7] = (s7 + (1 << 20)) >> 21
  2178. s8 += carry[7]
  2179. s7 -= carry[7] << 21
  2180. carry[9] = (s9 + (1 << 20)) >> 21
  2181. s10 += carry[9]
  2182. s9 -= carry[9] << 21
  2183. carry[11] = (s11 + (1 << 20)) >> 21
  2184. s12 += carry[11]
  2185. s11 -= carry[11] << 21
  2186. carry[13] = (s13 + (1 << 20)) >> 21
  2187. s14 += carry[13]
  2188. s13 -= carry[13] << 21
  2189. carry[15] = (s15 + (1 << 20)) >> 21
  2190. s16 += carry[15]
  2191. s15 -= carry[15] << 21
  2192. carry[17] = (s17 + (1 << 20)) >> 21
  2193. s18 += carry[17]
  2194. s17 -= carry[17] << 21
  2195. carry[19] = (s19 + (1 << 20)) >> 21
  2196. s20 += carry[19]
  2197. s19 -= carry[19] << 21
  2198. carry[21] = (s21 + (1 << 20)) >> 21
  2199. s22 += carry[21]
  2200. s21 -= carry[21] << 21
  2201. s11 += s23 * 666643
  2202. s12 += s23 * 470296
  2203. s13 += s23 * 654183
  2204. s14 -= s23 * 997805
  2205. s15 += s23 * 136657
  2206. s16 -= s23 * 683901
  2207. s23 = 0
  2208. s10 += s22 * 666643
  2209. s11 += s22 * 470296
  2210. s12 += s22 * 654183
  2211. s13 -= s22 * 997805
  2212. s14 += s22 * 136657
  2213. s15 -= s22 * 683901
  2214. s22 = 0
  2215. s9 += s21 * 666643
  2216. s10 += s21 * 470296
  2217. s11 += s21 * 654183
  2218. s12 -= s21 * 997805
  2219. s13 += s21 * 136657
  2220. s14 -= s21 * 683901
  2221. s21 = 0
  2222. s8 += s20 * 666643
  2223. s9 += s20 * 470296
  2224. s10 += s20 * 654183
  2225. s11 -= s20 * 997805
  2226. s12 += s20 * 136657
  2227. s13 -= s20 * 683901
  2228. s20 = 0
  2229. s7 += s19 * 666643
  2230. s8 += s19 * 470296
  2231. s9 += s19 * 654183
  2232. s10 -= s19 * 997805
  2233. s11 += s19 * 136657
  2234. s12 -= s19 * 683901
  2235. s19 = 0
  2236. s6 += s18 * 666643
  2237. s7 += s18 * 470296
  2238. s8 += s18 * 654183
  2239. s9 -= s18 * 997805
  2240. s10 += s18 * 136657
  2241. s11 -= s18 * 683901
  2242. s18 = 0
  2243. carry[6] = (s6 + (1 << 20)) >> 21
  2244. s7 += carry[6]
  2245. s6 -= carry[6] << 21
  2246. carry[8] = (s8 + (1 << 20)) >> 21
  2247. s9 += carry[8]
  2248. s8 -= carry[8] << 21
  2249. carry[10] = (s10 + (1 << 20)) >> 21
  2250. s11 += carry[10]
  2251. s10 -= carry[10] << 21
  2252. carry[12] = (s12 + (1 << 20)) >> 21
  2253. s13 += carry[12]
  2254. s12 -= carry[12] << 21
  2255. carry[14] = (s14 + (1 << 20)) >> 21
  2256. s15 += carry[14]
  2257. s14 -= carry[14] << 21
  2258. carry[16] = (s16 + (1 << 20)) >> 21
  2259. s17 += carry[16]
  2260. s16 -= carry[16] << 21
  2261. carry[7] = (s7 + (1 << 20)) >> 21
  2262. s8 += carry[7]
  2263. s7 -= carry[7] << 21
  2264. carry[9] = (s9 + (1 << 20)) >> 21
  2265. s10 += carry[9]
  2266. s9 -= carry[9] << 21
  2267. carry[11] = (s11 + (1 << 20)) >> 21
  2268. s12 += carry[11]
  2269. s11 -= carry[11] << 21
  2270. carry[13] = (s13 + (1 << 20)) >> 21
  2271. s14 += carry[13]
  2272. s13 -= carry[13] << 21
  2273. carry[15] = (s15 + (1 << 20)) >> 21
  2274. s16 += carry[15]
  2275. s15 -= carry[15] << 21
  2276. s5 += s17 * 666643
  2277. s6 += s17 * 470296
  2278. s7 += s17 * 654183
  2279. s8 -= s17 * 997805
  2280. s9 += s17 * 136657
  2281. s10 -= s17 * 683901
  2282. s17 = 0
  2283. s4 += s16 * 666643
  2284. s5 += s16 * 470296
  2285. s6 += s16 * 654183
  2286. s7 -= s16 * 997805
  2287. s8 += s16 * 136657
  2288. s9 -= s16 * 683901
  2289. s16 = 0
  2290. s3 += s15 * 666643
  2291. s4 += s15 * 470296
  2292. s5 += s15 * 654183
  2293. s6 -= s15 * 997805
  2294. s7 += s15 * 136657
  2295. s8 -= s15 * 683901
  2296. s15 = 0
  2297. s2 += s14 * 666643
  2298. s3 += s14 * 470296
  2299. s4 += s14 * 654183
  2300. s5 -= s14 * 997805
  2301. s6 += s14 * 136657
  2302. s7 -= s14 * 683901
  2303. s14 = 0
  2304. s1 += s13 * 666643
  2305. s2 += s13 * 470296
  2306. s3 += s13 * 654183
  2307. s4 -= s13 * 997805
  2308. s5 += s13 * 136657
  2309. s6 -= s13 * 683901
  2310. s13 = 0
  2311. s0 += s12 * 666643
  2312. s1 += s12 * 470296
  2313. s2 += s12 * 654183
  2314. s3 -= s12 * 997805
  2315. s4 += s12 * 136657
  2316. s5 -= s12 * 683901
  2317. s12 = 0
  2318. carry[0] = (s0 + (1 << 20)) >> 21
  2319. s1 += carry[0]
  2320. s0 -= carry[0] << 21
  2321. carry[2] = (s2 + (1 << 20)) >> 21
  2322. s3 += carry[2]
  2323. s2 -= carry[2] << 21
  2324. carry[4] = (s4 + (1 << 20)) >> 21
  2325. s5 += carry[4]
  2326. s4 -= carry[4] << 21
  2327. carry[6] = (s6 + (1 << 20)) >> 21
  2328. s7 += carry[6]
  2329. s6 -= carry[6] << 21
  2330. carry[8] = (s8 + (1 << 20)) >> 21
  2331. s9 += carry[8]
  2332. s8 -= carry[8] << 21
  2333. carry[10] = (s10 + (1 << 20)) >> 21
  2334. s11 += carry[10]
  2335. s10 -= carry[10] << 21
  2336. carry[1] = (s1 + (1 << 20)) >> 21
  2337. s2 += carry[1]
  2338. s1 -= carry[1] << 21
  2339. carry[3] = (s3 + (1 << 20)) >> 21
  2340. s4 += carry[3]
  2341. s3 -= carry[3] << 21
  2342. carry[5] = (s5 + (1 << 20)) >> 21
  2343. s6 += carry[5]
  2344. s5 -= carry[5] << 21
  2345. carry[7] = (s7 + (1 << 20)) >> 21
  2346. s8 += carry[7]
  2347. s7 -= carry[7] << 21
  2348. carry[9] = (s9 + (1 << 20)) >> 21
  2349. s10 += carry[9]
  2350. s9 -= carry[9] << 21
  2351. carry[11] = (s11 + (1 << 20)) >> 21
  2352. s12 += carry[11]
  2353. s11 -= carry[11] << 21
  2354. s0 += s12 * 666643
  2355. s1 += s12 * 470296
  2356. s2 += s12 * 654183
  2357. s3 -= s12 * 997805
  2358. s4 += s12 * 136657
  2359. s5 -= s12 * 683901
  2360. s12 = 0
  2361. carry[0] = s0 >> 21
  2362. s1 += carry[0]
  2363. s0 -= carry[0] << 21
  2364. carry[1] = s1 >> 21
  2365. s2 += carry[1]
  2366. s1 -= carry[1] << 21
  2367. carry[2] = s2 >> 21
  2368. s3 += carry[2]
  2369. s2 -= carry[2] << 21
  2370. carry[3] = s3 >> 21
  2371. s4 += carry[3]
  2372. s3 -= carry[3] << 21
  2373. carry[4] = s4 >> 21
  2374. s5 += carry[4]
  2375. s4 -= carry[4] << 21
  2376. carry[5] = s5 >> 21
  2377. s6 += carry[5]
  2378. s5 -= carry[5] << 21
  2379. carry[6] = s6 >> 21
  2380. s7 += carry[6]
  2381. s6 -= carry[6] << 21
  2382. carry[7] = s7 >> 21
  2383. s8 += carry[7]
  2384. s7 -= carry[7] << 21
  2385. carry[8] = s8 >> 21
  2386. s9 += carry[8]
  2387. s8 -= carry[8] << 21
  2388. carry[9] = s9 >> 21
  2389. s10 += carry[9]
  2390. s9 -= carry[9] << 21
  2391. carry[10] = s10 >> 21
  2392. s11 += carry[10]
  2393. s10 -= carry[10] << 21
  2394. carry[11] = s11 >> 21
  2395. s12 += carry[11]
  2396. s11 -= carry[11] << 21
  2397. s0 += s12 * 666643
  2398. s1 += s12 * 470296
  2399. s2 += s12 * 654183
  2400. s3 -= s12 * 997805
  2401. s4 += s12 * 136657
  2402. s5 -= s12 * 683901
  2403. s12 = 0
  2404. carry[0] = s0 >> 21
  2405. s1 += carry[0]
  2406. s0 -= carry[0] << 21
  2407. carry[1] = s1 >> 21
  2408. s2 += carry[1]
  2409. s1 -= carry[1] << 21
  2410. carry[2] = s2 >> 21
  2411. s3 += carry[2]
  2412. s2 -= carry[2] << 21
  2413. carry[3] = s3 >> 21
  2414. s4 += carry[3]
  2415. s3 -= carry[3] << 21
  2416. carry[4] = s4 >> 21
  2417. s5 += carry[4]
  2418. s4 -= carry[4] << 21
  2419. carry[5] = s5 >> 21
  2420. s6 += carry[5]
  2421. s5 -= carry[5] << 21
  2422. carry[6] = s6 >> 21
  2423. s7 += carry[6]
  2424. s6 -= carry[6] << 21
  2425. carry[7] = s7 >> 21
  2426. s8 += carry[7]
  2427. s7 -= carry[7] << 21
  2428. carry[8] = s8 >> 21
  2429. s9 += carry[8]
  2430. s8 -= carry[8] << 21
  2431. carry[9] = s9 >> 21
  2432. s10 += carry[9]
  2433. s9 -= carry[9] << 21
  2434. carry[10] = s10 >> 21
  2435. s11 += carry[10]
  2436. s10 -= carry[10] << 21
  2437. s[0] = byte(s0 >> 0)
  2438. s[1] = byte(s0 >> 8)
  2439. s[2] = byte((s0 >> 16) | (s1 << 5))
  2440. s[3] = byte(s1 >> 3)
  2441. s[4] = byte(s1 >> 11)
  2442. s[5] = byte((s1 >> 19) | (s2 << 2))
  2443. s[6] = byte(s2 >> 6)
  2444. s[7] = byte((s2 >> 14) | (s3 << 7))
  2445. s[8] = byte(s3 >> 1)
  2446. s[9] = byte(s3 >> 9)
  2447. s[10] = byte((s3 >> 17) | (s4 << 4))
  2448. s[11] = byte(s4 >> 4)
  2449. s[12] = byte(s4 >> 12)
  2450. s[13] = byte((s4 >> 20) | (s5 << 1))
  2451. s[14] = byte(s5 >> 7)
  2452. s[15] = byte((s5 >> 15) | (s6 << 6))
  2453. s[16] = byte(s6 >> 2)
  2454. s[17] = byte(s6 >> 10)
  2455. s[18] = byte((s6 >> 18) | (s7 << 3))
  2456. s[19] = byte(s7 >> 5)
  2457. s[20] = byte(s7 >> 13)
  2458. s[21] = byte(s8 >> 0)
  2459. s[22] = byte(s8 >> 8)
  2460. s[23] = byte((s8 >> 16) | (s9 << 5))
  2461. s[24] = byte(s9 >> 3)
  2462. s[25] = byte(s9 >> 11)
  2463. s[26] = byte((s9 >> 19) | (s10 << 2))
  2464. s[27] = byte(s10 >> 6)
  2465. s[28] = byte((s10 >> 14) | (s11 << 7))
  2466. s[29] = byte(s11 >> 1)
  2467. s[30] = byte(s11 >> 9)
  2468. s[31] = byte(s11 >> 17)
  2469. }
  2470. // Input:
  2471. // s[0]+256*s[1]+...+256^63*s[63] = s
  2472. //
  2473. // Output:
  2474. // s[0]+256*s[1]+...+256^31*s[31] = s mod l
  2475. // where l = 2^252 + 27742317777372353535851937790883648493.
  2476. func ScReduce(out *Key, s *[64]byte) {
  2477. s0 := 2097151 & load3(s[:])
  2478. s1 := 2097151 & (load4(s[2:]) >> 5)
  2479. s2 := 2097151 & (load3(s[5:]) >> 2)
  2480. s3 := 2097151 & (load4(s[7:]) >> 7)
  2481. s4 := 2097151 & (load4(s[10:]) >> 4)
  2482. s5 := 2097151 & (load3(s[13:]) >> 1)
  2483. s6 := 2097151 & (load4(s[15:]) >> 6)
  2484. s7 := 2097151 & (load3(s[18:]) >> 3)
  2485. s8 := 2097151 & load3(s[21:])
  2486. s9 := 2097151 & (load4(s[23:]) >> 5)
  2487. s10 := 2097151 & (load3(s[26:]) >> 2)
  2488. s11 := 2097151 & (load4(s[28:]) >> 7)
  2489. s12 := 2097151 & (load4(s[31:]) >> 4)
  2490. s13 := 2097151 & (load3(s[34:]) >> 1)
  2491. s14 := 2097151 & (load4(s[36:]) >> 6)
  2492. s15 := 2097151 & (load3(s[39:]) >> 3)
  2493. s16 := 2097151 & load3(s[42:])
  2494. s17 := 2097151 & (load4(s[44:]) >> 5)
  2495. s18 := 2097151 & (load3(s[47:]) >> 2)
  2496. s19 := 2097151 & (load4(s[49:]) >> 7)
  2497. s20 := 2097151 & (load4(s[52:]) >> 4)
  2498. s21 := 2097151 & (load3(s[55:]) >> 1)
  2499. s22 := 2097151 & (load4(s[57:]) >> 6)
  2500. s23 := (load4(s[60:]) >> 3)
  2501. s11 += s23 * 666643
  2502. s12 += s23 * 470296
  2503. s13 += s23 * 654183
  2504. s14 -= s23 * 997805
  2505. s15 += s23 * 136657
  2506. s16 -= s23 * 683901
  2507. s23 = 0
  2508. s10 += s22 * 666643
  2509. s11 += s22 * 470296
  2510. s12 += s22 * 654183
  2511. s13 -= s22 * 997805
  2512. s14 += s22 * 136657
  2513. s15 -= s22 * 683901
  2514. s22 = 0
  2515. s9 += s21 * 666643
  2516. s10 += s21 * 470296
  2517. s11 += s21 * 654183
  2518. s12 -= s21 * 997805
  2519. s13 += s21 * 136657
  2520. s14 -= s21 * 683901
  2521. s21 = 0
  2522. s8 += s20 * 666643
  2523. s9 += s20 * 470296
  2524. s10 += s20 * 654183
  2525. s11 -= s20 * 997805
  2526. s12 += s20 * 136657
  2527. s13 -= s20 * 683901
  2528. s20 = 0
  2529. s7 += s19 * 666643
  2530. s8 += s19 * 470296
  2531. s9 += s19 * 654183
  2532. s10 -= s19 * 997805
  2533. s11 += s19 * 136657
  2534. s12 -= s19 * 683901
  2535. s19 = 0
  2536. s6 += s18 * 666643
  2537. s7 += s18 * 470296
  2538. s8 += s18 * 654183
  2539. s9 -= s18 * 997805
  2540. s10 += s18 * 136657
  2541. s11 -= s18 * 683901
  2542. s18 = 0
  2543. var carry [17]int64
  2544. carry[6] = (s6 + (1 << 20)) >> 21
  2545. s7 += carry[6]
  2546. s6 -= carry[6] << 21
  2547. carry[8] = (s8 + (1 << 20)) >> 21
  2548. s9 += carry[8]
  2549. s8 -= carry[8] << 21
  2550. carry[10] = (s10 + (1 << 20)) >> 21
  2551. s11 += carry[10]
  2552. s10 -= carry[10] << 21
  2553. carry[12] = (s12 + (1 << 20)) >> 21
  2554. s13 += carry[12]
  2555. s12 -= carry[12] << 21
  2556. carry[14] = (s14 + (1 << 20)) >> 21
  2557. s15 += carry[14]
  2558. s14 -= carry[14] << 21
  2559. carry[16] = (s16 + (1 << 20)) >> 21
  2560. s17 += carry[16]
  2561. s16 -= carry[16] << 21
  2562. carry[7] = (s7 + (1 << 20)) >> 21
  2563. s8 += carry[7]
  2564. s7 -= carry[7] << 21
  2565. carry[9] = (s9 + (1 << 20)) >> 21
  2566. s10 += carry[9]
  2567. s9 -= carry[9] << 21
  2568. carry[11] = (s11 + (1 << 20)) >> 21
  2569. s12 += carry[11]
  2570. s11 -= carry[11] << 21
  2571. carry[13] = (s13 + (1 << 20)) >> 21
  2572. s14 += carry[13]
  2573. s13 -= carry[13] << 21
  2574. carry[15] = (s15 + (1 << 20)) >> 21
  2575. s16 += carry[15]
  2576. s15 -= carry[15] << 21
  2577. s5 += s17 * 666643
  2578. s6 += s17 * 470296
  2579. s7 += s17 * 654183
  2580. s8 -= s17 * 997805
  2581. s9 += s17 * 136657
  2582. s10 -= s17 * 683901
  2583. s17 = 0
  2584. s4 += s16 * 666643
  2585. s5 += s16 * 470296
  2586. s6 += s16 * 654183
  2587. s7 -= s16 * 997805
  2588. s8 += s16 * 136657
  2589. s9 -= s16 * 683901
  2590. s16 = 0
  2591. s3 += s15 * 666643
  2592. s4 += s15 * 470296
  2593. s5 += s15 * 654183
  2594. s6 -= s15 * 997805
  2595. s7 += s15 * 136657
  2596. s8 -= s15 * 683901
  2597. s15 = 0
  2598. s2 += s14 * 666643
  2599. s3 += s14 * 470296
  2600. s4 += s14 * 654183
  2601. s5 -= s14 * 997805
  2602. s6 += s14 * 136657
  2603. s7 -= s14 * 683901
  2604. s14 = 0
  2605. s1 += s13 * 666643
  2606. s2 += s13 * 470296
  2607. s3 += s13 * 654183
  2608. s4 -= s13 * 997805
  2609. s5 += s13 * 136657
  2610. s6 -= s13 * 683901
  2611. s13 = 0
  2612. s0 += s12 * 666643
  2613. s1 += s12 * 470296
  2614. s2 += s12 * 654183
  2615. s3 -= s12 * 997805
  2616. s4 += s12 * 136657
  2617. s5 -= s12 * 683901
  2618. s12 = 0
  2619. carry[0] = (s0 + (1 << 20)) >> 21
  2620. s1 += carry[0]
  2621. s0 -= carry[0] << 21
  2622. carry[2] = (s2 + (1 << 20)) >> 21
  2623. s3 += carry[2]
  2624. s2 -= carry[2] << 21
  2625. carry[4] = (s4 + (1 << 20)) >> 21
  2626. s5 += carry[4]
  2627. s4 -= carry[4] << 21
  2628. carry[6] = (s6 + (1 << 20)) >> 21
  2629. s7 += carry[6]
  2630. s6 -= carry[6] << 21
  2631. carry[8] = (s8 + (1 << 20)) >> 21
  2632. s9 += carry[8]
  2633. s8 -= carry[8] << 21
  2634. carry[10] = (s10 + (1 << 20)) >> 21
  2635. s11 += carry[10]
  2636. s10 -= carry[10] << 21
  2637. carry[1] = (s1 + (1 << 20)) >> 21
  2638. s2 += carry[1]
  2639. s1 -= carry[1] << 21
  2640. carry[3] = (s3 + (1 << 20)) >> 21
  2641. s4 += carry[3]
  2642. s3 -= carry[3] << 21
  2643. carry[5] = (s5 + (1 << 20)) >> 21
  2644. s6 += carry[5]
  2645. s5 -= carry[5] << 21
  2646. carry[7] = (s7 + (1 << 20)) >> 21
  2647. s8 += carry[7]
  2648. s7 -= carry[7] << 21
  2649. carry[9] = (s9 + (1 << 20)) >> 21
  2650. s10 += carry[9]
  2651. s9 -= carry[9] << 21
  2652. carry[11] = (s11 + (1 << 20)) >> 21
  2653. s12 += carry[11]
  2654. s11 -= carry[11] << 21
  2655. s0 += s12 * 666643
  2656. s1 += s12 * 470296
  2657. s2 += s12 * 654183
  2658. s3 -= s12 * 997805
  2659. s4 += s12 * 136657
  2660. s5 -= s12 * 683901
  2661. s12 = 0
  2662. carry[0] = s0 >> 21
  2663. s1 += carry[0]
  2664. s0 -= carry[0] << 21
  2665. carry[1] = s1 >> 21
  2666. s2 += carry[1]
  2667. s1 -= carry[1] << 21
  2668. carry[2] = s2 >> 21
  2669. s3 += carry[2]
  2670. s2 -= carry[2] << 21
  2671. carry[3] = s3 >> 21
  2672. s4 += carry[3]
  2673. s3 -= carry[3] << 21
  2674. carry[4] = s4 >> 21
  2675. s5 += carry[4]
  2676. s4 -= carry[4] << 21
  2677. carry[5] = s5 >> 21
  2678. s6 += carry[5]
  2679. s5 -= carry[5] << 21
  2680. carry[6] = s6 >> 21
  2681. s7 += carry[6]
  2682. s6 -= carry[6] << 21
  2683. carry[7] = s7 >> 21
  2684. s8 += carry[7]
  2685. s7 -= carry[7] << 21
  2686. carry[8] = s8 >> 21
  2687. s9 += carry[8]
  2688. s8 -= carry[8] << 21
  2689. carry[9] = s9 >> 21
  2690. s10 += carry[9]
  2691. s9 -= carry[9] << 21
  2692. carry[10] = s10 >> 21
  2693. s11 += carry[10]
  2694. s10 -= carry[10] << 21
  2695. carry[11] = s11 >> 21
  2696. s12 += carry[11]
  2697. s11 -= carry[11] << 21
  2698. s0 += s12 * 666643
  2699. s1 += s12 * 470296
  2700. s2 += s12 * 654183
  2701. s3 -= s12 * 997805
  2702. s4 += s12 * 136657
  2703. s5 -= s12 * 683901
  2704. s12 = 0
  2705. carry[0] = s0 >> 21
  2706. s1 += carry[0]
  2707. s0 -= carry[0] << 21
  2708. carry[1] = s1 >> 21
  2709. s2 += carry[1]
  2710. s1 -= carry[1] << 21
  2711. carry[2] = s2 >> 21
  2712. s3 += carry[2]
  2713. s2 -= carry[2] << 21
  2714. carry[3] = s3 >> 21
  2715. s4 += carry[3]
  2716. s3 -= carry[3] << 21
  2717. carry[4] = s4 >> 21
  2718. s5 += carry[4]
  2719. s4 -= carry[4] << 21
  2720. carry[5] = s5 >> 21
  2721. s6 += carry[5]
  2722. s5 -= carry[5] << 21
  2723. carry[6] = s6 >> 21
  2724. s7 += carry[6]
  2725. s6 -= carry[6] << 21
  2726. carry[7] = s7 >> 21
  2727. s8 += carry[7]
  2728. s7 -= carry[7] << 21
  2729. carry[8] = s8 >> 21
  2730. s9 += carry[8]
  2731. s8 -= carry[8] << 21
  2732. carry[9] = s9 >> 21
  2733. s10 += carry[9]
  2734. s9 -= carry[9] << 21
  2735. carry[10] = s10 >> 21
  2736. s11 += carry[10]
  2737. s10 -= carry[10] << 21
  2738. out[0] = byte(s0 >> 0)
  2739. out[1] = byte(s0 >> 8)
  2740. out[2] = byte((s0 >> 16) | (s1 << 5))
  2741. out[3] = byte(s1 >> 3)
  2742. out[4] = byte(s1 >> 11)
  2743. out[5] = byte((s1 >> 19) | (s2 << 2))
  2744. out[6] = byte(s2 >> 6)
  2745. out[7] = byte((s2 >> 14) | (s3 << 7))
  2746. out[8] = byte(s3 >> 1)
  2747. out[9] = byte(s3 >> 9)
  2748. out[10] = byte((s3 >> 17) | (s4 << 4))
  2749. out[11] = byte(s4 >> 4)
  2750. out[12] = byte(s4 >> 12)
  2751. out[13] = byte((s4 >> 20) | (s5 << 1))
  2752. out[14] = byte(s5 >> 7)
  2753. out[15] = byte((s5 >> 15) | (s6 << 6))
  2754. out[16] = byte(s6 >> 2)
  2755. out[17] = byte(s6 >> 10)
  2756. out[18] = byte((s6 >> 18) | (s7 << 3))
  2757. out[19] = byte(s7 >> 5)
  2758. out[20] = byte(s7 >> 13)
  2759. out[21] = byte(s8 >> 0)
  2760. out[22] = byte(s8 >> 8)
  2761. out[23] = byte((s8 >> 16) | (s9 << 5))
  2762. out[24] = byte(s9 >> 3)
  2763. out[25] = byte(s9 >> 11)
  2764. out[26] = byte((s9 >> 19) | (s10 << 2))
  2765. out[27] = byte(s10 >> 6)
  2766. out[28] = byte((s10 >> 14) | (s11 << 7))
  2767. out[29] = byte(s11 >> 1)
  2768. out[30] = byte(s11 >> 9)
  2769. out[31] = byte(s11 >> 17)
  2770. }
  2771. func ScReduce32(s *Key) {
  2772. s0 := 2097151 & load3(s[:])
  2773. s1 := 2097151 & (load4(s[2:]) >> 5)
  2774. s2 := 2097151 & (load3(s[5:]) >> 2)
  2775. s3 := 2097151 & (load4(s[7:]) >> 7)
  2776. s4 := 2097151 & (load4(s[10:]) >> 4)
  2777. s5 := 2097151 & (load3(s[13:]) >> 1)
  2778. s6 := 2097151 & (load4(s[15:]) >> 6)
  2779. s7 := 2097151 & (load3(s[18:]) >> 3)
  2780. s8 := 2097151 & load3(s[21:])
  2781. s9 := 2097151 & (load4(s[23:]) >> 5)
  2782. s10 := 2097151 & (load3(s[26:]) >> 2)
  2783. s11 := (load4(s[28:]) >> 7)
  2784. s12 := int64(0)
  2785. var carry [12]int64
  2786. carry[0] = (s0 + (1 << 20)) >> 21
  2787. s1 += carry[0]
  2788. s0 -= carry[0] << 21
  2789. carry[2] = (s2 + (1 << 20)) >> 21
  2790. s3 += carry[2]
  2791. s2 -= carry[2] << 21
  2792. carry[4] = (s4 + (1 << 20)) >> 21
  2793. s5 += carry[4]
  2794. s4 -= carry[4] << 21
  2795. carry[6] = (s6 + (1 << 20)) >> 21
  2796. s7 += carry[6]
  2797. s6 -= carry[6] << 21
  2798. carry[8] = (s8 + (1 << 20)) >> 21
  2799. s9 += carry[8]
  2800. s8 -= carry[8] << 21
  2801. carry[10] = (s10 + (1 << 20)) >> 21
  2802. s11 += carry[10]
  2803. s10 -= carry[10] << 21
  2804. carry[1] = (s1 + (1 << 20)) >> 21
  2805. s2 += carry[1]
  2806. s1 -= carry[1] << 21
  2807. carry[3] = (s3 + (1 << 20)) >> 21
  2808. s4 += carry[3]
  2809. s3 -= carry[3] << 21
  2810. carry[5] = (s5 + (1 << 20)) >> 21
  2811. s6 += carry[5]
  2812. s5 -= carry[5] << 21
  2813. carry[7] = (s7 + (1 << 20)) >> 21
  2814. s8 += carry[7]
  2815. s7 -= carry[7] << 21
  2816. carry[9] = (s9 + (1 << 20)) >> 21
  2817. s10 += carry[9]
  2818. s9 -= carry[9] << 21
  2819. carry[11] = (s11 + (1 << 20)) >> 21
  2820. s12 += carry[11]
  2821. s11 -= carry[11] << 21
  2822. s0 += s12 * 666643
  2823. s1 += s12 * 470296
  2824. s2 += s12 * 654183
  2825. s3 -= s12 * 997805
  2826. s4 += s12 * 136657
  2827. s5 -= s12 * 683901
  2828. s12 = 0
  2829. carry[0] = s0 >> 21
  2830. s1 += carry[0]
  2831. s0 -= carry[0] << 21
  2832. carry[1] = s1 >> 21
  2833. s2 += carry[1]
  2834. s1 -= carry[1] << 21
  2835. carry[2] = s2 >> 21
  2836. s3 += carry[2]
  2837. s2 -= carry[2] << 21
  2838. carry[3] = s3 >> 21
  2839. s4 += carry[3]
  2840. s3 -= carry[3] << 21
  2841. carry[4] = s4 >> 21
  2842. s5 += carry[4]
  2843. s4 -= carry[4] << 21
  2844. carry[5] = s5 >> 21
  2845. s6 += carry[5]
  2846. s5 -= carry[5] << 21
  2847. carry[6] = s6 >> 21
  2848. s7 += carry[6]
  2849. s6 -= carry[6] << 21
  2850. carry[7] = s7 >> 21
  2851. s8 += carry[7]
  2852. s7 -= carry[7] << 21
  2853. carry[8] = s8 >> 21
  2854. s9 += carry[8]
  2855. s8 -= carry[8] << 21
  2856. carry[9] = s9 >> 21
  2857. s10 += carry[9]
  2858. s9 -= carry[9] << 21
  2859. carry[10] = s10 >> 21
  2860. s11 += carry[10]
  2861. s10 -= carry[10] << 21
  2862. carry[11] = s11 >> 21
  2863. s12 += carry[11]
  2864. s11 -= carry[11] << 21
  2865. s0 += s12 * 666643
  2866. s1 += s12 * 470296
  2867. s2 += s12 * 654183
  2868. s3 -= s12 * 997805
  2869. s4 += s12 * 136657
  2870. s5 -= s12 * 683901
  2871. carry[0] = s0 >> 21
  2872. s1 += carry[0]
  2873. s0 -= carry[0] << 21
  2874. carry[1] = s1 >> 21
  2875. s2 += carry[1]
  2876. s1 -= carry[1] << 21
  2877. carry[2] = s2 >> 21
  2878. s3 += carry[2]
  2879. s2 -= carry[2] << 21
  2880. carry[3] = s3 >> 21
  2881. s4 += carry[3]
  2882. s3 -= carry[3] << 21
  2883. carry[4] = s4 >> 21
  2884. s5 += carry[4]
  2885. s4 -= carry[4] << 21
  2886. carry[5] = s5 >> 21
  2887. s6 += carry[5]
  2888. s5 -= carry[5] << 21
  2889. carry[6] = s6 >> 21
  2890. s7 += carry[6]
  2891. s6 -= carry[6] << 21
  2892. carry[7] = s7 >> 21
  2893. s8 += carry[7]
  2894. s7 -= carry[7] << 21
  2895. carry[8] = s8 >> 21
  2896. s9 += carry[8]
  2897. s8 -= carry[8] << 21
  2898. carry[9] = s9 >> 21
  2899. s10 += carry[9]
  2900. s9 -= carry[9] << 21
  2901. carry[10] = s10 >> 21
  2902. s11 += carry[10]
  2903. s10 -= carry[10] << 21
  2904. s[0] = byte(s0 >> 0)
  2905. s[1] = byte(s0 >> 8)
  2906. s[2] = byte((s0 >> 16) | (s1 << 5))
  2907. s[3] = byte(s1 >> 3)
  2908. s[4] = byte(s1 >> 11)
  2909. s[5] = byte((s1 >> 19) | (s2 << 2))
  2910. s[6] = byte(s2 >> 6)
  2911. s[7] = byte((s2 >> 14) | (s3 << 7))
  2912. s[8] = byte(s3 >> 1)
  2913. s[9] = byte(s3 >> 9)
  2914. s[10] = byte((s3 >> 17) | (s4 << 4))
  2915. s[11] = byte(s4 >> 4)
  2916. s[12] = byte(s4 >> 12)
  2917. s[13] = byte((s4 >> 20) | (s5 << 1))
  2918. s[14] = byte(s5 >> 7)
  2919. s[15] = byte((s5 >> 15) | (s6 << 6))
  2920. s[16] = byte(s6 >> 2)
  2921. s[17] = byte(s6 >> 10)
  2922. s[18] = byte((s6 >> 18) | (s7 << 3))
  2923. s[19] = byte(s7 >> 5)
  2924. s[20] = byte(s7 >> 13)
  2925. s[21] = byte(s8 >> 0)
  2926. s[22] = byte(s8 >> 8)
  2927. s[23] = byte((s8 >> 16) | (s9 << 5))
  2928. s[24] = byte(s9 >> 3)
  2929. s[25] = byte(s9 >> 11)
  2930. s[26] = byte((s9 >> 19) | (s10 << 2))
  2931. s[27] = byte(s10 >> 6)
  2932. s[28] = byte((s10 >> 14) | (s11 << 7))
  2933. s[29] = byte(s11 >> 1)
  2934. s[30] = byte(s11 >> 9)
  2935. s[31] = byte(s11 >> 17)
  2936. }