Skip to content

Commit

Permalink
more test & fix makefiles
Browse files Browse the repository at this point in the history
  • Loading branch information
ruipedro16 committed Mar 7, 2024
1 parent 470573f commit 09bd874
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 91 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-avx2-jasmin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- thash_4x
- hash4x
- forsx4
- wots
steps:
- uses: actions/checkout@v4
- name: Compile tests
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/test-ref-jasmin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
- thash
- hash
- fors
- wotsx
- merkle
- sign
steps:
Expand Down
6 changes: 1 addition & 5 deletions avx2-jasmin/test/forsx4/test_fors.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,9 @@ void test_api(void) {
}

randombytes(message, message_length);

crypto_sign_keypair(public_key, secret_key);

crypto_sign_signature(signature, &signature_length, message, message_length, secret_key);

assert(signature_length == CRYPTO_BYTES); // TODO: See if this works

assert(signature_length == CRYPTO_BYTES);
assert(crypto_sign_verify(signature, signature_length, message, message_length, public_key) == 0);
}
}
Expand Down
81 changes: 76 additions & 5 deletions avx2-jasmin/test/wots/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- Makefile -*-

#-----------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------

AS ?= as
CC ?= clang
CFLAGS ?= -w -march=native #-Wall -Wextra -Wpedantic -Wmissing-prototypes -O3 -std=c99 -march=native -fomit-frame-pointer -flto
AS ?= as
CC ?= clang
CFLAGS ?= -w -march=native

JASMIN ?= jasminc
JFLAGS ?= -lazy-regalloc -nowarning ${JADDFLAGS}
Expand All @@ -19,4 +19,75 @@ THASH_LIST := robust

TLIST := $(foreach H,$(HASH_LIST), $(foreach P,$(PARAMS_LIST), $(foreach T,$(THASH_LIST),$(H)_$(P)_$(T))))

#-----------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------

TESTS := $(addprefix bin/test_wots_, $(TLIST))
OUT := $(addsuffix .out, $(TESTS))

GET_HASH = $(word 1, $(subst _, ,$*))
GET_PARAM = $(word 2, $(subst _, ,$*))
GET_THASH = $(word 3, $(subst _, ,$*))

#-----------------------------------------------------------------------------------------------------------------------

JPP_FILES := $(addsuffix .jpp, $(TESTS))
ASM_FILES := $(JPP_FILES:.jpp=.s)
OBJ_FILES := $(ASM_FILES:.s=.o)

#-----------------------------------------------------------------------------------------------------------------------

SOURCES = ../../keccak4x/KeccakP-1600-times4-SIMD256.c ../../fips202x4.c ../../fips202.c \
../../hash_$(GET_HASH)x4.c ../../hash_$(GET_HASH).c \
../../thash_$(GET_HASH)_$(GET_THASH)x4.c ../../thash_$(GET_HASH)_$(GET_THASH).c \
../../fors.c ../../utilsx4.c \
../../address.c ../../utils.c \
../../wots.c ../../merkle.c ../../sign.c

TEST_FLAGS = -DTEST_WOTS_GEN_CHAIN -DTEST_WOTS_CHECKSUM -DTEST_WOTS_CHAIN_LENGTHS -DTEST_WOTS_PK_FROM_SIG

#-----------------------------------------------------------------------------------------------------------------------

default: $(TESTS)
run: $(OUT)

jpp_files: $(JPP_FILES)
asm_files: $(ASM_FILES)
obj_files: $(OBJ_FILES)

#-----------------------------------------------------------------------------------------------------------------------

%.o: %.s
$(AS) $< -o $@

# ------------------------------------------------------------------------------

$(JPP_FILES):
.PRECIOUS: bin/test_wots_%.jpp
bin/test_wots_%.jpp: | bin/
cp test_wots.jazz $@
sed -i "1 i\from Sphincs require \"params/params-sphincs-$(GET_HASH)-$(GET_PARAM).jinc\"" $@
sed -i "2 i\from Sphincs require \"thash/thash_$(GET_HASH)_$(GET_THASH).jtmpl\"" $@
$(JPP) -I Sphincs:../../ -in $@ -out $@
$(PREPROCESSOR) --input_file $@ --output_file $@

.PRECIOUS: bin/test_wots_%.s
bin/test_wots_%.s: bin/test_wots_%.jpp
$(JASMIN) $(JFLAGS) $< -o $@

$(TESTS):
bin/test_wots_%: bin/test_wots_%.s | bin/
$(CC) $(CFLAGS) -o $@ -DPARAMS=sphincs-$(GET_HASH)-$(GET_PARAM) $(TEST_FLAGS) \
-I../common/ -I../../ -I../../keccak4x ../common/print.c ../common/notrandombytes.c \
test_wots.c $< $(SOURCES)

bin/test_wots_%.out: bin/test_wots_%
@./$<

# ------------------------------------------------------------------------------

bin/:
mkdir -p $@

.PHONY: clean
clean:
rm -fr bin/
113 changes: 113 additions & 0 deletions avx2-jasmin/test/wots/test_wots.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "address.h"
#include "api.h"
#include "context.h"
#include "hash.h"
#include "hashx4.h"
#include "print.h"
#include "utils.h"
#include "utilsx4.h"
#include "wots.h"
#include "wotsx4.h"

#ifndef TESTS
#define TESTS 10000
#endif

extern void wots_checksum_jazz(uint32_t *csum_base_w, const uint32_t *msg_base_w);
extern void chain_lengths_jazz(uint32_t *lengths, const uint8_t *msg);

void test_wots_checksum(void) {
bool debug = true;

uint32_t csum_base_w_ref[SPX_WOTS_LEN2], csum_base_w_jazz[SPX_WOTS_LEN2];
uint32_t msg_base_w[SPX_WOTS_LEN];

for (int i = 0; i < TESTS; i++) {
if (debug) {
printf("[%s]: Wots Checksum: Test %d/%d\n", xstr(PARAMS), i, TESTS);
}

memset((uint8_t *)csum_base_w_ref, 0, SPX_WOTS_LEN2 * sizeof(uint32_t));
memset((uint8_t *)csum_base_w_jazz, 0, SPX_WOTS_LEN2 * sizeof(uint32_t));

randombytes((uint8_t *)msg_base_w, SPX_WOTS_LEN * sizeof(uint32_t));

wots_checksum(csum_base_w_ref, msg_base_w);
wots_checksum_jazz(csum_base_w_jazz, msg_base_w);

if (memcmp(csum_base_w_ref, csum_base_w_jazz, SPX_WOTS_LEN2 * sizeof(uint32_t)) != 0) {
print_str_u8("ref", (uint8_t *)csum_base_w_ref, SPX_WOTS_LEN2 * sizeof(uint32_t));
print_str_u8("jazz", (uint8_t *)csum_base_w_jazz, SPX_WOTS_LEN2 * sizeof(uint32_t));
}

assert(memcmp(csum_base_w_ref, csum_base_w_jazz, SPX_WOTS_LEN2 * sizeof(uint32_t)) == 0);
}
}

void test_chain_lengths(void) {
bool debug = true;

unsigned int lengths_ref[SPX_WOTS_LEN];
uint32_t lengths_jazz[SPX_WOTS_LEN];
uint8_t msg[SPX_N];

for (int t = 0; t < TESTS; t++) {
if (debug) {
printf("[%s]: Chain Lengths: Test %d/%d\n", xstr(PARAMS), t, TESTS);
}

memset(lengths_ref, 0, SPX_WOTS_LEN * sizeof(unsigned int));
memset(lengths_jazz, 0, SPX_WOTS_LEN * sizeof(uint32_t));
randombytes(msg, SPX_N);

chain_lengths(lengths_ref, msg);
chain_lengths_jazz(lengths_jazz, msg);

assert(memcmp(lengths_ref, lengths_jazz, SPX_WOTS_LEN * sizeof(uint32_t)) == 0);
}
}

void test_api(void) {
bool debug = true;

#define MAX_MESSAGE_LENGTH 1024
#define TESTS 100

uint8_t secret_key[CRYPTO_SECRETKEYBYTES];
uint8_t public_key[CRYPTO_PUBLICKEYBYTES];

uint8_t signature[CRYPTO_BYTES];
size_t signature_length;

uint8_t message[MAX_MESSAGE_LENGTH];

for (int i = 0; i < TESTS; i++) {
for (size_t message_length = 1; message_length < MAX_MESSAGE_LENGTH; message_length++) {
if (debug) {
printf("[%s]: Test %d/%d [Len=%ld]\n", xstr(PARAMS), i, TESTS, message_length);
}

randombytes(message, message_length);
crypto_sign_keypair(public_key, secret_key);
crypto_sign_signature(signature, &signature_length, message, message_length, secret_key);
assert(signature_length == CRYPTO_BYTES);
assert(crypto_sign_verify(signature, signature_length, message, message_length, public_key) == 0);
}
}

#undef MESSAGE_LENGTH
}

int main(void) {
test_wots_checksum(); // Same as ref-jasmin
test_chain_lengths(); // Same as ref-jasmin
test_api();
return 0;
}
52 changes: 52 additions & 0 deletions avx2-jasmin/test/wots/test_wots.jazz
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from Sphincs require "wots/wots.jtmpl"
from Sphincs require "generic/utils.jtmpl"

export fn wots_checksum_jazz(reg u64 _csum_base_w _msg_base_w)
{
stack u32[SPX_WOTS_LEN2] csum_base_w;
stack u32[SPX_WOTS_LEN] msg_base_w;

reg ptr u32[SPX_WOTS_LEN2] csum_base_w_p;
reg ptr u32[SPX_WOTS_LEN] msg_base_w_p;

stack u64 s_csum_base_w s_msg_base_w;

csum_base_w_p = csum_base_w;
csum_base_w_p = __load_u32_array<SPX_WOTS_LEN2>(csum_base_w_p, _csum_base_w);

msg_base_w_p = msg_base_w;
msg_base_w_p = __load_u32_array<SPX_WOTS_LEN>(msg_base_w_p, _msg_base_w);

s_csum_base_w = _csum_base_w; // spill

csum_base_w_p = __wots_checksum(csum_base_w_p, msg_base_w_p);

_csum_base_w = s_csum_base_w; // unspill

__store_u32_array<SPX_WOTS_LEN2>(csum_base_w_p, _csum_base_w);
}

export fn chain_lengths_jazz(reg u64 _lengths _msg)
{
stack u32[SPX_WOTS_LEN] lengths;
stack u8[SPX_N] msg;

reg ptr u32[SPX_WOTS_LEN] lengths_p;
reg ptr u8[SPX_N] msg_p;

stack u64 s_lengths s_msg;

lengths_p = lengths;
lengths_p = __load_u32_array<SPX_WOTS_LEN>(lengths_p, _lengths);

msg_p = msg;
msg_p = __load_u8_array<SPX_N>(msg_p, _msg);

s_lengths = _lengths; s_msg = _msg; // spill

lengths_p = __chain_lengths(lengths_p, msg_p);
msg = msg_p;

s_lengths = _lengths; // spill
__store_u32_array<SPX_WOTS_LEN>(lengths_p, _lengths);
}
Loading

0 comments on commit 09bd874

Please sign in to comment.