diff --git a/main.c b/main.c index f2ef23d..61a4181 100644 --- a/main.c +++ b/main.c @@ -166,26 +166,28 @@ unsigned __int128 kinai_maradek_tetel(uint64_t *m, uint64_t d, prime_test_t *p, // C1: c^(d mod P-1) mod P uint64_t temp_exponent = d % (p->prime - 1); - uint64_t m_bin_length = 0; - uint64_t *m_as_binary = dec_to_bin(*m, &m_bin_length); - uint64_t c1 = quick_pow(m_as_binary, p->base, p->prime, m_bin_length); + uint64_t exponent_bin_length = 0; + uint64_t *exponent_as_binary = dec_to_bin(temp_exponent, &exponent_bin_length); + uint64_t c1 = quick_pow(exponent_as_binary, *m, p->prime, exponent_bin_length); + free(exponent_as_binary); // C2: c^(d mod Q-1) mod Q temp_exponent = d % (q->prime - 1); - uint64_t c2 = quick_pow(m_as_binary, q->base, q->prime, m_bin_length); - free(m_as_binary); + exponent_as_binary = dec_to_bin(temp_exponent, &exponent_bin_length); + uint64_t c2 = quick_pow(exponent_as_binary, *m, q->prime, exponent_bin_length); + free(exponent_as_binary); euklidian_result_t y = euklidian_algorigthm_extended(Mp, Mq); // in the struct the x will mean the y1 and y will mean the y2 - // if either of them is less then 0 shift them into postive range with fi_n - while (y.x < 0) { - y.x += p->prime; - } - while (y.y < 0) { - y.y += q->prime; - } + // if either of them is less a negative number shift them into postive range with with hte modulo + y.x %= p->prime; + y.x += p->prime; + y.y %= q->prime; + y.y += q->prime; - return (c1 * y.x * Mp) + (c2 * y.y * Mq) % M; + unsigned __int128 s1 = (c1 * y.x * Mp) % M; + unsigned __int128 s2 = (c2 * y.y * Mq) % M; + return (s1 + s2) % M; } unsigned __int128 rsa_encrypt(uint64_t *m, prime_test_t *p, prime_test_t *q) { @@ -206,14 +208,12 @@ unsigned __int128 rsa_encrypt(uint64_t *m, prime_test_t *p, prime_test_t *q) { } while (e <= 1 && e >= fi_n && prime_test(e, p->base)); // the p and q base is used everywhere anyways, i wont pass in another arg euklidian_result_t calc_d = euklidian_algorigthm_extended(fi_n, e); - // if either of them is less then 0 shift them into postive range with fi_n - while (calc_d.x < 0) { - calc_d.x += fi_n; - } - while (calc_d.y < 0) { - calc_d.y += fi_n; - } - __int128 d = calc_d.y; + + // if either of them is less a negative number shift them into postive range with with hte modulo + calc_d.x %= fi_n; + calc_d.y %= fi_n; + + unsigned __int128 d = calc_d.y; uint64_t length = 0; uint64_t *nyenye = dec_to_bin(e, &length);