ModularFunctions.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <ali.mpfard@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  28. //#define NT_DEBUG
  29. namespace Crypto {
  30. namespace NumberTheory {
  31. static auto ModularInverse(const UnsignedBigInteger& a_, const UnsignedBigInteger& b) -> UnsignedBigInteger
  32. {
  33. if (b == 1)
  34. return { 1 };
  35. UnsignedBigInteger one { 1 };
  36. UnsignedBigInteger temp_1;
  37. UnsignedBigInteger temp_2;
  38. UnsignedBigInteger temp_3;
  39. UnsignedBigInteger temp_4;
  40. UnsignedBigInteger temp_plus;
  41. UnsignedBigInteger temp_minus;
  42. UnsignedBigInteger temp_quotient;
  43. UnsignedBigInteger temp_remainder;
  44. UnsignedBigInteger d;
  45. auto a = a_;
  46. auto u = a;
  47. if (a.words()[0] % 2 == 0) {
  48. // u += b
  49. UnsignedBigInteger::add_without_allocation(u, b, temp_plus);
  50. u.set_to(temp_plus);
  51. }
  52. auto v = b;
  53. UnsignedBigInteger x { 0 };
  54. // d = b - 1
  55. UnsignedBigInteger::subtract_without_allocation(b, one, d);
  56. while (!(v == 1)) {
  57. while (v < u) {
  58. // u -= v
  59. UnsignedBigInteger::subtract_without_allocation(u, v, temp_minus);
  60. u.set_to(temp_minus);
  61. // d += x
  62. UnsignedBigInteger::add_without_allocation(d, x, temp_plus);
  63. d.set_to(temp_plus);
  64. while (u.words()[0] % 2 == 0) {
  65. if (d.words()[0] % 2 == 1) {
  66. // d += b
  67. UnsignedBigInteger::add_without_allocation(d, b, temp_plus);
  68. d.set_to(temp_plus);
  69. }
  70. // u /= 2
  71. UnsignedBigInteger::divide_u16_without_allocation(u, 2, temp_quotient, temp_remainder);
  72. u.set_to(temp_quotient);
  73. // d /= 2
  74. UnsignedBigInteger::divide_u16_without_allocation(d, 2, temp_quotient, temp_remainder);
  75. d.set_to(temp_quotient);
  76. }
  77. }
  78. // v -= u
  79. UnsignedBigInteger::subtract_without_allocation(v, u, temp_minus);
  80. v.set_to(temp_minus);
  81. // x += d
  82. UnsignedBigInteger::add_without_allocation(x, d, temp_plus);
  83. x.set_to(temp_plus);
  84. while (v.words()[0] % 2 == 0) {
  85. if (x.words()[0] % 2 == 1) {
  86. // x += b
  87. UnsignedBigInteger::add_without_allocation(x, b, temp_plus);
  88. x.set_to(temp_plus);
  89. }
  90. // v /= 2
  91. UnsignedBigInteger::divide_u16_without_allocation(v, 2, temp_quotient, temp_remainder);
  92. v.set_to(temp_quotient);
  93. // x /= 2
  94. UnsignedBigInteger::divide_u16_without_allocation(x, 2, temp_quotient, temp_remainder);
  95. x.set_to(temp_quotient);
  96. }
  97. }
  98. // x % b
  99. UnsignedBigInteger::divide_without_allocation(x, b, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  100. return temp_remainder;
  101. }
  102. static auto ModularPower(const UnsignedBigInteger& b, const UnsignedBigInteger& e, const UnsignedBigInteger& m) -> UnsignedBigInteger
  103. {
  104. if (m == 1)
  105. return 0;
  106. UnsignedBigInteger ep { e };
  107. UnsignedBigInteger base { b };
  108. UnsignedBigInteger exp { 1 };
  109. UnsignedBigInteger temp_1;
  110. UnsignedBigInteger temp_2;
  111. UnsignedBigInteger temp_3;
  112. UnsignedBigInteger temp_4;
  113. UnsignedBigInteger temp_multiply;
  114. UnsignedBigInteger temp_quotient;
  115. UnsignedBigInteger temp_remainder;
  116. while (!(ep < 1)) {
  117. #ifdef NT_DEBUG
  118. dbg() << ep.to_base10();
  119. #endif
  120. if (ep.words()[0] % 2 == 1) {
  121. // exp = (exp * base) % m;
  122. UnsignedBigInteger::multiply_without_allocation(exp, base, temp_1, temp_2, temp_3, temp_4, temp_multiply);
  123. UnsignedBigInteger::divide_without_allocation(temp_multiply, m, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  124. exp.set_to(temp_remainder);
  125. }
  126. // ep = ep / 2;
  127. UnsignedBigInteger::divide_u16_without_allocation(ep, 2, temp_quotient, temp_remainder);
  128. ep.set_to(temp_quotient);
  129. // base = (base * base) % m;
  130. UnsignedBigInteger::multiply_without_allocation(base, base, temp_1, temp_2, temp_3, temp_4, temp_multiply);
  131. UnsignedBigInteger::divide_without_allocation(temp_multiply, m, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  132. base.set_to(temp_remainder);
  133. }
  134. return exp;
  135. }
  136. static void GCD_without_allocation(
  137. const UnsignedBigInteger& a,
  138. const UnsignedBigInteger& b,
  139. UnsignedBigInteger& temp_a,
  140. UnsignedBigInteger& temp_b,
  141. UnsignedBigInteger& temp_1,
  142. UnsignedBigInteger& temp_2,
  143. UnsignedBigInteger& temp_3,
  144. UnsignedBigInteger& temp_4,
  145. UnsignedBigInteger& temp_quotient,
  146. UnsignedBigInteger& temp_remainder,
  147. UnsignedBigInteger& output)
  148. {
  149. temp_a.set_to(a);
  150. temp_b.set_to(b);
  151. for (;;) {
  152. if (temp_a == 0) {
  153. output.set_to(temp_b);
  154. return;
  155. }
  156. // temp_b %= temp_a
  157. UnsignedBigInteger::divide_without_allocation(temp_b, temp_a, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  158. temp_b.set_to(temp_remainder);
  159. if (temp_b == 0) {
  160. output.set_to(temp_a);
  161. return;
  162. }
  163. // temp_a %= temp_b
  164. UnsignedBigInteger::divide_without_allocation(temp_a, temp_b, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  165. temp_a.set_to(temp_remainder);
  166. }
  167. }
  168. static UnsignedBigInteger GCD(const UnsignedBigInteger& a, const UnsignedBigInteger& b)
  169. {
  170. UnsignedBigInteger temp_a;
  171. UnsignedBigInteger temp_b;
  172. UnsignedBigInteger temp_1;
  173. UnsignedBigInteger temp_2;
  174. UnsignedBigInteger temp_3;
  175. UnsignedBigInteger temp_4;
  176. UnsignedBigInteger temp_quotient;
  177. UnsignedBigInteger temp_remainder;
  178. UnsignedBigInteger output;
  179. GCD_without_allocation(a, b, temp_a, temp_b, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder, output);
  180. return output;
  181. }
  182. static auto LCM(const UnsignedBigInteger& a, const UnsignedBigInteger& b) -> UnsignedBigInteger
  183. {
  184. UnsignedBigInteger temp_a;
  185. UnsignedBigInteger temp_b;
  186. UnsignedBigInteger temp_1;
  187. UnsignedBigInteger temp_2;
  188. UnsignedBigInteger temp_3;
  189. UnsignedBigInteger temp_4;
  190. UnsignedBigInteger temp_quotient;
  191. UnsignedBigInteger temp_remainder;
  192. UnsignedBigInteger gcd_output;
  193. UnsignedBigInteger output { 0 };
  194. GCD_without_allocation(a, b, temp_a, temp_b, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder, gcd_output);
  195. if (gcd_output == 0) {
  196. #ifdef NT_DEBUG
  197. dbg() << "GCD is zero";
  198. #endif
  199. return output;
  200. }
  201. // output = (a / gcd_output) * b
  202. UnsignedBigInteger::divide_without_allocation(a, gcd_output, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  203. UnsignedBigInteger::multiply_without_allocation(temp_quotient, b, temp_1, temp_2, temp_3, temp_4, output);
  204. #ifdef NT_DEBUG
  205. dbg() << "quot: " << temp_quotient << " rem: " << temp_remainder << " out: " << output;
  206. #endif
  207. return output;
  208. }
  209. template<size_t test_count>
  210. static bool MR_primality_test(UnsignedBigInteger n, const Vector<UnsignedBigInteger, test_count>& tests)
  211. {
  212. auto prev = n.minus({ 1 });
  213. auto b = prev;
  214. auto r = 0;
  215. auto div_result = b.divided_by(2);
  216. while (div_result.quotient == 0) {
  217. div_result = b.divided_by(2);
  218. b = div_result.quotient;
  219. ++r;
  220. }
  221. for (size_t i = 0; i < tests.size(); ++i) {
  222. auto return_ = true;
  223. if (n < tests[i])
  224. continue;
  225. auto x = ModularPower(tests[i], b, n);
  226. if (x == 1 || x == prev)
  227. continue;
  228. for (auto d = r - 1; d != 0; --d) {
  229. x = ModularPower(x, 2, n);
  230. if (x == 1)
  231. return false;
  232. if (x == prev) {
  233. return_ = false;
  234. break;
  235. }
  236. }
  237. if (return_)
  238. return false;
  239. }
  240. return true;
  241. }
  242. static UnsignedBigInteger random_number(const UnsignedBigInteger& min, const UnsignedBigInteger& max)
  243. {
  244. ASSERT(min < max);
  245. auto range = max.minus(min);
  246. UnsignedBigInteger base;
  247. // FIXME: Need a cryptographically secure rng
  248. auto size = range.trimmed_length() * sizeof(u32);
  249. u8 buf[size];
  250. arc4random_buf(buf, size);
  251. Vector<u32> vec;
  252. for (size_t i = 0; i < size / sizeof(u32); ++i) {
  253. vec.append(*(u32*)buf + i);
  254. }
  255. UnsignedBigInteger offset { move(vec) };
  256. return offset.plus(min);
  257. }
  258. static bool is_probably_prime(const UnsignedBigInteger& p)
  259. {
  260. if (p == 2 || p == 3 || p == 5)
  261. return true;
  262. if (p < 49)
  263. return true;
  264. Vector<UnsignedBigInteger, 256> tests;
  265. UnsignedBigInteger seven { 7 };
  266. for (size_t i = 0; i < tests.size(); ++i)
  267. tests.append(random_number(seven, p.minus(2)));
  268. return MR_primality_test(p, tests);
  269. }
  270. static UnsignedBigInteger random_big_prime(size_t bits)
  271. {
  272. ASSERT(bits >= 33);
  273. UnsignedBigInteger min = UnsignedBigInteger::from_base10("6074001000").shift_left(bits - 33);
  274. UnsignedBigInteger max = UnsignedBigInteger { 1 }.shift_left(bits).minus(1);
  275. for (;;) {
  276. auto p = random_number(min, max);
  277. if (is_probably_prime(p))
  278. return p;
  279. }
  280. }
  281. }
  282. }