|
1 | | -// Copyright (c) 2020-2022 Jeffrey Hurchalla. |
| 1 | +// Copyright (c) 2020-2025 Jeffrey Hurchalla. |
2 | 2 | /* |
3 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
4 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
15 | 15 | #include <cassert> |
16 | 16 | #include <cstdint> |
17 | 17 |
|
18 | | -#ifndef NDEBUG |
19 | | -// remove this if you want to allow asserts |
20 | | -// (they're very good for testing and debugging but may drastically slow down |
21 | | -// the library). |
22 | | -#error "Performance warning: asserts are enabled and will slow performance" |
23 | | -#endif |
24 | 18 |
|
25 | 19 | int main() |
26 | 20 | { |
27 | 21 | namespace hc = ::hurchalla; |
28 | | - int64_t modulus = 333333333; |
29 | | - int64_t base = 42; |
30 | | - int64_t exponent = 123456789; |
| 22 | + |
| 23 | + // you could use any integer type that the compiler supports |
| 24 | + // (including __uint128_t) |
| 25 | + using T = uint64_t; |
| 26 | + |
| 27 | + T modulus = 333333333; |
| 28 | + T base = 42; |
| 29 | + T exponent = 123456789; |
31 | 30 |
|
32 | 31 | // ---- Demonstration of modular exponentiation ---- |
33 | 32 |
|
34 | 33 | // Montgomery arithmetic version: |
35 | 34 | assert(modulus % 2 == 1); // montgomery arithmetic always needs odd modulus. |
36 | | - // first construct a MontgomeryForm object to do Montgomery arithmetic |
37 | | - // with a particular modulus. |
38 | | - hc::MontgomeryForm<int64_t> mf(modulus); |
39 | | - // convert base to its Montgomery representation. |
40 | | - auto base_montval = mf.convertIn(base); |
41 | | - // get the pow result in Montgomery representation. |
42 | | - auto result_montval = mf.pow(base_montval, exponent); |
43 | | - // convert the Montgomery representation result to normal integer domain. |
44 | | - int64_t result1 = mf.convertOut(result_montval); |
| 35 | + // First construct a MontgomeryForm object to do Montgomery arithmetic |
| 36 | + // with the modulus we chose. |
| 37 | + hc::MontgomeryForm<T> mf(modulus); |
| 38 | + // Convert base to its Montgomery representation. |
| 39 | + auto mont_base = mf.convertIn(base); |
| 40 | + // Get the pow result in Montgomery representation. |
| 41 | + auto mont_result = mf.pow(mont_base, exponent); |
| 42 | + // Convert the Montgomery representation result to normal integer domain. |
| 43 | + T result1 = mf.convertOut(mont_result); |
45 | 44 |
|
46 | 45 |
|
47 | 46 | // Standard arithmetic version: (note that Montgomery arithmetic is |
48 | | - // typically faster, and that modular_pow() requires an unsigned type) |
49 | | - uint64_t result2 = hc::modular_pow(static_cast<uint64_t>(base), |
50 | | - static_cast<uint64_t>(exponent), |
51 | | - static_cast<uint64_t>(modulus)); |
| 47 | + // usually much faster) |
| 48 | + T result2 = hc::modular_pow(base, exponent, modulus); |
52 | 49 |
|
53 | 50 |
|
54 | 51 | std::cout << "Example results for " << base << "^" << exponent |
|
0 commit comments