csidh: run some tests on reference implementation

This commit is contained in:
Henry Case 2019-01-25 11:56:38 +00:00
parent 029ec00936
commit 8307bf8769
2 changed files with 23 additions and 47 deletions

View File

@ -29,9 +29,10 @@ $(BUILD_DIR)/%.o: %.s
all: $(CODE_OBJ)
$(AR) $(BUILD_DIR)/libcsidh.a $^
$(RANLIB) $(BUILD_DIR)/libcsidh.a
$(CC) -o $(BUILD_DIR)/test test/main.c -L$(BUILD_DIR) -lcsidh
$(CC) -o $(BUILD_DIR)/test_ref test/main.c -L$(BUILD_DIR) -lcsidh
run: all
$(BUILD_DIR)/test_ref
GOPATH=$(GOPATH) go run test/torturer.go
clean:

View File

@ -11,12 +11,20 @@ static void u512_print(u512 const *x)
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;
@ -24,6 +32,7 @@ static void fp_print(fp const *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]);
@ -31,14 +40,15 @@ static void fp_print_n(fp const *x) {
printf("\n");
}
static void fp_cmp(fp const *x, uint64_t const *org)
static bool fp_cmp(fp const *l, fp const *r)
{
u512 y;
fp_dec(&y, x);
assert(memcmp(&y.c, org, sizeof(y.c)) == 0);
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 void testLoopRef() {
static bool testLoopRef() {
for(size_t i=0; i<10; i++) {
private_key prA, prB;
public_key pkA, pkB;
@ -55,45 +65,11 @@ static void testLoopRef() {
//csidh
csidh(&shA, &pkA, &prB);
csidh(&shB, &pkB, &prA);
//int_print(prA.e);
//fp_print(&pkA.A);
assert(memcmp(&shA, &shB, sizeof(shB))==0);
return (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() {
static bool testImportExport() {
uint8_t buf_pub[64]={0};
private_key prv1 = {0};
private_key prv2 = {0};
@ -107,12 +83,11 @@ static void testImportExport() {
export_public(buf_pub, &pub1);
import_public(&pub2, buf_pub);
fp_print_n(&pub1.A);
fp_print_n(&pub2.A);
return fp_cmp(&pub1.A, &pub2.A);
}
int main() {
testImportExport();
testHardcoded();
//testLoopRef();
return !(
testImportExport() &&
testLoopRef());
}