Browse Source

working on templatization

master
Michael Hamburg 9 years ago
parent
commit
e6441d0c3c
6 changed files with 400 additions and 399 deletions
  1. +1
    -1
      Makefile
  2. +0
    -5
      include/decaf.h
  3. +306
    -317
      src/decaf_fast.c
  4. +38
    -33
      src/decaf_gen_tables.c
  5. +24
    -16
      test/bench_decaf.cxx
  6. +31
    -27
      test/test_decaf.cxx

+ 1
- 1
Makefile View File

@@ -16,7 +16,7 @@ LD = $(CC)
LDXX = $(CXX)
ASM ?= $(CC)

DECAF ?= decaf
DECAF ?= decaf_fast

ifneq (,$(findstring x86_64,$(MACHINE)))
ARCH ?= arch_x86_64


+ 0
- 5
include/decaf.h View File

@@ -96,11 +96,6 @@ static const decaf_bool_t DECAF_TRUE = -(decaf_bool_t)1, DECAF_FALSE = 0;
static const decaf_bool_t DECAF_SUCCESS = -(decaf_bool_t)1 /*DECAF_TRUE*/,
DECAF_FAILURE = 0 /*DECAF_FALSE*/;

/** The prime p, for debugging purposes.
* TODO: prevent this scalar from actually being used for non-debugging purposes?
*/
extern const decaf_448_scalar_t decaf_448_scalar_p API_VIS;

/** A scalar equal to 1. */
extern const decaf_448_scalar_t decaf_448_scalar_one API_VIS;



+ 306
- 317
src/decaf_fast.c
File diff suppressed because it is too large
View File


+ 38
- 33
src/decaf_gen_tables.c View File

@@ -12,26 +12,29 @@
#include <stdio.h>
#include <stdlib.h>
#include "decaf.h"
#include "decaf_448_config.h"
#include "decaf_448_config.h" /* MAGIC */

#define API_NS(_id) decaf_448_##_id
#define API_NS2(_pref,_id) _pref##_decaf_448_##_id

/* To satisfy linker. */
const decaf_word_t decaf_448_precomputed_base_as_words[1];
const decaf_448_scalar_t decaf_448_precomputed_scalarmul_adjustment;
const decaf_448_scalar_t decaf_448_point_scalarmul_adjustment;
const decaf_word_t API_NS(precomputed_base_as_words)[1];
const API_NS(scalar_t) API_NS(precomputed_scalarmul_adjustment);
const API_NS(scalar_t) API_NS(point_scalarmul_adjustment);

struct niels_s;
const decaf_word_t *decaf_448_precomputed_wnaf_as_words;
extern const size_t sizeof_decaf_448_precomputed_wnafs;
const decaf_word_t *API_NS(precomputed_wnaf_as_words);
extern const size_t API_NS2(sizeof,precomputed_wnafs);

void decaf_448_precompute_wnafs (
void API_NS(precompute_wnafs) (
struct niels_s *out,
const decaf_448_point_t base
const API_NS(point_t) base
);

static void scalar_print(const char *name, const decaf_448_scalar_t sc) {
printf("const decaf_448_scalar_t %s = {{{\n", name);
static void scalar_print(const char *name, const API_NS(scalar_t) sc) {
printf("const API_NS(scalar_t) %s = {{{\n", name);
unsigned i;
for (i=0; i<sizeof(decaf_448_scalar_t)/sizeof(decaf_word_t); i++) {
for (i=0; i<sizeof(API_NS(scalar_t))/sizeof(decaf_word_t); i++) {
if (i) printf(", ");
printf("0x%0*llxull", (int)sizeof(decaf_word_t)*2, (unsigned long long)sc->limb[i] );
}
@@ -41,26 +44,28 @@ static void scalar_print(const char *name, const decaf_448_scalar_t sc) {
int main(int argc, char **argv) {
(void)argc; (void)argv;
decaf_448_precomputed_s *pre;
int ret = posix_memalign((void**)&pre, alignof_decaf_448_precomputed_s, sizeof_decaf_448_precomputed_s);
API_NS(precomputed_s) *pre;
int ret = posix_memalign((void**)&pre, API_NS2(alignof,precomputed_s), API_NS2(sizeof,precomputed_s));
if (ret || !pre) return 1;
decaf_448_precompute(pre, decaf_448_point_base);
API_NS(precompute)(pre, API_NS(point_base));
struct niels_s *preWnaf;
ret = posix_memalign((void**)&preWnaf, alignof_decaf_448_precomputed_s, sizeof_decaf_448_precomputed_wnafs);
ret = posix_memalign((void**)&preWnaf, API_NS2(alignof,precomputed_s), API_NS2(sizeof,precomputed_wnafs));
if (ret || !preWnaf) return 1;
decaf_448_precompute_wnafs(preWnaf, decaf_448_point_base);
API_NS(precompute_wnafs)(preWnaf, API_NS(point_base));

const decaf_word_t *output = (const decaf_word_t *)pre;
unsigned i;
printf("/** @warning: this file was automatically generated. */\n");
printf("#include \"decaf.h\"\n\n");
printf("const decaf_word_t decaf_448_precomputed_base_as_words[%d]\n",
(int)(sizeof_decaf_448_precomputed_s / sizeof(decaf_word_t)));
printf("__attribute__((aligned(%d),visibility(\"hidden\"))) = {\n ", (int)alignof_decaf_448_precomputed_s);
printf("#define API_NS(_id) decaf_448_##_id\n");
printf("#define API_NS2(_pref,_id) _pref##_decaf_448_##_id\n");
printf("const decaf_word_t API_NS(precomputed_base_as_words)[%d]\n",
(int)(API_NS2(sizeof,precomputed_s) / sizeof(decaf_word_t)));
printf("__attribute__((aligned(%d),visibility(\"hidden\"))) = {\n ", (int)API_NS2(alignof,precomputed_s));
for (i=0; i < sizeof_decaf_448_precomputed_s; i+=sizeof(decaf_word_t)) {
for (i=0; i < API_NS2(sizeof,precomputed_s); i+=sizeof(decaf_word_t)) {
if (i && (i%8==0)) printf(",\n ");
else if (i) printf(", ");
printf("0x%0*llxull", (int)sizeof(decaf_word_t)*2, (unsigned long long)*output );
@@ -69,10 +74,10 @@ int main(int argc, char **argv) {
printf("\n};\n");
output = (const decaf_word_t *)preWnaf;
printf("const decaf_word_t decaf_448_precomputed_wnaf_as_words[%d]\n",
(int)(sizeof_decaf_448_precomputed_wnafs / sizeof(decaf_word_t)));
printf("__attribute__((aligned(%d),visibility(\"hidden\"))) = {\n ", (int)alignof_decaf_448_precomputed_s);
for (i=0; i < sizeof_decaf_448_precomputed_wnafs; i+=sizeof(decaf_word_t)) {
printf("const decaf_word_t API_NS(precomputed_wnaf_as_words)[%d]\n",
(int)(API_NS2(sizeof,precomputed_wnafs) / sizeof(decaf_word_t)));
printf("__attribute__((aligned(%d),visibility(\"hidden\"))) = {\n ", (int)API_NS2(alignof,precomputed_s));
for (i=0; i < API_NS2(sizeof,precomputed_wnafs); i+=sizeof(decaf_word_t)) {
if (i && (i%8==0)) printf(",\n ");
else if (i) printf(", ");
printf("0x%0*llxull", (int)sizeof(decaf_word_t)*2, (unsigned long long)*output );
@@ -80,22 +85,22 @@ int main(int argc, char **argv) {
}
printf("\n};\n");
decaf_448_scalar_t smadj;
decaf_448_scalar_copy(smadj,decaf_448_scalar_one);
API_NS(scalar_t) smadj;
API_NS(scalar_copy)(smadj,API_NS(scalar_one));

for (i=0; i<DECAF_COMBS_N*DECAF_COMBS_T*DECAF_COMBS_S; i++) {
decaf_448_scalar_add(smadj,smadj,smadj);
API_NS(scalar_add)(smadj,smadj,smadj);
}
decaf_448_scalar_sub(smadj, smadj, decaf_448_scalar_one);
scalar_print("decaf_448_precomputed_scalarmul_adjustment", smadj);
API_NS(scalar_sub)(smadj, smadj, API_NS(scalar_one));
scalar_print("API_NS(precomputed_scalarmul_adjustment)", smadj);
decaf_448_scalar_copy(smadj,decaf_448_scalar_one);
API_NS(scalar_copy)(smadj,API_NS(scalar_one));
for (i=0; i<DECAF_448_SCALAR_BITS-1 + DECAF_WINDOW_BITS
- ((DECAF_448_SCALAR_BITS-1)%DECAF_WINDOW_BITS); i++) {
decaf_448_scalar_add(smadj,smadj,smadj);
API_NS(scalar_add)(smadj,smadj,smadj);
}
decaf_448_scalar_sub(smadj, smadj, decaf_448_scalar_one);
scalar_print("decaf_448_point_scalarmul_adjustment", smadj);
API_NS(scalar_sub)(smadj, smadj, API_NS(scalar_one));
scalar_print("API_NS(point_scalarmul_adjustment)", smadj);
return 0;
}

+ 24
- 16
test/bench_decaf.cxx View File

@@ -113,18 +113,19 @@ public:
double Benchmark::totalCy = 0, Benchmark::totalS = 0;

static void tdh (
SpongeRng &rng,
SpongeRng &clientRng,
SpongeRng &serverRng,
Scalar x, const Block &gx,
Scalar y, const Block &gy
) {
Strobe client(Strobe::CLIENT), server(Strobe::SERVER);
Scalar xe(rng);
Scalar xe(clientRng);
SecureBuffer gxe = Precomputed::base() * xe;
client.send_plaintext(gxe);
server.recv_plaintext(gxe);
Scalar ye(rng);
Scalar ye(serverRng);
SecureBuffer gye = Precomputed::base() * ye;
server.send_plaintext(gye);
client.recv_plaintext(gye);
@@ -152,21 +153,22 @@ static void tdh (
}

static void fhmqv (
SpongeRng &rng,
SpongeRng &clientRng,
SpongeRng &serverRng,
Scalar x, const Block &gx,
Scalar y, const Block &gy
) {
/* Don't use this, it's probably patented */
Strobe client(Strobe::CLIENT), server(Strobe::SERVER);
Scalar xe(rng);
Scalar xe(clientRng);
client.send_plaintext(gx);
server.recv_plaintext(gx);
SecureBuffer gxe = Precomputed::base() * xe;
server.send_plaintext(gxe);
client.recv_plaintext(gxe);

Scalar ye(rng);
Scalar ye(serverRng);
server.send_plaintext(gy);
client.recv_plaintext(gy);
SecureBuffer gye = Precomputed::base() * ye;
@@ -191,10 +193,15 @@ static void fhmqv (
server.respec(STROBE_KEYED_128);
}

static void spake2ee(const Block &hashed_password, SpongeRng &rng, bool aug) {
static void spake2ee(
SpongeRng &clientRng,
SpongeRng &serverRng,
const Block &hashed_password,
bool aug
) {
Strobe client(Strobe::CLIENT), server(Strobe::SERVER);
Scalar x(rng);
Scalar x(clientRng);
SHAKE<256> shake;
shake.update(hashed_password);
@@ -212,7 +219,7 @@ static void spake2ee(const Block &hashed_password, SpongeRng &rng, bool aug) {
client.send_plaintext(gx);
server.recv_plaintext(gx);
Scalar y(rng);
Scalar y(serverRng);
SecureBuffer gy(Precomputed::base() * y + hs);
server.send_plaintext(gy);
client.recv_plaintext(gy);
@@ -332,27 +339,28 @@ int main(int argc, char **argv) {
}

printf("\nProtocol benchmarks:\n");
SpongeRng rng(Block("my rng seed"));
SpongeRng clientRng(Block("client rng seed"));
SpongeRng serverRng(Block("server rng seed"));
SecureBuffer hashedPassword("hello world");
for (Benchmark b("Spake2ee c+s",0.1); b.iter(); ) {
spake2ee(hashedPassword,rng,false);
spake2ee(clientRng, serverRng, hashedPassword,false);
}
for (Benchmark b("Spake2ee c+s aug",0.1); b.iter(); ) {
spake2ee(hashedPassword,rng,true);
spake2ee(clientRng, serverRng, hashedPassword,true);
}
Scalar x(rng);
Scalar x(clientRng);
SecureBuffer gx(Precomputed::base() * x);
Scalar y(rng);
Scalar y(serverRng);
SecureBuffer gy(Precomputed::base() * y);
for (Benchmark b("FHMQV c+s",0.1); b.iter(); ) {
fhmqv(rng,x,gx,y,gy);
fhmqv(clientRng, serverRng,x,gx,y,gy);
}
for (Benchmark b("TripleDH anon c+s",0.1); b.iter(); ) {
tdh(rng,x,gx,y,gy);
tdh(clientRng, serverRng, x,gx,y,gy);
}
printf("\n");


+ 31
- 27
test/test_decaf.cxx View File

@@ -13,33 +13,9 @@
#include "shake.hxx"
#include <stdio.h>

typedef decaf::decaf<448>::Scalar Scalar;
typedef decaf::decaf<448>::Point Point;
typedef decaf::decaf<448>::Precomputed Precomputed;

static const long NTESTS = 10000;

static void print(const char *name, const Scalar &x) {
unsigned char buffer[DECAF_448_SCALAR_BYTES];
x.encode(buffer);
printf(" %s = 0x", name);
for (int i=sizeof(buffer)-1; i>=0; i--) {
printf("%02x", buffer[i]);
}
printf("\n");
}

static void print(const char *name, const Point &x) {
unsigned char buffer[DECAF_448_SER_BYTES];
x.encode(buffer);
printf(" %s = 0x", name);
for (int i=sizeof(buffer)-1; i>=0; i--) {
printf("%02x", buffer[i]);
}
printf("\n");
}

static bool passing = true;
static const long NTESTS = 10000;

class Test {
public:
@@ -64,6 +40,32 @@ public:
}
};

template<decaf::GroupId GROUP> struct Tests {

typedef typename decaf::decaf<GROUP>::Scalar Scalar;
typedef typename decaf::decaf<GROUP>::Point Point;
typedef typename decaf::decaf<GROUP>::Precomputed Precomputed;

static void print(const char *name, const Scalar &x) {
unsigned char buffer[DECAF_448_SCALAR_BYTES];
x.encode(buffer);
printf(" %s = 0x", name);
for (int i=sizeof(buffer)-1; i>=0; i--) {
printf("%02x", buffer[i]);
}
printf("\n");
}

static void print(const char *name, const Point &x) {
unsigned char buffer[DECAF_448_SER_BYTES];
x.encode(buffer);
printf(" %s = 0x", name);
for (int i=sizeof(buffer)-1; i>=0; i--) {
printf("%02x", buffer[i]);
}
printf("\n");
}

static bool arith_check(
Test &test,
const Scalar &x,
@@ -191,11 +193,13 @@ static void test_ec() {
}
}

}; // template<decaf::GroupId GROUP>

int main(int argc, char **argv) {
(void) argc; (void) argv;
test_arithmetic();
test_ec();
Tests<448>::test_arithmetic();
Tests<448>::test_ec();
if (passing) printf("Passed all tests.\n");


Loading…
Cancel
Save