@@ -64,7 +64,7 @@ ASFLAGS = $(ARCHFLAGS) $(XASFLAGS) | |||||
.PHONY: clean all test bench todo doc lib bat | .PHONY: clean all test bench todo doc lib bat | ||||
.PRECIOUS: build/%.s | .PRECIOUS: build/%.s | ||||
HEADERS= Makefile $(shell find . -name "*.h") $(shell find . -name "*.hxx") build/timestamp | |||||
HEADERS= Makefile $(shell find src include test -name "*.h") $(shell find . -name "*.hxx") build/timestamp | |||||
DECAFCOMPONENTS= build/$(DECAF).o build/shake.o build/decaf_crypto.o \ | DECAFCOMPONENTS= build/$(DECAF).o build/shake.o build/decaf_crypto.o \ | ||||
@@ -11,6 +11,7 @@ | |||||
#include <string.h> | #include <string.h> | ||||
#include "api.h" | #include "api.h" | ||||
#include "crypto_sign.h" | #include "crypto_sign.h" | ||||
#include "randombytes.h" | |||||
int crypto_sign_keypair ( | int crypto_sign_keypair ( | ||||
unsigned char pk[PUBLICKEY_BYTES], | unsigned char pk[PUBLICKEY_BYTES], | ||||
@@ -35,7 +36,7 @@ int crypto_sign ( | |||||
unsigned char sig[SIGNATURE_BYTES]; | unsigned char sig[SIGNATURE_BYTES]; | ||||
decaf_448_sign( | decaf_448_sign( | ||||
sig, | sig, | ||||
(const struct goldilocks_private_key_t *)sk, | |||||
(const decaf_448_private_key_s *)sk, | |||||
m, mlen | m, mlen | ||||
); | ); | ||||
memmove(sm + SIGNATURE_BYTES, m, mlen); | memmove(sm + SIGNATURE_BYTES, m, mlen); | ||||
@@ -30,12 +30,9 @@ | |||||
#if WBITS == 64 | #if WBITS == 64 | ||||
typedef __int128_t decaf_sdword_t; | typedef __int128_t decaf_sdword_t; | ||||
#define LIMB(x) (x##ull) | |||||
#define SC_LIMB(x) (x##ull) | #define SC_LIMB(x) (x##ull) | ||||
#elif WBITS == 32 | #elif WBITS == 32 | ||||
typedef int64_t decaf_sdword_t; | typedef int64_t decaf_sdword_t; | ||||
#define LBITS 28 /* MAGIC */ | |||||
#define LIMB(x) (x##ull)&((1ull<<LBITS)-1), (x##ull)>>LBITS | |||||
#define SC_LIMB(x) (x##ull)&((1ull<<32)-1), (x##ull)>>32 | #define SC_LIMB(x) (x##ull)&((1ull<<32)-1), (x##ull)>>32 | ||||
#else | #else | ||||
#error "Only supporting 32- and 64-bit platforms right now" | #error "Only supporting 32- and 64-bit platforms right now" | ||||
@@ -85,28 +82,23 @@ static const decaf_word_t MONTGOMERY_FACTOR = (decaf_word_t)(0x3bd440fae918bc5ul | |||||
/** base = twist of Goldilocks base point (~,19). */ | /** base = twist of Goldilocks base point (~,19). */ | ||||
#ifndef FIELD_LITERAL | |||||
# define FIELD_LITERAL(a,b,c,d,e,f,g,h) \ | |||||
LIMB(a),LIMB(b),LIMB(c),LIMB(d),LIMB(e),LIMB(f),LIMB(g),LIMB(h) | |||||
#endif | |||||
const point_t API_NS(point_base) = {{ | const point_t API_NS(point_base) = {{ | ||||
{{{ FIELD_LITERAL( | |||||
{ FIELD_LITERAL( | |||||
0xb39a2d57e08c7b,0xb38639c75ff281, | 0xb39a2d57e08c7b,0xb38639c75ff281, | ||||
0x2ec981082b3288,0x99fe8607e5237c, | 0x2ec981082b3288,0x99fe8607e5237c, | ||||
0x0e33fbb1fadd1f,0xe714f67055eb4a, | 0x0e33fbb1fadd1f,0xe714f67055eb4a, | ||||
0xc9ae06d64067dd,0xf7be45054760fa )}}}, | |||||
{{{ FIELD_LITERAL( | |||||
0xc9ae06d64067dd,0xf7be45054760fa )}, | |||||
{ FIELD_LITERAL( | |||||
0xbd8715f551617f,0x8c17fbeca8f5fc, | 0xbd8715f551617f,0x8c17fbeca8f5fc, | ||||
0xaae0eec209c06f,0xce41ad80cbe6b8, | 0xaae0eec209c06f,0xce41ad80cbe6b8, | ||||
0xdf360b5c828c00,0xaf25b6bbb40e3b, | 0xdf360b5c828c00,0xaf25b6bbb40e3b, | ||||
0x8ed37f0ce4ed31,0x72a1c3214557b9 )}}}, | |||||
{{{ 1 }}}, | |||||
{{{ FIELD_LITERAL( | |||||
0x8ed37f0ce4ed31,0x72a1c3214557b9 )}, | |||||
{{{ 1 }}}, | |||||
{ FIELD_LITERAL( | |||||
0x97ca9c8ed8bde9,0xf0b780da83304c, | 0x97ca9c8ed8bde9,0xf0b780da83304c, | ||||
0x0d79c0a7729a69,0xc18d3f24aebc1c, | 0x0d79c0a7729a69,0xc18d3f24aebc1c, | ||||
0x1fbb5389b3fda5,0xbb24f674635948, | 0x1fbb5389b3fda5,0xbb24f674635948, | ||||
0x723a55709a3983,0xe1c0107a823dd4 )}}} | |||||
0x723a55709a3983,0xe1c0107a823dd4 )} | |||||
}}; | }}; | ||||
/* Projective Niels coordinates */ | /* Projective Niels coordinates */ | ||||
@@ -116,9 +108,9 @@ typedef struct { niels_t n; gf z; } pniels_s, pniels_t[1]; | |||||
/* Precomputed base */ | /* Precomputed base */ | ||||
struct precomputed_s { niels_t table [DECAF_COMBS_N<<(DECAF_COMBS_T-1)]; }; | struct precomputed_s { niels_t table [DECAF_COMBS_N<<(DECAF_COMBS_T-1)]; }; | ||||
extern const decaf_word_t API_NS(precomputed_base_as_words)[]; | |||||
extern const field_t API_NS(precomputed_base_as_fe)[]; | |||||
const precomputed_s *API_NS(precomputed_base) = | const precomputed_s *API_NS(precomputed_base) = | ||||
(const precomputed_s *) &API_NS(precomputed_base_as_words); | |||||
(const precomputed_s *) &API_NS(precomputed_base_as_fe); | |||||
const size_t API_NS2(sizeof,precomputed_s) = sizeof(precomputed_s); | const size_t API_NS2(sizeof,precomputed_s) = sizeof(precomputed_s); | ||||
const size_t API_NS2(alignof,precomputed_s) = 32; | const size_t API_NS2(alignof,precomputed_s) = 32; | ||||
@@ -1497,8 +1489,8 @@ sv prepare_wnaf_table( | |||||
} | } | ||||
} | } | ||||
extern const decaf_word_t API_NS(precomputed_wnaf_as_words)[]; | |||||
static const niels_t *API_NS(wnaf_base) = (const niels_t *)API_NS(precomputed_wnaf_as_words); | |||||
extern const field_t API_NS(precomputed_wnaf_as_fe)[]; | |||||
static const niels_t *API_NS(wnaf_base) = (const niels_t *)API_NS(precomputed_wnaf_as_fe); | |||||
const size_t API_NS2(sizeof,precomputed_wnafs) __attribute((visibility("hidden"))) | const size_t API_NS2(sizeof,precomputed_wnafs) __attribute((visibility("hidden"))) | ||||
= sizeof(niels_t)<<DECAF_WNAF_FIXED_TABLE_BITS; | = sizeof(niels_t)<<DECAF_WNAF_FIXED_TABLE_BITS; | ||||
@@ -13,17 +13,18 @@ | |||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include "decaf.h" | #include "decaf.h" | ||||
#include "decaf_448_config.h" /* MAGIC */ | #include "decaf_448_config.h" /* MAGIC */ | ||||
#include "field.h" | |||||
#define API_NS(_id) decaf_448_##_id | #define API_NS(_id) decaf_448_##_id | ||||
#define API_NS2(_pref,_id) _pref##_decaf_448_##_id | #define API_NS2(_pref,_id) _pref##_decaf_448_##_id | ||||
/* To satisfy linker. */ | /* To satisfy linker. */ | ||||
const decaf_word_t API_NS(precomputed_base_as_words)[1]; | |||||
const field_t API_NS(precomputed_base_as_fe)[1]; | |||||
const API_NS(scalar_t) API_NS(precomputed_scalarmul_adjustment); | const API_NS(scalar_t) API_NS(precomputed_scalarmul_adjustment); | ||||
const API_NS(scalar_t) API_NS(point_scalarmul_adjustment); | const API_NS(scalar_t) API_NS(point_scalarmul_adjustment); | ||||
struct niels_s; | struct niels_s; | ||||
const decaf_word_t *API_NS(precomputed_wnaf_as_words); | |||||
const field_t *API_NS(precomputed_wnaf_as_fe); | |||||
extern const size_t API_NS2(sizeof,precomputed_wnafs); | extern const size_t API_NS2(sizeof,precomputed_wnafs); | ||||
void API_NS(precompute_wnafs) ( | void API_NS(precompute_wnafs) ( | ||||
@@ -41,6 +42,29 @@ static void scalar_print(const char *name, const API_NS(scalar_t) sc) { | |||||
printf("}}};\n\n"); | printf("}}};\n\n"); | ||||
} | } | ||||
static void field_print(const field_t *f) { | |||||
const int FIELD_SER_BYTES = (FIELD_BITS + 7) / 8; | |||||
unsigned char ser[FIELD_SER_BYTES]; | |||||
field_serialize(ser,f); | |||||
int b=0, i, comma=0; | |||||
unsigned long long limb = 0; | |||||
printf("FIELD_LITERAL("); | |||||
for (i=0; i<FIELD_SER_BYTES; i++) { | |||||
limb |= ((uint64_t)ser[i])<<b; | |||||
b += 8; | |||||
if (b >= FIELD_LIT_LIMB_BITS) { | |||||
limb &= (1ull<<FIELD_LIT_LIMB_BITS) -1; | |||||
b -= FIELD_LIT_LIMB_BITS; | |||||
if (comma) printf(","); | |||||
comma = 1; | |||||
printf("0x%016llx", limb); | |||||
limb = ((uint64_t)ser[i])>>(8-b); | |||||
} | |||||
} | |||||
printf(")"); | |||||
assert(b<8); | |||||
} | |||||
int main(int argc, char **argv) { | int main(int argc, char **argv) { | ||||
(void)argc; (void)argv; | (void)argc; (void)argv; | ||||
@@ -54,34 +78,31 @@ int main(int argc, char **argv) { | |||||
if (ret || !preWnaf) return 1; | if (ret || !preWnaf) return 1; | ||||
API_NS(precompute_wnafs)(preWnaf, API_NS(point_base)); | API_NS(precompute_wnafs)(preWnaf, API_NS(point_base)); | ||||
const decaf_word_t *output = (const decaf_word_t *)pre; | |||||
const field_t *output = (const field_t *)pre; | |||||
unsigned i; | unsigned i; | ||||
printf("/** @warning: this file was automatically generated. */\n"); | printf("/** @warning: this file was automatically generated. */\n"); | ||||
printf("#include \"field.h\"\n\n"); | |||||
printf("#include \"decaf.h\"\n\n"); | printf("#include \"decaf.h\"\n\n"); | ||||
printf("#define API_NS(_id) decaf_448_##_id\n"); | printf("#define API_NS(_id) decaf_448_##_id\n"); | ||||
printf("#define API_NS2(_pref,_id) _pref##_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("const field_t API_NS(precomputed_base_as_fe)[%d]\n", | |||||
(int)(API_NS2(sizeof,precomputed_s) / sizeof(field_t))); | |||||
printf("__attribute__((aligned(%d),visibility(\"hidden\"))) = {\n ", (int)API_NS2(alignof,precomputed_s)); | printf("__attribute__((aligned(%d),visibility(\"hidden\"))) = {\n ", (int)API_NS2(alignof,precomputed_s)); | ||||
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 ); | |||||
output++; | |||||
for (i=0; i < API_NS2(sizeof,precomputed_s); i+=sizeof(field_t)) { | |||||
if (i) printf(",\n "); | |||||
field_print(output++); | |||||
} | } | ||||
printf("\n};\n"); | printf("\n};\n"); | ||||
output = (const decaf_word_t *)preWnaf; | |||||
printf("const decaf_word_t API_NS(precomputed_wnaf_as_words)[%d]\n", | |||||
(int)(API_NS2(sizeof,precomputed_wnafs) / sizeof(decaf_word_t))); | |||||
output = (const field_t *)preWnaf; | |||||
printf("const field_t API_NS(precomputed_wnaf_as_fe)[%d]\n", | |||||
(int)(API_NS2(sizeof,precomputed_wnafs) / sizeof(field_t))); | |||||
printf("__attribute__((aligned(%d),visibility(\"hidden\"))) = {\n ", (int)API_NS2(alignof,precomputed_s)); | 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 ); | |||||
output++; | |||||
for (i=0; i < API_NS2(sizeof,precomputed_wnafs); i+=sizeof(field_t)) { | |||||
if (i) printf(",\n "); | |||||
field_print(output++); | |||||
} | } | ||||
printf("\n};\n"); | printf("\n};\n"); | ||||
@@ -13,6 +13,11 @@ typedef struct p448_t { | |||||
uint32_t limb[16]; | uint32_t limb[16]; | ||||
} __attribute__((aligned(32))) p448_t; | } __attribute__((aligned(32))) p448_t; | ||||
#define LBITS 28 | |||||
#define LIMB(x) (x##ull)&((1ull<<LBITS)-1), (x##ull)>>LBITS | |||||
#define FIELD_LITERAL(a,b,c,d,e,f,g,h) \ | |||||
{{LIMB(a),LIMB(b),LIMB(c),LIMB(d),LIMB(e),LIMB(f),LIMB(g),LIMB(h)}} | |||||
#ifdef __cplusplus | #ifdef __cplusplus | ||||
extern "C" { | extern "C" { | ||||
#endif | #endif | ||||
@@ -13,6 +13,11 @@ typedef struct p448_t { | |||||
uint32_t limb[16]; | uint32_t limb[16]; | ||||
} __attribute__((aligned(32))) p448_t; | } __attribute__((aligned(32))) p448_t; | ||||
#define LBITS 28 | |||||
#define LIMB(x) (x##ull)&((1ull<<LBITS)-1), (x##ull)>>LBITS | |||||
#define FIELD_LITERAL(a,b,c,d,e,f,g,h) \ | |||||
{{LIMB(a),LIMB(b),LIMB(c),LIMB(d),LIMB(e),LIMB(f),LIMB(g),LIMB(h)}} | |||||
#ifdef __cplusplus | #ifdef __cplusplus | ||||
extern "C" { | extern "C" { | ||||
#endif | #endif | ||||
@@ -15,13 +15,14 @@ typedef struct p448_t { | |||||
#define LIMBPERM(x) (((x)<<1 | (x)>>3) & 15) | #define LIMBPERM(x) (((x)<<1 | (x)>>3) & 15) | ||||
#define USE_NEON_PERM 1 | #define USE_NEON_PERM 1 | ||||
#define LBITS 28 | |||||
#define LIMBHI(x) ((x##ull)>>LBITS) | #define LIMBHI(x) ((x##ull)>>LBITS) | ||||
#define LIMBLO(x) ((x##ull)&((1ull<<LBITS)-1)) | #define LIMBLO(x) ((x##ull)&((1ull<<LBITS)-1)) | ||||
# define FIELD_LITERAL(a,b,c,d,e,f,g,h) \ | # define FIELD_LITERAL(a,b,c,d,e,f,g,h) \ | ||||
LIMBLO(a),LIMBLO(e), LIMBHI(a),LIMBHI(e), \ | |||||
LIMBLO(b),LIMBLO(f), LIMBHI(b),LIMBHI(f), \ | |||||
LIMBLO(c),LIMBLO(g), LIMBHI(c),LIMBHI(g), \ | |||||
LIMBLO(d),LIMBLO(h), LIMBHI(d),LIMBHI(h) | |||||
{{LIMBLO(a),LIMBLO(e), LIMBHI(a),LIMBHI(e), \ | |||||
LIMBLO(b),LIMBLO(f), LIMBHI(b),LIMBHI(f), \ | |||||
LIMBLO(c),LIMBLO(g), LIMBHI(c),LIMBHI(g), \ | |||||
LIMBLO(d),LIMBLO(h), LIMBHI(d),LIMBHI(h)}} | |||||
#ifdef __cplusplus | #ifdef __cplusplus | ||||
extern "C" { | extern "C" { | ||||
@@ -14,6 +14,9 @@ typedef struct p448_t { | |||||
uint64_t limb[8]; | uint64_t limb[8]; | ||||
} __attribute__((aligned(32))) p448_t; | } __attribute__((aligned(32))) p448_t; | ||||
#define LBITS 56 | |||||
#define FIELD_LITERAL(a,b,c,d,e,f,g,h) {{a,b,c,d,e,f,g,h}} | |||||
#ifdef __cplusplus | #ifdef __cplusplus | ||||
extern "C" { | extern "C" { | ||||
#endif | #endif | ||||
@@ -13,6 +13,9 @@ typedef struct p448_t { | |||||
uint64_t limb[8]; | uint64_t limb[8]; | ||||
} __attribute__((aligned(32))) p448_t; | } __attribute__((aligned(32))) p448_t; | ||||
#define LBITS 56 | |||||
#define FIELD_LITERAL(a,b,c,d,e,f,g,h) {{a,b,c,d,e,f,g,h}} | |||||
#ifdef __cplusplus | #ifdef __cplusplus | ||||
extern "C" { | extern "C" { | ||||
#endif | #endif | ||||
@@ -13,6 +13,7 @@ | |||||
#include <string.h> | #include <string.h> | ||||
#include "p448.h" | #include "p448.h" | ||||
#define FIELD_LIT_LIMB_BITS 56 | |||||
#define FIELD_BITS 448 | #define FIELD_BITS 448 | ||||
#define field_t p448_t | #define field_t p448_t | ||||
#define field_mul p448_mul | #define field_mul p448_mul | ||||
@@ -13,6 +13,7 @@ | |||||
#include <string.h> | #include <string.h> | ||||
#include "p480.h" | #include "p480.h" | ||||
#define FIELD_LIT_LIMB_BITS 60 | |||||
#define FIELD_BITS 480 | #define FIELD_BITS 480 | ||||
#define field_t p480_t | #define field_t p480_t | ||||
#define field_mul p480_mul | #define field_mul p480_mul | ||||
@@ -13,6 +13,7 @@ | |||||
#include "constant_time.h" | #include "constant_time.h" | ||||
#include "p521.h" | #include "p521.h" | ||||
#define FIELD_LIT_LIMB_BITS 58 | |||||
#define FIELD_BITS 521 | #define FIELD_BITS 521 | ||||
#define field_t p521_t | #define field_t p521_t | ||||
#define field_mul p521_mul | #define field_mul p521_mul | ||||