sidh_torture/csidh/ref/test/main.c

116 lines
2.4 KiB
C

#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "../csidh/csidh.h"
#include "../csidh/rng.h"
static void u512_print(u512 const *x)
{
for (size_t i=0; i<8; i++) {
printf("0x%016lX,", x->c[i]);
}
printf("\n");
}
void print_bytes(uint8_t *out, size_t sz) {
for(size_t i=0; i<sz; i++) {
printf("%02X", out[i]);
}
printf("\n");
}
static void int_print(const int8_t v[37]) {
for (size_t i = 0; i<37; i++)
printf("0x%X,", (unsigned char)v[i]);
printf("\n");
}
// Print after convertion from Montgomery domain
static void fp_print(fp const *x)
{
u512 y;
fp_dec(&y, x);
u512_print(&y);
}
// Print without converting from Montgomery domain
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");
}
static bool fp_cmp(fp const *l, fp const *r)
{
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);
}
static bool testLoopRef() {
for(size_t i=0; i<10; i++) {
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);
return (memcmp(&shA, &shB, sizeof(shB))==0);
}
}
static bool testImportExport() {
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);
return fp_cmp(&pub1.A, &pub2.A);
}
// special cases:
// * diag 0
// * only 0
static void validator() {
uint8_t rnd[64] = {0};
private_key prA = {0};
public_key pubA={0};
csidh_private(&prA);
csidh(&pubA, &base, &prA);
export_public(rnd, &pubA);
for (size_t i=0; i<64; i++) {
uint8_t r[64] = {0};
memcpy(r, rnd, sizeof(r));
r[i]=0x01;
printf("%u -> ", import_public(&pubA, r));
}
}
int main() {
// validator();
return !(
testImportExport() &&
testLoopRef());
}