Remove junk, add testing and assertions
This commit is contained in:
parent
7d20e7f009
commit
bfcbb77570
1 changed files with 22 additions and 58 deletions
80
main.c
80
main.c
|
@ -8,6 +8,7 @@
|
|||
|
||||
#define LED_PIN PD6
|
||||
#define RANDOM
|
||||
#define W 16
|
||||
|
||||
void exit_blink() {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
|
@ -60,88 +61,51 @@ int main() {
|
|||
|
||||
enter_blink();
|
||||
|
||||
#ifdef RANDOM
|
||||
#define W 16
|
||||
const int64_t p = gen_prime(1 << (W - 1), 1 << W);
|
||||
test_mulmod();
|
||||
test_modexp();
|
||||
|
||||
int64_t qprev = p;
|
||||
const u64 p = gen_prime(1 << (W - 1), 1 << W);
|
||||
printf("P: %u\n", (u32)p);
|
||||
|
||||
u64 qprev = p;
|
||||
while (p == qprev) qprev = gen_prime(1 << (W - 1), 1 << W);
|
||||
|
||||
const i64 q = qprev;
|
||||
#undef W
|
||||
#else
|
||||
int64_t p = 56857;
|
||||
int64_t q = 47963;
|
||||
#endif
|
||||
const u64 q = qprev;
|
||||
printf("Q: %u\n", (u32)q);
|
||||
|
||||
int64_t n = p * q;
|
||||
int64_t phi_n = (p - 1) * (q - 1);
|
||||
ASSERT(gcd(p - 1, PUBEXP) == 1);
|
||||
ASSERT(gcd(q - 1, PUBEXP) == 1);
|
||||
|
||||
// 'e' is public. E for encrypt.
|
||||
int64_t e = 0;
|
||||
do {
|
||||
e = prand_range(3, phi_n - 1);
|
||||
} while (gcd(e, phi_n) != 1);
|
||||
u64 n = p * q;
|
||||
printf("N: %u\n", (u32)n);
|
||||
|
||||
// 'd' is our private key. D as in decrypt
|
||||
int64_t d = mod_inverse(e, phi_n);
|
||||
u64 phi_n = (p - 1) * (q - 1);
|
||||
printf("Phi_N: %u\n", (u32)phi_n);
|
||||
|
||||
u64 d = mod_inverse(PUBEXP, phi_n);
|
||||
printf("D: %u\n", (u32)d);
|
||||
if (d == 0 || d == 1) {
|
||||
printf("Modular inverse not found...");
|
||||
while (1);
|
||||
}
|
||||
|
||||
{
|
||||
char test = 'o';
|
||||
u64 enc = modexp(test, e, n);
|
||||
char dec = (char)modexp(enc, d, n);
|
||||
|
||||
if (dec != test) {
|
||||
printf("ERROR: %c != %c => %d != %d\n", test, dec, test, dec);
|
||||
// while (1);
|
||||
}
|
||||
}
|
||||
{
|
||||
char test = 'c';
|
||||
|
||||
u64 p = 3, q = 11;
|
||||
u64 n = p * q;
|
||||
u64 e = 7;
|
||||
u64 d = 3;
|
||||
u64 enc = modexp(test, e, n);
|
||||
char dec = (char)modexp(enc, d, n);
|
||||
|
||||
if (dec != test) {
|
||||
printf("ERROR: %c != %c => %d != %d\n", test, dec, test, dec);
|
||||
} else
|
||||
printf("INFO: %c == %c => %d == %d\n", test, dec, test, dec);
|
||||
}
|
||||
ASSERT_EQ(mulmod(PUBEXP, d, phi_n), 1);
|
||||
|
||||
char msg[] = "Hello";
|
||||
int64_t coded[sizeof(msg)] = {0};
|
||||
u64 coded[sizeof(msg)] = {0};
|
||||
char decoded[sizeof(msg)] = {0};
|
||||
|
||||
// Encode the message
|
||||
for (int i = 0; i < strlen(msg); i++) {
|
||||
coded[i] = modexp((int64_t)msg[i], e, n);
|
||||
coded[i] = modexp((u64)msg[i], PUBEXP, n);
|
||||
}
|
||||
|
||||
// Decode the message
|
||||
for (int i = 0; i < strlen(msg); i++) {
|
||||
int64_t dec = modexp(coded[i], d, n);
|
||||
u64 dec = modexp(coded[i], d, n);
|
||||
decoded[i] = dec & 0xFF;
|
||||
}
|
||||
|
||||
test_mulmod();
|
||||
test_modexp();
|
||||
|
||||
{
|
||||
printf("P: %u\n", (uint32_t)p);
|
||||
printf("Q: %u\n", (uint32_t)q);
|
||||
printf("N: %u\n", (uint32_t)n);
|
||||
printf("Phi_N: %u\n", (uint32_t)phi_n);
|
||||
printf("Pubkey (e): %u\n", (uint32_t)e);
|
||||
printf("Privkey (d): %u\n", (uint32_t)d);
|
||||
|
||||
printf("Message: %s\n", msg);
|
||||
printf("Decoded: %s\n", decoded);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue