warscript 0.0.1-dev.404878c

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 (493) hide show
  1. package/LICENSE +23 -0
  2. package/binaryreader.d.ts +20 -0
  3. package/binaryreader.lua +50 -0
  4. package/binarywriter.d.ts +21 -0
  5. package/binarywriter.lua +90 -0
  6. package/core/dummy.d.ts +22 -0
  7. package/core/dummy.lua +133 -0
  8. package/core/game.d.ts +3 -0
  9. package/core/game.lua +24 -0
  10. package/core/mapbounds.d.ts +8 -0
  11. package/core/mapbounds.lua +12 -0
  12. package/core/thread.d.ts +8 -0
  13. package/core/thread.lua +40 -0
  14. package/core/types/ability.d.ts +4 -0
  15. package/core/types/ability.lua +10 -0
  16. package/core/types/async.d.ts +3 -0
  17. package/core/types/async.lua +2 -0
  18. package/core/types/camera.d.ts +4 -0
  19. package/core/types/camera.lua +21 -0
  20. package/core/types/cameraField.d.ts +15 -0
  21. package/core/types/cameraField.lua +38 -0
  22. package/core/types/color.d.ts +16 -0
  23. package/core/types/color.lua +93 -0
  24. package/core/types/destructable.d.ts +31 -0
  25. package/core/types/destructable.lua +245 -0
  26. package/core/types/effect.d.ts +31 -0
  27. package/core/types/effect.lua +209 -0
  28. package/core/types/frame.d.ts +140 -0
  29. package/core/types/frame.lua +882 -0
  30. package/core/types/game.d.ts +8 -0
  31. package/core/types/game.lua +41 -0
  32. package/core/types/group.d.ts +22 -0
  33. package/core/types/group.lua +94 -0
  34. package/core/types/handle.d.ts +48 -0
  35. package/core/types/handle.lua +169 -0
  36. package/core/types/image.d.ts +30 -0
  37. package/core/types/image.lua +121 -0
  38. package/core/types/internal/ability+cooldownRemaining.d.ts +1 -0
  39. package/core/types/internal/ability+cooldownRemaining.lua +0 -0
  40. package/core/types/item.d.ts +4 -0
  41. package/core/types/item.lua +6 -0
  42. package/core/types/missile.d.ts +22 -0
  43. package/core/types/missile.lua +233 -0
  44. package/core/types/order.d.ts +25 -0
  45. package/core/types/order.lua +55 -0
  46. package/core/types/player.d.ts +71 -0
  47. package/core/types/player.lua +448 -0
  48. package/core/types/playerCamera.d.ts +32 -0
  49. package/core/types/playerCamera.lua +162 -0
  50. package/core/types/playerColor.d.ts +40 -0
  51. package/core/types/playerColor.lua +203 -0
  52. package/core/types/rect.d.ts +39 -0
  53. package/core/types/rect.lua +152 -0
  54. package/core/types/region.d.ts +14 -0
  55. package/core/types/region.lua +84 -0
  56. package/core/types/sound.d.ts +69 -0
  57. package/core/types/sound.lua +247 -0
  58. package/core/types/tileCell.d.ts +13 -0
  59. package/core/types/tileCell.lua +100 -0
  60. package/core/types/timer.d.ts +36 -0
  61. package/core/types/timer.lua +230 -0
  62. package/core/types/timerDialog.d.ts +19 -0
  63. package/core/types/timerDialog.lua +95 -0
  64. package/core/types/unit.d.ts +10 -0
  65. package/core/types/unit.lua +24 -0
  66. package/core/types/widget.d.ts +5 -0
  67. package/core/types/widget.lua +2 -0
  68. package/core/util.d.ts +62 -0
  69. package/core/util.lua +464 -0
  70. package/core/vecmath/common.d.ts +18 -0
  71. package/core/vecmath/common.lua +29 -0
  72. package/core/vecmath/mat2.d.ts +73 -0
  73. package/core/vecmath/mat2.lua +261 -0
  74. package/core/vecmath/vec3.d.ts +47 -0
  75. package/core/vecmath/vec3.lua +350 -0
  76. package/core/vecmath/vec4.d.ts +54 -0
  77. package/core/vecmath/vec4.lua +400 -0
  78. package/crypto/ecc.d.ts +6 -0
  79. package/crypto/ecc.lua +1734 -0
  80. package/decl/index.d.ts +116 -0
  81. package/decl/native.d.ts +8198 -0
  82. package/destroyable.d.ts +20 -0
  83. package/destroyable.lua +31 -0
  84. package/engine/ability.d.ts +5 -0
  85. package/engine/ability.lua +7 -0
  86. package/engine/behavior.d.ts +28 -0
  87. package/engine/behavior.lua +181 -0
  88. package/engine/behaviour/ability/apply-buff.d.ts +38 -0
  89. package/engine/behaviour/ability/apply-buff.lua +126 -0
  90. package/engine/behaviour/ability/apply-unit-behavior.d.ts +12 -0
  91. package/engine/behaviour/ability/apply-unit-behavior.lua +36 -0
  92. package/engine/behaviour/ability/damage.d.ts +25 -0
  93. package/engine/behaviour/ability/damage.lua +75 -0
  94. package/engine/behaviour/ability/heal.d.ts +16 -0
  95. package/engine/behaviour/ability/heal.lua +35 -0
  96. package/engine/behaviour/ability/instant-impact.d.ts +6 -0
  97. package/engine/behaviour/ability/instant-impact.lua +25 -0
  98. package/engine/behaviour/ability.d.ts +40 -0
  99. package/engine/behaviour/ability.lua +175 -0
  100. package/engine/behaviour/unit/stun-immunity.d.ts +19 -0
  101. package/engine/behaviour/unit/stun-immunity.lua +67 -0
  102. package/engine/behaviour/unit.d.ts +20 -0
  103. package/engine/behaviour/unit.lua +75 -0
  104. package/engine/buff.d.ts +221 -0
  105. package/engine/buff.lua +1093 -0
  106. package/engine/constants.d.ts +7 -0
  107. package/engine/constants.lua +8 -0
  108. package/engine/index.d.ts +5 -0
  109. package/engine/index.lua +2 -0
  110. package/engine/internal/ability.d.ts +91 -0
  111. package/engine/internal/ability.lua +531 -0
  112. package/engine/internal/item/ability.d.ts +2 -0
  113. package/engine/internal/item/ability.lua +48 -0
  114. package/engine/internal/item+owner.d.ts +7 -0
  115. package/engine/internal/item+owner.lua +22 -0
  116. package/engine/internal/item.d.ts +86 -0
  117. package/engine/internal/item.lua +580 -0
  118. package/engine/internal/mechanics/ability-duration.d.ts +4 -0
  119. package/engine/internal/mechanics/ability-duration.lua +11 -0
  120. package/engine/internal/mechanics/area-damage.d.ts +2 -0
  121. package/engine/internal/mechanics/area-damage.lua +33 -0
  122. package/engine/internal/misc/dummy-units.d.ts +2 -0
  123. package/engine/internal/misc/dummy-units.lua +14 -0
  124. package/engine/internal/object-data/armor-increase.d.ts +2 -0
  125. package/engine/internal/object-data/armor-increase.lua +19 -0
  126. package/engine/internal/object-data/attribute-bonus.d.ts +2 -0
  127. package/engine/internal/object-data/attribute-bonus.lua +15 -0
  128. package/engine/internal/object-data/auto-attack-damage-increase.d.ts +2 -0
  129. package/engine/internal/object-data/auto-attack-damage-increase.lua +17 -0
  130. package/engine/internal/object-data/auto-attack-speed-increase.d.ts +2 -0
  131. package/engine/internal/object-data/auto-attack-speed-increase.lua +14 -0
  132. package/engine/internal/object-data/dummy-inventory.d.ts +2 -0
  133. package/engine/internal/object-data/dummy-inventory.lua +16 -0
  134. package/engine/internal/object-data/dummy-item.d.ts +2 -0
  135. package/engine/internal/object-data/dummy-item.lua +10 -0
  136. package/engine/internal/object-data/ghost-visible.d.ts +2 -0
  137. package/engine/internal/object-data/ghost-visible.lua +15 -0
  138. package/engine/internal/object-data/invulnerable.d.ts +2 -0
  139. package/engine/internal/object-data/invulnerable.lua +11 -0
  140. package/engine/internal/object-data/movement-speed-increase-factor.d.ts +2 -0
  141. package/engine/internal/object-data/movement-speed-increase-factor.lua +18 -0
  142. package/engine/internal/unit/ability.d.ts +143 -0
  143. package/engine/internal/unit/ability.lua +369 -0
  144. package/engine/internal/unit/allowed-targets.d.ts +17 -0
  145. package/engine/internal/unit/allowed-targets.lua +19 -0
  146. package/engine/internal/unit/band-aids/ancestral-spirit-cannibalize.d.ts +2 -0
  147. package/engine/internal/unit/band-aids/ancestral-spirit-cannibalize.lua +21 -0
  148. package/engine/internal/unit/bonus.d.ts +41 -0
  149. package/engine/internal/unit/bonus.lua +188 -0
  150. package/engine/internal/unit/buff.d.ts +38 -0
  151. package/engine/internal/unit/buff.lua +81 -0
  152. package/engine/internal/unit/expiration-timer.d.ts +22 -0
  153. package/engine/internal/unit/expiration-timer.lua +44 -0
  154. package/engine/internal/unit/ghost-counter.d.ts +12 -0
  155. package/engine/internal/unit/ghost-counter.lua +26 -0
  156. package/engine/internal/unit/invulnerability-counter.d.ts +12 -0
  157. package/engine/internal/unit/invulnerability-counter.lua +26 -0
  158. package/engine/internal/unit/missile.d.ts +42 -0
  159. package/engine/internal/unit/missile.lua +70 -0
  160. package/engine/internal/unit/movement-speed.d.ts +18 -0
  161. package/engine/internal/unit/movement-speed.lua +42 -0
  162. package/engine/internal/unit/summon-event.d.ts +12 -0
  163. package/engine/internal/unit/summon-event.lua +50 -0
  164. package/engine/internal/unit+ability.d.ts +9 -0
  165. package/engine/internal/unit+ability.lua +59 -0
  166. package/engine/internal/unit+bonus.d.ts +8 -0
  167. package/engine/internal/unit+bonus.lua +40 -0
  168. package/engine/internal/unit+damage.d.ts +69 -0
  169. package/engine/internal/unit+damage.lua +123 -0
  170. package/engine/internal/unit+rally.d.ts +10 -0
  171. package/engine/internal/unit+rally.lua +159 -0
  172. package/engine/internal/unit+spellSteal.d.ts +7 -0
  173. package/engine/internal/unit+spellSteal.lua +64 -0
  174. package/engine/internal/unit+transport.d.ts +12 -0
  175. package/engine/internal/unit+transport.lua +102 -0
  176. package/engine/internal/unit-missile-data.d.ts +2 -0
  177. package/engine/internal/unit-missile-data.lua +107 -0
  178. package/engine/internal/unit-missile-launch.d.ts +7 -0
  179. package/engine/internal/unit-missile-launch.lua +32 -0
  180. package/engine/internal/unit.d.ts +312 -0
  181. package/engine/internal/unit.lua +2430 -0
  182. package/engine/internal/utility.d.ts +2 -0
  183. package/engine/internal/utility.lua +13 -0
  184. package/engine/lightning.d.ts +51 -0
  185. package/engine/lightning.lua +303 -0
  186. package/engine/local-client.d.ts +16 -0
  187. package/engine/local-client.lua +86 -0
  188. package/engine/object-data/auxiliary/animation-name.d.ts +14 -0
  189. package/engine/object-data/auxiliary/animation-name.lua +2 -0
  190. package/engine/object-data/auxiliary/animation-qualifier.d.ts +53 -0
  191. package/engine/object-data/auxiliary/animation-qualifier.lua +2 -0
  192. package/engine/object-data/auxiliary/armor-sound-type.d.ts +8 -0
  193. package/engine/object-data/auxiliary/armor-sound-type.lua +2 -0
  194. package/engine/object-data/auxiliary/attachment-preset.d.ts +15 -0
  195. package/engine/object-data/auxiliary/attachment-preset.lua +29 -0
  196. package/engine/object-data/auxiliary/attack-type.d.ts +11 -0
  197. package/engine/object-data/auxiliary/attack-type.lua +2 -0
  198. package/engine/object-data/auxiliary/buff-polarity.d.ts +6 -0
  199. package/engine/object-data/auxiliary/buff-polarity.lua +2 -0
  200. package/engine/object-data/auxiliary/buff-resistance-type.d.ts +6 -0
  201. package/engine/object-data/auxiliary/buff-resistance-type.lua +2 -0
  202. package/engine/object-data/auxiliary/combat-classification.d.ts +46 -0
  203. package/engine/object-data/auxiliary/combat-classification.lua +206 -0
  204. package/engine/object-data/auxiliary/detection-type.d.ts +7 -0
  205. package/engine/object-data/auxiliary/detection-type.lua +2 -0
  206. package/engine/object-data/auxiliary/model-node-name.d.ts +12 -0
  207. package/engine/object-data/auxiliary/model-node-name.lua +2 -0
  208. package/engine/object-data/auxiliary/model-node-qualifier.d.ts +21 -0
  209. package/engine/object-data/auxiliary/model-node-qualifier.lua +2 -0
  210. package/engine/object-data/auxiliary/movement-type.d.ts +10 -0
  211. package/engine/object-data/auxiliary/movement-type.lua +2 -0
  212. package/engine/object-data/auxiliary/race.d.ts +14 -0
  213. package/engine/object-data/auxiliary/race.lua +2 -0
  214. package/engine/object-data/auxiliary/sound-preset-name.d.ts +8 -0
  215. package/engine/object-data/auxiliary/sound-preset-name.lua +2 -0
  216. package/engine/object-data/auxiliary/sound-set-name.d.ts +35 -0
  217. package/engine/object-data/auxiliary/sound-set-name.lua +2 -0
  218. package/engine/object-data/auxiliary/targeting-type.d.ts +8 -0
  219. package/engine/object-data/auxiliary/targeting-type.lua +2 -0
  220. package/engine/object-data/auxiliary/tech-tree-dependency.d.ts +16 -0
  221. package/engine/object-data/auxiliary/tech-tree-dependency.lua +8 -0
  222. package/engine/object-data/auxiliary/unit-classification.d.ts +19 -0
  223. package/engine/object-data/auxiliary/unit-classification.lua +50 -0
  224. package/engine/object-data/auxiliary/weapon-sound-type.d.ts +17 -0
  225. package/engine/object-data/auxiliary/weapon-sound-type.lua +2 -0
  226. package/engine/object-data/entry/ability-type/armor-increase.d.ts +8 -0
  227. package/engine/object-data/entry/ability-type/armor-increase.lua +26 -0
  228. package/engine/object-data/entry/ability-type/attribute-increase.d.ts +12 -0
  229. package/engine/object-data/entry/ability-type/attribute-increase.lua +52 -0
  230. package/engine/object-data/entry/ability-type/auto-attack-damage-increase.d.ts +8 -0
  231. package/engine/object-data/entry/ability-type/auto-attack-damage-increase.lua +26 -0
  232. package/engine/object-data/entry/ability-type/auto-attack-heal-reduction-effect.d.ts +12 -0
  233. package/engine/object-data/entry/ability-type/auto-attack-heal-reduction-effect.lua +52 -0
  234. package/engine/object-data/entry/ability-type/auto-attack-speed-increase.d.ts +8 -0
  235. package/engine/object-data/entry/ability-type/auto-attack-speed-increase.lua +26 -0
  236. package/engine/object-data/entry/ability-type/bash.d.ts +16 -0
  237. package/engine/object-data/entry/ability-type/bash.lua +78 -0
  238. package/engine/object-data/entry/ability-type/berserk.d.ts +10 -0
  239. package/engine/object-data/entry/ability-type/berserk.lua +39 -0
  240. package/engine/object-data/entry/ability-type/blank-configurable.d.ts +21 -0
  241. package/engine/object-data/entry/ability-type/blank-configurable.lua +249 -0
  242. package/engine/object-data/entry/ability-type/blank-passive.d.ts +7 -0
  243. package/engine/object-data/entry/ability-type/blank-passive.lua +28 -0
  244. package/engine/object-data/entry/ability-type/blizzard.d.ts +16 -0
  245. package/engine/object-data/entry/ability-type/blizzard.lua +78 -0
  246. package/engine/object-data/entry/ability-type/blood-lust.d.ts +12 -0
  247. package/engine/object-data/entry/ability-type/blood-lust.lua +52 -0
  248. package/engine/object-data/entry/ability-type/chain-lightning.d.ts +12 -0
  249. package/engine/object-data/entry/ability-type/chain-lightning.lua +52 -0
  250. package/engine/object-data/entry/ability-type/channel.d.ts +48 -0
  251. package/engine/object-data/entry/ability-type/channel.lua +140 -0
  252. package/engine/object-data/entry/ability-type/charge-gold-and-lumber.d.ts +14 -0
  253. package/engine/object-data/entry/ability-type/charge-gold-and-lumber.lua +65 -0
  254. package/engine/object-data/entry/ability-type/cleaving-attack.d.ts +8 -0
  255. package/engine/object-data/entry/ability-type/cleaving-attack.lua +26 -0
  256. package/engine/object-data/entry/ability-type/cluster-rockets.d.ts +18 -0
  257. package/engine/object-data/entry/ability-type/cluster-rockets.lua +91 -0
  258. package/engine/object-data/entry/ability-type/cripple.d.ts +12 -0
  259. package/engine/object-data/entry/ability-type/cripple.lua +52 -0
  260. package/engine/object-data/entry/ability-type/critical-strike.d.ts +18 -0
  261. package/engine/object-data/entry/ability-type/critical-strike.lua +91 -0
  262. package/engine/object-data/entry/ability-type/curse.d.ts +8 -0
  263. package/engine/object-data/entry/ability-type/curse.lua +26 -0
  264. package/engine/object-data/entry/ability-type/dark-summoning.d.ts +10 -0
  265. package/engine/object-data/entry/ability-type/dark-summoning.lua +39 -0
  266. package/engine/object-data/entry/ability-type/death-coil.d.ts +8 -0
  267. package/engine/object-data/entry/ability-type/death-coil.lua +26 -0
  268. package/engine/object-data/entry/ability-type/disease-cloud.d.ts +15 -0
  269. package/engine/object-data/entry/ability-type/disease-cloud.lua +65 -0
  270. package/engine/object-data/entry/ability-type/divine-shield.d.ts +5 -0
  271. package/engine/object-data/entry/ability-type/divine-shield.lua +12 -0
  272. package/engine/object-data/entry/ability-type/endurance-aura.d.ts +10 -0
  273. package/engine/object-data/entry/ability-type/endurance-aura.lua +39 -0
  274. package/engine/object-data/entry/ability-type/engineering-upgrade.d.ts +13 -0
  275. package/engine/object-data/entry/ability-type/engineering-upgrade.lua +88 -0
  276. package/engine/object-data/entry/ability-type/evasion.d.ts +8 -0
  277. package/engine/object-data/entry/ability-type/evasion.lua +26 -0
  278. package/engine/object-data/entry/ability-type/faerie-fire.d.ts +10 -0
  279. package/engine/object-data/entry/ability-type/faerie-fire.lua +39 -0
  280. package/engine/object-data/entry/ability-type/far-sight.d.ts +9 -0
  281. package/engine/object-data/entry/ability-type/far-sight.lua +26 -0
  282. package/engine/object-data/entry/ability-type/feral-spirit.d.ts +11 -0
  283. package/engine/object-data/entry/ability-type/feral-spirit.lua +39 -0
  284. package/engine/object-data/entry/ability-type/frenzy.d.ts +12 -0
  285. package/engine/object-data/entry/ability-type/frenzy.lua +52 -0
  286. package/engine/object-data/entry/ability-type/frost-nova.d.ts +12 -0
  287. package/engine/object-data/entry/ability-type/frost-nova.lua +52 -0
  288. package/engine/object-data/entry/ability-type/ghost-visible.d.ts +10 -0
  289. package/engine/object-data/entry/ability-type/ghost-visible.lua +39 -0
  290. package/engine/object-data/entry/ability-type/gold-mine.d.ts +12 -0
  291. package/engine/object-data/entry/ability-type/gold-mine.lua +52 -0
  292. package/engine/object-data/entry/ability-type/heal.d.ts +8 -0
  293. package/engine/object-data/entry/ability-type/heal.lua +26 -0
  294. package/engine/object-data/entry/ability-type/healing-salve.d.ts +22 -0
  295. package/engine/object-data/entry/ability-type/healing-salve.lua +78 -0
  296. package/engine/object-data/entry/ability-type/healing-spray.d.ts +18 -0
  297. package/engine/object-data/entry/ability-type/healing-spray.lua +91 -0
  298. package/engine/object-data/entry/ability-type/healing-wave.d.ts +12 -0
  299. package/engine/object-data/entry/ability-type/healing-wave.lua +52 -0
  300. package/engine/object-data/entry/ability-type/holy-light.d.ts +8 -0
  301. package/engine/object-data/entry/ability-type/holy-light.lua +26 -0
  302. package/engine/object-data/entry/ability-type/incinerate.d.ts +18 -0
  303. package/engine/object-data/entry/ability-type/incinerate.lua +91 -0
  304. package/engine/object-data/entry/ability-type/inner-fire.d.ts +14 -0
  305. package/engine/object-data/entry/ability-type/inner-fire.lua +65 -0
  306. package/engine/object-data/entry/ability-type/inventory.d.ts +16 -0
  307. package/engine/object-data/entry/ability-type/inventory.lua +78 -0
  308. package/engine/object-data/entry/ability-type/invulnerable.d.ts +5 -0
  309. package/engine/object-data/entry/ability-type/invulnerable.lua +12 -0
  310. package/engine/object-data/entry/ability-type/lightning-shield.d.ts +8 -0
  311. package/engine/object-data/entry/ability-type/lightning-shield.lua +26 -0
  312. package/engine/object-data/entry/ability-type/movement-speed-increase.d.ts +8 -0
  313. package/engine/object-data/entry/ability-type/movement-speed-increase.lua +26 -0
  314. package/engine/object-data/entry/ability-type/permanent-immolation.d.ts +8 -0
  315. package/engine/object-data/entry/ability-type/permanent-immolation.lua +26 -0
  316. package/engine/object-data/entry/ability-type/phoenix-morph.d.ts +25 -0
  317. package/engine/object-data/entry/ability-type/phoenix-morph.lua +130 -0
  318. package/engine/object-data/entry/ability-type/recall.d.ts +10 -0
  319. package/engine/object-data/entry/ability-type/recall.lua +39 -0
  320. package/engine/object-data/entry/ability-type/rejuvenation.d.ts +20 -0
  321. package/engine/object-data/entry/ability-type/rejuvenation.lua +65 -0
  322. package/engine/object-data/entry/ability-type/searing-arrows.d.ts +8 -0
  323. package/engine/object-data/entry/ability-type/searing-arrows.lua +26 -0
  324. package/engine/object-data/entry/ability-type/shock-wave.d.ts +10 -0
  325. package/engine/object-data/entry/ability-type/shock-wave.lua +39 -0
  326. package/engine/object-data/entry/ability-type/slow-poison.d.ts +12 -0
  327. package/engine/object-data/entry/ability-type/slow-poison.lua +52 -0
  328. package/engine/object-data/entry/ability-type/slow.d.ts +12 -0
  329. package/engine/object-data/entry/ability-type/slow.lua +52 -0
  330. package/engine/object-data/entry/ability-type/spell-damage-reduction.d.ts +10 -0
  331. package/engine/object-data/entry/ability-type/spell-damage-reduction.lua +39 -0
  332. package/engine/object-data/entry/ability-type/spiked-carapace.d.ts +12 -0
  333. package/engine/object-data/entry/ability-type/spiked-carapace.lua +52 -0
  334. package/engine/object-data/entry/ability-type/spirit-touch.d.ts +14 -0
  335. package/engine/object-data/entry/ability-type/spirit-touch.lua +65 -0
  336. package/engine/object-data/entry/ability-type/storm-bolt.d.ts +8 -0
  337. package/engine/object-data/entry/ability-type/storm-bolt.lua +26 -0
  338. package/engine/object-data/entry/ability-type/summon-quilbeast.d.ts +11 -0
  339. package/engine/object-data/entry/ability-type/summon-quilbeast.lua +39 -0
  340. package/engine/object-data/entry/ability-type/summon-water-elemental.d.ts +11 -0
  341. package/engine/object-data/entry/ability-type/summon-water-elemental.lua +39 -0
  342. package/engine/object-data/entry/ability-type.d.ts +148 -0
  343. package/engine/object-data/entry/ability-type.lua +1058 -0
  344. package/engine/object-data/entry/buff-type/applicable.d.ts +16 -0
  345. package/engine/object-data/entry/buff-type/applicable.lua +377 -0
  346. package/engine/object-data/entry/buff-type/blank.d.ts +8 -0
  347. package/engine/object-data/entry/buff-type/blank.lua +17 -0
  348. package/engine/object-data/entry/buff-type.d.ts +64 -0
  349. package/engine/object-data/entry/buff-type.lua +316 -0
  350. package/engine/object-data/entry/destructible-type.d.ts +21 -0
  351. package/engine/object-data/entry/destructible-type.lua +83 -0
  352. package/engine/object-data/entry/item-type/blank.d.ts +8 -0
  353. package/engine/object-data/entry/item-type/blank.lua +21 -0
  354. package/engine/object-data/entry/item-type.d.ts +49 -0
  355. package/engine/object-data/entry/item-type.lua +259 -0
  356. package/engine/object-data/entry/lightning-type.d.ts +37 -0
  357. package/engine/object-data/entry/lightning-type.lua +191 -0
  358. package/engine/object-data/entry/unit-type/blank.d.ts +6 -0
  359. package/engine/object-data/entry/unit-type/blank.lua +13 -0
  360. package/engine/object-data/entry/unit-type.d.ts +218 -0
  361. package/engine/object-data/entry/unit-type.lua +1306 -0
  362. package/engine/object-data/entry/upgrade/blank.d.ts +8 -0
  363. package/engine/object-data/entry/upgrade/blank.lua +22 -0
  364. package/engine/object-data/entry/upgrade.d.ts +59 -0
  365. package/engine/object-data/entry/upgrade.lua +238 -0
  366. package/engine/object-data/entry.d.ts +80 -0
  367. package/engine/object-data/entry.lua +634 -0
  368. package/engine/object-data/utility/object-data-entry-id-generator.d.ts +6 -0
  369. package/engine/object-data/utility/object-data-entry-id-generator.lua +63 -0
  370. package/engine/object-data/utility/order-type-string-id-factory.d.ts +9 -0
  371. package/engine/object-data/utility/order-type-string-id-factory.lua +368 -0
  372. package/engine/object-field/ability.d.ts +115 -0
  373. package/engine/object-field/ability.lua +471 -0
  374. package/engine/object-field/unit.d.ts +15 -0
  375. package/engine/object-field/unit.lua +49 -0
  376. package/engine/object-field.d.ts +84 -0
  377. package/engine/object-field.lua +444 -0
  378. package/engine/random.d.ts +10 -0
  379. package/engine/random.lua +9 -0
  380. package/engine/standard/entries/ability-type.d.ts +8 -0
  381. package/engine/standard/entries/ability-type.lua +8 -0
  382. package/engine/standard/entries/lightning-type.d.ts +16 -0
  383. package/engine/standard/entries/lightning-type.lua +16 -0
  384. package/engine/standard/entries/unit-type.d.ts +63 -0
  385. package/engine/standard/entries/unit-type.lua +63 -0
  386. package/engine/standard/entries/upgrade.d.ts +92 -0
  387. package/engine/standard/entries/upgrade.lua +92 -0
  388. package/engine/standard/fields/ability.d.ts +737 -0
  389. package/engine/standard/fields/ability.lua +749 -0
  390. package/engine/standard/pathing-textures.d.ts +6 -0
  391. package/engine/standard/pathing-textures.lua +7 -0
  392. package/engine/text-tag.d.ts +25 -0
  393. package/engine/text-tag.lua +109 -0
  394. package/engine/unit.d.ts +20 -0
  395. package/engine/unit.lua +31 -0
  396. package/event.d.ts +53 -0
  397. package/event.lua +269 -0
  398. package/exception.d.ts +17 -0
  399. package/exception.lua +53 -0
  400. package/file.d.ts +4 -0
  401. package/file.lua +42 -0
  402. package/global/math.d.ts +21 -0
  403. package/global/math.lua +72 -0
  404. package/global/vec2.d.ts +66 -0
  405. package/global/vec2.lua +159 -0
  406. package/index.d.ts +8 -0
  407. package/index.lua +9 -0
  408. package/lualib_bundle.lua +2629 -0
  409. package/math/vec2.d.ts +18 -0
  410. package/math/vec2.lua +42 -0
  411. package/math.d.ts +39 -0
  412. package/math.lua +91 -0
  413. package/net/signal.d.ts +11 -0
  414. package/net/signal.lua +56 -0
  415. package/net/socket.d.ts +9 -0
  416. package/net/socket.lua +26 -0
  417. package/network.d.ts +31 -0
  418. package/network.lua +194 -0
  419. package/objutil/ability.d.ts +710 -0
  420. package/objutil/ability.lua +3781 -0
  421. package/objutil/buff.d.ts +110 -0
  422. package/objutil/buff.lua +769 -0
  423. package/objutil/dummy.d.ts +2 -0
  424. package/objutil/dummy.lua +49 -0
  425. package/objutil/idgen.d.ts +9 -0
  426. package/objutil/idgen.lua +92 -0
  427. package/objutil/item.d.ts +38 -0
  428. package/objutil/item.lua +254 -0
  429. package/objutil/object.d.ts +43 -0
  430. package/objutil/object.lua +213 -0
  431. package/objutil/unit.d.ts +134 -0
  432. package/objutil/unit.lua +913 -0
  433. package/objutil/upgrade.d.ts +22 -0
  434. package/objutil/upgrade.lua +127 -0
  435. package/operation.d.ts +35 -0
  436. package/operation.lua +179 -0
  437. package/package.json +47 -0
  438. package/patch-lualib.d.ts +2 -0
  439. package/patch-lualib.lua +67 -0
  440. package/patch-natives.d.ts +11 -0
  441. package/patch-natives.lua +14 -0
  442. package/string.d.ts +106 -0
  443. package/string.lua +34 -0
  444. package/time/dayOfWeek.d.ts +97 -0
  445. package/time/dayOfWeek.lua +53 -0
  446. package/time/localDate.d.ts +145 -0
  447. package/time/localDate.lua +236 -0
  448. package/time/localDateTime.d.ts +133 -0
  449. package/time/localDateTime.lua +154 -0
  450. package/time/localTime.d.ts +87 -0
  451. package/time/localTime.lua +91 -0
  452. package/time/month.d.ts +146 -0
  453. package/time/month.lua +129 -0
  454. package/time/year.d.ts +117 -0
  455. package/time/year.lua +81 -0
  456. package/types.d.ts +13 -0
  457. package/types.lua +0 -0
  458. package/util/objectPool.d.ts +6 -0
  459. package/util/objectPool.lua +28 -0
  460. package/util/stream.d.ts +29 -0
  461. package/util/stream.lua +453 -0
  462. package/util/stringBuilder.d.ts +8 -0
  463. package/util/stringBuilder.lua +25 -0
  464. package/utility/arrays.d.ts +57 -0
  465. package/utility/arrays.lua +369 -0
  466. package/utility/base64.d.ts +23 -0
  467. package/utility/base64.lua +177 -0
  468. package/utility/bit-set.d.ts +18 -0
  469. package/utility/bit-set.lua +12 -0
  470. package/utility/functions.d.ts +5 -0
  471. package/utility/functions.lua +90 -0
  472. package/utility/json.d.ts +2 -0
  473. package/utility/json.lua +400 -0
  474. package/utility/linked-set.d.ts +49 -0
  475. package/utility/linked-set.lua +181 -0
  476. package/utility/lua-maps.d.ts +6 -0
  477. package/utility/lua-maps.lua +25 -0
  478. package/utility/lua-sets.d.ts +6 -0
  479. package/utility/lua-sets.lua +37 -0
  480. package/utility/lzw.d.ts +2 -0
  481. package/utility/lzw.lua +161 -0
  482. package/utility/observable-array.d.ts +17 -0
  483. package/utility/observable-array.lua +30 -0
  484. package/utility/preconditions.d.ts +23 -0
  485. package/utility/preconditions.lua +58 -0
  486. package/utility/records.d.ts +3 -0
  487. package/utility/records.lua +9 -0
  488. package/utility/reflection.d.ts +4 -0
  489. package/utility/reflection.lua +31 -0
  490. package/utility/strings.d.ts +2 -0
  491. package/utility/strings.lua +44 -0
  492. package/utility/types.d.ts +39 -0
  493. package/utility/types.lua +2 -0
package/crypto/ecc.lua ADDED
@@ -0,0 +1,1734 @@
1
+ -- Elliptic Curve Cryptography in Computercraft
2
+
3
+ ---- Update (Jul 30 2020)
4
+ -- Make randomModQ and use it instead of hashing from random.random()
5
+ ---- Update (Feb 10 2020)
6
+ -- Make a more robust encoding/decoding implementation
7
+ ---- Update (Dec 30 2019)
8
+ -- Fix rng not accumulating entropy from loop
9
+ -- (older versions should be fine from other sources + stored in disk)
10
+ ---- Update (Dec 28 2019)
11
+ -- Slightly better integer multiplication and squaring
12
+ -- Fix global variable declarations in modQ division and verify() (no security concerns)
13
+ -- Small tweaks from SquidDev's illuaminate (https://github.com/SquidDev/illuaminate/)
14
+
15
+ local unpack = table.unpack
16
+
17
+ local time = os.time()
18
+ local clock = os.clock()
19
+
20
+ local byteTableMT = {
21
+ __tostring = function(a) return string.char(unpack(a)) end,
22
+ __index = {
23
+ toHex = function(self) return ("%02x"):rep(#self):format(unpack(self)) end,
24
+ isEqual = function(self, t)
25
+ if type(t) ~= "table" then return false end
26
+ if #self ~= #t then return false end
27
+ local ret = 0
28
+ for i = 1, #self do
29
+ ret = ret | (self[i] ~ t[i])
30
+ end
31
+ return ret == 0
32
+ end
33
+ }
34
+ }
35
+
36
+ -- SHA-256, HMAC and PBKDF2 functions in ComputerCraft
37
+ -- By Anavrins
38
+ -- For help and details, you can PM me on the CC forums
39
+ -- You may use this code in your projects without asking me, as long as credit is given and this header is kept intact
40
+ -- http://www.computercraft.info/forums2/index.php?/user/12870-anavrins
41
+ -- http://pastebin.com/6UV4qfNF
42
+ -- Last update: October 10, 2017
43
+ local sha256 = (function()
44
+ local upack = unpack
45
+
46
+ local function rrotate(n, b)
47
+ local s = n // (1 << b)
48
+ local f = s%1
49
+ return (s-f)-- + f*mod32
50
+ end
51
+ local function brshift(int, by) -- Thanks bit32 for bad rshift
52
+ local s = int // (1 << by)
53
+ return s - s%1
54
+ end
55
+
56
+ local H = {
57
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
58
+ 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
59
+ }
60
+
61
+ local K = {
62
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
63
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
64
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
65
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
66
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
67
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
68
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
69
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
70
+ }
71
+
72
+ local function counter(incr)
73
+ local t1, t2 = 0, 0
74
+ if 0xFFFFFFFF - t1 < incr then
75
+ t2 = t2 + 1
76
+ t1 = incr - (0xFFFFFFFF - t1) - 1
77
+ else t1 = t1 + incr
78
+ end
79
+ return t2, t1
80
+ end
81
+
82
+ local function BE_toInt(bs, i)
83
+ return (((bs[i] or 0) << 24) & 0xFFFFFFFF) + (((bs[i+1] or 0) << 16) & 0xFFFFFFFF) + (((bs[i+2] or 0) << 8) & 0xFFFFFFFF) + (bs[i+3] or 0)
84
+ end
85
+
86
+ local function preprocess(data)
87
+ local len = #data
88
+ local proc = {}
89
+ data[#data+1] = 0x80
90
+ while #data%64~=56 do data[#data+1] = 0 end
91
+ local blocks = math.ceil(#data/64)
92
+ for i = 1, blocks do
93
+ proc[i] = {}
94
+ for j = 1, 16 do
95
+ proc[i][j] = BE_toInt(data, 1+((i-1)*64)+((j-1)*4))
96
+ end
97
+ end
98
+ proc[blocks][15], proc[blocks][16] = counter(len*8)
99
+ return proc
100
+ end
101
+
102
+ local function digestblock(w, C)
103
+ for j = 17, 64 do
104
+ local s0 = (rrotate(w[j-15], 7) ~ rrotate(w[j-15], 18)) ~ brshift(w[j-15], 3)
105
+ local s1 = (rrotate(w[j-2], 17) ~ rrotate(w[j-2], 19)) ~ brshift(w[j-2], 10)
106
+ w[j] = (w[j-16] + s0 + w[j-7] + s1) & 0xFFFFFFFF
107
+ end
108
+ local a, b, c, d, e, f, g, h = upack(C)
109
+ for j = 1, 64 do
110
+ local S1 = (rrotate(e, 6) ~ rrotate(e, 11)) ~ rrotate(e, 25)
111
+ local ch = (e & f) ~ ((~e & 0xFFFFFFFF) & g)
112
+ local temp1 = (h + S1 + ch + K[j] + w[j]) & 0xFFFFFFFF
113
+ local S0 = (rrotate(a, 2) ~ rrotate(a, 13)) ~ rrotate(a, 22)
114
+ local maj = ((a & b) ~ (a & c)) ~ (b & c)
115
+ local temp2 = (S0 + maj) & 0xFFFFFFFF
116
+ h, g, f, e, d, c, b, a = g, f, e, (d+temp1) & 0xFFFFFFFF, c, b, a, (temp1+temp2) & 0xFFFFFFFF
117
+ end
118
+ C[1] = (C[1] + a) & 0xFFFFFFFF
119
+ C[2] = (C[2] + b) & 0xFFFFFFFF
120
+ C[3] = (C[3] + c) & 0xFFFFFFFF
121
+ C[4] = (C[4] + d) & 0xFFFFFFFF
122
+ C[5] = (C[5] + e) & 0xFFFFFFFF
123
+ C[6] = (C[6] + f) & 0xFFFFFFFF
124
+ C[7] = (C[7] + g) & 0xFFFFFFFF
125
+ C[8] = (C[8] + h) & 0xFFFFFFFF
126
+ return C
127
+ end
128
+
129
+ local function toBytes(t, n)
130
+ local b = {}
131
+ for i = 1, n do
132
+ b[(i-1)*4+1] = brshift(t[i], 24) & 0xFF
133
+ b[(i-1)*4+2] = brshift(t[i], 16) & 0xFF
134
+ b[(i-1)*4+3] = brshift(t[i], 8) & 0xFF
135
+ b[(i-1)*4+4] = t[i] & 0xFF
136
+ end
137
+ return setmetatable(b, byteTableMT)
138
+ end
139
+
140
+ local function digest(data)
141
+ data = data or ""
142
+ data = type(data) == "table" and {upack(data)} or {tostring(data):byte(1,-1)}
143
+
144
+ data = preprocess(data)
145
+ local C = {upack(H)}
146
+ for i = 1, #data do C = digestblock(data[i], C) end
147
+ return toBytes(C, 8)
148
+ end
149
+
150
+ local function hmac(data, key)
151
+ local data = type(data) == "table" and {upack(data)} or {tostring(data):byte(1,-1)}
152
+ local key = type(key) == "table" and {upack(key)} or {tostring(key):byte(1,-1)}
153
+
154
+ local blocksize = 64
155
+
156
+ key = #key > blocksize and digest(key) or key
157
+
158
+ local ipad = {}
159
+ local opad = {}
160
+ local padded_key = {}
161
+
162
+ for i = 1, blocksize do
163
+ ipad[i] = 0x36 ~ (key[i] or 0)
164
+ opad[i] = 0x5C ~ (key[i] or 0)
165
+ end
166
+
167
+ for i = 1, #data do
168
+ ipad[blocksize+i] = data[i]
169
+ end
170
+
171
+ ipad = digest(ipad)
172
+
173
+ for i = 1, blocksize do
174
+ padded_key[i] = opad[i]
175
+ padded_key[blocksize+i] = ipad[i]
176
+ end
177
+
178
+ return digest(padded_key)
179
+ end
180
+
181
+ local function pbkdf2(pass, salt, iter, dklen)
182
+ local salt = type(salt) == "table" and salt or {tostring(salt):byte(1,-1)}
183
+ local hashlen = 32
184
+ local dklen = dklen or 32
185
+ local block = 1
186
+ local out = {}
187
+
188
+ while dklen > 0 do
189
+ local ikey = {}
190
+ local isalt = {upack(salt)}
191
+ local clen = dklen > hashlen and hashlen or dklen
192
+
193
+ isalt[#isalt+1] = brshift(block, 24) & 0xFF
194
+ isalt[#isalt+1] = brshift(block, 16) & 0xFF
195
+ isalt[#isalt+1] = brshift(block, 8) & 0xFF
196
+ isalt[#isalt+1] = block & 0xFF
197
+
198
+ for j = 1, iter do
199
+ isalt = hmac(isalt, pass)
200
+ for k = 1, clen do ikey[k] = isalt[k] ~ (ikey[k] or 0) end
201
+ if j % 200 == 0 then os.queueEvent("PBKDF2", j) coroutine.yield("PBKDF2") end
202
+ end
203
+ dklen = dklen - clen
204
+ block = block+1
205
+ for k = 1, clen do out[#out+1] = ikey[k] end
206
+ end
207
+
208
+ return setmetatable(out, byteTableMT)
209
+ end
210
+
211
+ return {
212
+ digest = digest,
213
+ hmac = hmac,
214
+ pbkdf2 = pbkdf2
215
+ }
216
+ end)()
217
+
218
+ -- Chacha20 cipher in ComputerCraft
219
+ -- By Anavrins
220
+ -- For help and details, you can PM me on the CC forums
221
+ -- You may use this code in your projects without asking me, as long as credit is given and this header is kept intact
222
+ -- http://www.computercraft.info/forums2/index.php?/user/12870-anavrins
223
+ -- http://pastebin.com/GPzf9JSa
224
+ -- Last update: April 17, 2017
225
+ local chacha20 = (function()
226
+ local mod = 2^32
227
+ local tau = {("expand 16-byte k"):byte(1,-1)}
228
+ local sigma = {("expand 32-byte k"):byte(1,-1)}
229
+
230
+ local function rotl(n, b)
231
+ local s = n/(2^(32-b))
232
+ local f = s%1
233
+ return (s-f) + f*mod
234
+ end
235
+
236
+ local function quarterRound(s, a, b, c, d)
237
+ s[a] = (s[a]+s[b])%mod; s[d] = rotl(s[d] ~ s[a], 16)
238
+ s[c] = (s[c]+s[d])%mod; s[b] = rotl(s[b] ~ s[c], 12)
239
+ s[a] = (s[a]+s[b])%mod; s[d] = rotl(s[d] ~ s[a], 8)
240
+ s[c] = (s[c]+s[d])%mod; s[b] = rotl(s[b] ~ s[c], 7)
241
+ return s
242
+ end
243
+
244
+ local function hashBlock(state, rnd)
245
+ local s = {unpack(state)}
246
+ for i = 1, rnd do
247
+ local r = i%2==1
248
+ s = r and quarterRound(s, 1, 5, 9, 13) or quarterRound(s, 1, 6, 11, 16)
249
+ s = r and quarterRound(s, 2, 6, 10, 14) or quarterRound(s, 2, 7, 12, 13)
250
+ s = r and quarterRound(s, 3, 7, 11, 15) or quarterRound(s, 3, 8, 9, 14)
251
+ s = r and quarterRound(s, 4, 8, 12, 16) or quarterRound(s, 4, 5, 10, 15)
252
+ end
253
+ for i = 1, 16 do s[i] = (s[i]+state[i])%mod end
254
+ return s
255
+ end
256
+
257
+ local function LE_toInt(bs, i)
258
+ return (bs[i+1] or 0)+
259
+ (((bs[i+2] or 0) << 8) & 0xFFFFFFFF) +
260
+ (((bs[i+3] or 0) << 16) & 0xFFFFFFFF) +
261
+ (((bs[i+4] or 0) << 24) & 0xFFFFFFFF)
262
+ end
263
+
264
+ local function initState(key, nonce, counter)
265
+ local isKey256 = #key == 32
266
+ local const = isKey256 and sigma or tau
267
+ local state = {}
268
+
269
+ state[ 1] = LE_toInt(const, 0)
270
+ state[ 2] = LE_toInt(const, 4)
271
+ state[ 3] = LE_toInt(const, 8)
272
+ state[ 4] = LE_toInt(const, 12)
273
+
274
+ state[ 5] = LE_toInt(key, 0)
275
+ state[ 6] = LE_toInt(key, 4)
276
+ state[ 7] = LE_toInt(key, 8)
277
+ state[ 8] = LE_toInt(key, 12)
278
+ state[ 9] = LE_toInt(key, isKey256 and 16 or 0)
279
+ state[10] = LE_toInt(key, isKey256 and 20 or 4)
280
+ state[11] = LE_toInt(key, isKey256 and 24 or 8)
281
+ state[12] = LE_toInt(key, isKey256 and 28 or 12)
282
+
283
+ state[13] = counter
284
+ state[14] = LE_toInt(nonce, 0)
285
+ state[15] = LE_toInt(nonce, 4)
286
+ state[16] = LE_toInt(nonce, 8)
287
+
288
+ return state
289
+ end
290
+
291
+ local function serialize(state)
292
+ local r = {}
293
+ for i = 1, 16 do
294
+ r[#r+1] = state[i] & 0xFF
295
+ r[#r+1] = (state[i] & 0xFF00) >> 8
296
+ r[#r+1] = (state[i] & 0xFF0000) >> 16
297
+ r[#r+1] = (state[i] & 0xFF000000) >> 24
298
+ end
299
+ return r
300
+ end
301
+
302
+ local function crypt(data, key, nonce, cntr, round)
303
+ assert(type(key) == "table", "ChaCha20: Invalid key format ("..type(key).."), must be table")
304
+ assert(type(nonce) == "table", "ChaCha20: Invalid nonce format ("..type(nonce).."), must be table")
305
+ assert(#key == 16 or #key == 32, "ChaCha20: Invalid key length ("..#key.."), must be 16 or 32")
306
+ assert(#nonce == 12, "ChaCha20: Invalid nonce length ("..#nonce.."), must be 12")
307
+
308
+ local data = type(data) == "table" and {unpack(data)} or {tostring(data):byte(1,-1)}
309
+ cntr = tonumber(cntr) or 1
310
+ round = tonumber(round) or 20
311
+
312
+ local out = {}
313
+ local state = initState(key, nonce, cntr)
314
+ local blockAmt = math.floor(#data/64)
315
+ for i = 0, blockAmt do
316
+ local ks = serialize(hashBlock(state, round))
317
+ state[13] = (state[13]+1) % mod
318
+
319
+ local block = {}
320
+ for j = 1, 64 do
321
+ block[j] = data[((i)*64)+j]
322
+ end
323
+ for j = 1, #block do
324
+ out[#out+1] = block[j] ~ ks[j]
325
+ end
326
+
327
+ if i % 1000 == 0 then
328
+ os.queueEvent("")
329
+ os.pullEvent("")
330
+ end
331
+ end
332
+ return setmetatable(out, byteTableMT)
333
+ end
334
+
335
+ return {
336
+ crypt = crypt
337
+ }
338
+ end)()
339
+
340
+ -- random.lua - Random Byte Generator
341
+ local random = (function()
342
+ local entropy = ""
343
+ local accumulator = ""
344
+ local entropyPath = "/.random"
345
+
346
+ local function feed(data)
347
+ accumulator = accumulator .. (data or "")
348
+ end
349
+
350
+ local function digest()
351
+ entropy = tostring(sha256.digest(entropy .. accumulator))
352
+ accumulator = ""
353
+ end
354
+
355
+ if fs and fs.exists(entropyPath) then
356
+ local entropyFile = fs.readFile(entropyPath)
357
+ if entropyFile then
358
+ feed(entropyFile)
359
+ end
360
+ end
361
+
362
+ feed("init")
363
+ feed(tostring(math.random(1, 2147483647)))
364
+ feed("|")
365
+ feed(tostring(math.random(1, 2147483647)))
366
+ feed("|")
367
+ feed(tostring(math.random(1, 16)))
368
+ feed("|")
369
+ feed(tostring(math.ceil(((os.clock() - clock) + time) * 1000)))
370
+ feed("|")
371
+ for i = 1, 10000 do
372
+ local s = tostring({}):sub(8)
373
+ if (s:sub(1, 2) == "0x") then
374
+ s = s:sub(3)
375
+ end
376
+ while #s < 8 do
377
+ s = "0" .. s
378
+ end
379
+ feed(string.char(math.ceil(((os.clock() - clock) + time) * 1000) % 256))
380
+ feed(string.char(tonumber(s:sub(1, 2), 16)))
381
+ feed(string.char(tonumber(s:sub(3, 4), 16)))
382
+ feed(string.char(tonumber(s:sub(5, 6), 16)))
383
+ feed(string.char(tonumber(s:sub(7, 8), 16)))
384
+ end
385
+ digest()
386
+ feed(tostring(math.ceil(((os.clock() - clock) + time) * 1000)))
387
+ digest()
388
+
389
+ local function save()
390
+ feed("save")
391
+ feed(tostring(math.ceil(((os.clock() - clock) + time) * 1000)))
392
+ feed(tostring({}))
393
+ digest()
394
+ if fs then
395
+ fs.writeFile(entropyPath, tostring(sha256.hmac("save", entropy)))
396
+ end
397
+ entropy = tostring(sha256.digest(entropy))
398
+ end
399
+ save()
400
+
401
+ local function seed(data)
402
+ feed("seed")
403
+ feed(tostring(math.ceil(((os.clock() - clock) + time) * 1000)))
404
+ feed(tostring({}))
405
+ feed(data)
406
+ digest()
407
+ save()
408
+ end
409
+
410
+ local function random()
411
+ feed("random")
412
+ feed(tostring(math.ceil(((os.clock() - clock) + time) * 1000)))
413
+ feed(tostring({}))
414
+ digest()
415
+ save()
416
+
417
+ local result = sha256.hmac("out", entropy)
418
+ entropy = tostring(sha256.digest(entropy))
419
+
420
+ return result
421
+ end
422
+
423
+ return {
424
+ seed = seed,
425
+ save = save,
426
+ random = random
427
+ }
428
+ end)()
429
+
430
+ -- Big integer arithmetic for 168-bit (and 336-bit) numbers
431
+ -- Numbers are represented as little-endian tables of 24-bit integers
432
+ local arith = (function()
433
+ local function isEqual(a, b)
434
+ return (
435
+ a[1] == b[1]
436
+ and a[2] == b[2]
437
+ and a[3] == b[3]
438
+ and a[4] == b[4]
439
+ and a[5] == b[5]
440
+ and a[6] == b[6]
441
+ and a[7] == b[7]
442
+ )
443
+ end
444
+
445
+ local function compare(a, b)
446
+ for i = 7, 1, -1 do
447
+ if a[i] > b[i] then
448
+ return 1
449
+ elseif a[i] < b[i] then
450
+ return -1
451
+ end
452
+ end
453
+
454
+ return 0
455
+ end
456
+
457
+ local function add(a, b)
458
+ -- c7 may be greater than 2^24 before reduction
459
+ local c1 = a[1] + b[1]
460
+ local c2 = a[2] + b[2]
461
+ local c3 = a[3] + b[3]
462
+ local c4 = a[4] + b[4]
463
+ local c5 = a[5] + b[5]
464
+ local c6 = a[6] + b[6]
465
+ local c7 = a[7] + b[7]
466
+
467
+ if c1 > 0xffffff then
468
+ c2 = c2 + 1
469
+ c1 = c1 - 0x1000000
470
+ end
471
+ if c2 > 0xffffff then
472
+ c3 = c3 + 1
473
+ c2 = c2 - 0x1000000
474
+ end
475
+ if c3 > 0xffffff then
476
+ c4 = c4 + 1
477
+ c3 = c3 - 0x1000000
478
+ end
479
+ if c4 > 0xffffff then
480
+ c5 = c5 + 1
481
+ c4 = c4 - 0x1000000
482
+ end
483
+ if c5 > 0xffffff then
484
+ c6 = c6 + 1
485
+ c5 = c5 - 0x1000000
486
+ end
487
+ if c6 > 0xffffff then
488
+ c7 = c7 + 1
489
+ c6 = c6 - 0x1000000
490
+ end
491
+
492
+ return {c1, c2, c3, c4, c5, c6, c7}
493
+ end
494
+
495
+ local function sub(a, b)
496
+ -- c7 may be negative before reduction
497
+ local c1 = a[1] - b[1]
498
+ local c2 = a[2] - b[2]
499
+ local c3 = a[3] - b[3]
500
+ local c4 = a[4] - b[4]
501
+ local c5 = a[5] - b[5]
502
+ local c6 = a[6] - b[6]
503
+ local c7 = a[7] - b[7]
504
+
505
+ if c1 < 0 then
506
+ c2 = c2 - 1
507
+ c1 = c1 + 0x1000000
508
+ end
509
+ if c2 < 0 then
510
+ c3 = c3 - 1
511
+ c2 = c2 + 0x1000000
512
+ end
513
+ if c3 < 0 then
514
+ c4 = c4 - 1
515
+ c3 = c3 + 0x1000000
516
+ end
517
+ if c4 < 0 then
518
+ c5 = c5 - 1
519
+ c4 = c4 + 0x1000000
520
+ end
521
+ if c5 < 0 then
522
+ c6 = c6 - 1
523
+ c5 = c5 + 0x1000000
524
+ end
525
+ if c6 < 0 then
526
+ c7 = c7 - 1
527
+ c6 = c6 + 0x1000000
528
+ end
529
+
530
+ return {c1, c2, c3, c4, c5, c6, c7}
531
+ end
532
+
533
+ local function rShift(a)
534
+ local c1 = a[1]
535
+ local c2 = a[2]
536
+ local c3 = a[3]
537
+ local c4 = a[4]
538
+ local c5 = a[5]
539
+ local c6 = a[6]
540
+ local c7 = a[7]
541
+
542
+ c1 = c1 / 2
543
+ c1 = c1 - c1 % 1
544
+ c1 = c1 + (c2 % 2) * 0x800000
545
+ c2 = c2 / 2
546
+ c2 = c2 - c2 % 1
547
+ c2 = c2 + (c3 % 2) * 0x800000
548
+ c3 = c3 / 2
549
+ c3 = c3 - c3 % 1
550
+ c3 = c3 + (c4 % 2) * 0x800000
551
+ c4 = c4 / 2
552
+ c4 = c4 - c4 % 1
553
+ c4 = c4 + (c5 % 2) * 0x800000
554
+ c5 = c5 / 2
555
+ c5 = c5 - c5 % 1
556
+ c5 = c5 + (c6 % 2) * 0x800000
557
+ c6 = c6 / 2
558
+ c6 = c6 - c6 % 1
559
+ c6 = c6 + (c7 % 2) * 0x800000
560
+ c7 = c7 / 2
561
+ c7 = c7 - c7 % 1
562
+
563
+ return {c1, c2, c3, c4, c5, c6, c7}
564
+ end
565
+
566
+ local function addDouble(a, b)
567
+ -- a and b are 336-bit integers (14 words)
568
+ local c1 = a[1] + b[1]
569
+ local c2 = a[2] + b[2]
570
+ local c3 = a[3] + b[3]
571
+ local c4 = a[4] + b[4]
572
+ local c5 = a[5] + b[5]
573
+ local c6 = a[6] + b[6]
574
+ local c7 = a[7] + b[7]
575
+ local c8 = a[8] + b[8]
576
+ local c9 = a[9] + b[9]
577
+ local c10 = a[10] + b[10]
578
+ local c11 = a[11] + b[11]
579
+ local c12 = a[12] + b[12]
580
+ local c13 = a[13] + b[13]
581
+ local c14 = a[14] + b[14]
582
+
583
+ if c1 > 0xffffff then
584
+ c2 = c2 + 1
585
+ c1 = c1 - 0x1000000
586
+ end
587
+ if c2 > 0xffffff then
588
+ c3 = c3 + 1
589
+ c2 = c2 - 0x1000000
590
+ end
591
+ if c3 > 0xffffff then
592
+ c4 = c4 + 1
593
+ c3 = c3 - 0x1000000
594
+ end
595
+ if c4 > 0xffffff then
596
+ c5 = c5 + 1
597
+ c4 = c4 - 0x1000000
598
+ end
599
+ if c5 > 0xffffff then
600
+ c6 = c6 + 1
601
+ c5 = c5 - 0x1000000
602
+ end
603
+ if c6 > 0xffffff then
604
+ c7 = c7 + 1
605
+ c6 = c6 - 0x1000000
606
+ end
607
+ if c7 > 0xffffff then
608
+ c8 = c8 + 1
609
+ c7 = c7 - 0x1000000
610
+ end
611
+ if c8 > 0xffffff then
612
+ c9 = c9 + 1
613
+ c8 = c8 - 0x1000000
614
+ end
615
+ if c9 > 0xffffff then
616
+ c10 = c10 + 1
617
+ c9 = c9 - 0x1000000
618
+ end
619
+ if c10 > 0xffffff then
620
+ c11 = c11 + 1
621
+ c10 = c10 - 0x1000000
622
+ end
623
+ if c11 > 0xffffff then
624
+ c12 = c12 + 1
625
+ c11 = c11 - 0x1000000
626
+ end
627
+ if c12 > 0xffffff then
628
+ c13 = c13 + 1
629
+ c12 = c12 - 0x1000000
630
+ end
631
+ if c13 > 0xffffff then
632
+ c14 = c14 + 1
633
+ c13 = c13 - 0x1000000
634
+ end
635
+
636
+ return {c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14}
637
+ end
638
+
639
+ local function mult(a, b, half_multiply)
640
+ local a1, a2, a3, a4, a5, a6, a7 = unpack(a)
641
+ local b1, b2, b3, b4, b5, b6, b7 = unpack(b)
642
+
643
+ local c1 = a1 * b1
644
+ local c2 = a1 * b2 + a2 * b1
645
+ local c3 = a1 * b3 + a2 * b2 + a3 * b1
646
+ local c4 = a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1
647
+ local c5 = a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1
648
+ local c6 = a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 + a6 * b1
649
+ local c7 = a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 + a6 * b2
650
+ + a7 * b1
651
+ local c8, c9, c10, c11, c12, c13, c14
652
+ if not half_multiply then
653
+ c8 = a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 + a6 * b3 + a7 * b2
654
+ c9 = a3 * b7 + a4 * b6 + a5 * b5 + a6 * b4 + a7 * b3
655
+ c10 = a4 * b7 + a5 * b6 + a6 * b5 + a7 * b4
656
+ c11 = a5 * b7 + a6 * b6 + a7 * b5
657
+ c12 = a6 * b7 + a7 * b6
658
+ c13 = a7 * b7
659
+ c14 = 0
660
+ else
661
+ c8 = 0
662
+ end
663
+
664
+ local temp
665
+ temp = c1
666
+ c1 = c1 % 0x1000000
667
+ c2 = c2 + (temp - c1) / 0x1000000
668
+ temp = c2
669
+ c2 = c2 % 0x1000000
670
+ c3 = c3 + (temp - c2) / 0x1000000
671
+ temp = c3
672
+ c3 = c3 % 0x1000000
673
+ c4 = c4 + (temp - c3) / 0x1000000
674
+ temp = c4
675
+ c4 = c4 % 0x1000000
676
+ c5 = c5 + (temp - c4) / 0x1000000
677
+ temp = c5
678
+ c5 = c5 % 0x1000000
679
+ c6 = c6 + (temp - c5) / 0x1000000
680
+ temp = c6
681
+ c6 = c6 % 0x1000000
682
+ c7 = c7 + (temp - c6) / 0x1000000
683
+ temp = c7
684
+ c7 = c7 % 0x1000000
685
+ if not half_multiply then
686
+ c8 = c8 + (temp - c7) / 0x1000000
687
+ temp = c8
688
+ c8 = c8 % 0x1000000
689
+ c9 = c9 + (temp - c8) / 0x1000000
690
+ temp = c9
691
+ c9 = c9 % 0x1000000
692
+ c10 = c10 + (temp - c9) / 0x1000000
693
+ temp = c10
694
+ c10 = c10 % 0x1000000
695
+ c11 = c11 + (temp - c10) / 0x1000000
696
+ temp = c11
697
+ c11 = c11 % 0x1000000
698
+ c12 = c12 + (temp - c11) / 0x1000000
699
+ temp = c12
700
+ c12 = c12 % 0x1000000
701
+ c13 = c13 + (temp - c12) / 0x1000000
702
+ temp = c13
703
+ c13 = c13 % 0x1000000
704
+ c14 = c14 + (temp - c13) / 0x1000000
705
+ end
706
+
707
+ return {c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14}
708
+ end
709
+
710
+ local function square(a)
711
+ -- returns a 336-bit integer (14 words)
712
+ local a1, a2, a3, a4, a5, a6, a7 = unpack(a)
713
+
714
+ local c1 = a1 * a1
715
+ local c2 = a1 * a2 * 2
716
+ local c3 = a1 * a3 * 2 + a2 * a2
717
+ local c4 = a1 * a4 * 2 + a2 * a3 * 2
718
+ local c5 = a1 * a5 * 2 + a2 * a4 * 2 + a3 * a3
719
+ local c6 = a1 * a6 * 2 + a2 * a5 * 2 + a3 * a4 * 2
720
+ local c7 = a1 * a7 * 2 + a2 * a6 * 2 + a3 * a5 * 2 + a4 * a4
721
+ local c8 = a2 * a7 * 2 + a3 * a6 * 2 + a4 * a5 * 2
722
+ local c9 = a3 * a7 * 2 + a4 * a6 * 2 + a5 * a5
723
+ local c10 = a4 * a7 * 2 + a5 * a6 * 2
724
+ local c11 = a5 * a7 * 2 + a6 * a6
725
+ local c12 = a6 * a7 * 2
726
+ local c13 = a7 * a7
727
+ local c14 = 0
728
+
729
+ local temp
730
+ temp = c1
731
+ c1 = c1 % 0x1000000
732
+ c2 = c2 + (temp - c1) / 0x1000000
733
+ temp = c2
734
+ c2 = c2 % 0x1000000
735
+ c3 = c3 + (temp - c2) / 0x1000000
736
+ temp = c3
737
+ c3 = c3 % 0x1000000
738
+ c4 = c4 + (temp - c3) / 0x1000000
739
+ temp = c4
740
+ c4 = c4 % 0x1000000
741
+ c5 = c5 + (temp - c4) / 0x1000000
742
+ temp = c5
743
+ c5 = c5 % 0x1000000
744
+ c6 = c6 + (temp - c5) / 0x1000000
745
+ temp = c6
746
+ c6 = c6 % 0x1000000
747
+ c7 = c7 + (temp - c6) / 0x1000000
748
+ temp = c7
749
+ c7 = c7 % 0x1000000
750
+ c8 = c8 + (temp - c7) / 0x1000000
751
+ temp = c8
752
+ c8 = c8 % 0x1000000
753
+ c9 = c9 + (temp - c8) / 0x1000000
754
+ temp = c9
755
+ c9 = c9 % 0x1000000
756
+ c10 = c10 + (temp - c9) / 0x1000000
757
+ temp = c10
758
+ c10 = c10 % 0x1000000
759
+ c11 = c11 + (temp - c10) / 0x1000000
760
+ temp = c11
761
+ c11 = c11 % 0x1000000
762
+ c12 = c12 + (temp - c11) / 0x1000000
763
+ temp = c12
764
+ c12 = c12 % 0x1000000
765
+ c13 = c13 + (temp - c12) / 0x1000000
766
+ temp = c13
767
+ c13 = c13 % 0x1000000
768
+ c14 = c14 + (temp - c13) / 0x1000000
769
+
770
+ return {c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14}
771
+ end
772
+
773
+ local function encodeInt(a)
774
+ local enc = {}
775
+
776
+ for i = 1, 7 do
777
+ local word = a[i]
778
+ for j = 1, 3 do
779
+ enc[#enc + 1] = word % 256
780
+ word = math.floor(word / 256)
781
+ end
782
+ end
783
+
784
+ return enc
785
+ end
786
+
787
+ local function decodeInt(enc)
788
+ local a = {}
789
+ local encCopy = {}
790
+
791
+ for i = 1, 21 do
792
+ local byte = enc[i]
793
+ assert(type(byte) == "number", "integer decoding failure")
794
+ assert(byte >= 0 and byte <= 255, "integer decoding failure")
795
+ assert(byte % 1 == 0, "integer decoding failure")
796
+ encCopy[i] = byte
797
+ end
798
+
799
+ for i = 1, 21, 3 do
800
+ local word = 0
801
+ for j = 2, 0, -1 do
802
+ word = word * 256
803
+ word = word + encCopy[i + j]
804
+ end
805
+ a[#a + 1] = word
806
+ end
807
+
808
+ return a
809
+ end
810
+
811
+ local function mods(d, w)
812
+ local result = d[1] % 2^w
813
+
814
+ if result >= 2^(w - 1) then
815
+ result = result - 2^w
816
+ end
817
+
818
+ return result
819
+ end
820
+
821
+ -- Represents a 168-bit number as the (2^w)-ary Non-Adjacent Form
822
+ local function NAF(d, w)
823
+ local t = {}
824
+ local d = {unpack(d)}
825
+
826
+ for i = 1, 168 do
827
+ if d[1] % 2 == 1 then
828
+ t[#t + 1] = mods(d, w)
829
+ d = sub(d, {t[#t], 0, 0, 0, 0, 0, 0})
830
+ else
831
+ t[#t + 1] = 0
832
+ end
833
+
834
+ d = rShift(d)
835
+ end
836
+
837
+ return t
838
+ end
839
+
840
+ return {
841
+ isEqual = isEqual,
842
+ compare = compare,
843
+ add = add,
844
+ sub = sub,
845
+ addDouble = addDouble,
846
+ mult = mult,
847
+ square = square,
848
+ encodeInt = encodeInt,
849
+ decodeInt = decodeInt,
850
+ NAF = NAF
851
+ }
852
+ end)()
853
+
854
+ -- Arithmetic on the finite field of integers modulo p
855
+ -- Where p is the finite field modulus
856
+ local modp = (function()
857
+ local add = arith.add
858
+ local sub = arith.sub
859
+ local addDouble = arith.addDouble
860
+ local mult = arith.mult
861
+ local square = arith.square
862
+
863
+ local p = {3, 0, 0, 0, 0, 0, 15761408}
864
+
865
+ -- We're using the Montgomery Reduction for fast modular multiplication.
866
+ -- https://en.wikipedia.org/wiki/Montgomery_modular_multiplication
867
+ -- r = 2^168
868
+ -- p * pInverse = -1 (mod r)
869
+ -- r2 = r * r (mod p)
870
+ local pInverse = {5592405, 5592405, 5592405, 5592405, 5592405, 5592405, 14800213}
871
+ local r2 = {13533400, 837116, 6278376, 13533388, 837116, 6278376, 7504076}
872
+
873
+ local function multByP(a)
874
+ local a1, a2, a3, a4, a5, a6, a7 = unpack(a)
875
+
876
+ local c1 = a1 * 3
877
+ local c2 = a2 * 3
878
+ local c3 = a3 * 3
879
+ local c4 = a4 * 3
880
+ local c5 = a5 * 3
881
+ local c6 = a6 * 3
882
+ local c7 = a1 * 15761408
883
+ c7 = c7 + a7 * 3
884
+ local c8 = a2 * 15761408
885
+ local c9 = a3 * 15761408
886
+ local c10 = a4 * 15761408
887
+ local c11 = a5 * 15761408
888
+ local c12 = a6 * 15761408
889
+ local c13 = a7 * 15761408
890
+ local c14 = 0
891
+
892
+ local temp
893
+ temp = c1 / 0x1000000
894
+ c2 = c2 + (temp - temp % 1)
895
+ c1 = c1 % 0x1000000
896
+ temp = c2 / 0x1000000
897
+ c3 = c3 + (temp - temp % 1)
898
+ c2 = c2 % 0x1000000
899
+ temp = c3 / 0x1000000
900
+ c4 = c4 + (temp - temp % 1)
901
+ c3 = c3 % 0x1000000
902
+ temp = c4 / 0x1000000
903
+ c5 = c5 + (temp - temp % 1)
904
+ c4 = c4 % 0x1000000
905
+ temp = c5 / 0x1000000
906
+ c6 = c6 + (temp - temp % 1)
907
+ c5 = c5 % 0x1000000
908
+ temp = c6 / 0x1000000
909
+ c7 = c7 + (temp - temp % 1)
910
+ c6 = c6 % 0x1000000
911
+ temp = c7 / 0x1000000
912
+ c8 = c8 + (temp - temp % 1)
913
+ c7 = c7 % 0x1000000
914
+ temp = c8 / 0x1000000
915
+ c9 = c9 + (temp - temp % 1)
916
+ c8 = c8 % 0x1000000
917
+ temp = c9 / 0x1000000
918
+ c10 = c10 + (temp - temp % 1)
919
+ c9 = c9 % 0x1000000
920
+ temp = c10 / 0x1000000
921
+ c11 = c11 + (temp - temp % 1)
922
+ c10 = c10 % 0x1000000
923
+ temp = c11 / 0x1000000
924
+ c12 = c12 + (temp - temp % 1)
925
+ c11 = c11 % 0x1000000
926
+ temp = c12 / 0x1000000
927
+ c13 = c13 + (temp - temp % 1)
928
+ c12 = c12 % 0x1000000
929
+ temp = c13 / 0x1000000
930
+ c14 = c14 + (temp - temp % 1)
931
+ c13 = c13 % 0x1000000
932
+
933
+ return {c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14}
934
+ end
935
+
936
+ -- Reduces a number from [0, 2p - 1] to [0, p - 1]
937
+ local function reduceModP(a)
938
+ -- a < p
939
+ if a[7] < 15761408 or a[7] == 15761408 and a[1] < 3 then
940
+ return {unpack(a)}
941
+ end
942
+
943
+ -- a > p
944
+ local c1 = a[1]
945
+ local c2 = a[2]
946
+ local c3 = a[3]
947
+ local c4 = a[4]
948
+ local c5 = a[5]
949
+ local c6 = a[6]
950
+ local c7 = a[7]
951
+
952
+ c1 = c1 - 3
953
+ c7 = c7 - 15761408
954
+
955
+ if c1 < 0 then
956
+ c2 = c2 - 1
957
+ c1 = c1 + 0x1000000
958
+ end
959
+ if c2 < 0 then
960
+ c3 = c3 - 1
961
+ c2 = c2 + 0x1000000
962
+ end
963
+ if c3 < 0 then
964
+ c4 = c4 - 1
965
+ c3 = c3 + 0x1000000
966
+ end
967
+ if c4 < 0 then
968
+ c5 = c5 - 1
969
+ c4 = c4 + 0x1000000
970
+ end
971
+ if c5 < 0 then
972
+ c6 = c6 - 1
973
+ c5 = c5 + 0x1000000
974
+ end
975
+ if c6 < 0 then
976
+ c7 = c7 - 1
977
+ c6 = c6 + 0x1000000
978
+ end
979
+
980
+ return {c1, c2, c3, c4, c5, c6, c7}
981
+ end
982
+
983
+ local function addModP(a, b)
984
+ return reduceModP(add(a, b))
985
+ end
986
+
987
+ local function subModP(a, b)
988
+ local result = sub(a, b)
989
+
990
+ if result[7] < 0 then
991
+ result = add(result, p)
992
+ end
993
+
994
+ return result
995
+ end
996
+
997
+ -- Montgomery REDC algorithn
998
+ -- Reduces a number from [0, p^2 - 1] to [0, p - 1]
999
+ local function REDC(T)
1000
+ local m = mult(T, pInverse, true)
1001
+ local t = {unpack(addDouble(T, multByP(m)), 8, 14)}
1002
+
1003
+ return reduceModP(t)
1004
+ end
1005
+
1006
+ local function multModP(a, b)
1007
+ -- Only works with a, b in Montgomery form
1008
+ return REDC(mult(a, b))
1009
+ end
1010
+
1011
+ local function squareModP(a)
1012
+ -- Only works with a in Montgomery form
1013
+ return REDC(square(a))
1014
+ end
1015
+
1016
+ local function montgomeryModP(a)
1017
+ return multModP(a, r2)
1018
+ end
1019
+
1020
+ local function inverseMontgomeryModP(a)
1021
+ local a = {unpack(a)}
1022
+
1023
+ for i = 8, 14 do
1024
+ a[i] = 0
1025
+ end
1026
+
1027
+ return REDC(a)
1028
+ end
1029
+
1030
+ local ONE = montgomeryModP({1, 0, 0, 0, 0, 0, 0})
1031
+
1032
+ local function expModP(base, exponentBinary)
1033
+ local base = {unpack(base)}
1034
+ local result = {unpack(ONE)}
1035
+
1036
+ for i = 1, 168 do
1037
+ if exponentBinary[i] == 1 then
1038
+ result = multModP(result, base)
1039
+ end
1040
+ base = squareModP(base)
1041
+ end
1042
+
1043
+ return result
1044
+ end
1045
+
1046
+ return {
1047
+ addModP = addModP,
1048
+ subModP = subModP,
1049
+ multModP = multModP,
1050
+ squareModP = squareModP,
1051
+ montgomeryModP = montgomeryModP,
1052
+ inverseMontgomeryModP = inverseMontgomeryModP,
1053
+ expModP = expModP
1054
+ }
1055
+ end)()
1056
+
1057
+ -- Arithmetic on the Finite Field of Integers modulo q
1058
+ -- Where q is the generator's subgroup order.
1059
+ local modq = (function()
1060
+ local isEqual = arith.isEqual
1061
+ local compare = arith.compare
1062
+ local add = arith.add
1063
+ local sub = arith.sub
1064
+ local addDouble = arith.addDouble
1065
+ local mult = arith.mult
1066
+ local square = arith.square
1067
+ local encodeInt = arith.encodeInt
1068
+ local decodeInt = arith.decodeInt
1069
+
1070
+ local modQMT
1071
+
1072
+ local q = {9622359, 6699217, 13940450, 16775734, 16777215, 16777215, 3940351}
1073
+ local qMinusTwoBinary = {1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1}
1074
+
1075
+ -- We're using the Montgomery Reduction for fast modular multiplication.
1076
+ -- https://en.wikipedia.org/wiki/Montgomery_modular_multiplication
1077
+ -- r = 2^168
1078
+ -- q * qInverse = -1 (mod r)
1079
+ -- r2 = r * r (mod q)
1080
+ local qInverse = {15218585, 5740955, 3271338, 9903997, 9067368, 7173545, 6988392}
1081
+ local r2 = {1336213, 11071705, 9716828, 11083885, 9188643, 1494868, 3306114}
1082
+
1083
+ -- Reduces a number from [0, 2q - 1] to [0, q - 1]
1084
+ local function reduceModQ(a)
1085
+ local result = {unpack(a)}
1086
+
1087
+ if compare(result, q) >= 0 then
1088
+ result = sub(result, q)
1089
+ end
1090
+
1091
+ return setmetatable(result, modQMT)
1092
+ end
1093
+
1094
+ local function addModQ(a, b)
1095
+ return reduceModQ(add(a, b))
1096
+ end
1097
+
1098
+ local function subModQ(a, b)
1099
+ local result = sub(a, b)
1100
+
1101
+ if result[7] < 0 then
1102
+ result = add(result, q)
1103
+ end
1104
+
1105
+ return setmetatable(result, modQMT)
1106
+ end
1107
+
1108
+ -- Montgomery REDC algorithn
1109
+ -- Reduces a number from [0, q^2 - 1] to [0, q - 1]
1110
+ local function REDC(T)
1111
+ local m = {unpack(mult({unpack(T, 1, 7)}, qInverse, true), 1, 7)}
1112
+ local t = {unpack(addDouble(T, mult(m, q)), 8, 14)}
1113
+
1114
+ return reduceModQ(t)
1115
+ end
1116
+
1117
+ local function multModQ(a, b)
1118
+ -- Only works with a, b in Montgomery form
1119
+ return REDC(mult(a, b))
1120
+ end
1121
+
1122
+ local function squareModQ(a)
1123
+ -- Only works with a in Montgomery form
1124
+ return REDC(square(a))
1125
+ end
1126
+
1127
+ local function montgomeryModQ(a)
1128
+ return multModQ(a, r2)
1129
+ end
1130
+
1131
+ local function inverseMontgomeryModQ(a)
1132
+ local a = {unpack(a)}
1133
+
1134
+ for i = 8, 14 do
1135
+ a[i] = 0
1136
+ end
1137
+
1138
+ return REDC(a)
1139
+ end
1140
+
1141
+ local ONE = montgomeryModQ({1, 0, 0, 0, 0, 0, 0})
1142
+
1143
+ local function expModQ(base, exponentBinary)
1144
+ local base = {unpack(base)}
1145
+ local result = {unpack(ONE)}
1146
+
1147
+ for i = 1, 168 do
1148
+ if exponentBinary[i] == 1 then
1149
+ result = multModQ(result, base)
1150
+ end
1151
+ base = squareModQ(base)
1152
+ end
1153
+
1154
+ return result
1155
+ end
1156
+
1157
+ local function intExpModQ(base, exponent)
1158
+ local base = {unpack(base)}
1159
+ local result = setmetatable({unpack(ONE)}, modQMT)
1160
+
1161
+ if exponent < 0 then
1162
+ base = expModQ(base, qMinusTwoBinary)
1163
+ exponent = -exponent
1164
+ end
1165
+
1166
+ while exponent > 0 do
1167
+ if exponent % 2 == 1 then
1168
+ result = multModQ(result, base)
1169
+ end
1170
+ base = squareModQ(base)
1171
+ exponent = exponent / 2
1172
+ exponent = exponent - exponent % 1
1173
+ end
1174
+
1175
+ return result
1176
+ end
1177
+
1178
+ local function encodeModQ(a)
1179
+ local result = encodeInt(a)
1180
+
1181
+ return setmetatable(result, byteTableMT)
1182
+ end
1183
+
1184
+ local function decodeModQ(s)
1185
+ s = type(s) == "table" and {unpack(s, 1, 21)} or {tostring(s):byte(1, 21)}
1186
+ local result = decodeInt(s)
1187
+ result[7] = result[7] % q[7]
1188
+
1189
+ return setmetatable(result, modQMT)
1190
+ end
1191
+
1192
+ local function randomModQ()
1193
+ while true do
1194
+ local s = {unpack(random.random(), 1, 21)}
1195
+ local result = decodeInt(s)
1196
+ if result[7] < q[7] then
1197
+ return setmetatable(result, modQMT)
1198
+ end
1199
+ end
1200
+ end
1201
+
1202
+ local function hashModQ(data)
1203
+ return decodeModQ(sha256.digest(data))
1204
+ end
1205
+
1206
+ modQMT = {
1207
+ __index = {
1208
+ encode = function(self)
1209
+ return encodeModQ(self)
1210
+ end
1211
+ },
1212
+
1213
+ __tostring = function(self)
1214
+ return self:encode():toHex()
1215
+ end,
1216
+
1217
+ __add = function(self, other)
1218
+ if type(self) == "number" then
1219
+ return other + self
1220
+ end
1221
+
1222
+ if type(other) == "number" then
1223
+ assert(other < 2^24, "number operand too big")
1224
+ other = montgomeryModQ({other, 0, 0, 0, 0, 0, 0})
1225
+ end
1226
+
1227
+ return addModQ(self, other)
1228
+ end,
1229
+
1230
+ __sub = function(a, b)
1231
+ if type(a) == "number" then
1232
+ assert(a < 2^24, "number operand too big")
1233
+ a = montgomeryModQ({a, 0, 0, 0, 0, 0, 0})
1234
+ end
1235
+
1236
+ if type(b) == "number" then
1237
+ assert(b < 2^24, "number operand too big")
1238
+ b = montgomeryModQ({b, 0, 0, 0, 0, 0, 0})
1239
+ end
1240
+
1241
+ return subModQ(a, b)
1242
+ end,
1243
+
1244
+ __unm = function(self)
1245
+ return subModQ(q, self)
1246
+ end,
1247
+
1248
+ __eq = function(self, other)
1249
+ return isEqual(self, other)
1250
+ end,
1251
+
1252
+ __mul = function(self, other)
1253
+ if type(self) == "number" then
1254
+ return other * self
1255
+ end
1256
+
1257
+ -- EC point
1258
+ -- Use the point's metatable to handle multiplication
1259
+ if type(other) == "table" and type(other[1]) == "table" then
1260
+ return other * self
1261
+ end
1262
+
1263
+ if type(other) == "number" then
1264
+ assert(other < 2^24, "number operand too big")
1265
+ other = montgomeryModQ({other, 0, 0, 0, 0, 0, 0})
1266
+ end
1267
+
1268
+ return multModQ(self, other)
1269
+ end,
1270
+
1271
+ __div = function(a, b)
1272
+ if type(a) == "number" then
1273
+ assert(a < 2^24, "number operand too big")
1274
+ a = montgomeryModQ({a, 0, 0, 0, 0, 0, 0})
1275
+ end
1276
+
1277
+ if type(b) == "number" then
1278
+ assert(b < 2^24, "number operand too big")
1279
+ b = montgomeryModQ({b, 0, 0, 0, 0, 0, 0})
1280
+ end
1281
+
1282
+ local bInv = expModQ(b, qMinusTwoBinary)
1283
+
1284
+ return multModQ(a, bInv)
1285
+ end,
1286
+
1287
+ __pow = function(self, other)
1288
+ return intExpModQ(self, other)
1289
+ end
1290
+ }
1291
+
1292
+ return {
1293
+ hashModQ = hashModQ,
1294
+ randomModQ = randomModQ,
1295
+ decodeModQ = decodeModQ,
1296
+ inverseMontgomeryModQ = inverseMontgomeryModQ
1297
+ }
1298
+ end)()
1299
+
1300
+ -- Elliptic curve arithmetic
1301
+ local curve = (function()
1302
+ ---- About the Curve Itself
1303
+ -- Field Size: 168 bits
1304
+ -- Field Modulus (p): 481 * 2^159 + 3
1305
+ -- Equation: x^2 + y^2 = 1 + 122 * x^2 * y^2
1306
+ -- Parameters: Edwards Curve with d = 122
1307
+ -- Curve Order (n): 351491143778082151827986174289773107581916088585564
1308
+ -- Cofactor (h): 4
1309
+ -- Generator Order (q): 87872785944520537956996543572443276895479022146391
1310
+ ---- About the Curve's Security
1311
+ -- Current best attack security: 81.777 bits (Small Subgroup + Rho)
1312
+ -- Rho Security: log2(0.884 * sqrt(q)) = 82.777 bits
1313
+ -- Transfer Security? Yes: p ~= q; k > 20
1314
+ -- Field Discriminant Security? Yes:
1315
+ -- t = 27978492958645335688000168
1316
+ -- s = 10
1317
+ -- |D| = 6231685068753619775430107799412237267322159383147 > 2^100
1318
+ -- Rigidity? No, not at all.
1319
+ -- XZ/YZ Ladder Security? No: Single coordinate ladders are insecure.
1320
+ -- Small Subgroup Security? No.
1321
+ -- Invalid Curve Security? Yes: Points are checked before every operation.
1322
+ -- Invalid Curve Twist Security? No: Don't use single coordinate ladders.
1323
+ -- Completeness? Yes: The curve is complete.
1324
+ -- Indistinguishability? Yes (Elligator 2), but not implemented.
1325
+
1326
+ local isEqual = arith.isEqual
1327
+ local NAF = arith.NAF
1328
+ local encodeInt = arith.encodeInt
1329
+ local decodeInt = arith.decodeInt
1330
+ local multModP = modp.multModP
1331
+ local squareModP = modp.squareModP
1332
+ local addModP = modp.addModP
1333
+ local subModP = modp.subModP
1334
+ local montgomeryModP = modp.montgomeryModP
1335
+ local expModP = modp.expModP
1336
+ local inverseMontgomeryModQ = modq.inverseMontgomeryModQ
1337
+
1338
+ local pointMT
1339
+ local ZERO = {0, 0, 0, 0, 0, 0, 0}
1340
+ local ONE = montgomeryModP({1, 0, 0, 0, 0, 0, 0})
1341
+
1342
+ -- Curve Parameters
1343
+ local d = montgomeryModP({122, 0, 0, 0, 0, 0, 0})
1344
+ local p = {3, 0, 0, 0, 0, 0, 15761408}
1345
+ local pMinusTwoBinary = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1}
1346
+ local pMinusThreeOverFourBinary = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1}
1347
+ local G = {
1348
+ {6636044, 10381432, 15741790, 2914241, 5785600, 264923, 4550291},
1349
+ {13512827, 8449886, 5647959, 1135556, 5489843, 7177356, 8002203},
1350
+ {unpack(ONE)}
1351
+ }
1352
+ local O = {
1353
+ {unpack(ZERO)},
1354
+ {unpack(ONE)},
1355
+ {unpack(ONE)}
1356
+ }
1357
+
1358
+ -- Projective Coordinates for Edwards curves for point addition/doubling.
1359
+ -- Points are represented as: (X:Y:Z) where x = X/Z and y = Y/Z
1360
+ -- The identity element is represented by (0:1:1)
1361
+ -- Point operation formulas are available on the EFD:
1362
+ -- https://www.hyperelliptic.org/EFD/g1p/auto-edwards-projective.html
1363
+ local function pointDouble(P1)
1364
+ -- 3M + 4S
1365
+ local X1, Y1, Z1 = unpack(P1)
1366
+
1367
+ local b = addModP(X1, Y1)
1368
+ local B = squareModP(b)
1369
+ local C = squareModP(X1)
1370
+ local D = squareModP(Y1)
1371
+ local E = addModP(C, D)
1372
+ local H = squareModP(Z1)
1373
+ local J = subModP(E, addModP(H, H))
1374
+ local X3 = multModP(subModP(B, E), J)
1375
+ local Y3 = multModP(E, subModP(C, D))
1376
+ local Z3 = multModP(E, J)
1377
+ local P3 = {X3, Y3, Z3}
1378
+
1379
+ return setmetatable(P3, pointMT)
1380
+ end
1381
+
1382
+ local function pointAdd(P1, P2)
1383
+ -- 10M + 1S
1384
+ local X1, Y1, Z1 = unpack(P1)
1385
+ local X2, Y2, Z2 = unpack(P2)
1386
+
1387
+ local A = multModP(Z1, Z2)
1388
+ local B = squareModP(A)
1389
+ local C = multModP(X1, X2)
1390
+ local D = multModP(Y1, Y2)
1391
+ local E = multModP(d, multModP(C, D))
1392
+ local F = subModP(B, E)
1393
+ local G = addModP(B, E)
1394
+ local X3 = multModP(A, multModP(F, subModP(multModP(addModP(X1, Y1), addModP(X2, Y2)), addModP(C, D))))
1395
+ local Y3 = multModP(A, multModP(G, subModP(D, C)))
1396
+ local Z3 = multModP(F, G)
1397
+ local P3 = {X3, Y3, Z3}
1398
+
1399
+ return setmetatable(P3, pointMT)
1400
+ end
1401
+
1402
+ local function pointNeg(P1)
1403
+ local X1, Y1, Z1 = unpack(P1)
1404
+
1405
+ local X3 = subModP(ZERO, X1)
1406
+ local Y3 = {unpack(Y1)}
1407
+ local Z3 = {unpack(Z1)}
1408
+ local P3 = {X3, Y3, Z3}
1409
+
1410
+ return setmetatable(P3, pointMT)
1411
+ end
1412
+
1413
+ local function pointSub(P1, P2)
1414
+ return pointAdd(P1, pointNeg(P2))
1415
+ end
1416
+
1417
+ -- Converts (X:Y:Z) into (X:Y:1) = (x:y:1)
1418
+ local function pointScale(P1)
1419
+ local X1, Y1, Z1 = unpack(P1)
1420
+
1421
+ local A = expModP(Z1, pMinusTwoBinary)
1422
+ local X3 = multModP(X1, A)
1423
+ local Y3 = multModP(Y1, A)
1424
+ local Z3 = {unpack(ONE)}
1425
+ local P3 = {X3, Y3, Z3}
1426
+
1427
+ return setmetatable(P3, pointMT)
1428
+ end
1429
+
1430
+ local function pointIsEqual(P1, P2)
1431
+ local X1, Y1, Z1 = unpack(P1)
1432
+ local X2, Y2, Z2 = unpack(P2)
1433
+
1434
+ local A1 = multModP(X1, Z2)
1435
+ local B1 = multModP(Y1, Z2)
1436
+ local A2 = multModP(X2, Z1)
1437
+ local B2 = multModP(Y2, Z1)
1438
+
1439
+ return isEqual(A1, A2) and isEqual(B1, B2)
1440
+ end
1441
+
1442
+ -- Checks if a projective point satisfies the curve equation
1443
+ local function pointIsOnCurve(P1)
1444
+ local X1, Y1, Z1 = unpack(P1)
1445
+
1446
+ local X12 = squareModP(X1)
1447
+ local Y12 = squareModP(Y1)
1448
+ local Z12 = squareModP(Z1)
1449
+ local Z14 = squareModP(Z12)
1450
+ local a = addModP(X12, Y12)
1451
+ a = multModP(a, Z12)
1452
+ local b = multModP(d, multModP(X12, Y12))
1453
+ b = addModP(Z14, b)
1454
+
1455
+ return isEqual(a, b)
1456
+ end
1457
+
1458
+ local function pointIsInf(P1)
1459
+ return isEqual(P1[1], ZERO)
1460
+ end
1461
+
1462
+ -- W-ary Non-Adjacent Form (wNAF) method for scalar multiplication:
1463
+ -- https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#w-ary_non-adjacent_form_(wNAF)_method
1464
+ local function scalarMult(multiplier, P1)
1465
+ -- w = 5
1466
+ local naf = NAF(multiplier, 5)
1467
+ local PTable = {P1}
1468
+ local P2 = pointDouble(P1)
1469
+ local Q = {{unpack(ZERO)}, {unpack(ONE)}, {unpack(ONE)}}
1470
+
1471
+ for i = 3, 31, 2 do
1472
+ PTable[i] = pointAdd(PTable[i - 2], P2)
1473
+ end
1474
+
1475
+ for i = #naf, 1, -1 do
1476
+ Q = pointDouble(Q)
1477
+ if naf[i] > 0 then
1478
+ Q = pointAdd(Q, PTable[naf[i]])
1479
+ elseif naf[i] < 0 then
1480
+ Q = pointSub(Q, PTable[-naf[i]])
1481
+ end
1482
+ end
1483
+
1484
+ return setmetatable(Q, pointMT)
1485
+ end
1486
+
1487
+ -- Lookup table 4-ary NAF method for scalar multiplication by G.
1488
+ -- Precomputations for the regular NAF method are done before the multiplication.
1489
+ local GTable = {G}
1490
+ for i = 2, 168 do
1491
+ GTable[i] = pointDouble(GTable[i - 1])
1492
+ end
1493
+
1494
+ local function scalarMultG(multiplier)
1495
+ local naf = NAF(multiplier, 2)
1496
+ local Q = {{unpack(ZERO)}, {unpack(ONE)}, {unpack(ONE)}}
1497
+
1498
+ for i = 1, 168 do
1499
+ if naf[i] == 1 then
1500
+ Q = pointAdd(Q, GTable[i])
1501
+ elseif naf[i] == -1 then
1502
+ Q = pointSub(Q, GTable[i])
1503
+ end
1504
+ end
1505
+
1506
+ return setmetatable(Q, pointMT)
1507
+ end
1508
+
1509
+ -- Point compression and encoding.
1510
+ -- Compresses curve points to 22 bytes.
1511
+ local function pointEncode(P1)
1512
+ P1 = pointScale(P1)
1513
+ local result = {}
1514
+ local x, y = unpack(P1)
1515
+
1516
+ -- Encode y
1517
+ result = encodeInt(y)
1518
+ -- Encode one bit from x
1519
+ result[22] = x[1] % 2
1520
+
1521
+ return setmetatable(result, byteTableMT)
1522
+ end
1523
+
1524
+ local function pointDecode(enc)
1525
+ enc = type(enc) == "table" and {unpack(enc, 1, 22)} or {tostring(enc):byte(1, 22)}
1526
+ -- Decode y
1527
+ local y = decodeInt(enc)
1528
+ y[7] = y[7] % p[7]
1529
+ -- Find {x, -x} using curve equation
1530
+ local y2 = squareModP(y)
1531
+ local u = subModP(y2, ONE)
1532
+ local v = subModP(multModP(d, y2), ONE)
1533
+ local u2 = squareModP(u)
1534
+ local u3 = multModP(u, u2)
1535
+ local u5 = multModP(u3, u2)
1536
+ local v3 = multModP(v, squareModP(v))
1537
+ local w = multModP(u5, v3)
1538
+ local x = multModP(u3, multModP(v, expModP(w, pMinusThreeOverFourBinary)))
1539
+ -- Use enc[22] to find x from {x, -x}
1540
+ if x[1] % 2 ~= enc[22] then
1541
+ x = subModP(ZERO, x)
1542
+ end
1543
+ local P3 = {x, y, {unpack(ONE)}}
1544
+
1545
+ return setmetatable(P3, pointMT)
1546
+ end
1547
+
1548
+ pointMT = {
1549
+ __index = {
1550
+ isOnCurve = function(self)
1551
+ return pointIsOnCurve(self)
1552
+ end,
1553
+
1554
+ isInf = function(self)
1555
+ return self:isOnCurve() and pointIsInf(self)
1556
+ end,
1557
+
1558
+ encode = function(self)
1559
+ return pointEncode(self)
1560
+ end
1561
+ },
1562
+
1563
+ __tostring = function(self)
1564
+ return self:encode():toHex()
1565
+ end,
1566
+
1567
+ __add = function(P1, P2)
1568
+ assert(P1:isOnCurve(), "invalid point")
1569
+ assert(P2:isOnCurve(), "invalid point")
1570
+
1571
+ return pointAdd(P1, P2)
1572
+ end,
1573
+
1574
+ __sub = function(P1, P2)
1575
+ assert(P1:isOnCurve(), "invalid point")
1576
+ assert(P2:isOnCurve(), "invalid point")
1577
+
1578
+ return pointSub(P1, P2)
1579
+ end,
1580
+
1581
+ __unm = function(self)
1582
+ assert(self:isOnCurve(), "invalid point")
1583
+
1584
+ return pointNeg(self)
1585
+ end,
1586
+
1587
+ __eq = function(P1, P2)
1588
+ assert(P1:isOnCurve(), "invalid point")
1589
+ assert(P2:isOnCurve(), "invalid point")
1590
+
1591
+ return pointIsEqual(P1, P2)
1592
+ end,
1593
+
1594
+ __mul = function(P1, s)
1595
+ if type(P1) == "number" then
1596
+ return s * P1
1597
+ end
1598
+
1599
+ if type(s) == "number" then
1600
+ assert(s < 2^24, "number multiplier too big")
1601
+ s = {s, 0, 0, 0, 0, 0, 0}
1602
+ else
1603
+ s = inverseMontgomeryModQ(s)
1604
+ end
1605
+
1606
+ if P1 == G then
1607
+ return scalarMultG(s)
1608
+ else
1609
+ return scalarMult(s, P1)
1610
+ end
1611
+ end
1612
+ }
1613
+
1614
+ G = setmetatable(G, pointMT)
1615
+ O = setmetatable(O, pointMT)
1616
+
1617
+ return {
1618
+ G = G,
1619
+ O = O,
1620
+ pointDecode = pointDecode
1621
+ }
1622
+ end)()
1623
+
1624
+ local function getNonceFromEpoch()
1625
+ local nonce = {}
1626
+ local epoch = math.ceil(((os.clock() - clock) + time) * 1000)
1627
+ for i = 1, 12 do
1628
+ nonce[#nonce + 1] = epoch % 256
1629
+ epoch = epoch / 256
1630
+ epoch = epoch - epoch % 1
1631
+ end
1632
+
1633
+ return nonce
1634
+ end
1635
+
1636
+ local function encrypt(data, key)
1637
+ local encKey = sha256.hmac("encKey", key)
1638
+ local macKey = sha256.hmac("macKey", key)
1639
+ local nonce = getNonceFromEpoch()
1640
+ local ciphertext = chacha20.crypt(data, encKey, nonce)
1641
+ local result = nonce
1642
+ for i = 1, #ciphertext do
1643
+ result[#result + 1] = ciphertext[i]
1644
+ end
1645
+ local mac = sha256.hmac(result, macKey)
1646
+ for i = 1, #mac do
1647
+ result[#result + 1] = mac[i]
1648
+ end
1649
+
1650
+ return setmetatable(result, byteTableMT)
1651
+ end
1652
+
1653
+ local function decrypt(data, key)
1654
+ local data = type(data) == "table" and {unpack(data)} or {tostring(data):byte(1,-1)}
1655
+ local encKey = sha256.hmac("encKey", key)
1656
+ local macKey = sha256.hmac("macKey", key)
1657
+ local mac = sha256.hmac({unpack(data, 1, #data - 32)}, macKey)
1658
+ local messageMac = {unpack(data, #data - 31)}
1659
+ assert(mac:isEqual(messageMac), "invalid mac")
1660
+ local nonce = {unpack(data, 1, 12)}
1661
+ local ciphertext = {unpack(data, 13, #data - 32)}
1662
+ local result = chacha20.crypt(ciphertext, encKey, nonce)
1663
+
1664
+ return setmetatable(result, byteTableMT)
1665
+ end
1666
+
1667
+ local function keypair(seed)
1668
+ local x
1669
+ if seed then
1670
+ x = modq.hashModQ(seed)
1671
+ else
1672
+ x = modq.randomModQ()
1673
+ end
1674
+ local Y = curve.G * x
1675
+
1676
+ local privateKey = x:encode()
1677
+ local publicKey = Y:encode()
1678
+
1679
+ return tostring(privateKey), tostring(publicKey)
1680
+ end
1681
+
1682
+ local function exchange(privateKey, publicKey)
1683
+ local x = modq.decodeModQ(privateKey)
1684
+ local Y = curve.pointDecode(publicKey)
1685
+
1686
+ local Z = Y * x
1687
+
1688
+ local sharedSecret = sha256.digest(Z:encode())
1689
+
1690
+ return sharedSecret
1691
+ end
1692
+
1693
+ local function sign(privateKey, message)
1694
+ local message = type(message) == "table" and string.char(unpack(message)) or tostring(message)
1695
+ local privateKey = type(privateKey) == "table" and string.char(unpack(privateKey)) or tostring(privateKey)
1696
+ local x = modq.decodeModQ(privateKey)
1697
+ local k = modq.randomModQ()
1698
+ local R = curve.G * k
1699
+ local e = modq.hashModQ(message .. tostring(R))
1700
+ local s = k - x * e
1701
+
1702
+ e = e:encode()
1703
+ s = s:encode()
1704
+
1705
+ local result = e
1706
+ for i = 1, #s do
1707
+ result[#result + 1] = s[i]
1708
+ end
1709
+
1710
+ return tostring(setmetatable(result, byteTableMT))
1711
+ end
1712
+
1713
+ local function verify(publicKey, signature, message)
1714
+ local message = type(message) == "table" and string.char(unpack(message)) or tostring(message)
1715
+ local Y = curve.pointDecode(publicKey)
1716
+ local e = modq.decodeModQ({signature:byte(1, #signature / 2)})
1717
+ local s = modq.decodeModQ({signature:byte(#signature / 2 + 1, -1)})
1718
+ local Rv = curve.G * s + Y * e
1719
+ local ev = modq.hashModQ(message .. tostring(Rv))
1720
+
1721
+ return ev == e
1722
+ end
1723
+
1724
+ return {
1725
+ chacha20 = chacha20,
1726
+ sha256 = sha256,
1727
+ random = random,
1728
+ encrypt = encrypt,
1729
+ decrypt = decrypt,
1730
+ keypair = keypair,
1731
+ exchange = exchange,
1732
+ sign = sign,
1733
+ verify = verify
1734
+ }