cleaned up logs, added 128bit logger started working on second phase of rsa: keygen
This commit is contained in:
25
helper.c
Normal file
25
helper.c
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void print_uint128(unsigned __int128 n) {
|
||||||
|
if (n == 0) {
|
||||||
|
printf("0\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A 128-bit number can have up to 39 decimal digits
|
||||||
|
char buffer[40];
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
// Extract digits from right to left
|
||||||
|
while (n > 0) {
|
||||||
|
buffer[i++] = (n % 10) + '0'; // Convert math digit to character
|
||||||
|
n /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print them in reverse order (left to right)
|
||||||
|
while (i > 0) {
|
||||||
|
putchar(buffer[--i]);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
32
main.c
32
main.c
@@ -4,6 +4,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "helper.c"
|
||||||
|
|
||||||
uint64_t *dec_to_bin(uint64_t d, uint64_t *length) {
|
uint64_t *dec_to_bin(uint64_t d, uint64_t *length) {
|
||||||
uint64_t *binary_form = calloc(100, sizeof(uint64_t));
|
uint64_t *binary_form = calloc(100, sizeof(uint64_t));
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@@ -15,11 +17,6 @@ uint64_t *dec_to_bin(uint64_t d, uint64_t *length) {
|
|||||||
|
|
||||||
*length = index;
|
*length = index;
|
||||||
|
|
||||||
for (int i = index; i >= 0; i--) {
|
|
||||||
printf("%ju", binary_form[i]);
|
|
||||||
}
|
|
||||||
printf(" index: %d\n", index);
|
|
||||||
|
|
||||||
return binary_form;
|
return binary_form;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,10 +63,7 @@ bool prime_test(uint64_t n, int a) {
|
|||||||
while (d % 2 == 0) {
|
while (d % 2 == 0) {
|
||||||
d = d / 2;
|
d = d / 2;
|
||||||
S++;
|
S++;
|
||||||
printf("%ju ", d);
|
|
||||||
}
|
}
|
||||||
printf("S: %ju", S);
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
uint64_t r = S - 1; // this stores the number of elements from 0 to S-1
|
uint64_t r = S - 1; // this stores the number of elements from 0 to S-1
|
||||||
|
|
||||||
@@ -81,7 +75,6 @@ bool prime_test(uint64_t n, int a) {
|
|||||||
|
|
||||||
if ((first_qp_res = quick_pow(d_binary, a, n, length)) == 1) {
|
if ((first_qp_res = quick_pow(d_binary, a, n, length)) == 1) {
|
||||||
free(d_binary);
|
free(d_binary);
|
||||||
printf("true\n");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +91,6 @@ bool prime_test(uint64_t n, int a) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\nfalse\n\n");
|
|
||||||
free(d_binary);
|
free(d_binary);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -111,5 +103,25 @@ int main() {
|
|||||||
prime_test(661, 2);
|
prime_test(661, 2);
|
||||||
prime_test(18446744073709551557UL, 2);
|
prime_test(18446744073709551557UL, 2);
|
||||||
prime_test(18446744073709551533UL, 3);
|
prime_test(18446744073709551533UL, 3);
|
||||||
|
|
||||||
|
uint64_t p = 11;
|
||||||
|
uint64_t q = 29;
|
||||||
|
|
||||||
|
// probably i should generate instead of asking the user to input
|
||||||
|
if (!prime_test(p, 2) || !prime_test(q, 2)) {
|
||||||
|
printf("given numbers were not primes");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
unsigned __int128 n = p * q;
|
||||||
|
print_uint128(n);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
unsigned __int128 fi_n = (p - 1) * (q - 1);
|
||||||
|
print_uint128(fi_n);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user