GCD.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
  3. * Copyright (c) 2020-2021, Dex♪ <dexes.ttp@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "UnsignedBigIntegerAlgorithms.h"
  8. namespace Crypto {
  9. void UnsignedBigIntegerAlgorithms::destructive_GCD_without_allocation(
  10. UnsignedBigInteger& temp_a,
  11. UnsignedBigInteger& temp_b,
  12. UnsignedBigInteger& temp_1,
  13. UnsignedBigInteger& temp_2,
  14. UnsignedBigInteger& temp_3,
  15. UnsignedBigInteger& temp_4,
  16. UnsignedBigInteger& temp_quotient,
  17. UnsignedBigInteger& temp_remainder,
  18. UnsignedBigInteger& output)
  19. {
  20. for (;;) {
  21. if (temp_a == 0) {
  22. output.set_to(temp_b);
  23. return;
  24. }
  25. // temp_b %= temp_a
  26. divide_without_allocation(temp_b, temp_a, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  27. temp_b.set_to(temp_remainder);
  28. if (temp_b == 0) {
  29. output.set_to(temp_a);
  30. return;
  31. }
  32. // temp_a %= temp_b
  33. divide_without_allocation(temp_a, temp_b, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  34. temp_a.set_to(temp_remainder);
  35. }
  36. }
  37. }