ring-native 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (267) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGES.md +7 -0
  4. data/Makefile +5 -0
  5. data/README.md +12 -5
  6. data/Rakefile +4 -0
  7. data/ext/ring/extconf.rb +4 -5
  8. data/lib/ring/native.rb +3 -1
  9. data/lib/ring/native/version.rb +5 -1
  10. data/ring-native.gemspec +6 -6
  11. data/vendor/ring-ffi/Cargo.lock +26 -0
  12. data/vendor/ring-ffi/Cargo.toml +45 -0
  13. data/vendor/ring-ffi/LICENSE +16 -0
  14. data/vendor/ring-ffi/README.md +59 -0
  15. data/vendor/ring-ffi/src/lib.rs +79 -0
  16. metadata +10 -255
  17. data/vendor/ring/BUILDING.md +0 -40
  18. data/vendor/ring/Cargo.toml +0 -43
  19. data/vendor/ring/LICENSE +0 -185
  20. data/vendor/ring/Makefile +0 -35
  21. data/vendor/ring/PORTING.md +0 -163
  22. data/vendor/ring/README.md +0 -113
  23. data/vendor/ring/STYLE.md +0 -197
  24. data/vendor/ring/appveyor.yml +0 -27
  25. data/vendor/ring/build.rs +0 -108
  26. data/vendor/ring/crypto/aes/aes.c +0 -1142
  27. data/vendor/ring/crypto/aes/aes_test.Windows.vcxproj +0 -25
  28. data/vendor/ring/crypto/aes/aes_test.cc +0 -93
  29. data/vendor/ring/crypto/aes/asm/aes-586.pl +0 -2368
  30. data/vendor/ring/crypto/aes/asm/aes-armv4.pl +0 -1249
  31. data/vendor/ring/crypto/aes/asm/aes-x86_64.pl +0 -2246
  32. data/vendor/ring/crypto/aes/asm/aesni-x86.pl +0 -1318
  33. data/vendor/ring/crypto/aes/asm/aesni-x86_64.pl +0 -2084
  34. data/vendor/ring/crypto/aes/asm/aesv8-armx.pl +0 -675
  35. data/vendor/ring/crypto/aes/asm/bsaes-armv7.pl +0 -1364
  36. data/vendor/ring/crypto/aes/asm/bsaes-x86_64.pl +0 -1565
  37. data/vendor/ring/crypto/aes/asm/vpaes-x86.pl +0 -841
  38. data/vendor/ring/crypto/aes/asm/vpaes-x86_64.pl +0 -1116
  39. data/vendor/ring/crypto/aes/internal.h +0 -87
  40. data/vendor/ring/crypto/aes/mode_wrappers.c +0 -61
  41. data/vendor/ring/crypto/bn/add.c +0 -394
  42. data/vendor/ring/crypto/bn/asm/armv4-mont.pl +0 -694
  43. data/vendor/ring/crypto/bn/asm/armv8-mont.pl +0 -1503
  44. data/vendor/ring/crypto/bn/asm/bn-586.pl +0 -774
  45. data/vendor/ring/crypto/bn/asm/co-586.pl +0 -287
  46. data/vendor/ring/crypto/bn/asm/rsaz-avx2.pl +0 -1882
  47. data/vendor/ring/crypto/bn/asm/x86-mont.pl +0 -592
  48. data/vendor/ring/crypto/bn/asm/x86_64-gcc.c +0 -599
  49. data/vendor/ring/crypto/bn/asm/x86_64-mont.pl +0 -1393
  50. data/vendor/ring/crypto/bn/asm/x86_64-mont5.pl +0 -3507
  51. data/vendor/ring/crypto/bn/bn.c +0 -352
  52. data/vendor/ring/crypto/bn/bn_asn1.c +0 -74
  53. data/vendor/ring/crypto/bn/bn_test.Windows.vcxproj +0 -25
  54. data/vendor/ring/crypto/bn/bn_test.cc +0 -1696
  55. data/vendor/ring/crypto/bn/cmp.c +0 -200
  56. data/vendor/ring/crypto/bn/convert.c +0 -433
  57. data/vendor/ring/crypto/bn/ctx.c +0 -311
  58. data/vendor/ring/crypto/bn/div.c +0 -594
  59. data/vendor/ring/crypto/bn/exponentiation.c +0 -1335
  60. data/vendor/ring/crypto/bn/gcd.c +0 -711
  61. data/vendor/ring/crypto/bn/generic.c +0 -1019
  62. data/vendor/ring/crypto/bn/internal.h +0 -316
  63. data/vendor/ring/crypto/bn/montgomery.c +0 -516
  64. data/vendor/ring/crypto/bn/mul.c +0 -888
  65. data/vendor/ring/crypto/bn/prime.c +0 -829
  66. data/vendor/ring/crypto/bn/random.c +0 -334
  67. data/vendor/ring/crypto/bn/rsaz_exp.c +0 -262
  68. data/vendor/ring/crypto/bn/rsaz_exp.h +0 -53
  69. data/vendor/ring/crypto/bn/shift.c +0 -276
  70. data/vendor/ring/crypto/bytestring/bytestring_test.Windows.vcxproj +0 -25
  71. data/vendor/ring/crypto/bytestring/bytestring_test.cc +0 -421
  72. data/vendor/ring/crypto/bytestring/cbb.c +0 -399
  73. data/vendor/ring/crypto/bytestring/cbs.c +0 -227
  74. data/vendor/ring/crypto/bytestring/internal.h +0 -46
  75. data/vendor/ring/crypto/chacha/chacha_generic.c +0 -140
  76. data/vendor/ring/crypto/chacha/chacha_vec.c +0 -323
  77. data/vendor/ring/crypto/chacha/chacha_vec_arm.S +0 -1447
  78. data/vendor/ring/crypto/chacha/chacha_vec_arm_generate.go +0 -153
  79. data/vendor/ring/crypto/cipher/cipher_test.Windows.vcxproj +0 -25
  80. data/vendor/ring/crypto/cipher/e_aes.c +0 -390
  81. data/vendor/ring/crypto/cipher/e_chacha20poly1305.c +0 -208
  82. data/vendor/ring/crypto/cipher/internal.h +0 -173
  83. data/vendor/ring/crypto/cipher/test/aes_128_gcm_tests.txt +0 -543
  84. data/vendor/ring/crypto/cipher/test/aes_128_key_wrap_tests.txt +0 -9
  85. data/vendor/ring/crypto/cipher/test/aes_256_gcm_tests.txt +0 -475
  86. data/vendor/ring/crypto/cipher/test/aes_256_key_wrap_tests.txt +0 -23
  87. data/vendor/ring/crypto/cipher/test/chacha20_poly1305_old_tests.txt +0 -422
  88. data/vendor/ring/crypto/cipher/test/chacha20_poly1305_tests.txt +0 -484
  89. data/vendor/ring/crypto/cipher/test/cipher_test.txt +0 -100
  90. data/vendor/ring/crypto/constant_time_test.Windows.vcxproj +0 -25
  91. data/vendor/ring/crypto/constant_time_test.c +0 -304
  92. data/vendor/ring/crypto/cpu-arm-asm.S +0 -32
  93. data/vendor/ring/crypto/cpu-arm.c +0 -199
  94. data/vendor/ring/crypto/cpu-intel.c +0 -261
  95. data/vendor/ring/crypto/crypto.c +0 -151
  96. data/vendor/ring/crypto/curve25519/asm/x25519-arm.S +0 -2118
  97. data/vendor/ring/crypto/curve25519/curve25519.c +0 -4888
  98. data/vendor/ring/crypto/curve25519/x25519_test.cc +0 -128
  99. data/vendor/ring/crypto/digest/md32_common.h +0 -181
  100. data/vendor/ring/crypto/ec/asm/p256-x86_64-asm.pl +0 -2725
  101. data/vendor/ring/crypto/ec/ec.c +0 -193
  102. data/vendor/ring/crypto/ec/ec_curves.c +0 -61
  103. data/vendor/ring/crypto/ec/ec_key.c +0 -228
  104. data/vendor/ring/crypto/ec/ec_montgomery.c +0 -114
  105. data/vendor/ring/crypto/ec/example_mul.Windows.vcxproj +0 -25
  106. data/vendor/ring/crypto/ec/internal.h +0 -243
  107. data/vendor/ring/crypto/ec/oct.c +0 -253
  108. data/vendor/ring/crypto/ec/p256-64.c +0 -1794
  109. data/vendor/ring/crypto/ec/p256-x86_64-table.h +0 -9548
  110. data/vendor/ring/crypto/ec/p256-x86_64.c +0 -509
  111. data/vendor/ring/crypto/ec/simple.c +0 -1007
  112. data/vendor/ring/crypto/ec/util-64.c +0 -183
  113. data/vendor/ring/crypto/ec/wnaf.c +0 -508
  114. data/vendor/ring/crypto/ecdh/ecdh.c +0 -155
  115. data/vendor/ring/crypto/ecdsa/ecdsa.c +0 -304
  116. data/vendor/ring/crypto/ecdsa/ecdsa_asn1.c +0 -193
  117. data/vendor/ring/crypto/ecdsa/ecdsa_test.Windows.vcxproj +0 -25
  118. data/vendor/ring/crypto/ecdsa/ecdsa_test.cc +0 -327
  119. data/vendor/ring/crypto/header_removed.h +0 -17
  120. data/vendor/ring/crypto/internal.h +0 -495
  121. data/vendor/ring/crypto/libring.Windows.vcxproj +0 -101
  122. data/vendor/ring/crypto/mem.c +0 -98
  123. data/vendor/ring/crypto/modes/asm/aesni-gcm-x86_64.pl +0 -1045
  124. data/vendor/ring/crypto/modes/asm/ghash-armv4.pl +0 -517
  125. data/vendor/ring/crypto/modes/asm/ghash-x86.pl +0 -1393
  126. data/vendor/ring/crypto/modes/asm/ghash-x86_64.pl +0 -1741
  127. data/vendor/ring/crypto/modes/asm/ghashv8-armx.pl +0 -422
  128. data/vendor/ring/crypto/modes/ctr.c +0 -226
  129. data/vendor/ring/crypto/modes/gcm.c +0 -1206
  130. data/vendor/ring/crypto/modes/gcm_test.Windows.vcxproj +0 -25
  131. data/vendor/ring/crypto/modes/gcm_test.c +0 -348
  132. data/vendor/ring/crypto/modes/internal.h +0 -299
  133. data/vendor/ring/crypto/perlasm/arm-xlate.pl +0 -170
  134. data/vendor/ring/crypto/perlasm/readme +0 -100
  135. data/vendor/ring/crypto/perlasm/x86_64-xlate.pl +0 -1164
  136. data/vendor/ring/crypto/perlasm/x86asm.pl +0 -292
  137. data/vendor/ring/crypto/perlasm/x86gas.pl +0 -263
  138. data/vendor/ring/crypto/perlasm/x86masm.pl +0 -200
  139. data/vendor/ring/crypto/perlasm/x86nasm.pl +0 -187
  140. data/vendor/ring/crypto/poly1305/poly1305.c +0 -331
  141. data/vendor/ring/crypto/poly1305/poly1305_arm.c +0 -301
  142. data/vendor/ring/crypto/poly1305/poly1305_arm_asm.S +0 -2015
  143. data/vendor/ring/crypto/poly1305/poly1305_test.Windows.vcxproj +0 -25
  144. data/vendor/ring/crypto/poly1305/poly1305_test.cc +0 -80
  145. data/vendor/ring/crypto/poly1305/poly1305_test.txt +0 -52
  146. data/vendor/ring/crypto/poly1305/poly1305_vec.c +0 -892
  147. data/vendor/ring/crypto/rand/asm/rdrand-x86_64.pl +0 -75
  148. data/vendor/ring/crypto/rand/internal.h +0 -32
  149. data/vendor/ring/crypto/rand/rand.c +0 -189
  150. data/vendor/ring/crypto/rand/urandom.c +0 -219
  151. data/vendor/ring/crypto/rand/windows.c +0 -56
  152. data/vendor/ring/crypto/refcount_c11.c +0 -66
  153. data/vendor/ring/crypto/refcount_lock.c +0 -53
  154. data/vendor/ring/crypto/refcount_test.Windows.vcxproj +0 -25
  155. data/vendor/ring/crypto/refcount_test.c +0 -58
  156. data/vendor/ring/crypto/rsa/blinding.c +0 -462
  157. data/vendor/ring/crypto/rsa/internal.h +0 -108
  158. data/vendor/ring/crypto/rsa/padding.c +0 -300
  159. data/vendor/ring/crypto/rsa/rsa.c +0 -450
  160. data/vendor/ring/crypto/rsa/rsa_asn1.c +0 -261
  161. data/vendor/ring/crypto/rsa/rsa_impl.c +0 -944
  162. data/vendor/ring/crypto/rsa/rsa_test.Windows.vcxproj +0 -25
  163. data/vendor/ring/crypto/rsa/rsa_test.cc +0 -437
  164. data/vendor/ring/crypto/sha/asm/sha-armv8.pl +0 -436
  165. data/vendor/ring/crypto/sha/asm/sha-x86_64.pl +0 -2390
  166. data/vendor/ring/crypto/sha/asm/sha256-586.pl +0 -1275
  167. data/vendor/ring/crypto/sha/asm/sha256-armv4.pl +0 -735
  168. data/vendor/ring/crypto/sha/asm/sha256-armv8.pl +0 -14
  169. data/vendor/ring/crypto/sha/asm/sha256-x86_64.pl +0 -14
  170. data/vendor/ring/crypto/sha/asm/sha512-586.pl +0 -911
  171. data/vendor/ring/crypto/sha/asm/sha512-armv4.pl +0 -666
  172. data/vendor/ring/crypto/sha/asm/sha512-armv8.pl +0 -14
  173. data/vendor/ring/crypto/sha/asm/sha512-x86_64.pl +0 -14
  174. data/vendor/ring/crypto/sha/sha1.c +0 -271
  175. data/vendor/ring/crypto/sha/sha256.c +0 -204
  176. data/vendor/ring/crypto/sha/sha512.c +0 -355
  177. data/vendor/ring/crypto/test/file_test.cc +0 -326
  178. data/vendor/ring/crypto/test/file_test.h +0 -181
  179. data/vendor/ring/crypto/test/malloc.cc +0 -150
  180. data/vendor/ring/crypto/test/scoped_types.h +0 -95
  181. data/vendor/ring/crypto/test/test.Windows.vcxproj +0 -35
  182. data/vendor/ring/crypto/test/test_util.cc +0 -46
  183. data/vendor/ring/crypto/test/test_util.h +0 -41
  184. data/vendor/ring/crypto/thread_none.c +0 -55
  185. data/vendor/ring/crypto/thread_pthread.c +0 -165
  186. data/vendor/ring/crypto/thread_test.Windows.vcxproj +0 -25
  187. data/vendor/ring/crypto/thread_test.c +0 -200
  188. data/vendor/ring/crypto/thread_win.c +0 -282
  189. data/vendor/ring/examples/checkdigest.rs +0 -103
  190. data/vendor/ring/include/openssl/aes.h +0 -121
  191. data/vendor/ring/include/openssl/arm_arch.h +0 -129
  192. data/vendor/ring/include/openssl/base.h +0 -156
  193. data/vendor/ring/include/openssl/bn.h +0 -794
  194. data/vendor/ring/include/openssl/buffer.h +0 -18
  195. data/vendor/ring/include/openssl/bytestring.h +0 -235
  196. data/vendor/ring/include/openssl/chacha.h +0 -37
  197. data/vendor/ring/include/openssl/cmac.h +0 -76
  198. data/vendor/ring/include/openssl/cpu.h +0 -184
  199. data/vendor/ring/include/openssl/crypto.h +0 -43
  200. data/vendor/ring/include/openssl/curve25519.h +0 -88
  201. data/vendor/ring/include/openssl/ec.h +0 -225
  202. data/vendor/ring/include/openssl/ec_key.h +0 -129
  203. data/vendor/ring/include/openssl/ecdh.h +0 -110
  204. data/vendor/ring/include/openssl/ecdsa.h +0 -156
  205. data/vendor/ring/include/openssl/err.h +0 -201
  206. data/vendor/ring/include/openssl/mem.h +0 -101
  207. data/vendor/ring/include/openssl/obj_mac.h +0 -71
  208. data/vendor/ring/include/openssl/opensslfeatures.h +0 -68
  209. data/vendor/ring/include/openssl/opensslv.h +0 -18
  210. data/vendor/ring/include/openssl/ossl_typ.h +0 -18
  211. data/vendor/ring/include/openssl/poly1305.h +0 -51
  212. data/vendor/ring/include/openssl/rand.h +0 -70
  213. data/vendor/ring/include/openssl/rsa.h +0 -399
  214. data/vendor/ring/include/openssl/thread.h +0 -133
  215. data/vendor/ring/include/openssl/type_check.h +0 -71
  216. data/vendor/ring/mk/Common.props +0 -63
  217. data/vendor/ring/mk/Windows.props +0 -42
  218. data/vendor/ring/mk/WindowsTest.props +0 -18
  219. data/vendor/ring/mk/appveyor.bat +0 -62
  220. data/vendor/ring/mk/bottom_of_makefile.mk +0 -54
  221. data/vendor/ring/mk/ring.mk +0 -266
  222. data/vendor/ring/mk/top_of_makefile.mk +0 -214
  223. data/vendor/ring/mk/travis.sh +0 -40
  224. data/vendor/ring/mk/update-travis-yml.py +0 -229
  225. data/vendor/ring/ring.sln +0 -153
  226. data/vendor/ring/src/aead.rs +0 -682
  227. data/vendor/ring/src/agreement.rs +0 -248
  228. data/vendor/ring/src/c.rs +0 -129
  229. data/vendor/ring/src/constant_time.rs +0 -37
  230. data/vendor/ring/src/der.rs +0 -96
  231. data/vendor/ring/src/digest.rs +0 -690
  232. data/vendor/ring/src/digest_tests.txt +0 -57
  233. data/vendor/ring/src/ecc.rs +0 -28
  234. data/vendor/ring/src/ecc_build.rs +0 -279
  235. data/vendor/ring/src/ecc_curves.rs +0 -117
  236. data/vendor/ring/src/ed25519_tests.txt +0 -2579
  237. data/vendor/ring/src/exe_tests.rs +0 -46
  238. data/vendor/ring/src/ffi.rs +0 -29
  239. data/vendor/ring/src/file_test.rs +0 -187
  240. data/vendor/ring/src/hkdf.rs +0 -153
  241. data/vendor/ring/src/hkdf_tests.txt +0 -59
  242. data/vendor/ring/src/hmac.rs +0 -414
  243. data/vendor/ring/src/hmac_tests.txt +0 -97
  244. data/vendor/ring/src/input.rs +0 -312
  245. data/vendor/ring/src/lib.rs +0 -41
  246. data/vendor/ring/src/pbkdf2.rs +0 -265
  247. data/vendor/ring/src/pbkdf2_tests.txt +0 -113
  248. data/vendor/ring/src/polyfill.rs +0 -57
  249. data/vendor/ring/src/rand.rs +0 -28
  250. data/vendor/ring/src/signature.rs +0 -314
  251. data/vendor/ring/third-party/NIST/README.md +0 -9
  252. data/vendor/ring/third-party/NIST/SHAVS/SHA1LongMsg.rsp +0 -263
  253. data/vendor/ring/third-party/NIST/SHAVS/SHA1Monte.rsp +0 -309
  254. data/vendor/ring/third-party/NIST/SHAVS/SHA1ShortMsg.rsp +0 -267
  255. data/vendor/ring/third-party/NIST/SHAVS/SHA224LongMsg.rsp +0 -263
  256. data/vendor/ring/third-party/NIST/SHAVS/SHA224Monte.rsp +0 -309
  257. data/vendor/ring/third-party/NIST/SHAVS/SHA224ShortMsg.rsp +0 -267
  258. data/vendor/ring/third-party/NIST/SHAVS/SHA256LongMsg.rsp +0 -263
  259. data/vendor/ring/third-party/NIST/SHAVS/SHA256Monte.rsp +0 -309
  260. data/vendor/ring/third-party/NIST/SHAVS/SHA256ShortMsg.rsp +0 -267
  261. data/vendor/ring/third-party/NIST/SHAVS/SHA384LongMsg.rsp +0 -519
  262. data/vendor/ring/third-party/NIST/SHAVS/SHA384Monte.rsp +0 -309
  263. data/vendor/ring/third-party/NIST/SHAVS/SHA384ShortMsg.rsp +0 -523
  264. data/vendor/ring/third-party/NIST/SHAVS/SHA512LongMsg.rsp +0 -519
  265. data/vendor/ring/third-party/NIST/SHAVS/SHA512Monte.rsp +0 -309
  266. data/vendor/ring/third-party/NIST/SHAVS/SHA512ShortMsg.rsp +0 -523
  267. data/vendor/ring/third-party/NIST/sha256sums.txt +0 -1
@@ -1,1794 +0,0 @@
1
- /* Copyright (c) 2015, Google Inc.
2
- *
3
- * Permission to use, copy, modify, and/or distribute this software for any
4
- * purpose with or without fee is hereby granted, provided that the above
5
- * copyright notice and this permission notice appear in all copies.
6
- *
7
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
- * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
- * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
-
15
- /* A 64-bit implementation of the NIST P-256 elliptic curve point
16
- * multiplication
17
- *
18
- * OpenSSL integration was taken from Emilia Kasper's work in ecp_nistp224.c.
19
- * Otherwise based on Emilia's P224 work, which was inspired by my curve25519
20
- * work which got its smarts from Daniel J. Bernstein's work on the same. */
21
-
22
- #include <openssl/base.h>
23
-
24
- #if defined(OPENSSL_64_BIT) && !defined(OPENSSL_WINDOWS)
25
-
26
- #include <openssl/bn.h>
27
- #include <openssl/ec.h>
28
- #include <openssl/err.h>
29
- #include <openssl/mem.h>
30
- #include <openssl/obj_mac.h>
31
-
32
- #include <assert.h>
33
- #include <string.h>
34
-
35
- #include "internal.h"
36
-
37
-
38
- typedef uint8_t u8;
39
- typedef uint64_t u64;
40
- typedef int64_t s64;
41
- typedef __uint128_t uint128_t;
42
- typedef __int128_t int128_t;
43
-
44
- /* The underlying field. P256 operates over GF(2^256-2^224+2^192+2^96-1). We
45
- * can serialise an element of this field into 32 bytes. We call this an
46
- * felem_bytearray. */
47
- typedef u8 felem_bytearray[32];
48
-
49
-
50
- /* The representation of field elements.
51
- * ------------------------------------
52
- *
53
- * We represent field elements with either four 128-bit values, eight 128-bit
54
- * values, or four 64-bit values. The field element represented is:
55
- * v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + v[3]*2^192 (mod p)
56
- * or:
57
- * v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + ... + v[8]*2^512 (mod p)
58
- *
59
- * 128-bit values are called 'limbs'. Since the limbs are spaced only 64 bits
60
- * apart, but are 128-bits wide, the most significant bits of each limb overlap
61
- * with the least significant bits of the next.
62
- *
63
- * A field element with four limbs is an 'felem'. One with eight limbs is a
64
- * 'longfelem'
65
- *
66
- * A field element with four, 64-bit values is called a 'smallfelem'. Small
67
- * values are used as intermediate values before multiplication. */
68
-
69
- #define NLIMBS 4
70
-
71
- typedef uint128_t limb;
72
- typedef limb felem[NLIMBS];
73
- typedef limb longfelem[NLIMBS * 2];
74
- typedef u64 smallfelem[NLIMBS];
75
-
76
- /* This is the value of the prime as four 64-bit words, little-endian. */
77
- static const u64 kPrime[4] = {0xfffffffffffffffful, 0xffffffff, 0,
78
- 0xffffffff00000001ul};
79
- static const u64 bottom63bits = 0x7ffffffffffffffful;
80
-
81
- /* bin32_to_felem takes a little-endian byte array and converts it into felem
82
- * form. This assumes that the CPU is little-endian. */
83
- static void bin32_to_felem(felem out, const u8 in[32]) {
84
- out[0] = *((u64 *)&in[0]);
85
- out[1] = *((u64 *)&in[8]);
86
- out[2] = *((u64 *)&in[16]);
87
- out[3] = *((u64 *)&in[24]);
88
- }
89
-
90
- /* smallfelem_to_bin32 takes a smallfelem and serialises into a little endian,
91
- * 32 byte array. This assumes that the CPU is little-endian. */
92
- static void smallfelem_to_bin32(u8 out[32], const smallfelem in) {
93
- *((u64 *)&out[0]) = in[0];
94
- *((u64 *)&out[8]) = in[1];
95
- *((u64 *)&out[16]) = in[2];
96
- *((u64 *)&out[24]) = in[3];
97
- }
98
-
99
- /* To preserve endianness when using BN_bn2bin and BN_bin2bn. */
100
- static void flip_endian(u8 *out, const u8 *in, unsigned len) {
101
- unsigned i;
102
- for (i = 0; i < len; ++i) {
103
- out[i] = in[len - 1 - i];
104
- }
105
- }
106
-
107
- /* BN_to_felem converts an OpenSSL BIGNUM into an felem. */
108
- static int BN_to_felem(felem out, const BIGNUM *bn) {
109
- if (BN_is_negative(bn)) {
110
- OPENSSL_PUT_ERROR(EC, EC_R_BIGNUM_OUT_OF_RANGE);
111
- return 0;
112
- }
113
-
114
- felem_bytearray b_out;
115
- /* BN_bn2bin eats leading zeroes */
116
- memset(b_out, 0, sizeof(b_out));
117
- unsigned num_bytes = BN_num_bytes(bn);
118
- if (num_bytes > sizeof(b_out)) {
119
- OPENSSL_PUT_ERROR(EC, EC_R_BIGNUM_OUT_OF_RANGE);
120
- return 0;
121
- }
122
-
123
- felem_bytearray b_in;
124
- num_bytes = BN_bn2bin(bn, b_in);
125
- flip_endian(b_out, b_in, num_bytes);
126
- bin32_to_felem(out, b_out);
127
- return 1;
128
- }
129
-
130
- /* felem_to_BN converts an felem into an OpenSSL BIGNUM. */
131
- static BIGNUM *smallfelem_to_BN(BIGNUM *out, const smallfelem in) {
132
- felem_bytearray b_in, b_out;
133
- smallfelem_to_bin32(b_in, in);
134
- flip_endian(b_out, b_in, sizeof(b_out));
135
- return BN_bin2bn(b_out, sizeof(b_out), out);
136
- }
137
-
138
- /* Field operations. */
139
-
140
- static void smallfelem_one(smallfelem out) {
141
- out[0] = 1;
142
- out[1] = 0;
143
- out[2] = 0;
144
- out[3] = 0;
145
- }
146
-
147
- static void smallfelem_assign(smallfelem out, const smallfelem in) {
148
- out[0] = in[0];
149
- out[1] = in[1];
150
- out[2] = in[2];
151
- out[3] = in[3];
152
- }
153
-
154
- static void felem_assign(felem out, const felem in) {
155
- out[0] = in[0];
156
- out[1] = in[1];
157
- out[2] = in[2];
158
- out[3] = in[3];
159
- }
160
-
161
- /* felem_sum sets out = out + in. */
162
- static void felem_sum(felem out, const felem in) {
163
- out[0] += in[0];
164
- out[1] += in[1];
165
- out[2] += in[2];
166
- out[3] += in[3];
167
- }
168
-
169
- /* felem_small_sum sets out = out + in. */
170
- static void felem_small_sum(felem out, const smallfelem in) {
171
- out[0] += in[0];
172
- out[1] += in[1];
173
- out[2] += in[2];
174
- out[3] += in[3];
175
- }
176
-
177
- /* felem_scalar sets out = out * scalar */
178
- static void felem_scalar(felem out, const u64 scalar) {
179
- out[0] *= scalar;
180
- out[1] *= scalar;
181
- out[2] *= scalar;
182
- out[3] *= scalar;
183
- }
184
-
185
- /* longfelem_scalar sets out = out * scalar */
186
- static void longfelem_scalar(longfelem out, const u64 scalar) {
187
- out[0] *= scalar;
188
- out[1] *= scalar;
189
- out[2] *= scalar;
190
- out[3] *= scalar;
191
- out[4] *= scalar;
192
- out[5] *= scalar;
193
- out[6] *= scalar;
194
- out[7] *= scalar;
195
- }
196
-
197
- #define two105m41m9 (((limb)1) << 105) - (((limb)1) << 41) - (((limb)1) << 9)
198
- #define two105 (((limb)1) << 105)
199
- #define two105m41p9 (((limb)1) << 105) - (((limb)1) << 41) + (((limb)1) << 9)
200
-
201
- /* zero105 is 0 mod p */
202
- static const felem zero105 = {two105m41m9, two105, two105m41p9, two105m41p9};
203
-
204
- /* smallfelem_neg sets |out| to |-small|
205
- * On exit:
206
- * out[i] < out[i] + 2^105 */
207
- static void smallfelem_neg(felem out, const smallfelem small) {
208
- /* In order to prevent underflow, we subtract from 0 mod p. */
209
- out[0] = zero105[0] - small[0];
210
- out[1] = zero105[1] - small[1];
211
- out[2] = zero105[2] - small[2];
212
- out[3] = zero105[3] - small[3];
213
- }
214
-
215
- /* felem_diff subtracts |in| from |out|
216
- * On entry:
217
- * in[i] < 2^104
218
- * On exit:
219
- * out[i] < out[i] + 2^105. */
220
- static void felem_diff(felem out, const felem in) {
221
- /* In order to prevent underflow, we add 0 mod p before subtracting. */
222
- out[0] += zero105[0];
223
- out[1] += zero105[1];
224
- out[2] += zero105[2];
225
- out[3] += zero105[3];
226
-
227
- out[0] -= in[0];
228
- out[1] -= in[1];
229
- out[2] -= in[2];
230
- out[3] -= in[3];
231
- }
232
-
233
- #define two107m43m11 (((limb)1) << 107) - (((limb)1) << 43) - (((limb)1) << 11)
234
- #define two107 (((limb)1) << 107)
235
- #define two107m43p11 (((limb)1) << 107) - (((limb)1) << 43) + (((limb)1) << 11)
236
-
237
- /* zero107 is 0 mod p */
238
- static const felem zero107 = {two107m43m11, two107, two107m43p11, two107m43p11};
239
-
240
- /* An alternative felem_diff for larger inputs |in|
241
- * felem_diff_zero107 subtracts |in| from |out|
242
- * On entry:
243
- * in[i] < 2^106
244
- * On exit:
245
- * out[i] < out[i] + 2^107. */
246
- static void felem_diff_zero107(felem out, const felem in) {
247
- /* In order to prevent underflow, we add 0 mod p before subtracting. */
248
- out[0] += zero107[0];
249
- out[1] += zero107[1];
250
- out[2] += zero107[2];
251
- out[3] += zero107[3];
252
-
253
- out[0] -= in[0];
254
- out[1] -= in[1];
255
- out[2] -= in[2];
256
- out[3] -= in[3];
257
- }
258
-
259
- /* longfelem_diff subtracts |in| from |out|
260
- * On entry:
261
- * in[i] < 7*2^67
262
- * On exit:
263
- * out[i] < out[i] + 2^70 + 2^40. */
264
- static void longfelem_diff(longfelem out, const longfelem in) {
265
- static const limb two70m8p6 =
266
- (((limb)1) << 70) - (((limb)1) << 8) + (((limb)1) << 6);
267
- static const limb two70p40 = (((limb)1) << 70) + (((limb)1) << 40);
268
- static const limb two70 = (((limb)1) << 70);
269
- static const limb two70m40m38p6 = (((limb)1) << 70) - (((limb)1) << 40) -
270
- (((limb)1) << 38) + (((limb)1) << 6);
271
- static const limb two70m6 = (((limb)1) << 70) - (((limb)1) << 6);
272
-
273
- /* add 0 mod p to avoid underflow */
274
- out[0] += two70m8p6;
275
- out[1] += two70p40;
276
- out[2] += two70;
277
- out[3] += two70m40m38p6;
278
- out[4] += two70m6;
279
- out[5] += two70m6;
280
- out[6] += two70m6;
281
- out[7] += two70m6;
282
-
283
- /* in[i] < 7*2^67 < 2^70 - 2^40 - 2^38 + 2^6 */
284
- out[0] -= in[0];
285
- out[1] -= in[1];
286
- out[2] -= in[2];
287
- out[3] -= in[3];
288
- out[4] -= in[4];
289
- out[5] -= in[5];
290
- out[6] -= in[6];
291
- out[7] -= in[7];
292
- }
293
-
294
- #define two64m0 (((limb)1) << 64) - 1
295
- #define two110p32m0 (((limb)1) << 110) + (((limb)1) << 32) - 1
296
- #define two64m46 (((limb)1) << 64) - (((limb)1) << 46)
297
- #define two64m32 (((limb)1) << 64) - (((limb)1) << 32)
298
-
299
- /* zero110 is 0 mod p. */
300
- static const felem zero110 = {two64m0, two110p32m0, two64m46, two64m32};
301
-
302
- /* felem_shrink converts an felem into a smallfelem. The result isn't quite
303
- * minimal as the value may be greater than p.
304
- *
305
- * On entry:
306
- * in[i] < 2^109
307
- * On exit:
308
- * out[i] < 2^64. */
309
- static void felem_shrink(smallfelem out, const felem in) {
310
- felem tmp;
311
- u64 a, b, mask;
312
- s64 high, low;
313
- static const u64 kPrime3Test = 0x7fffffff00000001ul; /* 2^63 - 2^32 + 1 */
314
-
315
- /* Carry 2->3 */
316
- tmp[3] = zero110[3] + in[3] + ((u64)(in[2] >> 64));
317
- /* tmp[3] < 2^110 */
318
-
319
- tmp[2] = zero110[2] + (u64)in[2];
320
- tmp[0] = zero110[0] + in[0];
321
- tmp[1] = zero110[1] + in[1];
322
- /* tmp[0] < 2**110, tmp[1] < 2^111, tmp[2] < 2**65 */
323
-
324
- /* We perform two partial reductions where we eliminate the high-word of
325
- * tmp[3]. We don't update the other words till the end. */
326
- a = tmp[3] >> 64; /* a < 2^46 */
327
- tmp[3] = (u64)tmp[3];
328
- tmp[3] -= a;
329
- tmp[3] += ((limb)a) << 32;
330
- /* tmp[3] < 2^79 */
331
-
332
- b = a;
333
- a = tmp[3] >> 64; /* a < 2^15 */
334
- b += a; /* b < 2^46 + 2^15 < 2^47 */
335
- tmp[3] = (u64)tmp[3];
336
- tmp[3] -= a;
337
- tmp[3] += ((limb)a) << 32;
338
- /* tmp[3] < 2^64 + 2^47 */
339
-
340
- /* This adjusts the other two words to complete the two partial
341
- * reductions. */
342
- tmp[0] += b;
343
- tmp[1] -= (((limb)b) << 32);
344
-
345
- /* In order to make space in tmp[3] for the carry from 2 -> 3, we
346
- * conditionally subtract kPrime if tmp[3] is large enough. */
347
- high = tmp[3] >> 64;
348
- /* As tmp[3] < 2^65, high is either 1 or 0 */
349
- high <<= 63;
350
- high >>= 63;
351
- /* high is:
352
- * all ones if the high word of tmp[3] is 1
353
- * all zeros if the high word of tmp[3] if 0 */
354
- low = tmp[3];
355
- mask = low >> 63;
356
- /* mask is:
357
- * all ones if the MSB of low is 1
358
- * all zeros if the MSB of low if 0 */
359
- low &= bottom63bits;
360
- low -= kPrime3Test;
361
- /* if low was greater than kPrime3Test then the MSB is zero */
362
- low = ~low;
363
- low >>= 63;
364
- /* low is:
365
- * all ones if low was > kPrime3Test
366
- * all zeros if low was <= kPrime3Test */
367
- mask = (mask & low) | high;
368
- tmp[0] -= mask & kPrime[0];
369
- tmp[1] -= mask & kPrime[1];
370
- /* kPrime[2] is zero, so omitted */
371
- tmp[3] -= mask & kPrime[3];
372
- /* tmp[3] < 2**64 - 2**32 + 1 */
373
-
374
- tmp[1] += ((u64)(tmp[0] >> 64));
375
- tmp[0] = (u64)tmp[0];
376
- tmp[2] += ((u64)(tmp[1] >> 64));
377
- tmp[1] = (u64)tmp[1];
378
- tmp[3] += ((u64)(tmp[2] >> 64));
379
- tmp[2] = (u64)tmp[2];
380
- /* tmp[i] < 2^64 */
381
-
382
- out[0] = tmp[0];
383
- out[1] = tmp[1];
384
- out[2] = tmp[2];
385
- out[3] = tmp[3];
386
- }
387
-
388
- /* smallfelem_expand converts a smallfelem to an felem */
389
- static void smallfelem_expand(felem out, const smallfelem in) {
390
- out[0] = in[0];
391
- out[1] = in[1];
392
- out[2] = in[2];
393
- out[3] = in[3];
394
- }
395
-
396
- /* smallfelem_square sets |out| = |small|^2
397
- * On entry:
398
- * small[i] < 2^64
399
- * On exit:
400
- * out[i] < 7 * 2^64 < 2^67 */
401
- static void smallfelem_square(longfelem out, const smallfelem small) {
402
- limb a;
403
- u64 high, low;
404
-
405
- a = ((uint128_t)small[0]) * small[0];
406
- low = a;
407
- high = a >> 64;
408
- out[0] = low;
409
- out[1] = high;
410
-
411
- a = ((uint128_t)small[0]) * small[1];
412
- low = a;
413
- high = a >> 64;
414
- out[1] += low;
415
- out[1] += low;
416
- out[2] = high;
417
-
418
- a = ((uint128_t)small[0]) * small[2];
419
- low = a;
420
- high = a >> 64;
421
- out[2] += low;
422
- out[2] *= 2;
423
- out[3] = high;
424
-
425
- a = ((uint128_t)small[0]) * small[3];
426
- low = a;
427
- high = a >> 64;
428
- out[3] += low;
429
- out[4] = high;
430
-
431
- a = ((uint128_t)small[1]) * small[2];
432
- low = a;
433
- high = a >> 64;
434
- out[3] += low;
435
- out[3] *= 2;
436
- out[4] += high;
437
-
438
- a = ((uint128_t)small[1]) * small[1];
439
- low = a;
440
- high = a >> 64;
441
- out[2] += low;
442
- out[3] += high;
443
-
444
- a = ((uint128_t)small[1]) * small[3];
445
- low = a;
446
- high = a >> 64;
447
- out[4] += low;
448
- out[4] *= 2;
449
- out[5] = high;
450
-
451
- a = ((uint128_t)small[2]) * small[3];
452
- low = a;
453
- high = a >> 64;
454
- out[5] += low;
455
- out[5] *= 2;
456
- out[6] = high;
457
- out[6] += high;
458
-
459
- a = ((uint128_t)small[2]) * small[2];
460
- low = a;
461
- high = a >> 64;
462
- out[4] += low;
463
- out[5] += high;
464
-
465
- a = ((uint128_t)small[3]) * small[3];
466
- low = a;
467
- high = a >> 64;
468
- out[6] += low;
469
- out[7] = high;
470
- }
471
-
472
- /*felem_square sets |out| = |in|^2
473
- * On entry:
474
- * in[i] < 2^109
475
- * On exit:
476
- * out[i] < 7 * 2^64 < 2^67. */
477
- static void felem_square(longfelem out, const felem in) {
478
- u64 small[4];
479
- felem_shrink(small, in);
480
- smallfelem_square(out, small);
481
- }
482
-
483
- /* smallfelem_mul sets |out| = |small1| * |small2|
484
- * On entry:
485
- * small1[i] < 2^64
486
- * small2[i] < 2^64
487
- * On exit:
488
- * out[i] < 7 * 2^64 < 2^67. */
489
- static void smallfelem_mul(longfelem out, const smallfelem small1,
490
- const smallfelem small2) {
491
- limb a;
492
- u64 high, low;
493
-
494
- a = ((uint128_t)small1[0]) * small2[0];
495
- low = a;
496
- high = a >> 64;
497
- out[0] = low;
498
- out[1] = high;
499
-
500
- a = ((uint128_t)small1[0]) * small2[1];
501
- low = a;
502
- high = a >> 64;
503
- out[1] += low;
504
- out[2] = high;
505
-
506
- a = ((uint128_t)small1[1]) * small2[0];
507
- low = a;
508
- high = a >> 64;
509
- out[1] += low;
510
- out[2] += high;
511
-
512
- a = ((uint128_t)small1[0]) * small2[2];
513
- low = a;
514
- high = a >> 64;
515
- out[2] += low;
516
- out[3] = high;
517
-
518
- a = ((uint128_t)small1[1]) * small2[1];
519
- low = a;
520
- high = a >> 64;
521
- out[2] += low;
522
- out[3] += high;
523
-
524
- a = ((uint128_t)small1[2]) * small2[0];
525
- low = a;
526
- high = a >> 64;
527
- out[2] += low;
528
- out[3] += high;
529
-
530
- a = ((uint128_t)small1[0]) * small2[3];
531
- low = a;
532
- high = a >> 64;
533
- out[3] += low;
534
- out[4] = high;
535
-
536
- a = ((uint128_t)small1[1]) * small2[2];
537
- low = a;
538
- high = a >> 64;
539
- out[3] += low;
540
- out[4] += high;
541
-
542
- a = ((uint128_t)small1[2]) * small2[1];
543
- low = a;
544
- high = a >> 64;
545
- out[3] += low;
546
- out[4] += high;
547
-
548
- a = ((uint128_t)small1[3]) * small2[0];
549
- low = a;
550
- high = a >> 64;
551
- out[3] += low;
552
- out[4] += high;
553
-
554
- a = ((uint128_t)small1[1]) * small2[3];
555
- low = a;
556
- high = a >> 64;
557
- out[4] += low;
558
- out[5] = high;
559
-
560
- a = ((uint128_t)small1[2]) * small2[2];
561
- low = a;
562
- high = a >> 64;
563
- out[4] += low;
564
- out[5] += high;
565
-
566
- a = ((uint128_t)small1[3]) * small2[1];
567
- low = a;
568
- high = a >> 64;
569
- out[4] += low;
570
- out[5] += high;
571
-
572
- a = ((uint128_t)small1[2]) * small2[3];
573
- low = a;
574
- high = a >> 64;
575
- out[5] += low;
576
- out[6] = high;
577
-
578
- a = ((uint128_t)small1[3]) * small2[2];
579
- low = a;
580
- high = a >> 64;
581
- out[5] += low;
582
- out[6] += high;
583
-
584
- a = ((uint128_t)small1[3]) * small2[3];
585
- low = a;
586
- high = a >> 64;
587
- out[6] += low;
588
- out[7] = high;
589
- }
590
-
591
- /* felem_mul sets |out| = |in1| * |in2|
592
- * On entry:
593
- * in1[i] < 2^109
594
- * in2[i] < 2^109
595
- * On exit:
596
- * out[i] < 7 * 2^64 < 2^67 */
597
- static void felem_mul(longfelem out, const felem in1, const felem in2) {
598
- smallfelem small1, small2;
599
- felem_shrink(small1, in1);
600
- felem_shrink(small2, in2);
601
- smallfelem_mul(out, small1, small2);
602
- }
603
-
604
- /* felem_small_mul sets |out| = |small1| * |in2|
605
- * On entry:
606
- * small1[i] < 2^64
607
- * in2[i] < 2^109
608
- * On exit:
609
- * out[i] < 7 * 2^64 < 2^67 */
610
- static void felem_small_mul(longfelem out, const smallfelem small1,
611
- const felem in2) {
612
- smallfelem small2;
613
- felem_shrink(small2, in2);
614
- smallfelem_mul(out, small1, small2);
615
- }
616
-
617
- #define two100m36m4 (((limb)1) << 100) - (((limb)1) << 36) - (((limb)1) << 4)
618
- #define two100 (((limb)1) << 100)
619
- #define two100m36p4 (((limb)1) << 100) - (((limb)1) << 36) + (((limb)1) << 4)
620
-
621
- /* zero100 is 0 mod p */
622
- static const felem zero100 = {two100m36m4, two100, two100m36p4, two100m36p4};
623
-
624
- /* Internal function for the different flavours of felem_reduce.
625
- * felem_reduce_ reduces the higher coefficients in[4]-in[7].
626
- * On entry:
627
- * out[0] >= in[6] + 2^32*in[6] + in[7] + 2^32*in[7]
628
- * out[1] >= in[7] + 2^32*in[4]
629
- * out[2] >= in[5] + 2^32*in[5]
630
- * out[3] >= in[4] + 2^32*in[5] + 2^32*in[6]
631
- * On exit:
632
- * out[0] <= out[0] + in[4] + 2^32*in[5]
633
- * out[1] <= out[1] + in[5] + 2^33*in[6]
634
- * out[2] <= out[2] + in[7] + 2*in[6] + 2^33*in[7]
635
- * out[3] <= out[3] + 2^32*in[4] + 3*in[7] */
636
- static void felem_reduce_(felem out, const longfelem in) {
637
- int128_t c;
638
- /* combine common terms from below */
639
- c = in[4] + (in[5] << 32);
640
- out[0] += c;
641
- out[3] -= c;
642
-
643
- c = in[5] - in[7];
644
- out[1] += c;
645
- out[2] -= c;
646
-
647
- /* the remaining terms */
648
- /* 256: [(0,1),(96,-1),(192,-1),(224,1)] */
649
- out[1] -= (in[4] << 32);
650
- out[3] += (in[4] << 32);
651
-
652
- /* 320: [(32,1),(64,1),(128,-1),(160,-1),(224,-1)] */
653
- out[2] -= (in[5] << 32);
654
-
655
- /* 384: [(0,-1),(32,-1),(96,2),(128,2),(224,-1)] */
656
- out[0] -= in[6];
657
- out[0] -= (in[6] << 32);
658
- out[1] += (in[6] << 33);
659
- out[2] += (in[6] * 2);
660
- out[3] -= (in[6] << 32);
661
-
662
- /* 448: [(0,-1),(32,-1),(64,-1),(128,1),(160,2),(192,3)] */
663
- out[0] -= in[7];
664
- out[0] -= (in[7] << 32);
665
- out[2] += (in[7] << 33);
666
- out[3] += (in[7] * 3);
667
- }
668
-
669
- /* felem_reduce converts a longfelem into an felem.
670
- * To be called directly after felem_square or felem_mul.
671
- * On entry:
672
- * in[0] < 2^64, in[1] < 3*2^64, in[2] < 5*2^64, in[3] < 7*2^64
673
- * in[4] < 7*2^64, in[5] < 5*2^64, in[6] < 3*2^64, in[7] < 2*64
674
- * On exit:
675
- * out[i] < 2^101 */
676
- static void felem_reduce(felem out, const longfelem in) {
677
- out[0] = zero100[0] + in[0];
678
- out[1] = zero100[1] + in[1];
679
- out[2] = zero100[2] + in[2];
680
- out[3] = zero100[3] + in[3];
681
-
682
- felem_reduce_(out, in);
683
-
684
- /* out[0] > 2^100 - 2^36 - 2^4 - 3*2^64 - 3*2^96 - 2^64 - 2^96 > 0
685
- * out[1] > 2^100 - 2^64 - 7*2^96 > 0
686
- * out[2] > 2^100 - 2^36 + 2^4 - 5*2^64 - 5*2^96 > 0
687
- * out[3] > 2^100 - 2^36 + 2^4 - 7*2^64 - 5*2^96 - 3*2^96 > 0
688
- *
689
- * out[0] < 2^100 + 2^64 + 7*2^64 + 5*2^96 < 2^101
690
- * out[1] < 2^100 + 3*2^64 + 5*2^64 + 3*2^97 < 2^101
691
- * out[2] < 2^100 + 5*2^64 + 2^64 + 3*2^65 + 2^97 < 2^101
692
- * out[3] < 2^100 + 7*2^64 + 7*2^96 + 3*2^64 < 2^101 */
693
- }
694
-
695
- /* felem_reduce_zero105 converts a larger longfelem into an felem.
696
- * On entry:
697
- * in[0] < 2^71
698
- * On exit:
699
- * out[i] < 2^106 */
700
- static void felem_reduce_zero105(felem out, const longfelem in) {
701
- out[0] = zero105[0] + in[0];
702
- out[1] = zero105[1] + in[1];
703
- out[2] = zero105[2] + in[2];
704
- out[3] = zero105[3] + in[3];
705
-
706
- felem_reduce_(out, in);
707
-
708
- /* out[0] > 2^105 - 2^41 - 2^9 - 2^71 - 2^103 - 2^71 - 2^103 > 0
709
- * out[1] > 2^105 - 2^71 - 2^103 > 0
710
- * out[2] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 > 0
711
- * out[3] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 - 2^103 > 0
712
- *
713
- * out[0] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
714
- * out[1] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
715
- * out[2] < 2^105 + 2^71 + 2^71 + 2^71 + 2^103 < 2^106
716
- * out[3] < 2^105 + 2^71 + 2^103 + 2^71 < 2^106 */
717
- }
718
-
719
- /* subtract_u64 sets *result = *result - v and *carry to one if the
720
- * subtraction underflowed. */
721
- static void subtract_u64(u64 *result, u64 *carry, u64 v) {
722
- uint128_t r = *result;
723
- r -= v;
724
- *carry = (r >> 64) & 1;
725
- *result = (u64)r;
726
- }
727
-
728
- /* felem_contract converts |in| to its unique, minimal representation. On
729
- * entry: in[i] < 2^109. */
730
- static void felem_contract(smallfelem out, const felem in) {
731
- u64 all_equal_so_far = 0, result = 0;
732
-
733
- felem_shrink(out, in);
734
- /* small is minimal except that the value might be > p */
735
-
736
- all_equal_so_far--;
737
- /* We are doing a constant time test if out >= kPrime. We need to compare
738
- * each u64, from most-significant to least significant. For each one, if
739
- * all words so far have been equal (m is all ones) then a non-equal
740
- * result is the answer. Otherwise we continue. */
741
- unsigned i;
742
- for (i = 3; i < 4; i--) {
743
- u64 equal;
744
- uint128_t a = ((uint128_t)kPrime[i]) - out[i];
745
- /* if out[i] > kPrime[i] then a will underflow and the high 64-bits
746
- * will all be set. */
747
- result |= all_equal_so_far & ((u64)(a >> 64));
748
-
749
- /* if kPrime[i] == out[i] then |equal| will be all zeros and the
750
- * decrement will make it all ones. */
751
- equal = kPrime[i] ^ out[i];
752
- equal--;
753
- equal &= equal << 32;
754
- equal &= equal << 16;
755
- equal &= equal << 8;
756
- equal &= equal << 4;
757
- equal &= equal << 2;
758
- equal &= equal << 1;
759
- equal = ((s64)equal) >> 63;
760
-
761
- all_equal_so_far &= equal;
762
- }
763
-
764
- /* if all_equal_so_far is still all ones then the two values are equal
765
- * and so out >= kPrime is true. */
766
- result |= all_equal_so_far;
767
-
768
- /* if out >= kPrime then we subtract kPrime. */
769
- u64 carry;
770
- subtract_u64(&out[0], &carry, result & kPrime[0]);
771
- subtract_u64(&out[1], &carry, carry);
772
- subtract_u64(&out[2], &carry, carry);
773
- subtract_u64(&out[3], &carry, carry);
774
-
775
- subtract_u64(&out[1], &carry, result & kPrime[1]);
776
- subtract_u64(&out[2], &carry, carry);
777
- subtract_u64(&out[3], &carry, carry);
778
-
779
- subtract_u64(&out[2], &carry, result & kPrime[2]);
780
- subtract_u64(&out[3], &carry, carry);
781
-
782
- subtract_u64(&out[3], &carry, result & kPrime[3]);
783
- }
784
-
785
- static void smallfelem_square_contract(smallfelem out, const smallfelem in) {
786
- longfelem longtmp;
787
- felem tmp;
788
-
789
- smallfelem_square(longtmp, in);
790
- felem_reduce(tmp, longtmp);
791
- felem_contract(out, tmp);
792
- }
793
-
794
- static void smallfelem_mul_contract(smallfelem out, const smallfelem in1,
795
- const smallfelem in2) {
796
- longfelem longtmp;
797
- felem tmp;
798
-
799
- smallfelem_mul(longtmp, in1, in2);
800
- felem_reduce(tmp, longtmp);
801
- felem_contract(out, tmp);
802
- }
803
-
804
- /* felem_is_zero returns a limb with all bits set if |in| == 0 (mod p) and 0
805
- * otherwise.
806
- * On entry:
807
- * small[i] < 2^64 */
808
- static limb smallfelem_is_zero(const smallfelem small) {
809
- limb result;
810
- u64 is_p;
811
-
812
- u64 is_zero = small[0] | small[1] | small[2] | small[3];
813
- is_zero--;
814
- is_zero &= is_zero << 32;
815
- is_zero &= is_zero << 16;
816
- is_zero &= is_zero << 8;
817
- is_zero &= is_zero << 4;
818
- is_zero &= is_zero << 2;
819
- is_zero &= is_zero << 1;
820
- is_zero = ((s64)is_zero) >> 63;
821
-
822
- is_p = (small[0] ^ kPrime[0]) | (small[1] ^ kPrime[1]) |
823
- (small[2] ^ kPrime[2]) | (small[3] ^ kPrime[3]);
824
- is_p--;
825
- is_p &= is_p << 32;
826
- is_p &= is_p << 16;
827
- is_p &= is_p << 8;
828
- is_p &= is_p << 4;
829
- is_p &= is_p << 2;
830
- is_p &= is_p << 1;
831
- is_p = ((s64)is_p) >> 63;
832
-
833
- is_zero |= is_p;
834
-
835
- result = is_zero;
836
- result |= ((limb)is_zero) << 64;
837
- return result;
838
- }
839
-
840
- static int smallfelem_is_zero_int(const smallfelem small) {
841
- return (int)(smallfelem_is_zero(small) & ((limb)1));
842
- }
843
-
844
- /* felem_inv calculates |out| = |in|^{-1}
845
- *
846
- * Based on Fermat's Little Theorem:
847
- * a^p = a (mod p)
848
- * a^{p-1} = 1 (mod p)
849
- * a^{p-2} = a^{-1} (mod p) */
850
- static void felem_inv(felem out, const felem in) {
851
- felem ftmp, ftmp2;
852
- /* each e_I will hold |in|^{2^I - 1} */
853
- felem e2, e4, e8, e16, e32, e64;
854
- longfelem tmp;
855
- unsigned i;
856
-
857
- felem_square(tmp, in);
858
- felem_reduce(ftmp, tmp); /* 2^1 */
859
- felem_mul(tmp, in, ftmp);
860
- felem_reduce(ftmp, tmp); /* 2^2 - 2^0 */
861
- felem_assign(e2, ftmp);
862
- felem_square(tmp, ftmp);
863
- felem_reduce(ftmp, tmp); /* 2^3 - 2^1 */
864
- felem_square(tmp, ftmp);
865
- felem_reduce(ftmp, tmp); /* 2^4 - 2^2 */
866
- felem_mul(tmp, ftmp, e2);
867
- felem_reduce(ftmp, tmp); /* 2^4 - 2^0 */
868
- felem_assign(e4, ftmp);
869
- felem_square(tmp, ftmp);
870
- felem_reduce(ftmp, tmp); /* 2^5 - 2^1 */
871
- felem_square(tmp, ftmp);
872
- felem_reduce(ftmp, tmp); /* 2^6 - 2^2 */
873
- felem_square(tmp, ftmp);
874
- felem_reduce(ftmp, tmp); /* 2^7 - 2^3 */
875
- felem_square(tmp, ftmp);
876
- felem_reduce(ftmp, tmp); /* 2^8 - 2^4 */
877
- felem_mul(tmp, ftmp, e4);
878
- felem_reduce(ftmp, tmp); /* 2^8 - 2^0 */
879
- felem_assign(e8, ftmp);
880
- for (i = 0; i < 8; i++) {
881
- felem_square(tmp, ftmp);
882
- felem_reduce(ftmp, tmp);
883
- } /* 2^16 - 2^8 */
884
- felem_mul(tmp, ftmp, e8);
885
- felem_reduce(ftmp, tmp); /* 2^16 - 2^0 */
886
- felem_assign(e16, ftmp);
887
- for (i = 0; i < 16; i++) {
888
- felem_square(tmp, ftmp);
889
- felem_reduce(ftmp, tmp);
890
- } /* 2^32 - 2^16 */
891
- felem_mul(tmp, ftmp, e16);
892
- felem_reduce(ftmp, tmp); /* 2^32 - 2^0 */
893
- felem_assign(e32, ftmp);
894
- for (i = 0; i < 32; i++) {
895
- felem_square(tmp, ftmp);
896
- felem_reduce(ftmp, tmp);
897
- } /* 2^64 - 2^32 */
898
- felem_assign(e64, ftmp);
899
- felem_mul(tmp, ftmp, in);
900
- felem_reduce(ftmp, tmp); /* 2^64 - 2^32 + 2^0 */
901
- for (i = 0; i < 192; i++) {
902
- felem_square(tmp, ftmp);
903
- felem_reduce(ftmp, tmp);
904
- } /* 2^256 - 2^224 + 2^192 */
905
-
906
- felem_mul(tmp, e64, e32);
907
- felem_reduce(ftmp2, tmp); /* 2^64 - 2^0 */
908
- for (i = 0; i < 16; i++) {
909
- felem_square(tmp, ftmp2);
910
- felem_reduce(ftmp2, tmp);
911
- } /* 2^80 - 2^16 */
912
- felem_mul(tmp, ftmp2, e16);
913
- felem_reduce(ftmp2, tmp); /* 2^80 - 2^0 */
914
- for (i = 0; i < 8; i++) {
915
- felem_square(tmp, ftmp2);
916
- felem_reduce(ftmp2, tmp);
917
- } /* 2^88 - 2^8 */
918
- felem_mul(tmp, ftmp2, e8);
919
- felem_reduce(ftmp2, tmp); /* 2^88 - 2^0 */
920
- for (i = 0; i < 4; i++) {
921
- felem_square(tmp, ftmp2);
922
- felem_reduce(ftmp2, tmp);
923
- } /* 2^92 - 2^4 */
924
- felem_mul(tmp, ftmp2, e4);
925
- felem_reduce(ftmp2, tmp); /* 2^92 - 2^0 */
926
- felem_square(tmp, ftmp2);
927
- felem_reduce(ftmp2, tmp); /* 2^93 - 2^1 */
928
- felem_square(tmp, ftmp2);
929
- felem_reduce(ftmp2, tmp); /* 2^94 - 2^2 */
930
- felem_mul(tmp, ftmp2, e2);
931
- felem_reduce(ftmp2, tmp); /* 2^94 - 2^0 */
932
- felem_square(tmp, ftmp2);
933
- felem_reduce(ftmp2, tmp); /* 2^95 - 2^1 */
934
- felem_square(tmp, ftmp2);
935
- felem_reduce(ftmp2, tmp); /* 2^96 - 2^2 */
936
- felem_mul(tmp, ftmp2, in);
937
- felem_reduce(ftmp2, tmp); /* 2^96 - 3 */
938
-
939
- felem_mul(tmp, ftmp2, ftmp);
940
- felem_reduce(out, tmp); /* 2^256 - 2^224 + 2^192 + 2^96 - 3 */
941
- }
942
-
943
- static void smallfelem_inv_contract(smallfelem out, const smallfelem in) {
944
- felem tmp;
945
-
946
- smallfelem_expand(tmp, in);
947
- felem_inv(tmp, tmp);
948
- felem_contract(out, tmp);
949
- }
950
-
951
- /* Group operations
952
- * ----------------
953
- *
954
- * Building on top of the field operations we have the operations on the
955
- * elliptic curve group itself. Points on the curve are represented in Jacobian
956
- * coordinates. */
957
-
958
- /* point_double calculates 2*(x_in, y_in, z_in)
959
- *
960
- * The method is taken from:
961
- * http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
962
- *
963
- * Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed.
964
- * while x_out == y_in is not (maybe this works, but it's not tested). */
965
- static void point_double(felem x_out, felem y_out, felem z_out,
966
- const felem x_in, const felem y_in, const felem z_in) {
967
- longfelem tmp, tmp2;
968
- felem delta, gamma, beta, alpha, ftmp, ftmp2;
969
- smallfelem small1, small2;
970
-
971
- felem_assign(ftmp, x_in);
972
- /* ftmp[i] < 2^106 */
973
- felem_assign(ftmp2, x_in);
974
- /* ftmp2[i] < 2^106 */
975
-
976
- /* delta = z^2 */
977
- felem_square(tmp, z_in);
978
- felem_reduce(delta, tmp);
979
- /* delta[i] < 2^101 */
980
-
981
- /* gamma = y^2 */
982
- felem_square(tmp, y_in);
983
- felem_reduce(gamma, tmp);
984
- /* gamma[i] < 2^101 */
985
- felem_shrink(small1, gamma);
986
-
987
- /* beta = x*gamma */
988
- felem_small_mul(tmp, small1, x_in);
989
- felem_reduce(beta, tmp);
990
- /* beta[i] < 2^101 */
991
-
992
- /* alpha = 3*(x-delta)*(x+delta) */
993
- felem_diff(ftmp, delta);
994
- /* ftmp[i] < 2^105 + 2^106 < 2^107 */
995
- felem_sum(ftmp2, delta);
996
- /* ftmp2[i] < 2^105 + 2^106 < 2^107 */
997
- felem_scalar(ftmp2, 3);
998
- /* ftmp2[i] < 3 * 2^107 < 2^109 */
999
- felem_mul(tmp, ftmp, ftmp2);
1000
- felem_reduce(alpha, tmp);
1001
- /* alpha[i] < 2^101 */
1002
- felem_shrink(small2, alpha);
1003
-
1004
- /* x' = alpha^2 - 8*beta */
1005
- smallfelem_square(tmp, small2);
1006
- felem_reduce(x_out, tmp);
1007
- felem_assign(ftmp, beta);
1008
- felem_scalar(ftmp, 8);
1009
- /* ftmp[i] < 8 * 2^101 = 2^104 */
1010
- felem_diff(x_out, ftmp);
1011
- /* x_out[i] < 2^105 + 2^101 < 2^106 */
1012
-
1013
- /* z' = (y + z)^2 - gamma - delta */
1014
- felem_sum(delta, gamma);
1015
- /* delta[i] < 2^101 + 2^101 = 2^102 */
1016
- felem_assign(ftmp, y_in);
1017
- felem_sum(ftmp, z_in);
1018
- /* ftmp[i] < 2^106 + 2^106 = 2^107 */
1019
- felem_square(tmp, ftmp);
1020
- felem_reduce(z_out, tmp);
1021
- felem_diff(z_out, delta);
1022
- /* z_out[i] < 2^105 + 2^101 < 2^106 */
1023
-
1024
- /* y' = alpha*(4*beta - x') - 8*gamma^2 */
1025
- felem_scalar(beta, 4);
1026
- /* beta[i] < 4 * 2^101 = 2^103 */
1027
- felem_diff_zero107(beta, x_out);
1028
- /* beta[i] < 2^107 + 2^103 < 2^108 */
1029
- felem_small_mul(tmp, small2, beta);
1030
- /* tmp[i] < 7 * 2^64 < 2^67 */
1031
- smallfelem_square(tmp2, small1);
1032
- /* tmp2[i] < 7 * 2^64 */
1033
- longfelem_scalar(tmp2, 8);
1034
- /* tmp2[i] < 8 * 7 * 2^64 = 7 * 2^67 */
1035
- longfelem_diff(tmp, tmp2);
1036
- /* tmp[i] < 2^67 + 2^70 + 2^40 < 2^71 */
1037
- felem_reduce_zero105(y_out, tmp);
1038
- /* y_out[i] < 2^106 */
1039
- }
1040
-
1041
- /* point_double_small is the same as point_double, except that it operates on
1042
- * smallfelems. */
1043
- static void point_double_small(smallfelem x_out, smallfelem y_out,
1044
- smallfelem z_out, const smallfelem x_in,
1045
- const smallfelem y_in, const smallfelem z_in) {
1046
- felem felem_x_out, felem_y_out, felem_z_out;
1047
- felem felem_x_in, felem_y_in, felem_z_in;
1048
-
1049
- smallfelem_expand(felem_x_in, x_in);
1050
- smallfelem_expand(felem_y_in, y_in);
1051
- smallfelem_expand(felem_z_in, z_in);
1052
- point_double(felem_x_out, felem_y_out, felem_z_out, felem_x_in, felem_y_in,
1053
- felem_z_in);
1054
- felem_shrink(x_out, felem_x_out);
1055
- felem_shrink(y_out, felem_y_out);
1056
- felem_shrink(z_out, felem_z_out);
1057
- }
1058
-
1059
- /* copy_conditional copies in to out iff mask is all ones. */
1060
- static void copy_conditional(felem out, const felem in, limb mask) {
1061
- unsigned i;
1062
- for (i = 0; i < NLIMBS; ++i) {
1063
- const limb tmp = mask & (in[i] ^ out[i]);
1064
- out[i] ^= tmp;
1065
- }
1066
- }
1067
-
1068
- /* copy_small_conditional copies in to out iff mask is all ones. */
1069
- static void copy_small_conditional(felem out, const smallfelem in, limb mask) {
1070
- unsigned i;
1071
- const u64 mask64 = mask;
1072
- for (i = 0; i < NLIMBS; ++i) {
1073
- out[i] = ((limb)(in[i] & mask64)) | (out[i] & ~mask);
1074
- }
1075
- }
1076
-
1077
- /* point_add calcuates (x1, y1, z1) + (x2, y2, z2)
1078
- *
1079
- * The method is taken from:
1080
- * http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl,
1081
- * adapted for mixed addition (z2 = 1, or z2 = 0 for the point at infinity).
1082
- *
1083
- * This function includes a branch for checking whether the two input points
1084
- * are equal, (while not equal to the point at infinity). This case never
1085
- * happens during single point multiplication, so there is no timing leak for
1086
- * ECDH or ECDSA signing. */
1087
- static void point_add(felem x3, felem y3, felem z3, const felem x1,
1088
- const felem y1, const felem z1, const int mixed,
1089
- const smallfelem x2, const smallfelem y2,
1090
- const smallfelem z2) {
1091
- felem ftmp, ftmp2, ftmp3, ftmp4, ftmp5, ftmp6, x_out, y_out, z_out;
1092
- longfelem tmp, tmp2;
1093
- smallfelem small1, small2, small3, small4, small5;
1094
- limb x_equal, y_equal, z1_is_zero, z2_is_zero;
1095
-
1096
- felem_shrink(small3, z1);
1097
-
1098
- z1_is_zero = smallfelem_is_zero(small3);
1099
- z2_is_zero = smallfelem_is_zero(z2);
1100
-
1101
- /* ftmp = z1z1 = z1**2 */
1102
- smallfelem_square(tmp, small3);
1103
- felem_reduce(ftmp, tmp);
1104
- /* ftmp[i] < 2^101 */
1105
- felem_shrink(small1, ftmp);
1106
-
1107
- if (!mixed) {
1108
- /* ftmp2 = z2z2 = z2**2 */
1109
- smallfelem_square(tmp, z2);
1110
- felem_reduce(ftmp2, tmp);
1111
- /* ftmp2[i] < 2^101 */
1112
- felem_shrink(small2, ftmp2);
1113
-
1114
- felem_shrink(small5, x1);
1115
-
1116
- /* u1 = ftmp3 = x1*z2z2 */
1117
- smallfelem_mul(tmp, small5, small2);
1118
- felem_reduce(ftmp3, tmp);
1119
- /* ftmp3[i] < 2^101 */
1120
-
1121
- /* ftmp5 = z1 + z2 */
1122
- felem_assign(ftmp5, z1);
1123
- felem_small_sum(ftmp5, z2);
1124
- /* ftmp5[i] < 2^107 */
1125
-
1126
- /* ftmp5 = (z1 + z2)**2 - (z1z1 + z2z2) = 2z1z2 */
1127
- felem_square(tmp, ftmp5);
1128
- felem_reduce(ftmp5, tmp);
1129
- /* ftmp2 = z2z2 + z1z1 */
1130
- felem_sum(ftmp2, ftmp);
1131
- /* ftmp2[i] < 2^101 + 2^101 = 2^102 */
1132
- felem_diff(ftmp5, ftmp2);
1133
- /* ftmp5[i] < 2^105 + 2^101 < 2^106 */
1134
-
1135
- /* ftmp2 = z2 * z2z2 */
1136
- smallfelem_mul(tmp, small2, z2);
1137
- felem_reduce(ftmp2, tmp);
1138
-
1139
- /* s1 = ftmp2 = y1 * z2**3 */
1140
- felem_mul(tmp, y1, ftmp2);
1141
- felem_reduce(ftmp6, tmp);
1142
- /* ftmp6[i] < 2^101 */
1143
- } else {
1144
- /* We'll assume z2 = 1 (special case z2 = 0 is handled later). */
1145
-
1146
- /* u1 = ftmp3 = x1*z2z2 */
1147
- felem_assign(ftmp3, x1);
1148
- /* ftmp3[i] < 2^106 */
1149
-
1150
- /* ftmp5 = 2z1z2 */
1151
- felem_assign(ftmp5, z1);
1152
- felem_scalar(ftmp5, 2);
1153
- /* ftmp5[i] < 2*2^106 = 2^107 */
1154
-
1155
- /* s1 = ftmp2 = y1 * z2**3 */
1156
- felem_assign(ftmp6, y1);
1157
- /* ftmp6[i] < 2^106 */
1158
- }
1159
-
1160
- /* u2 = x2*z1z1 */
1161
- smallfelem_mul(tmp, x2, small1);
1162
- felem_reduce(ftmp4, tmp);
1163
-
1164
- /* h = ftmp4 = u2 - u1 */
1165
- felem_diff_zero107(ftmp4, ftmp3);
1166
- /* ftmp4[i] < 2^107 + 2^101 < 2^108 */
1167
- felem_shrink(small4, ftmp4);
1168
-
1169
- x_equal = smallfelem_is_zero(small4);
1170
-
1171
- /* z_out = ftmp5 * h */
1172
- felem_small_mul(tmp, small4, ftmp5);
1173
- felem_reduce(z_out, tmp);
1174
- /* z_out[i] < 2^101 */
1175
-
1176
- /* ftmp = z1 * z1z1 */
1177
- smallfelem_mul(tmp, small1, small3);
1178
- felem_reduce(ftmp, tmp);
1179
-
1180
- /* s2 = tmp = y2 * z1**3 */
1181
- felem_small_mul(tmp, y2, ftmp);
1182
- felem_reduce(ftmp5, tmp);
1183
-
1184
- /* r = ftmp5 = (s2 - s1)*2 */
1185
- felem_diff_zero107(ftmp5, ftmp6);
1186
- /* ftmp5[i] < 2^107 + 2^107 = 2^108 */
1187
- felem_scalar(ftmp5, 2);
1188
- /* ftmp5[i] < 2^109 */
1189
- felem_shrink(small1, ftmp5);
1190
- y_equal = smallfelem_is_zero(small1);
1191
-
1192
- if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) {
1193
- point_double(x3, y3, z3, x1, y1, z1);
1194
- return;
1195
- }
1196
-
1197
- /* I = ftmp = (2h)**2 */
1198
- felem_assign(ftmp, ftmp4);
1199
- felem_scalar(ftmp, 2);
1200
- /* ftmp[i] < 2*2^108 = 2^109 */
1201
- felem_square(tmp, ftmp);
1202
- felem_reduce(ftmp, tmp);
1203
-
1204
- /* J = ftmp2 = h * I */
1205
- felem_mul(tmp, ftmp4, ftmp);
1206
- felem_reduce(ftmp2, tmp);
1207
-
1208
- /* V = ftmp4 = U1 * I */
1209
- felem_mul(tmp, ftmp3, ftmp);
1210
- felem_reduce(ftmp4, tmp);
1211
-
1212
- /* x_out = r**2 - J - 2V */
1213
- smallfelem_square(tmp, small1);
1214
- felem_reduce(x_out, tmp);
1215
- felem_assign(ftmp3, ftmp4);
1216
- felem_scalar(ftmp4, 2);
1217
- felem_sum(ftmp4, ftmp2);
1218
- /* ftmp4[i] < 2*2^101 + 2^101 < 2^103 */
1219
- felem_diff(x_out, ftmp4);
1220
- /* x_out[i] < 2^105 + 2^101 */
1221
-
1222
- /* y_out = r(V-x_out) - 2 * s1 * J */
1223
- felem_diff_zero107(ftmp3, x_out);
1224
- /* ftmp3[i] < 2^107 + 2^101 < 2^108 */
1225
- felem_small_mul(tmp, small1, ftmp3);
1226
- felem_mul(tmp2, ftmp6, ftmp2);
1227
- longfelem_scalar(tmp2, 2);
1228
- /* tmp2[i] < 2*2^67 = 2^68 */
1229
- longfelem_diff(tmp, tmp2);
1230
- /* tmp[i] < 2^67 + 2^70 + 2^40 < 2^71 */
1231
- felem_reduce_zero105(y_out, tmp);
1232
- /* y_out[i] < 2^106 */
1233
-
1234
- copy_small_conditional(x_out, x2, z1_is_zero);
1235
- copy_conditional(x_out, x1, z2_is_zero);
1236
- copy_small_conditional(y_out, y2, z1_is_zero);
1237
- copy_conditional(y_out, y1, z2_is_zero);
1238
- copy_small_conditional(z_out, z2, z1_is_zero);
1239
- copy_conditional(z_out, z1, z2_is_zero);
1240
- felem_assign(x3, x_out);
1241
- felem_assign(y3, y_out);
1242
- felem_assign(z3, z_out);
1243
- }
1244
-
1245
- /* point_add_small is the same as point_add, except that it operates on
1246
- * smallfelems. */
1247
- static void point_add_small(smallfelem x3, smallfelem y3, smallfelem z3,
1248
- smallfelem x1, smallfelem y1, smallfelem z1,
1249
- smallfelem x2, smallfelem y2, smallfelem z2) {
1250
- felem felem_x3, felem_y3, felem_z3;
1251
- felem felem_x1, felem_y1, felem_z1;
1252
- smallfelem_expand(felem_x1, x1);
1253
- smallfelem_expand(felem_y1, y1);
1254
- smallfelem_expand(felem_z1, z1);
1255
- point_add(felem_x3, felem_y3, felem_z3, felem_x1, felem_y1, felem_z1, 0, x2,
1256
- y2, z2);
1257
- felem_shrink(x3, felem_x3);
1258
- felem_shrink(y3, felem_y3);
1259
- felem_shrink(z3, felem_z3);
1260
- }
1261
-
1262
- /* Base point pre computation
1263
- * --------------------------
1264
- *
1265
- * Two different sorts of precomputed tables are used in the following code.
1266
- * Each contain various points on the curve, where each point is three field
1267
- * elements (x, y, z).
1268
- *
1269
- * For the base point table, z is usually 1 (0 for the point at infinity).
1270
- * This table has 2 * 16 elements, starting with the following:
1271
- * index | bits | point
1272
- * ------+---------+------------------------------
1273
- * 0 | 0 0 0 0 | 0G
1274
- * 1 | 0 0 0 1 | 1G
1275
- * 2 | 0 0 1 0 | 2^64G
1276
- * 3 | 0 0 1 1 | (2^64 + 1)G
1277
- * 4 | 0 1 0 0 | 2^128G
1278
- * 5 | 0 1 0 1 | (2^128 + 1)G
1279
- * 6 | 0 1 1 0 | (2^128 + 2^64)G
1280
- * 7 | 0 1 1 1 | (2^128 + 2^64 + 1)G
1281
- * 8 | 1 0 0 0 | 2^192G
1282
- * 9 | 1 0 0 1 | (2^192 + 1)G
1283
- * 10 | 1 0 1 0 | (2^192 + 2^64)G
1284
- * 11 | 1 0 1 1 | (2^192 + 2^64 + 1)G
1285
- * 12 | 1 1 0 0 | (2^192 + 2^128)G
1286
- * 13 | 1 1 0 1 | (2^192 + 2^128 + 1)G
1287
- * 14 | 1 1 1 0 | (2^192 + 2^128 + 2^64)G
1288
- * 15 | 1 1 1 1 | (2^192 + 2^128 + 2^64 + 1)G
1289
- * followed by a copy of this with each element multiplied by 2^32.
1290
- *
1291
- * The reason for this is so that we can clock bits into four different
1292
- * locations when doing simple scalar multiplies against the base point,
1293
- * and then another four locations using the second 16 elements.
1294
- *
1295
- * Tables for other points have table[i] = iG for i in 0 .. 16. */
1296
-
1297
- /* g_pre_comp is the table of precomputed base points */
1298
- static const smallfelem g_pre_comp[2][16][3] = {
1299
- {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
1300
- {{0xf4a13945d898c296, 0x77037d812deb33a0, 0xf8bce6e563a440f2,
1301
- 0x6b17d1f2e12c4247},
1302
- {0xcbb6406837bf51f5, 0x2bce33576b315ece, 0x8ee7eb4a7c0f9e16,
1303
- 0x4fe342e2fe1a7f9b},
1304
- {1, 0, 0, 0}},
1305
- {{0x90e75cb48e14db63, 0x29493baaad651f7e, 0x8492592e326e25de,
1306
- 0x0fa822bc2811aaa5},
1307
- {0xe41124545f462ee7, 0x34b1a65050fe82f5, 0x6f4ad4bcb3df188b,
1308
- 0xbff44ae8f5dba80d},
1309
- {1, 0, 0, 0}},
1310
- {{0x93391ce2097992af, 0xe96c98fd0d35f1fa, 0xb257c0de95e02789,
1311
- 0x300a4bbc89d6726f},
1312
- {0xaa54a291c08127a0, 0x5bb1eeada9d806a5, 0x7f1ddb25ff1e3c6f,
1313
- 0x72aac7e0d09b4644},
1314
- {1, 0, 0, 0}},
1315
- {{0x57c84fc9d789bd85, 0xfc35ff7dc297eac3, 0xfb982fd588c6766e,
1316
- 0x447d739beedb5e67},
1317
- {0x0c7e33c972e25b32, 0x3d349b95a7fae500, 0xe12e9d953a4aaff7,
1318
- 0x2d4825ab834131ee},
1319
- {1, 0, 0, 0}},
1320
- {{0x13949c932a1d367f, 0xef7fbd2b1a0a11b7, 0xddc6068bb91dfc60,
1321
- 0xef9519328a9c72ff},
1322
- {0x196035a77376d8a8, 0x23183b0895ca1740, 0xc1ee9807022c219c,
1323
- 0x611e9fc37dbb2c9b},
1324
- {1, 0, 0, 0}},
1325
- {{0xcae2b1920b57f4bc, 0x2936df5ec6c9bc36, 0x7dea6482e11238bf,
1326
- 0x550663797b51f5d8},
1327
- {0x44ffe216348a964c, 0x9fb3d576dbdefbe1, 0x0afa40018d9d50e5,
1328
- 0x157164848aecb851},
1329
- {1, 0, 0, 0}},
1330
- {{0xe48ecafffc5cde01, 0x7ccd84e70d715f26, 0xa2e8f483f43e4391,
1331
- 0xeb5d7745b21141ea},
1332
- {0xcac917e2731a3479, 0x85f22cfe2844b645, 0x0990e6a158006cee,
1333
- 0xeafd72ebdbecc17b},
1334
- {1, 0, 0, 0}},
1335
- {{0x6cf20ffb313728be, 0x96439591a3c6b94a, 0x2736ff8344315fc5,
1336
- 0xa6d39677a7849276},
1337
- {0xf2bab833c357f5f4, 0x824a920c2284059b, 0x66b8babd2d27ecdf,
1338
- 0x674f84749b0b8816},
1339
- {1, 0, 0, 0}},
1340
- {{0x2df48c04677c8a3e, 0x74e02f080203a56b, 0x31855f7db8c7fedb,
1341
- 0x4e769e7672c9ddad},
1342
- {0xa4c36165b824bbb0, 0xfb9ae16f3b9122a5, 0x1ec0057206947281,
1343
- 0x42b99082de830663},
1344
- {1, 0, 0, 0}},
1345
- {{0x6ef95150dda868b9, 0xd1f89e799c0ce131, 0x7fdc1ca008a1c478,
1346
- 0x78878ef61c6ce04d},
1347
- {0x9c62b9121fe0d976, 0x6ace570ebde08d4f, 0xde53142c12309def,
1348
- 0xb6cb3f5d7b72c321},
1349
- {1, 0, 0, 0}},
1350
- {{0x7f991ed2c31a3573, 0x5b82dd5bd54fb496, 0x595c5220812ffcae,
1351
- 0x0c88bc4d716b1287},
1352
- {0x3a57bf635f48aca8, 0x7c8181f4df2564f3, 0x18d1b5b39c04e6aa,
1353
- 0xdd5ddea3f3901dc6},
1354
- {1, 0, 0, 0}},
1355
- {{0xe96a79fb3e72ad0c, 0x43a0a28c42ba792f, 0xefe0a423083e49f3,
1356
- 0x68f344af6b317466},
1357
- {0xcdfe17db3fb24d4a, 0x668bfc2271f5c626, 0x604ed93c24d67ff3,
1358
- 0x31b9c405f8540a20},
1359
- {1, 0, 0, 0}},
1360
- {{0xd36b4789a2582e7f, 0x0d1a10144ec39c28, 0x663c62c3edbad7a0,
1361
- 0x4052bf4b6f461db9},
1362
- {0x235a27c3188d25eb, 0xe724f33999bfcc5b, 0x862be6bd71d70cc8,
1363
- 0xfecf4d5190b0fc61},
1364
- {1, 0, 0, 0}},
1365
- {{0x74346c10a1d4cfac, 0xafdf5cc08526a7a4, 0x123202a8f62bff7a,
1366
- 0x1eddbae2c802e41a},
1367
- {0x8fa0af2dd603f844, 0x36e06b7e4c701917, 0x0c45f45273db33a0,
1368
- 0x43104d86560ebcfc},
1369
- {1, 0, 0, 0}},
1370
- {{0x9615b5110d1d78e5, 0x66b0de3225c4744b, 0x0a4a46fb6aaf363a,
1371
- 0xb48e26b484f7a21c},
1372
- {0x06ebb0f621a01b2d, 0xc004e4048b7b0f98, 0x64131bcdfed6f668,
1373
- 0xfac015404d4d3dab},
1374
- {1, 0, 0, 0}}},
1375
- {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
1376
- {{0x3a5a9e22185a5943, 0x1ab919365c65dfb6, 0x21656b32262c71da,
1377
- 0x7fe36b40af22af89},
1378
- {0xd50d152c699ca101, 0x74b3d5867b8af212, 0x9f09f40407dca6f1,
1379
- 0xe697d45825b63624},
1380
- {1, 0, 0, 0}},
1381
- {{0xa84aa9397512218e, 0xe9a521b074ca0141, 0x57880b3a18a2e902,
1382
- 0x4a5b506612a677a6},
1383
- {0x0beada7a4c4f3840, 0x626db15419e26d9d, 0xc42604fbe1627d40,
1384
- 0xeb13461ceac089f1},
1385
- {1, 0, 0, 0}},
1386
- {{0xf9faed0927a43281, 0x5e52c4144103ecbc, 0xc342967aa815c857,
1387
- 0x0781b8291c6a220a},
1388
- {0x5a8343ceeac55f80, 0x88f80eeee54a05e3, 0x97b2a14f12916434,
1389
- 0x690cde8df0151593},
1390
- {1, 0, 0, 0}},
1391
- {{0xaee9c75df7f82f2a, 0x9e4c35874afdf43a, 0xf5622df437371326,
1392
- 0x8a535f566ec73617},
1393
- {0xc5f9a0ac223094b7, 0xcde533864c8c7669, 0x37e02819085a92bf,
1394
- 0x0455c08468b08bd7},
1395
- {1, 0, 0, 0}},
1396
- {{0x0c0a6e2c9477b5d9, 0xf9a4bf62876dc444, 0x5050a949b6cdc279,
1397
- 0x06bada7ab77f8276},
1398
- {0xc8b4aed1ea48dac9, 0xdebd8a4b7ea1070f, 0x427d49101366eb70,
1399
- 0x5b476dfd0e6cb18a},
1400
- {1, 0, 0, 0}},
1401
- {{0x7c5c3e44278c340a, 0x4d54606812d66f3b, 0x29a751b1ae23c5d8,
1402
- 0x3e29864e8a2ec908},
1403
- {0x142d2a6626dbb850, 0xad1744c4765bd780, 0x1f150e68e322d1ed,
1404
- 0x239b90ea3dc31e7e},
1405
- {1, 0, 0, 0}},
1406
- {{0x78c416527a53322a, 0x305dde6709776f8e, 0xdbcab759f8862ed4,
1407
- 0x820f4dd949f72ff7},
1408
- {0x6cc544a62b5debd4, 0x75be5d937b4e8cc4, 0x1b481b1b215c14d3,
1409
- 0x140406ec783a05ec},
1410
- {1, 0, 0, 0}},
1411
- {{0x6a703f10e895df07, 0xfd75f3fa01876bd8, 0xeb5b06e70ce08ffe,
1412
- 0x68f6b8542783dfee},
1413
- {0x90c76f8a78712655, 0xcf5293d2f310bf7f, 0xfbc8044dfda45028,
1414
- 0xcbe1feba92e40ce6},
1415
- {1, 0, 0, 0}},
1416
- {{0xe998ceea4396e4c1, 0xfc82ef0b6acea274, 0x230f729f2250e927,
1417
- 0xd0b2f94d2f420109},
1418
- {0x4305adddb38d4966, 0x10b838f8624c3b45, 0x7db2636658954e7a,
1419
- 0x971459828b0719e5},
1420
- {1, 0, 0, 0}},
1421
- {{0x4bd6b72623369fc9, 0x57f2929e53d0b876, 0xc2d5cba4f2340687,
1422
- 0x961610004a866aba},
1423
- {0x49997bcd2e407a5e, 0x69ab197d92ddcb24, 0x2cf1f2438fe5131c,
1424
- 0x7acb9fadcee75e44},
1425
- {1, 0, 0, 0}},
1426
- {{0x254e839423d2d4c0, 0xf57f0c917aea685b, 0xa60d880f6f75aaea,
1427
- 0x24eb9acca333bf5b},
1428
- {0xe3de4ccb1cda5dea, 0xfeef9341c51a6b4f, 0x743125f88bac4c4d,
1429
- 0x69f891c5acd079cc},
1430
- {1, 0, 0, 0}},
1431
- {{0xeee44b35702476b5, 0x7ed031a0e45c2258, 0xb422d1e7bd6f8514,
1432
- 0xe51f547c5972a107},
1433
- {0xa25bcd6fc9cf343d, 0x8ca922ee097c184e, 0xa62f98b3a9fe9a06,
1434
- 0x1c309a2b25bb1387},
1435
- {1, 0, 0, 0}},
1436
- {{0x9295dbeb1967c459, 0xb00148833472c98e, 0xc504977708011828,
1437
- 0x20b87b8aa2c4e503},
1438
- {0x3063175de057c277, 0x1bd539338fe582dd, 0x0d11adef5f69a044,
1439
- 0xf5c6fa49919776be},
1440
- {1, 0, 0, 0}},
1441
- {{0x8c944e760fd59e11, 0x3876cba1102fad5f, 0xa454c3fad83faa56,
1442
- 0x1ed7d1b9332010b9},
1443
- {0xa1011a270024b889, 0x05e4d0dcac0cd344, 0x52b520f0eb6a2a24,
1444
- 0x3a2b03f03217257a},
1445
- {1, 0, 0, 0}},
1446
- {{0xf20fc2afdf1d043d, 0xf330240db58d5a62, 0xfc7d229ca0058c3b,
1447
- 0x15fee545c78dd9f6},
1448
- {0x501e82885bc98cda, 0x41ef80e5d046ac04, 0x557d9f49461210fb,
1449
- 0x4ab5b6b2b8753f81},
1450
- {1, 0, 0, 0}}}};
1451
-
1452
- /* select_point selects the |idx|th point from a precomputation table and
1453
- * copies it to out. */
1454
- static void select_point(const u64 idx, unsigned int size,
1455
- const smallfelem pre_comp[16][3], smallfelem out[3]) {
1456
- unsigned i, j;
1457
- u64 *outlimbs = &out[0][0];
1458
- memset(outlimbs, 0, 3 * sizeof(smallfelem));
1459
-
1460
- for (i = 0; i < size; i++) {
1461
- const u64 *inlimbs = (u64 *)&pre_comp[i][0][0];
1462
- u64 mask = i ^ idx;
1463
- mask |= mask >> 4;
1464
- mask |= mask >> 2;
1465
- mask |= mask >> 1;
1466
- mask &= 1;
1467
- mask--;
1468
- for (j = 0; j < NLIMBS * 3; j++) {
1469
- outlimbs[j] |= inlimbs[j] & mask;
1470
- }
1471
- }
1472
- }
1473
-
1474
- /* get_bit returns the |i|th bit in |in| */
1475
- static char get_bit(const felem_bytearray in, int i) {
1476
- if (i < 0 || i >= 256) {
1477
- return 0;
1478
- }
1479
- return (in[i >> 3] >> (i & 7)) & 1;
1480
- }
1481
-
1482
- /* Interleaved point multiplication using precomputed point multiples: The
1483
- * small point multiples 0*P, 1*P, ..., 17*P are in pre_comp[], the scalars
1484
- * in scalars[]. If g_scalar is non-NULL, we also add this multiple of the
1485
- * generator, using certain (large) precomputed multiples in g_pre_comp.
1486
- * Output point (X, Y, Z) is stored in x_out, y_out, z_out. */
1487
- static void batch_mul(felem x_out, felem y_out, felem z_out,
1488
- const felem_bytearray scalars[],
1489
- const unsigned num_points, const u8 *g_scalar,
1490
- const int mixed, const smallfelem pre_comp[][17][3]) {
1491
- int i, skip;
1492
- unsigned num, gen_mul = (g_scalar != NULL);
1493
- felem nq[3], ftmp;
1494
- smallfelem tmp[3];
1495
- u64 bits;
1496
- u8 sign, digit;
1497
-
1498
- /* set nq to the point at infinity */
1499
- memset(nq, 0, 3 * sizeof(felem));
1500
-
1501
- /* Loop over all scalars msb-to-lsb, interleaving additions of multiples
1502
- * of the generator (two in each of the last 32 rounds) and additions of
1503
- * other points multiples (every 5th round). */
1504
-
1505
- skip = 1; /* save two point operations in the first
1506
- * round */
1507
- for (i = (num_points ? 255 : 31); i >= 0; --i) {
1508
- /* double */
1509
- if (!skip) {
1510
- point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
1511
- }
1512
-
1513
- /* add multiples of the generator */
1514
- if (gen_mul && i <= 31) {
1515
- /* first, look 32 bits upwards */
1516
- bits = get_bit(g_scalar, i + 224) << 3;
1517
- bits |= get_bit(g_scalar, i + 160) << 2;
1518
- bits |= get_bit(g_scalar, i + 96) << 1;
1519
- bits |= get_bit(g_scalar, i + 32);
1520
- /* select the point to add, in constant time */
1521
- select_point(bits, 16, g_pre_comp[1], tmp);
1522
-
1523
- if (!skip) {
1524
- /* Arg 1 below is for "mixed" */
1525
- point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1, tmp[0], tmp[1],
1526
- tmp[2]);
1527
- } else {
1528
- smallfelem_expand(nq[0], tmp[0]);
1529
- smallfelem_expand(nq[1], tmp[1]);
1530
- smallfelem_expand(nq[2], tmp[2]);
1531
- skip = 0;
1532
- }
1533
-
1534
- /* second, look at the current position */
1535
- bits = get_bit(g_scalar, i + 192) << 3;
1536
- bits |= get_bit(g_scalar, i + 128) << 2;
1537
- bits |= get_bit(g_scalar, i + 64) << 1;
1538
- bits |= get_bit(g_scalar, i);
1539
- /* select the point to add, in constant time */
1540
- select_point(bits, 16, g_pre_comp[0], tmp);
1541
- /* Arg 1 below is for "mixed" */
1542
- point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1, tmp[0], tmp[1],
1543
- tmp[2]);
1544
- }
1545
-
1546
- /* do other additions every 5 doublings */
1547
- if (num_points && (i % 5 == 0)) {
1548
- /* loop over all scalars */
1549
- for (num = 0; num < num_points; ++num) {
1550
- bits = get_bit(scalars[num], i + 4) << 5;
1551
- bits |= get_bit(scalars[num], i + 3) << 4;
1552
- bits |= get_bit(scalars[num], i + 2) << 3;
1553
- bits |= get_bit(scalars[num], i + 1) << 2;
1554
- bits |= get_bit(scalars[num], i) << 1;
1555
- bits |= get_bit(scalars[num], i - 1);
1556
- ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
1557
-
1558
- /* select the point to add or subtract, in constant time. */
1559
- select_point(digit, 17, pre_comp[num], tmp);
1560
- smallfelem_neg(ftmp, tmp[1]); /* (X, -Y, Z) is the negative
1561
- * point */
1562
- copy_small_conditional(ftmp, tmp[1], (((limb)sign) - 1));
1563
- felem_contract(tmp[1], ftmp);
1564
-
1565
- if (!skip) {
1566
- point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], mixed, tmp[0],
1567
- tmp[1], tmp[2]);
1568
- } else {
1569
- smallfelem_expand(nq[0], tmp[0]);
1570
- smallfelem_expand(nq[1], tmp[1]);
1571
- smallfelem_expand(nq[2], tmp[2]);
1572
- skip = 0;
1573
- }
1574
- }
1575
- }
1576
- }
1577
- felem_assign(x_out, nq[0]);
1578
- felem_assign(y_out, nq[1]);
1579
- felem_assign(z_out, nq[2]);
1580
- }
1581
-
1582
- /******************************************************************************/
1583
- /*
1584
- * OPENSSL EC_METHOD FUNCTIONS
1585
- */
1586
-
1587
- /* Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') =
1588
- * (X/Z^2, Y/Z^3). */
1589
- int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
1590
- const EC_POINT *point,
1591
- BIGNUM *x, BIGNUM *y,
1592
- BN_CTX *ctx) {
1593
- felem z1, z2, x_in, y_in;
1594
- smallfelem x_out, y_out;
1595
- longfelem tmp;
1596
-
1597
- if (EC_POINT_is_at_infinity(group, point)) {
1598
- OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
1599
- return 0;
1600
- }
1601
- if (!BN_to_felem(x_in, &point->X) ||
1602
- !BN_to_felem(y_in, &point->Y) ||
1603
- !BN_to_felem(z1, &point->Z)) {
1604
- return 0;
1605
- }
1606
- felem_inv(z2, z1);
1607
- felem_square(tmp, z2);
1608
- felem_reduce(z1, tmp);
1609
- felem_mul(tmp, x_in, z1);
1610
- felem_reduce(x_in, tmp);
1611
- felem_contract(x_out, x_in);
1612
- if (x != NULL && !smallfelem_to_BN(x, x_out)) {
1613
- OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1614
- return 0;
1615
- }
1616
- felem_mul(tmp, z1, z2);
1617
- felem_reduce(z1, tmp);
1618
- felem_mul(tmp, y_in, z1);
1619
- felem_reduce(y_in, tmp);
1620
- felem_contract(y_out, y_in);
1621
- if (y != NULL && !smallfelem_to_BN(y, y_out)) {
1622
- OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1623
- return 0;
1624
- }
1625
- return 1;
1626
- }
1627
-
1628
- /* points below is of size |num|, and tmp_smallfelems is of size |num+1| */
1629
- static void make_points_affine(size_t num, smallfelem points[][3],
1630
- smallfelem tmp_smallfelems[]) {
1631
- /* Runs in constant time, unless an input is the point at infinity (which
1632
- * normally shouldn't happen). */
1633
- ec_GFp_nistp_points_make_affine_internal(
1634
- num, points, sizeof(smallfelem), tmp_smallfelems,
1635
- (void (*)(void *))smallfelem_one,
1636
- (int (*)(const void *))smallfelem_is_zero_int,
1637
- (void (*)(void *, const void *))smallfelem_assign,
1638
- (void (*)(void *, const void *))smallfelem_square_contract,
1639
- (void (*)(void *, const void *, const void *))smallfelem_mul_contract,
1640
- (void (*)(void *, const void *))smallfelem_inv_contract,
1641
- /* nothing to contract */
1642
- (void (*)(void *, const void *))smallfelem_assign);
1643
- }
1644
-
1645
- int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
1646
- const BIGNUM *g_scalar, const EC_POINT *p_,
1647
- const BIGNUM *p_scalar_, BN_CTX *ctx) {
1648
- /* TODO: This function used to take |points| and |scalars| as arrays of
1649
- * |num| elements. The code below should be simplified to work in terms of |p|
1650
- * and |p_scalar|. */
1651
- size_t num = p_ != NULL ? 1 : 0;
1652
- const EC_POINT **points = p_ != NULL ? &p_ : NULL;
1653
- BIGNUM const *const *scalars = p_ != NULL ? &p_scalar_ : NULL;
1654
-
1655
- int ret = 0;
1656
- int j;
1657
- int mixed = 0;
1658
- BN_CTX *new_ctx = NULL;
1659
- BIGNUM *x, *y, *z;
1660
- felem_bytearray g_secret;
1661
- felem_bytearray *secrets = NULL;
1662
- smallfelem(*pre_comp)[17][3] = NULL;
1663
- smallfelem *tmp_smallfelems = NULL;
1664
- felem_bytearray tmp;
1665
- unsigned i, num_bytes;
1666
- size_t num_points = num;
1667
- smallfelem x_in, y_in, z_in;
1668
- felem x_out, y_out, z_out;
1669
- const EC_POINT *p = NULL;
1670
- const BIGNUM *p_scalar = NULL;
1671
-
1672
- if (ctx == NULL) {
1673
- ctx = new_ctx = BN_CTX_new();
1674
- if (ctx == NULL) {
1675
- return 0;
1676
- }
1677
- }
1678
-
1679
- BN_CTX_start(ctx);
1680
- if ((x = BN_CTX_get(ctx)) == NULL ||
1681
- (y = BN_CTX_get(ctx)) == NULL ||
1682
- (z = BN_CTX_get(ctx)) == NULL) {
1683
- goto err;
1684
- }
1685
-
1686
- if (num_points > 0) {
1687
- if (num_points >= 3) {
1688
- /* unless we precompute multiples for just one or two points,
1689
- * converting those into affine form is time well spent */
1690
- mixed = 1;
1691
- }
1692
- secrets = OPENSSL_malloc(num_points * sizeof(felem_bytearray));
1693
- pre_comp = OPENSSL_malloc(num_points * 17 * 3 * sizeof(smallfelem));
1694
- if (mixed) {
1695
- tmp_smallfelems =
1696
- OPENSSL_malloc((num_points * 17 + 1) * sizeof(smallfelem));
1697
- }
1698
- if (secrets == NULL || pre_comp == NULL ||
1699
- (mixed && tmp_smallfelems == NULL)) {
1700
- OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
1701
- goto err;
1702
- }
1703
-
1704
- /* we treat NULL scalars as 0, and NULL points as points at infinity,
1705
- * i.e., they contribute nothing to the linear combination. */
1706
- memset(secrets, 0, num_points * sizeof(felem_bytearray));
1707
- memset(pre_comp, 0, num_points * 17 * 3 * sizeof(smallfelem));
1708
- for (i = 0; i < num_points; ++i) {
1709
- if (i == num) {
1710
- /* we didn't have a valid precomputation, so we pick the generator. */
1711
- p = EC_GROUP_get0_generator(group);
1712
- p_scalar = g_scalar;
1713
- } else {
1714
- /* the i^th point */
1715
- p = points[i];
1716
- p_scalar = scalars[i];
1717
- }
1718
- if (p_scalar != NULL && p != NULL) {
1719
- assert(BN_cmp(p_scalar, EC_GROUP_get0_order(group)) < 0);
1720
- num_bytes = BN_bn2bin(p_scalar, tmp);
1721
- flip_endian(secrets[i], tmp, num_bytes);
1722
-
1723
- /* precompute multiples */
1724
- if (!BN_to_felem(x_out, &p->X) ||
1725
- !BN_to_felem(y_out, &p->Y) ||
1726
- !BN_to_felem(z_out, &p->Z)) {
1727
- goto err;
1728
- }
1729
- felem_shrink(pre_comp[i][1][0], x_out);
1730
- felem_shrink(pre_comp[i][1][1], y_out);
1731
- felem_shrink(pre_comp[i][1][2], z_out);
1732
- for (j = 2; j <= 16; ++j) {
1733
- if (j & 1) {
1734
- point_add_small(pre_comp[i][j][0], pre_comp[i][j][1],
1735
- pre_comp[i][j][2], pre_comp[i][1][0],
1736
- pre_comp[i][1][1], pre_comp[i][1][2],
1737
- pre_comp[i][j - 1][0], pre_comp[i][j - 1][1],
1738
- pre_comp[i][j - 1][2]);
1739
- } else {
1740
- point_double_small(pre_comp[i][j][0], pre_comp[i][j][1],
1741
- pre_comp[i][j][2], pre_comp[i][j / 2][0],
1742
- pre_comp[i][j / 2][1], pre_comp[i][j / 2][2]);
1743
- }
1744
- }
1745
- }
1746
- }
1747
- if (mixed) {
1748
- make_points_affine(num_points * 17, pre_comp[0], tmp_smallfelems);
1749
- }
1750
- }
1751
-
1752
- if (g_scalar != NULL) {
1753
- memset(g_secret, 0, sizeof(g_secret));
1754
- assert(BN_cmp(g_scalar, EC_GROUP_get0_order(group)) < 0);
1755
- num_bytes = BN_bn2bin(g_scalar, tmp);
1756
- flip_endian(g_secret, tmp, num_bytes);
1757
- }
1758
- batch_mul(x_out, y_out, z_out, (const felem_bytearray(*))secrets,
1759
- num_points, g_scalar != NULL ? g_secret : NULL, mixed,
1760
- (const smallfelem(*)[17][3])pre_comp);
1761
-
1762
- /* reduce the output to its unique minimal representation */
1763
- felem_contract(x_in, x_out);
1764
- felem_contract(y_in, y_out);
1765
- felem_contract(z_in, z_out);
1766
- if (!smallfelem_to_BN(x, x_in) ||
1767
- !smallfelem_to_BN(y, y_in) ||
1768
- !smallfelem_to_BN(z, z_in)) {
1769
- OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1770
- goto err;
1771
- }
1772
- ret = ec_point_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
1773
-
1774
- err:
1775
- BN_CTX_end(ctx);
1776
- BN_CTX_free(new_ctx);
1777
- OPENSSL_free(secrets);
1778
- OPENSSL_free(pre_comp);
1779
- OPENSSL_free(tmp_smallfelems);
1780
- return ret;
1781
- }
1782
-
1783
- const EC_METHOD EC_GFp_nistp256_method = {
1784
- ec_GFp_nistp256_point_get_affine_coordinates,
1785
- ec_GFp_nistp256_points_mul,
1786
- ec_GFp_nistp256_points_mul,
1787
- ec_GFp_simple_field_mul,
1788
- ec_GFp_simple_field_sqr,
1789
- NULL /* field_encode */,
1790
- NULL /* field_decode */,
1791
- NULL /* field_set_to_one */,
1792
- };
1793
-
1794
- #endif /* 64_BIT && !WINDOWS */