Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
test_big_int02.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <iostream>
25
27
28#include <jau/int_types.hpp>
29#include <jau/mp/big_int.hpp>
30
31using namespace jau;
32using namespace jau::mp;
33using namespace jau::int_literals;
34
35static BigInt phi(const BigInt& P, const BigInt& Q) {
36 const BigInt one(BigInt::one());
37 return (P-one)*(Q-one);
38}
39/**
40 * Returns e with `1 < e < Φ(n)`
41 *
42 * e must be co-prime to phi and smaller than phi
43 */
44static BigInt eval_e(BigInt e, const BigInt& phi) {
45 const BigInt one(BigInt::one());
46 while (e < phi && gcd(e, phi) != one ) {
47 ++e;
48 }
49 return e;
50}
51
52TEST_CASE( "MP Big Encryption Test 00", "[big_int_t][arithmetic][math]" ) {
53 std::cout << "big_int mp_word_bits " << std::to_string( mp_word_bits ) << std::endl;
54 // textbook RSA (insecure)
55 {
56 BigInt pub_P(53), pub_Q(59), pub_n(pub_P*pub_Q);
57 BigInt sec_phi = phi(pub_P, pub_Q);
58 BigInt pub_e = eval_e(BigInt(2), sec_phi);
59 std::cout << "Public Key:: P " << pub_P << ", Q " << pub_Q << ", n " << pub_n << ", e " << pub_e << std::endl;
60
61 // Private key (d stands for decrypt)
62 // choosing d such that it satisfies
63 // d*e = 1 + k * totient
64 BigInt sec_k = 2; // an arbitrary constant
65 BigInt sec_d = ( sec_k * sec_phi + 1 ) / pub_e;
66 std::cout << "Private Key:: phi " << sec_phi << ", k " << sec_k << ", d " << sec_d << std::endl;
67
68 // big_int_t clear("0x112233445566778899aabbccddeeff0102030405060708090a0b0c0d0e0f");
69 BigInt clear(1122);
70 std::cout << "clear:: " << clear.to_hex_string(true) << std::endl;
71
72 BigInt cipher = clear.mod_pow(pub_e, pub_n);
73 std::cout << "encrypted:: " << cipher.to_hex_string(true) << std::endl;
74
75 BigInt decrypted = cipher.mod_pow(sec_d, pub_n);
76 std::cout << "decrypted:: " << decrypted.to_hex_string(true) << std::endl;
77
78 REQUIRE( clear == decrypted );
79 }
80}
Arbitrary precision integer type.
Definition: big_int.hpp:43
std::string to_hex_string(bool add_details=false) const noexcept
Definition: big_int.hpp:754
BigInt mod_pow(BigInt e, BigInt m)
Returns (*this)^e % m, or pow(*this, e) % m.
Definition: big_int.hpp:602
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
void clear() noexcept
Clears internal list.
constexpr T gcd(T a, T b) noexcept
Returns the greatest common divisor (GCD) of the two given integer values following Euclid's algorith...
Definition: int_math.hpp:298
constexpr const jau::fraction_i64 one(1l, 1lu)
one is 10^0 or 1/1
big_int_t (this file) (c) 2024 Gothel Software e.K.
Definition: big_int.hpp:26
constexpr const size_t mp_word_bits
Definition: big_int_ops.hpp:41
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition: backtrace.hpp:32
static BigInt phi(const BigInt &P, const BigInt &Q)
TEST_CASE("MP Big Encryption Test 00", "[big_int_t][arithmetic][math]")
static BigInt eval_e(BigInt e, const BigInt &phi)
Returns e with 1 < e < Φ(n)