sidh_torture/csidh/test/main.c

119 lines
2.9 KiB
C

#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "../ref/csidh/csidh.h"
static void u512_print(u512 const *x)
{
for (size_t i=0; i<8; i++) {
printf("0x%016lX,", x->c[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");
}
static void fp_print(fp const *x)
{
u512 y;
fp_dec(&y, x);
u512_print(&y);
}
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 void fp_cmp(fp const *x, uint64_t const *org)
{
u512 y;
fp_dec(&y, x);
assert(memcmp(&y.c, org, sizeof(y.c)) == 0);
}
static void 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);
//int_print(prA.e);
//fp_print(&pkA.A);
assert(memcmp(&shA, &shB, sizeof(shB))==0);
}
}
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 testHardcoded() {
private_key prv1, prv2;
public_key pub1, pub2;
public_key shA;
uint8_t prv_bytes1[] = {0xaa, 0x54, 0xe4, 0xd4, 0xd0, 0xbd, 0xee, 0xcb, 0xf4, 0xd0, 0xc2, 0xbc, 0x52, 0x44, 0x11, 0xee, 0xe1, 0x14, 0xd2, 0x24, 0xe5, 0x0, 0xcc, 0xf5, 0xc0, 0xe1, 0x1e, 0xb3, 0x43, 0x52, 0x45, 0xbe, 0xfb, 0x54, 0xc0, 0x55, 0xb2};
uint8_t prv_bytes2[] = {0xbb, 0x54, 0xe4, 0xd4, 0xd0, 0xbd, 0xee, 0xcb, 0xf4, 0xd0, 0xc2, 0xbc, 0x52, 0x44, 0x11, 0xee, 0xe1, 0x14, 0xd2, 0x24, 0xe5, 0x0, 0xcc, 0xf5, 0xc0, 0xe1, 0x1e, 0xb3, 0x43, 0x52, 0x45, 0xbe, 0xfb, 0x54, 0xc0, 0x55, 0xb2};
memcpy(prv1.e, prv_bytes1, sizeof(prv1.e));
memcpy(prv2.e, prv_bytes2, sizeof(prv2.e));
// generate public key from private and compare to reference value
csidh(&pub1, &base, &prv1);
csidh(&pub2, &base, &prv2);
csidh(&shA, &pub2, &prv1);
uint8_t out[64];
export_public(out, &pub1);
print_bytes(out, sizeof(out));
export_public(out, &pub2);
print_bytes(out, sizeof(out));
export_public(out, &shA);
print_bytes(out, sizeof(out));
}
static void 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);
fp_print_n(&pub1.A);
fp_print_n(&pub2.A);
}
int main() {
testImportExport();
testHardcoded();
//testLoopRef();
}