visaljs 0.0.0-stage → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (520) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +45 -2
  3. package/cli.js +8084 -0
  4. package/node/include/js_node.h +46 -0
  5. package/node/prelude/async_hooks.js +88 -0
  6. package/node/prelude/buffer.js +31 -0
  7. package/node/prelude/crypto.js +114 -0
  8. package/node/prelude/events.js +227 -0
  9. package/node/prelude/fs.js +79 -0
  10. package/node/prelude/http.js +536 -0
  11. package/node/prelude/http2.js +30 -0
  12. package/node/prelude/internal/crypto/asym.js +333 -0
  13. package/node/prelude/internal/crypto/cipher.js +205 -0
  14. package/node/prelude/internal/crypto/hash.js +137 -0
  15. package/node/prelude/internal/crypto/kdf.js +182 -0
  16. package/node/prelude/internal/crypto/keys.js +603 -0
  17. package/node/prelude/internal/crypto/random.js +118 -0
  18. package/node/prelude/internal/crypto/util.js +76 -0
  19. package/node/prelude/internal/errors.js +88 -0
  20. package/node/prelude/internal/querystring.js +105 -0
  21. package/node/prelude/module.js +53 -0
  22. package/node/prelude/net.js +51 -0
  23. package/node/prelude/os.js +129 -0
  24. package/node/prelude/path.js +257 -0
  25. package/node/prelude/process.js +21 -0
  26. package/node/prelude/querystring.js +469 -0
  27. package/node/prelude/stream.js +1327 -0
  28. package/node/prelude/stream_promises.js +7 -0
  29. package/node/prelude/stream_web.js +6 -0
  30. package/node/prelude/string_decoder.js +236 -0
  31. package/node/prelude/tty.js +18 -0
  32. package/node/prelude/url.js +1156 -0
  33. package/node/prelude/util.js +64 -0
  34. package/node/prelude/zlib.js +81 -0
  35. package/node/src/host_fs.c +126 -0
  36. package/node/src/host_http.c +486 -0
  37. package/node/src/host_os.c +505 -0
  38. package/node/src/host_sys.c +78 -0
  39. package/node/third_party/libuv/LICENSE +19 -0
  40. package/node/third_party/libuv/include/uv/aix.h +32 -0
  41. package/node/third_party/libuv/include/uv/bsd.h +34 -0
  42. package/node/third_party/libuv/include/uv/darwin.h +61 -0
  43. package/node/third_party/libuv/include/uv/errno.h +483 -0
  44. package/node/third_party/libuv/include/uv/linux.h +34 -0
  45. package/node/third_party/libuv/include/uv/os390.h +33 -0
  46. package/node/third_party/libuv/include/uv/posix.h +31 -0
  47. package/node/third_party/libuv/include/uv/sunos.h +44 -0
  48. package/node/third_party/libuv/include/uv/threadpool.h +37 -0
  49. package/node/third_party/libuv/include/uv/tree.h +521 -0
  50. package/node/third_party/libuv/include/uv/unix.h +512 -0
  51. package/node/third_party/libuv/include/uv/version.h +43 -0
  52. package/node/third_party/libuv/include/uv/win.h +698 -0
  53. package/node/third_party/libuv/include/uv.h +1990 -0
  54. package/node/third_party/libuv/src/fs-poll.c +290 -0
  55. package/node/third_party/libuv/src/heap-inl.h +245 -0
  56. package/node/third_party/libuv/src/idna.c +560 -0
  57. package/node/third_party/libuv/src/idna.h +31 -0
  58. package/node/third_party/libuv/src/inet.c +298 -0
  59. package/node/third_party/libuv/src/queue.h +90 -0
  60. package/node/third_party/libuv/src/random.c +123 -0
  61. package/node/third_party/libuv/src/strscpy.c +38 -0
  62. package/node/third_party/libuv/src/strscpy.h +39 -0
  63. package/node/third_party/libuv/src/strtok.c +52 -0
  64. package/node/third_party/libuv/src/strtok.h +27 -0
  65. package/node/third_party/libuv/src/thread-common.c +175 -0
  66. package/node/third_party/libuv/src/threadpool.c +419 -0
  67. package/node/third_party/libuv/src/timer.c +200 -0
  68. package/node/third_party/libuv/src/unix/async.c +418 -0
  69. package/node/third_party/libuv/src/unix/bsd-ifaddrs.c +164 -0
  70. package/node/third_party/libuv/src/unix/core.c +2104 -0
  71. package/node/third_party/libuv/src/unix/darwin-proctitle.c +176 -0
  72. package/node/third_party/libuv/src/unix/darwin-stub.h +97 -0
  73. package/node/third_party/libuv/src/unix/darwin-syscalls.h +17 -0
  74. package/node/third_party/libuv/src/unix/darwin.c +237 -0
  75. package/node/third_party/libuv/src/unix/dl.c +80 -0
  76. package/node/third_party/libuv/src/unix/fs.c +2303 -0
  77. package/node/third_party/libuv/src/unix/fsevents.c +906 -0
  78. package/node/third_party/libuv/src/unix/getaddrinfo.c +252 -0
  79. package/node/third_party/libuv/src/unix/getnameinfo.c +121 -0
  80. package/node/third_party/libuv/src/unix/internal.h +530 -0
  81. package/node/third_party/libuv/src/unix/kqueue.c +667 -0
  82. package/node/third_party/libuv/src/unix/linux.c +2761 -0
  83. package/node/third_party/libuv/src/unix/loop-watcher.c +68 -0
  84. package/node/third_party/libuv/src/unix/loop.c +240 -0
  85. package/node/third_party/libuv/src/unix/pipe.c +555 -0
  86. package/node/third_party/libuv/src/unix/poll.c +159 -0
  87. package/node/third_party/libuv/src/unix/process.c +1123 -0
  88. package/node/third_party/libuv/src/unix/procfs-exepath.c +45 -0
  89. package/node/third_party/libuv/src/unix/proctitle.c +157 -0
  90. package/node/third_party/libuv/src/unix/random-devurandom.c +93 -0
  91. package/node/third_party/libuv/src/unix/random-getentropy.c +57 -0
  92. package/node/third_party/libuv/src/unix/random-getrandom.c +86 -0
  93. package/node/third_party/libuv/src/unix/random-sysctl-linux.c +99 -0
  94. package/node/third_party/libuv/src/unix/signal.c +581 -0
  95. package/node/third_party/libuv/src/unix/stream.c +1567 -0
  96. package/node/third_party/libuv/src/unix/tcp.c +673 -0
  97. package/node/third_party/libuv/src/unix/thread.c +975 -0
  98. package/node/third_party/libuv/src/unix/tty.c +510 -0
  99. package/node/third_party/libuv/src/unix/udp.c +1434 -0
  100. package/node/third_party/libuv/src/uv-common.c +1051 -0
  101. package/node/third_party/libuv/src/uv-common.h +474 -0
  102. package/node/third_party/libuv/src/uv-data-getter-setters.c +119 -0
  103. package/node/third_party/libuv/src/version.c +45 -0
  104. package/node/third_party/llhttp/LICENSE +22 -0
  105. package/node/third_party/llhttp/api.c +509 -0
  106. package/node/third_party/llhttp/http.c +170 -0
  107. package/node/third_party/llhttp/llhttp.c +10099 -0
  108. package/node/third_party/llhttp/llhttp.h +907 -0
  109. package/package.json +25 -4
  110. package/runtime/include/js_atoms.gen.h +68 -0
  111. package/runtime/include/js_atoms.h +54 -0
  112. package/runtime/include/js_bigint.h +93 -0
  113. package/runtime/include/js_buffer.h +79 -0
  114. package/runtime/include/js_builtins.h +168 -0
  115. package/runtime/include/js_console.h +58 -0
  116. package/runtime/include/js_convert.h +22 -0
  117. package/runtime/include/js_core.def +188 -0
  118. package/runtime/include/js_crypto.h +58 -0
  119. package/runtime/include/js_error.h +37 -0
  120. package/runtime/include/js_exception.h +65 -0
  121. package/runtime/include/js_function.h +167 -0
  122. package/runtime/include/js_gc.h +195 -0
  123. package/runtime/include/js_generator.h +96 -0
  124. package/runtime/include/js_host.h +19 -0
  125. package/runtime/include/js_jobs.h +84 -0
  126. package/runtime/include/js_module.h +16 -0
  127. package/runtime/include/js_object.h +384 -0
  128. package/runtime/include/js_ops.h +111 -0
  129. package/runtime/include/js_promise.h +45 -0
  130. package/runtime/include/js_regexp.h +25 -0
  131. package/runtime/include/js_runtime.h +30 -0
  132. package/runtime/include/js_symbol.h +62 -0
  133. package/runtime/include/js_web.h +110 -0
  134. package/runtime/include/jscell.h +48 -0
  135. package/runtime/include/jscontext.h +240 -0
  136. package/runtime/include/jsnum.h +42 -0
  137. package/runtime/include/jsstring.h +161 -0
  138. package/runtime/include/jsvalue.h +142 -0
  139. package/runtime/src/arguments.c +115 -0
  140. package/runtime/src/array.c +1145 -0
  141. package/runtime/src/arraybuffer.c +608 -0
  142. package/runtime/src/async_iter.c +479 -0
  143. package/runtime/src/atoms.c +157 -0
  144. package/runtime/src/base64.c +107 -0
  145. package/runtime/src/bigint.c +1189 -0
  146. package/runtime/src/blob.c +371 -0
  147. package/runtime/src/buffer.c +1357 -0
  148. package/runtime/src/builtin_table.c +100 -0
  149. package/runtime/src/builtins.c +987 -0
  150. package/runtime/src/builtins_util.h +53 -0
  151. package/runtime/src/collections.c +912 -0
  152. package/runtime/src/components.c +28 -0
  153. package/runtime/src/console.c +637 -0
  154. package/runtime/src/convert.c +82 -0
  155. package/runtime/src/core_all.c +21 -0
  156. package/runtime/src/crypto.c +116 -0
  157. package/runtime/src/crypto_asym.c +644 -0
  158. package/runtime/src/crypto_cipher.c +671 -0
  159. package/runtime/src/crypto_hash.c +260 -0
  160. package/runtime/src/crypto_host.c +414 -0
  161. package/runtime/src/crypto_internal.h +304 -0
  162. package/runtime/src/crypto_kdf.c +143 -0
  163. package/runtime/src/crypto_key.c +171 -0
  164. package/runtime/src/crypto_pkey.c +794 -0
  165. package/runtime/src/crypto_subtle.c +1675 -0
  166. package/runtime/src/date.c +940 -0
  167. package/runtime/src/domexception.c +169 -0
  168. package/runtime/src/elements.c +312 -0
  169. package/runtime/src/elements.h +45 -0
  170. package/runtime/src/encoding.c +485 -0
  171. package/runtime/src/error.c +297 -0
  172. package/runtime/src/events.c +793 -0
  173. package/runtime/src/exception.c +56 -0
  174. package/runtime/src/fetch.c +772 -0
  175. package/runtime/src/function.c +530 -0
  176. package/runtime/src/gc.c +803 -0
  177. package/runtime/src/generator.c +218 -0
  178. package/runtime/src/global.c +119 -0
  179. package/runtime/src/headers.c +557 -0
  180. package/runtime/src/inspect.c +1792 -0
  181. package/runtime/src/internal.h +80 -0
  182. package/runtime/src/iterate.c +94 -0
  183. package/runtime/src/iterator.c +409 -0
  184. package/runtime/src/jobs.c +337 -0
  185. package/runtime/src/jsnum.c +353 -0
  186. package/runtime/src/json.c +1028 -0
  187. package/runtime/src/math.c +243 -0
  188. package/runtime/src/module.c +336 -0
  189. package/runtime/src/number.c +176 -0
  190. package/runtime/src/object.c +1265 -0
  191. package/runtime/src/ops.c +322 -0
  192. package/runtime/src/os.c +196 -0
  193. package/runtime/src/os.h +29 -0
  194. package/runtime/src/process.c +432 -0
  195. package/runtime/src/promise.c +721 -0
  196. package/runtime/src/proxy.c +570 -0
  197. package/runtime/src/reflect.c +194 -0
  198. package/runtime/src/regexp.c +1141 -0
  199. package/runtime/src/regexp_anchor.h +34 -0
  200. package/runtime/src/regexp_message.h +32 -0
  201. package/runtime/src/shape.c +294 -0
  202. package/runtime/src/shape.h +46 -0
  203. package/runtime/src/streams.c +1046 -0
  204. package/runtime/src/streams.h +47 -0
  205. package/runtime/src/string.c +364 -0
  206. package/runtime/src/string_builtins.c +837 -0
  207. package/runtime/src/symbol.c +143 -0
  208. package/runtime/src/test_host.c +59 -0
  209. package/runtime/src/typedarray.c +1330 -0
  210. package/runtime/src/tznames.gen.h +712 -0
  211. package/runtime/src/uri.c +169 -0
  212. package/runtime/src/url.c +2116 -0
  213. package/runtime/src/weak.c +486 -0
  214. package/runtime/src/web.c +214 -0
  215. package/runtime/src/wrapper.c +59 -0
  216. package/runtime/src/writable.c +1486 -0
  217. package/runtime/third_party/ada-idna/LICENSE-MIT +18 -0
  218. package/runtime/third_party/ada-idna/README.md +27 -0
  219. package/runtime/third_party/ada-idna/convert.mjs +83 -0
  220. package/runtime/third_party/ada-idna/idna.c +416 -0
  221. package/runtime/third_party/ada-idna/idna.h +15 -0
  222. package/runtime/third_party/ada-idna/idna_tables.h +2657 -0
  223. package/runtime/third_party/bearssl/LICENSE.txt +21 -0
  224. package/runtime/third_party/bearssl/README.txt +136 -0
  225. package/runtime/third_party/bearssl/VERSION +1 -0
  226. package/runtime/third_party/bearssl/inc/bearssl.h +183 -0
  227. package/runtime/third_party/bearssl/inc/bearssl_aead.h +1059 -0
  228. package/runtime/third_party/bearssl/inc/bearssl_block.h +2618 -0
  229. package/runtime/third_party/bearssl/inc/bearssl_ec.h +967 -0
  230. package/runtime/third_party/bearssl/inc/bearssl_hash.h +1346 -0
  231. package/runtime/third_party/bearssl/inc/bearssl_hmac.h +241 -0
  232. package/runtime/third_party/bearssl/inc/bearssl_kdf.h +284 -0
  233. package/runtime/third_party/bearssl/inc/bearssl_pem.h +294 -0
  234. package/runtime/third_party/bearssl/inc/bearssl_prf.h +150 -0
  235. package/runtime/third_party/bearssl/inc/bearssl_rand.h +397 -0
  236. package/runtime/third_party/bearssl/inc/bearssl_rsa.h +1655 -0
  237. package/runtime/third_party/bearssl/inc/bearssl_ssl.h +4296 -0
  238. package/runtime/third_party/bearssl/inc/bearssl_x509.h +1474 -0
  239. package/runtime/third_party/bearssl/src/aead/ccm.c +346 -0
  240. package/runtime/third_party/bearssl/src/aead/eax.c +525 -0
  241. package/runtime/third_party/bearssl/src/aead/gcm.c +318 -0
  242. package/runtime/third_party/bearssl/src/codec/ccopy.c +44 -0
  243. package/runtime/third_party/bearssl/src/codec/dec16be.c +38 -0
  244. package/runtime/third_party/bearssl/src/codec/dec16le.c +38 -0
  245. package/runtime/third_party/bearssl/src/codec/dec32be.c +38 -0
  246. package/runtime/third_party/bearssl/src/codec/dec32le.c +38 -0
  247. package/runtime/third_party/bearssl/src/codec/dec64be.c +38 -0
  248. package/runtime/third_party/bearssl/src/codec/dec64le.c +38 -0
  249. package/runtime/third_party/bearssl/src/codec/enc16be.c +38 -0
  250. package/runtime/third_party/bearssl/src/codec/enc16le.c +38 -0
  251. package/runtime/third_party/bearssl/src/codec/enc32be.c +38 -0
  252. package/runtime/third_party/bearssl/src/codec/enc32le.c +38 -0
  253. package/runtime/third_party/bearssl/src/codec/enc64be.c +38 -0
  254. package/runtime/third_party/bearssl/src/codec/enc64le.c +38 -0
  255. package/runtime/third_party/bearssl/src/codec/pemdec.c +526 -0
  256. package/runtime/third_party/bearssl/src/codec/pemdec.t0 +314 -0
  257. package/runtime/third_party/bearssl/src/codec/pemenc.c +173 -0
  258. package/runtime/third_party/bearssl/src/config.h +247 -0
  259. package/runtime/third_party/bearssl/src/ec/ec_all_m15.c +121 -0
  260. package/runtime/third_party/bearssl/src/ec/ec_all_m31.c +171 -0
  261. package/runtime/third_party/bearssl/src/ec/ec_c25519_i15.c +398 -0
  262. package/runtime/third_party/bearssl/src/ec/ec_c25519_i31.c +390 -0
  263. package/runtime/third_party/bearssl/src/ec/ec_c25519_m15.c +1478 -0
  264. package/runtime/third_party/bearssl/src/ec/ec_c25519_m31.c +800 -0
  265. package/runtime/third_party/bearssl/src/ec/ec_c25519_m62.c +605 -0
  266. package/runtime/third_party/bearssl/src/ec/ec_c25519_m64.c +831 -0
  267. package/runtime/third_party/bearssl/src/ec/ec_curve25519.c +46 -0
  268. package/runtime/third_party/bearssl/src/ec/ec_default.c +36 -0
  269. package/runtime/third_party/bearssl/src/ec/ec_keygen.c +86 -0
  270. package/runtime/third_party/bearssl/src/ec/ec_p256_m15.c +2124 -0
  271. package/runtime/third_party/bearssl/src/ec/ec_p256_m31.c +1469 -0
  272. package/runtime/third_party/bearssl/src/ec/ec_p256_m62.c +1765 -0
  273. package/runtime/third_party/bearssl/src/ec/ec_p256_m64.c +1781 -0
  274. package/runtime/third_party/bearssl/src/ec/ec_prime_i15.c +824 -0
  275. package/runtime/third_party/bearssl/src/ec/ec_prime_i31.c +826 -0
  276. package/runtime/third_party/bearssl/src/ec/ec_pubkey.c +85 -0
  277. package/runtime/third_party/bearssl/src/ec/ec_secp256r1.c +51 -0
  278. package/runtime/third_party/bearssl/src/ec/ec_secp384r1.c +57 -0
  279. package/runtime/third_party/bearssl/src/ec/ec_secp521r1.c +64 -0
  280. package/runtime/third_party/bearssl/src/ec/ecdsa_atr.c +134 -0
  281. package/runtime/third_party/bearssl/src/ec/ecdsa_default_sign_asn1.c +36 -0
  282. package/runtime/third_party/bearssl/src/ec/ecdsa_default_sign_raw.c +36 -0
  283. package/runtime/third_party/bearssl/src/ec/ecdsa_default_vrfy_asn1.c +36 -0
  284. package/runtime/third_party/bearssl/src/ec/ecdsa_default_vrfy_raw.c +36 -0
  285. package/runtime/third_party/bearssl/src/ec/ecdsa_i15_bits.c +47 -0
  286. package/runtime/third_party/bearssl/src/ec/ecdsa_i15_sign_asn1.c +45 -0
  287. package/runtime/third_party/bearssl/src/ec/ecdsa_i15_sign_raw.c +174 -0
  288. package/runtime/third_party/bearssl/src/ec/ecdsa_i15_vrfy_asn1.c +48 -0
  289. package/runtime/third_party/bearssl/src/ec/ecdsa_i15_vrfy_raw.c +166 -0
  290. package/runtime/third_party/bearssl/src/ec/ecdsa_i31_bits.c +47 -0
  291. package/runtime/third_party/bearssl/src/ec/ecdsa_i31_sign_asn1.c +45 -0
  292. package/runtime/third_party/bearssl/src/ec/ecdsa_i31_sign_raw.c +173 -0
  293. package/runtime/third_party/bearssl/src/ec/ecdsa_i31_vrfy_asn1.c +48 -0
  294. package/runtime/third_party/bearssl/src/ec/ecdsa_i31_vrfy_raw.c +165 -0
  295. package/runtime/third_party/bearssl/src/ec/ecdsa_rta.c +121 -0
  296. package/runtime/third_party/bearssl/src/hash/dig_oid.c +84 -0
  297. package/runtime/third_party/bearssl/src/hash/dig_size.c +50 -0
  298. package/runtime/third_party/bearssl/src/hash/ghash_ctmul.c +345 -0
  299. package/runtime/third_party/bearssl/src/hash/ghash_ctmul32.c +251 -0
  300. package/runtime/third_party/bearssl/src/hash/ghash_ctmul64.c +154 -0
  301. package/runtime/third_party/bearssl/src/hash/ghash_pclmul.c +389 -0
  302. package/runtime/third_party/bearssl/src/hash/ghash_pwr8.c +411 -0
  303. package/runtime/third_party/bearssl/src/hash/md5.c +208 -0
  304. package/runtime/third_party/bearssl/src/hash/md5sha1.c +141 -0
  305. package/runtime/third_party/bearssl/src/hash/mgf1.c +56 -0
  306. package/runtime/third_party/bearssl/src/hash/multihash.c +166 -0
  307. package/runtime/third_party/bearssl/src/hash/sha1.c +191 -0
  308. package/runtime/third_party/bearssl/src/hash/sha2big.c +285 -0
  309. package/runtime/third_party/bearssl/src/hash/sha2small.c +341 -0
  310. package/runtime/third_party/bearssl/src/inner.h +2577 -0
  311. package/runtime/third_party/bearssl/src/int/i15_add.c +46 -0
  312. package/runtime/third_party/bearssl/src/int/i15_bitlen.c +44 -0
  313. package/runtime/third_party/bearssl/src/int/i15_decmod.c +124 -0
  314. package/runtime/third_party/bearssl/src/int/i15_decode.c +56 -0
  315. package/runtime/third_party/bearssl/src/int/i15_decred.c +100 -0
  316. package/runtime/third_party/bearssl/src/int/i15_encode.c +56 -0
  317. package/runtime/third_party/bearssl/src/int/i15_fmont.c +59 -0
  318. package/runtime/third_party/bearssl/src/int/i15_iszero.c +39 -0
  319. package/runtime/third_party/bearssl/src/int/i15_moddiv.c +465 -0
  320. package/runtime/third_party/bearssl/src/int/i15_modpow.c +50 -0
  321. package/runtime/third_party/bearssl/src/int/i15_modpow2.c +160 -0
  322. package/runtime/third_party/bearssl/src/int/i15_montmul.c +184 -0
  323. package/runtime/third_party/bearssl/src/int/i15_mulacc.c +61 -0
  324. package/runtime/third_party/bearssl/src/int/i15_muladd.c +173 -0
  325. package/runtime/third_party/bearssl/src/int/i15_ninv15.c +38 -0
  326. package/runtime/third_party/bearssl/src/int/i15_reduce.c +66 -0
  327. package/runtime/third_party/bearssl/src/int/i15_rshift.c +47 -0
  328. package/runtime/third_party/bearssl/src/int/i15_sub.c +46 -0
  329. package/runtime/third_party/bearssl/src/int/i15_tmont.c +36 -0
  330. package/runtime/third_party/bearssl/src/int/i31_add.c +46 -0
  331. package/runtime/third_party/bearssl/src/int/i31_bitlen.c +44 -0
  332. package/runtime/third_party/bearssl/src/int/i31_decmod.c +124 -0
  333. package/runtime/third_party/bearssl/src/int/i31_decode.c +57 -0
  334. package/runtime/third_party/bearssl/src/int/i31_decred.c +103 -0
  335. package/runtime/third_party/bearssl/src/int/i31_encode.c +79 -0
  336. package/runtime/third_party/bearssl/src/int/i31_fmont.c +60 -0
  337. package/runtime/third_party/bearssl/src/int/i31_iszero.c +39 -0
  338. package/runtime/third_party/bearssl/src/int/i31_moddiv.c +488 -0
  339. package/runtime/third_party/bearssl/src/int/i31_modpow.c +65 -0
  340. package/runtime/third_party/bearssl/src/int/i31_modpow2.c +160 -0
  341. package/runtime/third_party/bearssl/src/int/i31_montmul.c +127 -0
  342. package/runtime/third_party/bearssl/src/int/i31_mulacc.c +74 -0
  343. package/runtime/third_party/bearssl/src/int/i31_muladd.c +157 -0
  344. package/runtime/third_party/bearssl/src/int/i31_ninv31.c +39 -0
  345. package/runtime/third_party/bearssl/src/int/i31_reduce.c +66 -0
  346. package/runtime/third_party/bearssl/src/int/i31_rshift.c +47 -0
  347. package/runtime/third_party/bearssl/src/int/i31_sub.c +46 -0
  348. package/runtime/third_party/bearssl/src/int/i31_tmont.c +36 -0
  349. package/runtime/third_party/bearssl/src/int/i32_add.c +51 -0
  350. package/runtime/third_party/bearssl/src/int/i32_bitlen.c +44 -0
  351. package/runtime/third_party/bearssl/src/int/i32_decmod.c +77 -0
  352. package/runtime/third_party/bearssl/src/int/i32_decode.c +63 -0
  353. package/runtime/third_party/bearssl/src/int/i32_decred.c +107 -0
  354. package/runtime/third_party/bearssl/src/int/i32_div32.c +56 -0
  355. package/runtime/third_party/bearssl/src/int/i32_encode.c +72 -0
  356. package/runtime/third_party/bearssl/src/int/i32_fmont.c +60 -0
  357. package/runtime/third_party/bearssl/src/int/i32_iszero.c +39 -0
  358. package/runtime/third_party/bearssl/src/int/i32_modpow.c +65 -0
  359. package/runtime/third_party/bearssl/src/int/i32_montmul.c +69 -0
  360. package/runtime/third_party/bearssl/src/int/i32_mulacc.c +56 -0
  361. package/runtime/third_party/bearssl/src/int/i32_muladd.c +138 -0
  362. package/runtime/third_party/bearssl/src/int/i32_ninv32.c +39 -0
  363. package/runtime/third_party/bearssl/src/int/i32_reduce.c +66 -0
  364. package/runtime/third_party/bearssl/src/int/i32_sub.c +51 -0
  365. package/runtime/third_party/bearssl/src/int/i32_tmont.c +36 -0
  366. package/runtime/third_party/bearssl/src/int/i62_modpow2.c +493 -0
  367. package/runtime/third_party/bearssl/src/kdf/hkdf.c +107 -0
  368. package/runtime/third_party/bearssl/src/kdf/shake.c +590 -0
  369. package/runtime/third_party/bearssl/src/mac/hmac.c +122 -0
  370. package/runtime/third_party/bearssl/src/mac/hmac_ct.c +193 -0
  371. package/runtime/third_party/bearssl/src/rand/aesctr_drbg.c +206 -0
  372. package/runtime/third_party/bearssl/src/rand/hmac_drbg.c +157 -0
  373. package/runtime/third_party/bearssl/src/rand/sysrng.c +252 -0
  374. package/runtime/third_party/bearssl/src/rsa/rsa_default_keygen.c +38 -0
  375. package/runtime/third_party/bearssl/src/rsa/rsa_default_modulus.c +36 -0
  376. package/runtime/third_party/bearssl/src/rsa/rsa_default_oaep_decrypt.c +38 -0
  377. package/runtime/third_party/bearssl/src/rsa/rsa_default_oaep_encrypt.c +38 -0
  378. package/runtime/third_party/bearssl/src/rsa/rsa_default_pkcs1_sign.c +38 -0
  379. package/runtime/third_party/bearssl/src/rsa/rsa_default_pkcs1_vrfy.c +38 -0
  380. package/runtime/third_party/bearssl/src/rsa/rsa_default_priv.c +38 -0
  381. package/runtime/third_party/bearssl/src/rsa/rsa_default_privexp.c +36 -0
  382. package/runtime/third_party/bearssl/src/rsa/rsa_default_pss_sign.c +38 -0
  383. package/runtime/third_party/bearssl/src/rsa/rsa_default_pss_vrfy.c +38 -0
  384. package/runtime/third_party/bearssl/src/rsa/rsa_default_pub.c +38 -0
  385. package/runtime/third_party/bearssl/src/rsa/rsa_default_pubexp.c +36 -0
  386. package/runtime/third_party/bearssl/src/rsa/rsa_i15_keygen.c +583 -0
  387. package/runtime/third_party/bearssl/src/rsa/rsa_i15_modulus.c +99 -0
  388. package/runtime/third_party/bearssl/src/rsa/rsa_i15_oaep_decrypt.c +41 -0
  389. package/runtime/third_party/bearssl/src/rsa/rsa_i15_oaep_encrypt.c +44 -0
  390. package/runtime/third_party/bearssl/src/rsa/rsa_i15_pkcs1_sign.c +37 -0
  391. package/runtime/third_party/bearssl/src/rsa/rsa_i15_pkcs1_vrfy.c +43 -0
  392. package/runtime/third_party/bearssl/src/rsa/rsa_i15_priv.c +209 -0
  393. package/runtime/third_party/bearssl/src/rsa/rsa_i15_privexp.c +320 -0
  394. package/runtime/third_party/bearssl/src/rsa/rsa_i15_pss_sign.c +40 -0
  395. package/runtime/third_party/bearssl/src/rsa/rsa_i15_pss_vrfy.c +44 -0
  396. package/runtime/third_party/bearssl/src/rsa/rsa_i15_pub.c +113 -0
  397. package/runtime/third_party/bearssl/src/rsa/rsa_i15_pubexp.c +152 -0
  398. package/runtime/third_party/bearssl/src/rsa/rsa_i31_keygen.c +37 -0
  399. package/runtime/third_party/bearssl/src/rsa/rsa_i31_keygen_inner.c +608 -0
  400. package/runtime/third_party/bearssl/src/rsa/rsa_i31_modulus.c +99 -0
  401. package/runtime/third_party/bearssl/src/rsa/rsa_i31_oaep_decrypt.c +41 -0
  402. package/runtime/third_party/bearssl/src/rsa/rsa_i31_oaep_encrypt.c +44 -0
  403. package/runtime/third_party/bearssl/src/rsa/rsa_i31_pkcs1_sign.c +37 -0
  404. package/runtime/third_party/bearssl/src/rsa/rsa_i31_pkcs1_vrfy.c +43 -0
  405. package/runtime/third_party/bearssl/src/rsa/rsa_i31_priv.c +203 -0
  406. package/runtime/third_party/bearssl/src/rsa/rsa_i31_privexp.c +318 -0
  407. package/runtime/third_party/bearssl/src/rsa/rsa_i31_pss_sign.c +40 -0
  408. package/runtime/third_party/bearssl/src/rsa/rsa_i31_pss_vrfy.c +44 -0
  409. package/runtime/third_party/bearssl/src/rsa/rsa_i31_pub.c +106 -0
  410. package/runtime/third_party/bearssl/src/rsa/rsa_i31_pubexp.c +152 -0
  411. package/runtime/third_party/bearssl/src/rsa/rsa_i32_oaep_decrypt.c +41 -0
  412. package/runtime/third_party/bearssl/src/rsa/rsa_i32_oaep_encrypt.c +44 -0
  413. package/runtime/third_party/bearssl/src/rsa/rsa_i32_pkcs1_sign.c +37 -0
  414. package/runtime/third_party/bearssl/src/rsa/rsa_i32_pkcs1_vrfy.c +43 -0
  415. package/runtime/third_party/bearssl/src/rsa/rsa_i32_priv.c +160 -0
  416. package/runtime/third_party/bearssl/src/rsa/rsa_i32_pss_sign.c +40 -0
  417. package/runtime/third_party/bearssl/src/rsa/rsa_i32_pss_vrfy.c +44 -0
  418. package/runtime/third_party/bearssl/src/rsa/rsa_i32_pub.c +77 -0
  419. package/runtime/third_party/bearssl/src/rsa/rsa_i62_keygen.c +57 -0
  420. package/runtime/third_party/bearssl/src/rsa/rsa_i62_oaep_decrypt.c +61 -0
  421. package/runtime/third_party/bearssl/src/rsa/rsa_i62_oaep_encrypt.c +64 -0
  422. package/runtime/third_party/bearssl/src/rsa/rsa_i62_pkcs1_sign.c +57 -0
  423. package/runtime/third_party/bearssl/src/rsa/rsa_i62_pkcs1_vrfy.c +63 -0
  424. package/runtime/third_party/bearssl/src/rsa/rsa_i62_priv.c +223 -0
  425. package/runtime/third_party/bearssl/src/rsa/rsa_i62_pss_sign.c +60 -0
  426. package/runtime/third_party/bearssl/src/rsa/rsa_i62_pss_vrfy.c +64 -0
  427. package/runtime/third_party/bearssl/src/rsa/rsa_i62_pub.c +125 -0
  428. package/runtime/third_party/bearssl/src/rsa/rsa_oaep_pad.c +112 -0
  429. package/runtime/third_party/bearssl/src/rsa/rsa_oaep_unpad.c +145 -0
  430. package/runtime/third_party/bearssl/src/rsa/rsa_pkcs1_sig_pad.c +100 -0
  431. package/runtime/third_party/bearssl/src/rsa/rsa_pkcs1_sig_unpad.c +121 -0
  432. package/runtime/third_party/bearssl/src/rsa/rsa_pss_sig_pad.c +106 -0
  433. package/runtime/third_party/bearssl/src/rsa/rsa_pss_sig_unpad.c +121 -0
  434. package/runtime/third_party/bearssl/src/rsa/rsa_ssl_decrypt.c +52 -0
  435. package/runtime/third_party/bearssl/src/settings.c +306 -0
  436. package/runtime/third_party/bearssl/src/symcipher/aes_big_cbcdec.c +69 -0
  437. package/runtime/third_party/bearssl/src/symcipher/aes_big_cbcenc.c +67 -0
  438. package/runtime/third_party/bearssl/src/symcipher/aes_big_ctr.c +84 -0
  439. package/runtime/third_party/bearssl/src/symcipher/aes_big_ctrcbc.c +142 -0
  440. package/runtime/third_party/bearssl/src/symcipher/aes_big_dec.c +254 -0
  441. package/runtime/third_party/bearssl/src/symcipher/aes_big_enc.c +157 -0
  442. package/runtime/third_party/bearssl/src/symcipher/aes_common.c +112 -0
  443. package/runtime/third_party/bearssl/src/symcipher/aes_ct.c +328 -0
  444. package/runtime/third_party/bearssl/src/symcipher/aes_ct64.c +398 -0
  445. package/runtime/third_party/bearssl/src/symcipher/aes_ct64_cbcdec.c +104 -0
  446. package/runtime/third_party/bearssl/src/symcipher/aes_ct64_cbcenc.c +81 -0
  447. package/runtime/third_party/bearssl/src/symcipher/aes_ct64_ctr.c +114 -0
  448. package/runtime/third_party/bearssl/src/symcipher/aes_ct64_ctrcbc.c +433 -0
  449. package/runtime/third_party/bearssl/src/symcipher/aes_ct64_dec.c +159 -0
  450. package/runtime/third_party/bearssl/src/symcipher/aes_ct64_enc.c +115 -0
  451. package/runtime/third_party/bearssl/src/symcipher/aes_ct_cbcdec.c +111 -0
  452. package/runtime/third_party/bearssl/src/symcipher/aes_ct_cbcenc.c +91 -0
  453. package/runtime/third_party/bearssl/src/symcipher/aes_ct_ctr.c +116 -0
  454. package/runtime/third_party/bearssl/src/symcipher/aes_ct_ctrcbc.c +422 -0
  455. package/runtime/third_party/bearssl/src/symcipher/aes_ct_dec.c +170 -0
  456. package/runtime/third_party/bearssl/src/symcipher/aes_ct_enc.c +112 -0
  457. package/runtime/third_party/bearssl/src/symcipher/aes_pwr8.c +445 -0
  458. package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_cbcdec.c +670 -0
  459. package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_cbcenc.c +417 -0
  460. package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_ctr.c +717 -0
  461. package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_ctrcbc.c +946 -0
  462. package/runtime/third_party/bearssl/src/symcipher/aes_small_cbcdec.c +69 -0
  463. package/runtime/third_party/bearssl/src/symcipher/aes_small_cbcenc.c +67 -0
  464. package/runtime/third_party/bearssl/src/symcipher/aes_small_ctr.c +84 -0
  465. package/runtime/third_party/bearssl/src/symcipher/aes_small_ctrcbc.c +142 -0
  466. package/runtime/third_party/bearssl/src/symcipher/aes_small_dec.c +176 -0
  467. package/runtime/third_party/bearssl/src/symcipher/aes_small_enc.c +129 -0
  468. package/runtime/third_party/bearssl/src/symcipher/aes_x86ni.c +240 -0
  469. package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_cbcdec.c +223 -0
  470. package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_cbcenc.c +122 -0
  471. package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_ctr.c +211 -0
  472. package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_ctrcbc.c +596 -0
  473. package/runtime/third_party/bearssl/src/symcipher/chacha20_ct.c +106 -0
  474. package/runtime/third_party/bearssl/src/symcipher/chacha20_sse2.c +237 -0
  475. package/runtime/third_party/bearssl/src/symcipher/des_ct.c +411 -0
  476. package/runtime/third_party/bearssl/src/symcipher/des_ct_cbcdec.c +87 -0
  477. package/runtime/third_party/bearssl/src/symcipher/des_ct_cbcenc.c +69 -0
  478. package/runtime/third_party/bearssl/src/symcipher/des_support.c +166 -0
  479. package/runtime/third_party/bearssl/src/symcipher/des_tab.c +310 -0
  480. package/runtime/third_party/bearssl/src/symcipher/des_tab_cbcdec.c +85 -0
  481. package/runtime/third_party/bearssl/src/symcipher/des_tab_cbcenc.c +67 -0
  482. package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmul.c +260 -0
  483. package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmul32.c +297 -0
  484. package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmulq.c +475 -0
  485. package/runtime/third_party/bearssl/src/symcipher/poly1305_i15.c +221 -0
  486. package/runtime/third_party/bearssl/src/x509/asn1.t0 +757 -0
  487. package/runtime/third_party/bearssl/src/x509/asn1enc.c +93 -0
  488. package/runtime/third_party/bearssl/src/x509/encode_ec_pk8der.c +110 -0
  489. package/runtime/third_party/bearssl/src/x509/encode_ec_rawder.c +161 -0
  490. package/runtime/third_party/bearssl/src/x509/encode_rsa_pk8der.c +97 -0
  491. package/runtime/third_party/bearssl/src/x509/encode_rsa_rawder.c +96 -0
  492. package/runtime/third_party/bearssl/src/x509/skey_decoder.c +653 -0
  493. package/runtime/third_party/bearssl/src/x509/skey_decoder.t0 +376 -0
  494. package/runtime/third_party/bearssl/src/x509/x509_decoder.c +776 -0
  495. package/runtime/third_party/bearssl/src/x509/x509_decoder.t0 +324 -0
  496. package/runtime/third_party/bearssl/src/x509/x509_knownkey.c +105 -0
  497. package/runtime/third_party/bearssl/src/x509/x509_minimal.c +1722 -0
  498. package/runtime/third_party/bearssl/src/x509/x509_minimal.t0 +1526 -0
  499. package/runtime/third_party/bearssl/src/x509/x509_minimal_full.c +59 -0
  500. package/runtime/third_party/quickjs-dtoa/LICENSE +22 -0
  501. package/runtime/third_party/quickjs-dtoa/README.md +23 -0
  502. package/runtime/third_party/quickjs-dtoa/dtoa.c +1640 -0
  503. package/runtime/third_party/quickjs-dtoa/dtoa.h +83 -0
  504. package/runtime/third_party/quickjs-dtoa/dtoa_compat.h +89 -0
  505. package/runtime/third_party/quickjs-regexp/LICENSE +22 -0
  506. package/runtime/third_party/quickjs-regexp/README.md +22 -0
  507. package/runtime/third_party/quickjs-regexp/cutils.c +641 -0
  508. package/runtime/third_party/quickjs-regexp/cutils.h +457 -0
  509. package/runtime/third_party/quickjs-regexp/libregexp-opcode.h +73 -0
  510. package/runtime/third_party/quickjs-regexp/libregexp.c +3448 -0
  511. package/runtime/third_party/quickjs-regexp/libregexp.h +65 -0
  512. package/runtime/third_party/quickjs-regexp/libunicode-table.h +5206 -0
  513. package/runtime/third_party/quickjs-regexp/libunicode.c +2124 -0
  514. package/runtime/third_party/quickjs-regexp/libunicode.h +196 -0
  515. package/runtime/third_party/v8-ieee754/LICENSE +61 -0
  516. package/runtime/third_party/v8-ieee754/README.md +22 -0
  517. package/runtime/third_party/v8-ieee754/convert.py +84 -0
  518. package/runtime/third_party/v8-ieee754/ieee754.c +3030 -0
  519. package/runtime/third_party/v8-ieee754/ieee754.h +27 -0
  520. package/runtime/tools/regexp_compile.c +90 -0
@@ -0,0 +1,831 @@
1
+ /*
2
+ * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining
5
+ * a copy of this software and associated documentation files (the
6
+ * "Software"), to deal in the Software without restriction, including
7
+ * without limitation the rights to use, copy, modify, merge, publish,
8
+ * distribute, sublicense, and/or sell copies of the Software, and to
9
+ * permit persons to whom the Software is furnished to do so, subject to
10
+ * the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be
13
+ * included in all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ #include "inner.h"
26
+
27
+ #if BR_INT128 || BR_UMUL128
28
+
29
+ #if BR_UMUL128
30
+ #include <intrin.h>
31
+ #endif
32
+
33
+ static const unsigned char GEN[] = {
34
+ 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
35
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
36
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
38
+ };
39
+
40
+ static const unsigned char ORDER[] = {
41
+ 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
42
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
43
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
44
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
45
+ };
46
+
47
+ static const unsigned char *
48
+ api_generator(int curve, size_t *len)
49
+ {
50
+ (void)curve;
51
+ *len = 32;
52
+ return GEN;
53
+ }
54
+
55
+ static const unsigned char *
56
+ api_order(int curve, size_t *len)
57
+ {
58
+ (void)curve;
59
+ *len = 32;
60
+ return ORDER;
61
+ }
62
+
63
+ static size_t
64
+ api_xoff(int curve, size_t *len)
65
+ {
66
+ (void)curve;
67
+ *len = 32;
68
+ return 0;
69
+ }
70
+
71
+ /*
72
+ * A field element is encoded as four 64-bit integers, in basis 2^63.
73
+ * Operations return partially reduced values, which may range up to
74
+ * 2^255+37.
75
+ */
76
+
77
+ #define MASK63 (((uint64_t)1 << 63) - (uint64_t)1)
78
+
79
+ /*
80
+ * Swap two field elements, conditionally on a flag.
81
+ */
82
+ static inline void
83
+ f255_cswap(uint64_t *a, uint64_t *b, uint32_t ctl)
84
+ {
85
+ uint64_t m, w;
86
+
87
+ m = -(uint64_t)ctl;
88
+ w = m & (a[0] ^ b[0]); a[0] ^= w; b[0] ^= w;
89
+ w = m & (a[1] ^ b[1]); a[1] ^= w; b[1] ^= w;
90
+ w = m & (a[2] ^ b[2]); a[2] ^= w; b[2] ^= w;
91
+ w = m & (a[3] ^ b[3]); a[3] ^= w; b[3] ^= w;
92
+ }
93
+
94
+ /*
95
+ * Addition in the field.
96
+ */
97
+ static inline void
98
+ f255_add(uint64_t *d, const uint64_t *a, const uint64_t *b)
99
+ {
100
+ #if BR_INT128
101
+
102
+ uint64_t t0, t1, t2, t3, cc;
103
+ unsigned __int128 z;
104
+
105
+ z = (unsigned __int128)a[0] + (unsigned __int128)b[0];
106
+ t0 = (uint64_t)z;
107
+ z = (unsigned __int128)a[1] + (unsigned __int128)b[1] + (z >> 64);
108
+ t1 = (uint64_t)z;
109
+ z = (unsigned __int128)a[2] + (unsigned __int128)b[2] + (z >> 64);
110
+ t2 = (uint64_t)z;
111
+ z = (unsigned __int128)a[3] + (unsigned __int128)b[3] + (z >> 64);
112
+ t3 = (uint64_t)z & MASK63;
113
+ cc = (uint64_t)(z >> 63);
114
+
115
+ /*
116
+ * Since operands are at most 2^255+37, the sum is at most
117
+ * 2^256+74; thus, the carry cc is equal to 0, 1 or 2.
118
+ *
119
+ * We use: 2^255 = 19 mod p.
120
+ * Since we add 0, 19 or 38 to a value that fits on 255 bits,
121
+ * the result is at most 2^255+37.
122
+ */
123
+ z = (unsigned __int128)t0 + (unsigned __int128)(19 * cc);
124
+ d[0] = (uint64_t)z;
125
+ z = (unsigned __int128)t1 + (z >> 64);
126
+ d[1] = (uint64_t)z;
127
+ z = (unsigned __int128)t2 + (z >> 64);
128
+ d[2] = (uint64_t)z;
129
+ d[3] = t3 + (uint64_t)(z >> 64);
130
+
131
+ #elif BR_UMUL128
132
+
133
+ uint64_t t0, t1, t2, t3, cc;
134
+ unsigned char k;
135
+
136
+ k = _addcarry_u64(0, a[0], b[0], &t0);
137
+ k = _addcarry_u64(k, a[1], b[1], &t1);
138
+ k = _addcarry_u64(k, a[2], b[2], &t2);
139
+ k = _addcarry_u64(k, a[3], b[3], &t3);
140
+ cc = (k << 1) + (t3 >> 63);
141
+ t3 &= MASK63;
142
+
143
+ /*
144
+ * Since operands are at most 2^255+37, the sum is at most
145
+ * 2^256+74; thus, the carry cc is equal to 0, 1 or 2.
146
+ *
147
+ * We use: 2^255 = 19 mod p.
148
+ * Since we add 0, 19 or 38 to a value that fits on 255 bits,
149
+ * the result is at most 2^255+37.
150
+ */
151
+ k = _addcarry_u64(0, t0, 19 * cc, &d[0]);
152
+ k = _addcarry_u64(k, t1, 0, &d[1]);
153
+ k = _addcarry_u64(k, t2, 0, &d[2]);
154
+ (void)_addcarry_u64(k, t3, 0, &d[3]);
155
+
156
+ #endif
157
+ }
158
+
159
+ /*
160
+ * Subtraction.
161
+ */
162
+ static inline void
163
+ f255_sub(uint64_t *d, const uint64_t *a, const uint64_t *b)
164
+ {
165
+ #if BR_INT128
166
+
167
+ /*
168
+ * We compute t = 2^256 - 38 + a - b, which is necessarily
169
+ * positive but lower than 2^256 + 2^255, since a <= 2^255 + 37
170
+ * and b <= 2^255 + 37. We then subtract 0, p or 2*p, depending
171
+ * on the two upper bits of t (bits 255 and 256).
172
+ */
173
+
174
+ uint64_t t0, t1, t2, t3, t4, cc;
175
+ unsigned __int128 z;
176
+
177
+ z = (unsigned __int128)a[0] - (unsigned __int128)b[0] - 38;
178
+ t0 = (uint64_t)z;
179
+ cc = -(uint64_t)(z >> 64);
180
+ z = (unsigned __int128)a[1] - (unsigned __int128)b[1]
181
+ - (unsigned __int128)cc;
182
+ t1 = (uint64_t)z;
183
+ cc = -(uint64_t)(z >> 64);
184
+ z = (unsigned __int128)a[2] - (unsigned __int128)b[2]
185
+ - (unsigned __int128)cc;
186
+ t2 = (uint64_t)z;
187
+ cc = -(uint64_t)(z >> 64);
188
+ z = (unsigned __int128)a[3] - (unsigned __int128)b[3]
189
+ - (unsigned __int128)cc;
190
+ t3 = (uint64_t)z;
191
+ t4 = 1 + (uint64_t)(z >> 64);
192
+
193
+ /*
194
+ * We have a 257-bit result. The two top bits can be 00, 01 or 10,
195
+ * but not 11 (value t <= 2^256 - 38 + 2^255 + 37 = 2^256 + 2^255 - 1).
196
+ * Therefore, we can truncate to 255 bits, and add 0, 19 or 38.
197
+ * This guarantees that the result is at most 2^255+37.
198
+ */
199
+ cc = (38 & -t4) + (19 & -(t3 >> 63));
200
+ t3 &= MASK63;
201
+ z = (unsigned __int128)t0 + (unsigned __int128)cc;
202
+ d[0] = (uint64_t)z;
203
+ z = (unsigned __int128)t1 + (z >> 64);
204
+ d[1] = (uint64_t)z;
205
+ z = (unsigned __int128)t2 + (z >> 64);
206
+ d[2] = (uint64_t)z;
207
+ d[3] = t3 + (uint64_t)(z >> 64);
208
+
209
+ #elif BR_UMUL128
210
+
211
+ /*
212
+ * We compute t = 2^256 - 38 + a - b, which is necessarily
213
+ * positive but lower than 2^256 + 2^255, since a <= 2^255 + 37
214
+ * and b <= 2^255 + 37. We then subtract 0, p or 2*p, depending
215
+ * on the two upper bits of t (bits 255 and 256).
216
+ */
217
+
218
+ uint64_t t0, t1, t2, t3, t4;
219
+ unsigned char k;
220
+
221
+ k = _subborrow_u64(0, a[0], b[0], &t0);
222
+ k = _subborrow_u64(k, a[1], b[1], &t1);
223
+ k = _subborrow_u64(k, a[2], b[2], &t2);
224
+ k = _subborrow_u64(k, a[3], b[3], &t3);
225
+ (void)_subborrow_u64(k, 1, 0, &t4);
226
+
227
+ k = _subborrow_u64(0, t0, 38, &t0);
228
+ k = _subborrow_u64(k, t1, 0, &t1);
229
+ k = _subborrow_u64(k, t2, 0, &t2);
230
+ k = _subborrow_u64(k, t3, 0, &t3);
231
+ (void)_subborrow_u64(k, t4, 0, &t4);
232
+
233
+ /*
234
+ * We have a 257-bit result. The two top bits can be 00, 01 or 10,
235
+ * but not 11 (value t <= 2^256 - 38 + 2^255 + 37 = 2^256 + 2^255 - 1).
236
+ * Therefore, we can truncate to 255 bits, and add 0, 19 or 38.
237
+ * This guarantees that the result is at most 2^255+37.
238
+ */
239
+ t4 = (38 & -t4) + (19 & -(t3 >> 63));
240
+ t3 &= MASK63;
241
+ k = _addcarry_u64(0, t0, t4, &d[0]);
242
+ k = _addcarry_u64(k, t1, 0, &d[1]);
243
+ k = _addcarry_u64(k, t2, 0, &d[2]);
244
+ (void)_addcarry_u64(k, t3, 0, &d[3]);
245
+
246
+ #endif
247
+ }
248
+
249
+ /*
250
+ * Multiplication.
251
+ */
252
+ static inline void
253
+ f255_mul(uint64_t *d, uint64_t *a, uint64_t *b)
254
+ {
255
+ #if BR_INT128
256
+
257
+ unsigned __int128 z;
258
+ uint64_t t0, t1, t2, t3, t4, t5, t6, t7, th;
259
+
260
+ /*
261
+ * Compute the product a*b over plain integers.
262
+ */
263
+ z = (unsigned __int128)a[0] * (unsigned __int128)b[0];
264
+ t0 = (uint64_t)z;
265
+ z = (unsigned __int128)a[0] * (unsigned __int128)b[1] + (z >> 64);
266
+ t1 = (uint64_t)z;
267
+ z = (unsigned __int128)a[0] * (unsigned __int128)b[2] + (z >> 64);
268
+ t2 = (uint64_t)z;
269
+ z = (unsigned __int128)a[0] * (unsigned __int128)b[3] + (z >> 64);
270
+ t3 = (uint64_t)z;
271
+ t4 = (uint64_t)(z >> 64);
272
+
273
+ z = (unsigned __int128)a[1] * (unsigned __int128)b[0]
274
+ + (unsigned __int128)t1;
275
+ t1 = (uint64_t)z;
276
+ z = (unsigned __int128)a[1] * (unsigned __int128)b[1]
277
+ + (unsigned __int128)t2 + (z >> 64);
278
+ t2 = (uint64_t)z;
279
+ z = (unsigned __int128)a[1] * (unsigned __int128)b[2]
280
+ + (unsigned __int128)t3 + (z >> 64);
281
+ t3 = (uint64_t)z;
282
+ z = (unsigned __int128)a[1] * (unsigned __int128)b[3]
283
+ + (unsigned __int128)t4 + (z >> 64);
284
+ t4 = (uint64_t)z;
285
+ t5 = (uint64_t)(z >> 64);
286
+
287
+ z = (unsigned __int128)a[2] * (unsigned __int128)b[0]
288
+ + (unsigned __int128)t2;
289
+ t2 = (uint64_t)z;
290
+ z = (unsigned __int128)a[2] * (unsigned __int128)b[1]
291
+ + (unsigned __int128)t3 + (z >> 64);
292
+ t3 = (uint64_t)z;
293
+ z = (unsigned __int128)a[2] * (unsigned __int128)b[2]
294
+ + (unsigned __int128)t4 + (z >> 64);
295
+ t4 = (uint64_t)z;
296
+ z = (unsigned __int128)a[2] * (unsigned __int128)b[3]
297
+ + (unsigned __int128)t5 + (z >> 64);
298
+ t5 = (uint64_t)z;
299
+ t6 = (uint64_t)(z >> 64);
300
+
301
+ z = (unsigned __int128)a[3] * (unsigned __int128)b[0]
302
+ + (unsigned __int128)t3;
303
+ t3 = (uint64_t)z;
304
+ z = (unsigned __int128)a[3] * (unsigned __int128)b[1]
305
+ + (unsigned __int128)t4 + (z >> 64);
306
+ t4 = (uint64_t)z;
307
+ z = (unsigned __int128)a[3] * (unsigned __int128)b[2]
308
+ + (unsigned __int128)t5 + (z >> 64);
309
+ t5 = (uint64_t)z;
310
+ z = (unsigned __int128)a[3] * (unsigned __int128)b[3]
311
+ + (unsigned __int128)t6 + (z >> 64);
312
+ t6 = (uint64_t)z;
313
+ t7 = (uint64_t)(z >> 64);
314
+
315
+ /*
316
+ * Modulo p, we have:
317
+ *
318
+ * 2^255 = 19
319
+ * 2^510 = 19*19 = 361
320
+ *
321
+ * We split the intermediate t into three parts, in basis
322
+ * 2^255. The low one will be in t0..t3; the middle one in t4..t7.
323
+ * The upper one can only be a single bit (th), since the
324
+ * multiplication operands are at most 2^255+37 each.
325
+ */
326
+ th = t7 >> 62;
327
+ t7 = ((t7 << 1) | (t6 >> 63)) & MASK63;
328
+ t6 = (t6 << 1) | (t5 >> 63);
329
+ t5 = (t5 << 1) | (t4 >> 63);
330
+ t4 = (t4 << 1) | (t3 >> 63);
331
+ t3 &= MASK63;
332
+
333
+ /*
334
+ * Multiply the middle part (t4..t7) by 19. We truncate it to
335
+ * 255 bits; the extra bits will go along with th.
336
+ */
337
+ z = (unsigned __int128)t4 * 19;
338
+ t4 = (uint64_t)z;
339
+ z = (unsigned __int128)t5 * 19 + (z >> 64);
340
+ t5 = (uint64_t)z;
341
+ z = (unsigned __int128)t6 * 19 + (z >> 64);
342
+ t6 = (uint64_t)z;
343
+ z = (unsigned __int128)t7 * 19 + (z >> 64);
344
+ t7 = (uint64_t)z & MASK63;
345
+
346
+ th = (361 & -th) + (19 * (uint64_t)(z >> 63));
347
+
348
+ /*
349
+ * Add elements together.
350
+ * At this point:
351
+ * t0..t3 fits on 255 bits.
352
+ * t4..t7 fits on 255 bits.
353
+ * th <= 361 + 342 = 703.
354
+ */
355
+ z = (unsigned __int128)t0 + (unsigned __int128)t4
356
+ + (unsigned __int128)th;
357
+ t0 = (uint64_t)z;
358
+ z = (unsigned __int128)t1 + (unsigned __int128)t5 + (z >> 64);
359
+ t1 = (uint64_t)z;
360
+ z = (unsigned __int128)t2 + (unsigned __int128)t6 + (z >> 64);
361
+ t2 = (uint64_t)z;
362
+ z = (unsigned __int128)t3 + (unsigned __int128)t7 + (z >> 64);
363
+ t3 = (uint64_t)z & MASK63;
364
+ th = (uint64_t)(z >> 63);
365
+
366
+ /*
367
+ * Since the sum is at most 2^256 + 703, the two upper bits, in th,
368
+ * can only have value 0, 1 or 2. We just add th*19, which
369
+ * guarantees a result of at most 2^255+37.
370
+ */
371
+ z = (unsigned __int128)t0 + (19 * th);
372
+ d[0] = (uint64_t)z;
373
+ z = (unsigned __int128)t1 + (z >> 64);
374
+ d[1] = (uint64_t)z;
375
+ z = (unsigned __int128)t2 + (z >> 64);
376
+ d[2] = (uint64_t)z;
377
+ d[3] = t3 + (uint64_t)(z >> 64);
378
+
379
+ #elif BR_UMUL128
380
+
381
+ uint64_t t0, t1, t2, t3, t4, t5, t6, t7, th;
382
+ uint64_t h0, h1, h2, h3;
383
+ unsigned char k;
384
+
385
+ /*
386
+ * Compute the product a*b over plain integers.
387
+ */
388
+ t0 = _umul128(a[0], b[0], &h0);
389
+ t1 = _umul128(a[0], b[1], &h1);
390
+ k = _addcarry_u64(0, t1, h0, &t1);
391
+ t2 = _umul128(a[0], b[2], &h2);
392
+ k = _addcarry_u64(k, t2, h1, &t2);
393
+ t3 = _umul128(a[0], b[3], &h3);
394
+ k = _addcarry_u64(k, t3, h2, &t3);
395
+ (void)_addcarry_u64(k, h3, 0, &t4);
396
+
397
+ k = _addcarry_u64(0, _umul128(a[1], b[0], &h0), t1, &t1);
398
+ k = _addcarry_u64(k, _umul128(a[1], b[1], &h1), t2, &t2);
399
+ k = _addcarry_u64(k, _umul128(a[1], b[2], &h2), t3, &t3);
400
+ k = _addcarry_u64(k, _umul128(a[1], b[3], &h3), t4, &t4);
401
+ t5 = k;
402
+ k = _addcarry_u64(0, t2, h0, &t2);
403
+ k = _addcarry_u64(k, t3, h1, &t3);
404
+ k = _addcarry_u64(k, t4, h2, &t4);
405
+ (void)_addcarry_u64(k, t5, h3, &t5);
406
+
407
+ k = _addcarry_u64(0, _umul128(a[2], b[0], &h0), t2, &t2);
408
+ k = _addcarry_u64(k, _umul128(a[2], b[1], &h1), t3, &t3);
409
+ k = _addcarry_u64(k, _umul128(a[2], b[2], &h2), t4, &t4);
410
+ k = _addcarry_u64(k, _umul128(a[2], b[3], &h3), t5, &t5);
411
+ t6 = k;
412
+ k = _addcarry_u64(0, t3, h0, &t3);
413
+ k = _addcarry_u64(k, t4, h1, &t4);
414
+ k = _addcarry_u64(k, t5, h2, &t5);
415
+ (void)_addcarry_u64(k, t6, h3, &t6);
416
+
417
+ k = _addcarry_u64(0, _umul128(a[3], b[0], &h0), t3, &t3);
418
+ k = _addcarry_u64(k, _umul128(a[3], b[1], &h1), t4, &t4);
419
+ k = _addcarry_u64(k, _umul128(a[3], b[2], &h2), t5, &t5);
420
+ k = _addcarry_u64(k, _umul128(a[3], b[3], &h3), t6, &t6);
421
+ t7 = k;
422
+ k = _addcarry_u64(0, t4, h0, &t4);
423
+ k = _addcarry_u64(k, t5, h1, &t5);
424
+ k = _addcarry_u64(k, t6, h2, &t6);
425
+ (void)_addcarry_u64(k, t7, h3, &t7);
426
+
427
+ /*
428
+ * Modulo p, we have:
429
+ *
430
+ * 2^255 = 19
431
+ * 2^510 = 19*19 = 361
432
+ *
433
+ * We split the intermediate t into three parts, in basis
434
+ * 2^255. The low one will be in t0..t3; the middle one in t4..t7.
435
+ * The upper one can only be a single bit (th), since the
436
+ * multiplication operands are at most 2^255+37 each.
437
+ */
438
+ th = t7 >> 62;
439
+ t7 = ((t7 << 1) | (t6 >> 63)) & MASK63;
440
+ t6 = (t6 << 1) | (t5 >> 63);
441
+ t5 = (t5 << 1) | (t4 >> 63);
442
+ t4 = (t4 << 1) | (t3 >> 63);
443
+ t3 &= MASK63;
444
+
445
+ /*
446
+ * Multiply the middle part (t4..t7) by 19. We truncate it to
447
+ * 255 bits; the extra bits will go along with th.
448
+ */
449
+ t4 = _umul128(t4, 19, &h0);
450
+ t5 = _umul128(t5, 19, &h1);
451
+ t6 = _umul128(t6, 19, &h2);
452
+ t7 = _umul128(t7, 19, &h3);
453
+ k = _addcarry_u64(0, t5, h0, &t5);
454
+ k = _addcarry_u64(k, t6, h1, &t6);
455
+ k = _addcarry_u64(k, t7, h2, &t7);
456
+ (void)_addcarry_u64(k, h3, 0, &h3);
457
+ th = (361 & -th) + (19 * ((h3 << 1) + (t7 >> 63)));
458
+ t7 &= MASK63;
459
+
460
+ /*
461
+ * Add elements together.
462
+ * At this point:
463
+ * t0..t3 fits on 255 bits.
464
+ * t4..t7 fits on 255 bits.
465
+ * th <= 361 + 342 = 703.
466
+ */
467
+ k = _addcarry_u64(0, t0, t4, &t0);
468
+ k = _addcarry_u64(k, t1, t5, &t1);
469
+ k = _addcarry_u64(k, t2, t6, &t2);
470
+ k = _addcarry_u64(k, t3, t7, &t3);
471
+ t4 = k;
472
+ k = _addcarry_u64(0, t0, th, &t0);
473
+ k = _addcarry_u64(k, t1, 0, &t1);
474
+ k = _addcarry_u64(k, t2, 0, &t2);
475
+ k = _addcarry_u64(k, t3, 0, &t3);
476
+ (void)_addcarry_u64(k, t4, 0, &t4);
477
+
478
+ th = (t4 << 1) + (t3 >> 63);
479
+ t3 &= MASK63;
480
+
481
+ /*
482
+ * Since the sum is at most 2^256 + 703, the two upper bits, in th,
483
+ * can only have value 0, 1 or 2. We just add th*19, which
484
+ * guarantees a result of at most 2^255+37.
485
+ */
486
+ k = _addcarry_u64(0, t0, 19 * th, &d[0]);
487
+ k = _addcarry_u64(k, t1, 0, &d[1]);
488
+ k = _addcarry_u64(k, t2, 0, &d[2]);
489
+ (void)_addcarry_u64(k, t3, 0, &d[3]);
490
+
491
+ #endif
492
+ }
493
+
494
+ /*
495
+ * Multiplication by A24 = 121665.
496
+ */
497
+ static inline void
498
+ f255_mul_a24(uint64_t *d, const uint64_t *a)
499
+ {
500
+ #if BR_INT128
501
+
502
+ uint64_t t0, t1, t2, t3;
503
+ unsigned __int128 z;
504
+
505
+ z = (unsigned __int128)a[0] * 121665;
506
+ t0 = (uint64_t)z;
507
+ z = (unsigned __int128)a[1] * 121665 + (z >> 64);
508
+ t1 = (uint64_t)z;
509
+ z = (unsigned __int128)a[2] * 121665 + (z >> 64);
510
+ t2 = (uint64_t)z;
511
+ z = (unsigned __int128)a[3] * 121665 + (z >> 64);
512
+ t3 = (uint64_t)z & MASK63;
513
+
514
+ z = (unsigned __int128)t0 + (19 * (uint64_t)(z >> 63));
515
+ t0 = (uint64_t)z;
516
+ z = (unsigned __int128)t1 + (z >> 64);
517
+ t1 = (uint64_t)z;
518
+ z = (unsigned __int128)t2 + (z >> 64);
519
+ t2 = (uint64_t)z;
520
+ t3 = t3 + (uint64_t)(z >> 64);
521
+
522
+ z = (unsigned __int128)t0 + (19 & -(t3 >> 63));
523
+ d[0] = (uint64_t)z;
524
+ z = (unsigned __int128)t1 + (z >> 64);
525
+ d[1] = (uint64_t)z;
526
+ z = (unsigned __int128)t2 + (z >> 64);
527
+ d[2] = (uint64_t)z;
528
+ d[3] = (t3 & MASK63) + (uint64_t)(z >> 64);
529
+
530
+ #elif BR_UMUL128
531
+
532
+ uint64_t t0, t1, t2, t3, t4, h0, h1, h2, h3;
533
+ unsigned char k;
534
+
535
+ t0 = _umul128(a[0], 121665, &h0);
536
+ t1 = _umul128(a[1], 121665, &h1);
537
+ k = _addcarry_u64(0, t1, h0, &t1);
538
+ t2 = _umul128(a[2], 121665, &h2);
539
+ k = _addcarry_u64(k, t2, h1, &t2);
540
+ t3 = _umul128(a[3], 121665, &h3);
541
+ k = _addcarry_u64(k, t3, h2, &t3);
542
+ (void)_addcarry_u64(k, h3, 0, &t4);
543
+
544
+ t4 = (t4 << 1) + (t3 >> 63);
545
+ t3 &= MASK63;
546
+ k = _addcarry_u64(0, t0, 19 * t4, &t0);
547
+ k = _addcarry_u64(k, t1, 0, &t1);
548
+ k = _addcarry_u64(k, t2, 0, &t2);
549
+ (void)_addcarry_u64(k, t3, 0, &t3);
550
+
551
+ t4 = 19 & -(t3 >> 63);
552
+ t3 &= MASK63;
553
+ k = _addcarry_u64(0, t0, t4, &d[0]);
554
+ k = _addcarry_u64(k, t1, 0, &d[1]);
555
+ k = _addcarry_u64(k, t2, 0, &d[2]);
556
+ (void)_addcarry_u64(k, t3, 0, &d[3]);
557
+
558
+ #endif
559
+ }
560
+
561
+ /*
562
+ * Finalize reduction.
563
+ */
564
+ static inline void
565
+ f255_final_reduce(uint64_t *a)
566
+ {
567
+ #if BR_INT128
568
+
569
+ uint64_t t0, t1, t2, t3, m;
570
+ unsigned __int128 z;
571
+
572
+ /*
573
+ * We add 19. If the result (in t) is below 2^255, then a[]
574
+ * is already less than 2^255-19, thus already reduced.
575
+ * Otherwise, we subtract 2^255 from t[], in which case we
576
+ * have t = a - (2^255-19), and that's our result.
577
+ */
578
+ z = (unsigned __int128)a[0] + 19;
579
+ t0 = (uint64_t)z;
580
+ z = (unsigned __int128)a[1] + (z >> 64);
581
+ t1 = (uint64_t)z;
582
+ z = (unsigned __int128)a[2] + (z >> 64);
583
+ t2 = (uint64_t)z;
584
+ t3 = a[3] + (uint64_t)(z >> 64);
585
+
586
+ m = -(t3 >> 63);
587
+ t3 &= MASK63;
588
+ a[0] ^= m & (a[0] ^ t0);
589
+ a[1] ^= m & (a[1] ^ t1);
590
+ a[2] ^= m & (a[2] ^ t2);
591
+ a[3] ^= m & (a[3] ^ t3);
592
+
593
+ #elif BR_UMUL128
594
+
595
+ uint64_t t0, t1, t2, t3, m;
596
+ unsigned char k;
597
+
598
+ /*
599
+ * We add 19. If the result (in t) is below 2^255, then a[]
600
+ * is already less than 2^255-19, thus already reduced.
601
+ * Otherwise, we subtract 2^255 from t[], in which case we
602
+ * have t = a - (2^255-19), and that's our result.
603
+ */
604
+ k = _addcarry_u64(0, a[0], 19, &t0);
605
+ k = _addcarry_u64(k, a[1], 0, &t1);
606
+ k = _addcarry_u64(k, a[2], 0, &t2);
607
+ (void)_addcarry_u64(k, a[3], 0, &t3);
608
+
609
+ m = -(t3 >> 63);
610
+ t3 &= MASK63;
611
+ a[0] ^= m & (a[0] ^ t0);
612
+ a[1] ^= m & (a[1] ^ t1);
613
+ a[2] ^= m & (a[2] ^ t2);
614
+ a[3] ^= m & (a[3] ^ t3);
615
+
616
+ #endif
617
+ }
618
+
619
+ static uint32_t
620
+ api_mul(unsigned char *G, size_t Glen,
621
+ const unsigned char *kb, size_t kblen, int curve)
622
+ {
623
+ unsigned char k[32];
624
+ uint64_t x1[4], x2[4], z2[4], x3[4], z3[4];
625
+ uint32_t swap;
626
+ int i;
627
+
628
+ (void)curve;
629
+
630
+ /*
631
+ * Points are encoded over exactly 32 bytes. Multipliers must fit
632
+ * in 32 bytes as well.
633
+ */
634
+ if (Glen != 32 || kblen > 32) {
635
+ return 0;
636
+ }
637
+
638
+ /*
639
+ * RFC 7748 mandates that the high bit of the last point byte must
640
+ * be ignored/cleared.
641
+ */
642
+ x1[0] = br_dec64le(&G[ 0]);
643
+ x1[1] = br_dec64le(&G[ 8]);
644
+ x1[2] = br_dec64le(&G[16]);
645
+ x1[3] = br_dec64le(&G[24]) & MASK63;
646
+
647
+ /*
648
+ * We can use memset() to clear values, because exact-width types
649
+ * like uint64_t are guaranteed to have no padding bits or
650
+ * trap representations.
651
+ */
652
+ memset(x2, 0, sizeof x2);
653
+ x2[0] = 1;
654
+ memset(z2, 0, sizeof z2);
655
+ memcpy(x3, x1, sizeof x1);
656
+ memcpy(z3, x2, sizeof x2);
657
+
658
+ /*
659
+ * The multiplier is provided in big-endian notation, and
660
+ * possibly shorter than 32 bytes.
661
+ */
662
+ memset(k, 0, (sizeof k) - kblen);
663
+ memcpy(k + (sizeof k) - kblen, kb, kblen);
664
+ k[31] &= 0xF8;
665
+ k[0] &= 0x7F;
666
+ k[0] |= 0x40;
667
+
668
+ swap = 0;
669
+
670
+ for (i = 254; i >= 0; i --) {
671
+ uint64_t a[4], aa[4], b[4], bb[4], e[4];
672
+ uint64_t c[4], d[4], da[4], cb[4];
673
+ uint32_t kt;
674
+
675
+ kt = (k[31 - (i >> 3)] >> (i & 7)) & 1;
676
+ swap ^= kt;
677
+ f255_cswap(x2, x3, swap);
678
+ f255_cswap(z2, z3, swap);
679
+ swap = kt;
680
+
681
+ /* A = x_2 + z_2 */
682
+ f255_add(a, x2, z2);
683
+
684
+ /* AA = A^2 */
685
+ f255_mul(aa, a, a);
686
+
687
+ /* B = x_2 - z_2 */
688
+ f255_sub(b, x2, z2);
689
+
690
+ /* BB = B^2 */
691
+ f255_mul(bb, b, b);
692
+
693
+ /* E = AA - BB */
694
+ f255_sub(e, aa, bb);
695
+
696
+ /* C = x_3 + z_3 */
697
+ f255_add(c, x3, z3);
698
+
699
+ /* D = x_3 - z_3 */
700
+ f255_sub(d, x3, z3);
701
+
702
+ /* DA = D * A */
703
+ f255_mul(da, d, a);
704
+
705
+ /* CB = C * B */
706
+ f255_mul(cb, c, b);
707
+
708
+ /* x_3 = (DA + CB)^2 */
709
+ f255_add(x3, da, cb);
710
+ f255_mul(x3, x3, x3);
711
+
712
+ /* z_3 = x_1 * (DA - CB)^2 */
713
+ f255_sub(z3, da, cb);
714
+ f255_mul(z3, z3, z3);
715
+ f255_mul(z3, x1, z3);
716
+
717
+ /* x_2 = AA * BB */
718
+ f255_mul(x2, aa, bb);
719
+
720
+ /* z_2 = E * (AA + a24 * E) */
721
+ f255_mul_a24(z2, e);
722
+ f255_add(z2, aa, z2);
723
+ f255_mul(z2, e, z2);
724
+ }
725
+
726
+ f255_cswap(x2, x3, swap);
727
+ f255_cswap(z2, z3, swap);
728
+
729
+ /*
730
+ * Compute 1/z2 = z2^(p-2). Since p = 2^255-19, we can mutualize
731
+ * most non-squarings. We use x1 and x3, now useless, as temporaries.
732
+ */
733
+ memcpy(x1, z2, sizeof z2);
734
+ for (i = 0; i < 15; i ++) {
735
+ f255_mul(x1, x1, x1);
736
+ f255_mul(x1, x1, z2);
737
+ }
738
+ memcpy(x3, x1, sizeof x1);
739
+ for (i = 0; i < 14; i ++) {
740
+ int j;
741
+
742
+ for (j = 0; j < 16; j ++) {
743
+ f255_mul(x3, x3, x3);
744
+ }
745
+ f255_mul(x3, x3, x1);
746
+ }
747
+ for (i = 14; i >= 0; i --) {
748
+ f255_mul(x3, x3, x3);
749
+ if ((0xFFEB >> i) & 1) {
750
+ f255_mul(x3, z2, x3);
751
+ }
752
+ }
753
+
754
+ /*
755
+ * Compute x2/z2. We have 1/z2 in x3.
756
+ */
757
+ f255_mul(x2, x2, x3);
758
+ f255_final_reduce(x2);
759
+
760
+ /*
761
+ * Encode the final x2 value in little-endian.
762
+ */
763
+ br_enc64le(G, x2[0]);
764
+ br_enc64le(G + 8, x2[1]);
765
+ br_enc64le(G + 16, x2[2]);
766
+ br_enc64le(G + 24, x2[3]);
767
+ return 1;
768
+ }
769
+
770
+ static size_t
771
+ api_mulgen(unsigned char *R,
772
+ const unsigned char *x, size_t xlen, int curve)
773
+ {
774
+ const unsigned char *G;
775
+ size_t Glen;
776
+
777
+ G = api_generator(curve, &Glen);
778
+ memcpy(R, G, Glen);
779
+ api_mul(R, Glen, x, xlen, curve);
780
+ return Glen;
781
+ }
782
+
783
+ static uint32_t
784
+ api_muladd(unsigned char *A, const unsigned char *B, size_t len,
785
+ const unsigned char *x, size_t xlen,
786
+ const unsigned char *y, size_t ylen, int curve)
787
+ {
788
+ /*
789
+ * We don't implement this method, since it is used for ECDSA
790
+ * only, and there is no ECDSA over Curve25519 (which instead
791
+ * uses EdDSA).
792
+ */
793
+ (void)A;
794
+ (void)B;
795
+ (void)len;
796
+ (void)x;
797
+ (void)xlen;
798
+ (void)y;
799
+ (void)ylen;
800
+ (void)curve;
801
+ return 0;
802
+ }
803
+
804
+ /* see bearssl_ec.h */
805
+ const br_ec_impl br_ec_c25519_m64 = {
806
+ (uint32_t)0x20000000,
807
+ &api_generator,
808
+ &api_order,
809
+ &api_xoff,
810
+ &api_mul,
811
+ &api_mulgen,
812
+ &api_muladd
813
+ };
814
+
815
+ /* see bearssl_ec.h */
816
+ const br_ec_impl *
817
+ br_ec_c25519_m64_get(void)
818
+ {
819
+ return &br_ec_c25519_m64;
820
+ }
821
+
822
+ #else
823
+
824
+ /* see bearssl_ec.h */
825
+ const br_ec_impl *
826
+ br_ec_c25519_m64_get(void)
827
+ {
828
+ return 0;
829
+ }
830
+
831
+ #endif