2019-01-15 18:40:54 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
2019-01-30 16:17:20 +00:00
|
|
|
#include "../csidh/csidh.h"
|
|
|
|
#include "../csidh/rng.h"
|
2019-01-15 18:40:54 +00:00
|
|
|
|
|
|
|
static void u512_print(u512 const *x)
|
|
|
|
{
|
2019-01-17 10:28:56 +00:00
|
|
|
for (size_t i=0; i<8; i++) {
|
|
|
|
printf("0x%016lX,", x->c[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2019-01-25 11:56:38 +00:00
|
|
|
void print_bytes(uint8_t *out, size_t sz) {
|
|
|
|
for(size_t i=0; i<sz; i++) {
|
|
|
|
printf("%02X", out[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2019-01-17 10:28:56 +00:00
|
|
|
static void int_print(const int8_t v[37]) {
|
|
|
|
for (size_t i = 0; i<37; i++)
|
2019-01-25 11:46:53 +00:00
|
|
|
printf("0x%X,", (unsigned char)v[i]);
|
2019-01-15 18:40:54 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2019-01-25 11:56:38 +00:00
|
|
|
// Print after convertion from Montgomery domain
|
2019-01-15 18:40:54 +00:00
|
|
|
static void fp_print(fp const *x)
|
|
|
|
{
|
|
|
|
u512 y;
|
|
|
|
fp_dec(&y, x);
|
|
|
|
u512_print(&y);
|
|
|
|
}
|
|
|
|
|
2019-01-25 11:56:38 +00:00
|
|
|
// Print without converting from Montgomery domain
|
2019-01-25 11:46:53 +00:00
|
|
|
static void fp_print_n(fp const *x) {
|
|
|
|
for (size_t i=0; i<8; i++) {
|
|
|
|
printf("0x%016lX, ", x->x.c[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2019-01-25 11:56:38 +00:00
|
|
|
static bool fp_cmp(fp const *l, fp const *r)
|
2019-01-17 10:28:56 +00:00
|
|
|
{
|
2019-01-25 11:56:38 +00:00
|
|
|
u512 lu,ru;
|
|
|
|
fp_dec(&lu, l);
|
|
|
|
fp_dec(&ru, r);
|
|
|
|
return ( sizeof(lu.c) == sizeof(ru.c) ) && (memcmp(&lu.c, &ru.c, sizeof(lu.c)) == 0);
|
2019-01-16 17:20:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 11:56:38 +00:00
|
|
|
static bool testLoopRef() {
|
2019-01-17 10:28:56 +00:00
|
|
|
for(size_t i=0; i<10; i++) {
|
2019-01-15 18:40:54 +00:00
|
|
|
private_key prA, prB;
|
|
|
|
public_key pkA, pkB;
|
|
|
|
public_key shA, shB;
|
|
|
|
|
|
|
|
// private key
|
|
|
|
csidh_private(&prA);
|
|
|
|
csidh_private(&prB);
|
|
|
|
|
|
|
|
// public key
|
|
|
|
csidh(&pkA, &base, &prA);
|
|
|
|
csidh(&pkB, &base, &prB);
|
|
|
|
|
|
|
|
//csidh
|
|
|
|
csidh(&shA, &pkA, &prB);
|
|
|
|
csidh(&shB, &pkB, &prA);
|
2019-01-25 11:56:38 +00:00
|
|
|
return (memcmp(&shA, &shB, sizeof(shB))==0);
|
2019-01-15 18:40:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-17 10:28:56 +00:00
|
|
|
|
2019-01-25 11:56:38 +00:00
|
|
|
static bool testImportExport() {
|
2019-01-25 11:46:53 +00:00
|
|
|
uint8_t buf_pub[64]={0};
|
|
|
|
private_key prv1 = {0};
|
|
|
|
private_key prv2 = {0};
|
|
|
|
public_key pub1 = {0};
|
|
|
|
public_key pub2 = {0};
|
|
|
|
|
|
|
|
csidh_private(&prv1);
|
|
|
|
csidh_private(&prv2);
|
|
|
|
|
|
|
|
csidh(&pub1, &base, &prv1);
|
|
|
|
|
|
|
|
export_public(buf_pub, &pub1);
|
|
|
|
import_public(&pub2, buf_pub);
|
2019-01-25 11:56:38 +00:00
|
|
|
return fp_cmp(&pub1.A, &pub2.A);
|
2019-01-17 10:28:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 16:17:20 +00:00
|
|
|
static void validator() {
|
|
|
|
uint8_t rnd[64] = {0};
|
|
|
|
public_key pub = {0};
|
|
|
|
for (size_t i=0; i<100; i++) {
|
|
|
|
randombytes(rnd, sizeof(rnd));
|
|
|
|
printf("%llu=", i);
|
|
|
|
import_public(&pub, rnd);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 10:28:56 +00:00
|
|
|
int main() {
|
2019-01-25 11:56:38 +00:00
|
|
|
return !(
|
|
|
|
testImportExport() &&
|
|
|
|
testLoopRef());
|
2019-01-17 10:28:56 +00:00
|
|
|
}
|