Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lowram implementation #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions lowram/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
CC ?= /usr/bin/cc
CFLAGS += -Wall -Wextra -Wpedantic -Wmissing-prototypes -Wredundant-decls \
-Wshadow -Wvla -Wpointer-arith -O3 -fomit-frame-pointer
NISTFLAGS += -Wno-unused-result -O3 -fomit-frame-pointer
SOURCES = sign.c packing.c polyvec.c poly.c ntt.c reduce.c rounding.c lowram.c smallpoly.c smallntt_3329.c
HEADERS = config.h params.h api.h sign.h packing.h polyvec.h poly.h ntt.h \
reduce.h rounding.h symmetric.h randombytes.h lowram.h smallpoly.h smallntt.h
KECCAK_SOURCES = $(SOURCES) fips202.c symmetric-shake.c
KECCAK_HEADERS = $(HEADERS) fips202.h

.PHONY: all speed shared clean

all: \
test/test_dilithium2 \
test/test_dilithium3 \
test/test_dilithium5 \
test/test_vectors2 \
test/test_vectors3 \
test/test_vectors5

speed: \
test/test_mul \
test/test_speed2 \
test/test_speed3 \
test/test_speed5 \

shared: \
libpqcrystals_dilithium2_lowram.so \
libpqcrystals_dilithium3_lowram.so \
libpqcrystals_dilithium5_lowram.so \
libpqcrystals_fips202_lowram.so \

libpqcrystals_fips202_lowram.so: fips202.c fips202.h
$(CC) -shared -fPIC $(CFLAGS) -o $@ $<

libpqcrystals_dilithium2_lowram.so: $(SOURCES) $(HEADERS) symmetric-shake.c
$(CC) -shared -fPIC $(CFLAGS) -DDILITHIUM_MODE=2 \
-o $@ $(SOURCES) symmetric-shake.c

libpqcrystals_dilithium3_lowram.so: $(SOURCES) $(HEADERS) symmetric-shake.c
$(CC) -shared -fPIC $(CFLAGS) -DDILITHIUM_MODE=3 \
-o $@ $(SOURCES) symmetric-shake.c

libpqcrystals_dilithium5_lowram.so: $(SOURCES) $(HEADERS) symmetric-shake.c
$(CC) -shared -fPIC $(CFLAGS) -DDILITHIUM_MODE=5 \
-o $@ $(SOURCES) symmetric-shake.c

test/test_dilithium2: test/test_dilithium.c randombytes.c $(KECCAK_SOURCES) \
$(KECCAK_HEADERS)
$(CC) $(CFLAGS) -DDILITHIUM_MODE=2 \
-o $@ $< randombytes.c $(KECCAK_SOURCES)

test/test_dilithium3: test/test_dilithium.c randombytes.c $(KECCAK_SOURCES) \
$(KECCAK_HEADERS)
$(CC) $(CFLAGS) -DDILITHIUM_MODE=3 \
-o $@ $< randombytes.c $(KECCAK_SOURCES)

test/test_dilithium5: test/test_dilithium.c randombytes.c $(KECCAK_SOURCES) \
$(KECCAK_HEADERS)
$(CC) $(CFLAGS) -DDILITHIUM_MODE=5 \
-o $@ $< randombytes.c $(KECCAK_SOURCES)

test/test_vectors2: test/test_vectors.c $(KECCAK_SOURCES) \
$(KECCAK_HEADERS)
$(CC) $(CFLAGS) -DDILITHIUM_MODE=2 \
-o $@ $< $(KECCAK_SOURCES)

test/test_vectors3: test/test_vectors.c $(KECCAK_SOURCES) $(KECCAK_HEADERS)
$(CC) $(CFLAGS) -DDILITHIUM_MODE=3 \
-o $@ $< $(KECCAK_SOURCES)

test/test_vectors5: test/test_vectors.c $(KECCAK_SOURCES) \
$(KECCAK_HEADERS)
$(CC) $(CFLAGS) -DDILITHIUM_MODE=5 \
-o $@ $< $(KECCAK_SOURCES)

test/test_speed2: test/test_speed.c test/speed_print.c test/speed_print.h \
test/cpucycles.c test/cpucycles.h randombytes.c $(KECCAK_SOURCES) \
$(KECCAK_HEADERS)
$(CC) $(CFLAGS) -DDILITHIUM_MODE=2 \
-o $@ $< test/speed_print.c test/cpucycles.c randombytes.c \
$(KECCAK_SOURCES)

test/test_speed3: test/test_speed.c test/speed_print.c test/speed_print.h \
test/cpucycles.c test/cpucycles.h randombytes.c $(KECCAK_SOURCES) \
$(KECCAK_HEADERS)
$(CC) $(CFLAGS) -DDILITHIUM_MODE=3 \
-o $@ $< test/speed_print.c test/cpucycles.c randombytes.c \
$(KECCAK_SOURCES)

test/test_speed5: test/test_speed.c test/speed_print.c test/speed_print.h \
test/cpucycles.c test/cpucycles.h randombytes.c $(KECCAK_SOURCES) \
$(KECCAK_HEADERS)
$(CC) $(CFLAGS) -DDILITHIUM_MODE=5 \
-o $@ $< test/speed_print.c test/cpucycles.c randombytes.c \
$(KECCAK_SOURCES)

test/test_mul: test/test_mul.c randombytes.c $(KECCAK_SOURCES) $(KECCAK_HEADERS)
$(CC) $(CFLAGS) -UDBENCH -o $@ $< randombytes.c $(KECCAK_SOURCES)

clean:
rm -f *~ test/*~ *.gcno *.gcda *.lcov
rm -f libpqcrystals_dilithium2_lowram.so
rm -f libpqcrystals_dilithium3_lowram.so
rm -f libpqcrystals_dilithium5_lowram.so
rm -f libpqcrystals_fips202_lowram.so
rm -f test/test_dilithium2
rm -f test/test_dilithium3
rm -f test/test_dilithium5
rm -f test/test_vectors2
rm -f test/test_vectors3
rm -f test/test_vectors5
rm -f test/test_speed2
rm -f test/test_speed3
rm -f test/test_speed5
rm -f test/test_mul
98 changes: 98 additions & 0 deletions lowram/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#ifndef API_H
#define API_H

#include <stddef.h>
#include <stdint.h>

#define pqcrystals_dilithium2_PUBLICKEYBYTES 1312
#define pqcrystals_dilithium2_SECRETKEYBYTES 2560
#define pqcrystals_dilithium2_BYTES 2420

#define pqcrystals_dilithium2_lowram_PUBLICKEYBYTES pqcrystals_dilithium2_PUBLICKEYBYTES
#define pqcrystals_dilithium2_lowram_SECRETKEYBYTES pqcrystals_dilithium2_SECRETKEYBYTES
#define pqcrystals_dilithium2_lowram_BYTES pqcrystals_dilithium2_BYTES

int pqcrystals_dilithium2_lowram_keypair(uint8_t *pk, uint8_t *sk);

int pqcrystals_dilithium2_lowram_signature(uint8_t *sig, size_t *siglen,
const uint8_t *m, size_t mlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t *sk);

int pqcrystals_dilithium2_lowram(uint8_t *sm, size_t *smlen,
const uint8_t *m, size_t mlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t *sk);

int pqcrystals_dilithium2_lowram_verify(const uint8_t *sig, size_t siglen,
const uint8_t *m, size_t mlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t *pk);

int pqcrystals_dilithium2_lowram_open(uint8_t *m, size_t *mlen,
const uint8_t *sm, size_t smlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t *pk);

#define pqcrystals_dilithium3_PUBLICKEYBYTES 1952
#define pqcrystals_dilithium3_SECRETKEYBYTES 4032
#define pqcrystals_dilithium3_BYTES 3309

#define pqcrystals_dilithium3_lowram_PUBLICKEYBYTES pqcrystals_dilithium3_PUBLICKEYBYTES
#define pqcrystals_dilithium3_lowram_SECRETKEYBYTES pqcrystals_dilithium3_SECRETKEYBYTES
#define pqcrystals_dilithium3_lowram_BYTES pqcrystals_dilithium3_BYTES

int pqcrystals_dilithium3_lowram_keypair(uint8_t *pk, uint8_t *sk);

int pqcrystals_dilithium3_lowram_signature(uint8_t *sig, size_t *siglen,
const uint8_t *m, size_t mlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t *sk);

int pqcrystals_dilithium3_lowram(uint8_t *sm, size_t *smlen,
const uint8_t *m, size_t mlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t *sk);

int pqcrystals_dilithium3_lowram_verify(const uint8_t *sig, size_t siglen,
const uint8_t *m, size_t mlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t *pk);

int pqcrystals_dilithium3_lowram_open(uint8_t *m, size_t *mlen,
const uint8_t *sm, size_t smlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t *pk);

#define pqcrystals_dilithium5_PUBLICKEYBYTES 2592
#define pqcrystals_dilithium5_SECRETKEYBYTES 4896
#define pqcrystals_dilithium5_BYTES 4627

#define pqcrystals_dilithium5_lowram_PUBLICKEYBYTES pqcrystals_dilithium5_PUBLICKEYBYTES
#define pqcrystals_dilithium5_lowram_SECRETKEYBYTES pqcrystals_dilithium5_SECRETKEYBYTES
#define pqcrystals_dilithium5_lowram_BYTES pqcrystals_dilithium5_BYTES

int pqcrystals_dilithium5_lowram_keypair(uint8_t *pk, uint8_t *sk);

int pqcrystals_dilithium5_lowram_signature(uint8_t *sig, size_t *siglen,
const uint8_t *m, size_t mlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t *sk);

int pqcrystals_dilithium5_lowram(uint8_t *sm, size_t *smlen,
const uint8_t *m, size_t mlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t *sk);

int pqcrystals_dilithium5_lowram_verify(const uint8_t *sig, size_t siglen,
const uint8_t *m, size_t mlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t *pk);

int pqcrystals_dilithium5_lowram_open(uint8_t *m, size_t *mlen,
const uint8_t *sm, size_t smlen,
const uint8_t *ctx, size_t ctxlen,
const uint8_t *pk);


#endif
27 changes: 27 additions & 0 deletions lowram/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef CONFIG_H
#define CONFIG_H

//#define DILITHIUM_MODE 2
// #define DILITHIUM_RANDOMIZED_SIGNING
//#define USE_RDPMC
//#define DBENCH

#ifndef DILITHIUM_MODE
#define DILITHIUM_MODE 2
#endif

#if DILITHIUM_MODE == 2
#define CRYPTO_ALGNAME "Dilithium2"
#define DILITHIUM_NAMESPACETOP pqcrystals_dilithium2_lowram
#define DILITHIUM_NAMESPACE(s) pqcrystals_dilithium2_lowram_##s
#elif DILITHIUM_MODE == 3
#define CRYPTO_ALGNAME "Dilithium3"
#define DILITHIUM_NAMESPACETOP pqcrystals_dilithium3_lowram
#define DILITHIUM_NAMESPACE(s) pqcrystals_dilithium3_lowram_##s
#elif DILITHIUM_MODE == 5
#define CRYPTO_ALGNAME "Dilithium5"
#define DILITHIUM_NAMESPACETOP pqcrystals_dilithium5_lowram
#define DILITHIUM_NAMESPACE(s) pqcrystals_dilithium5_lowram_##s
#endif

#endif
1 change: 1 addition & 0 deletions lowram/fips202.c
1 change: 1 addition & 0 deletions lowram/fips202.h
Loading