csidh: adds interface with POD types only

This commit is contained in:
Henry Case 2019-01-25 09:31:11 +00:00
parent 0a242b5d0b
commit 1a54c2f55e
2 changed files with 66 additions and 0 deletions

View File

@ -5,6 +5,8 @@
#include "csidh.h"
#include "rng.h"
#include <stdio.h>
/* specific to p, should perhaps be somewhere else */
const unsigned primes[num_primes] = {
3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
@ -218,3 +220,59 @@ bool csidh(public_key *out, public_key const *in, private_key const *priv)
return true;
}
/// Public API
void export_public(uint8_t *out, const public_key *pub) {
for(size_t i=0; i<64; i++) {
size_t j = i / 8;
size_t k = i % 8;
out[i] = ((uint64_t)pub->A.x.c[j] >> (8*k)) & 0xFF;
}
}
void import_public(public_key *pub, const uint8_t *out) {
for (size_t i=0; i<64; i++) {
size_t j = i / 8;
size_t k = i%8;
uint64_t tmp = out[i];
pub->A.x.c[j] |= tmp << (8*k);
}
}
void export_private(uint8_t *out, const private_key *prv) {
for(size_t i=0; i<37; i++) {
out[i] = (uint8_t) prv->e[i];
}
}
void import_private(private_key *prv, const uint8_t *out) {
for (size_t i=0; i<37; i++) {
prv->e[i] = (int8_t) out[i];
}
}
void keygen_prv(uint8_t private[37]) {
private_key priv;
csidh_private(&priv);
export_private(&private[0], &priv);
}
void keygen_pub(uint8_t public[64], const uint8_t private[37]) {
private_key prv1 = {0};
public_key pub1 = {0};
import_private(&prv1, &private[0]);
csidh(&pub1, &base, &prv1);
export_public(&public[0], &pub1);
}
void derive(uint8_t result[64], const uint8_t public[64], const uint8_t private[37]) {
public_key pub = {0};
public_key ss = {0};
private_key prv;
import_private(&prv, &private[0]);
import_public(&pub, &public[0]);
csidh(&ss, &pub, &prv);
export_public(&result[0], &ss);
}

View File

@ -23,4 +23,12 @@ extern const public_key base;
void csidh_private(private_key *priv);
bool csidh(public_key *out, public_key const *in, private_key const *priv);
void keygen_prv(uint8_t private[37]);
void keygen_pub(uint8_t public[64], const uint8_t private[37]);
void derive(uint8_t result[64], const uint8_t public[64], const uint8_t private_key[37]);
void export_public(uint8_t *out, const public_key *pub);
void import_public(public_key *pub, const uint8_t *out);
void export_private(uint8_t *out, const private_key *prv);
void import_private(private_key *prv, const uint8_t *out);
#endif