some fixes and optimizations

This commit is contained in:
cathugger 2017-09-27 21:38:15 +03:00
parent 188f821778
commit 9e9468b453
4 changed files with 29 additions and 21 deletions

View file

@ -1,8 +1,9 @@
CC= @CC@
CSTD= -std=c99 -Wall -D_POSIX_C_SOURCE=200112L -no-pie
CSTD= -std=c99 -Wall -D_POSIX_C_SOURCE=200112L
CFLAGS= $(CSTD) @CFLAGS@ -DED25519_@ED25519IMPL@ @FILTERDEF@
ASFLAGS= -no-pie
ASFLAGS=
LDFLAGS= -no-pie
MV= mv
ED25519_ref10= $(patsubst %.c,%.c.o,$(wildcard ed25519/ref10/*.c))
@ -58,16 +59,16 @@ default: mkp224o
all: $(EXE)
mkp224o: $(MAINOBJ)
$(CC) $(CFLAGS) -o $@.tmp $^ $(MAINLIB) && $(MV) $@.tmp $@
$(CC) $(CFLAGS) $(LDFLAGS) -o $@.tmp $^ $(MAINLIB) && $(MV) $@.tmp $@
test_base32: $(TEST_BASE32OBJ)
$(CC) $(CFLAGS) -o $@.tmp $^ && $(MV) $@.tmp $@
$(CC) $(CFLAGS) $(LDFLAGS) -o $@.tmp $^ && $(MV) $@.tmp $@
test_base16: $(TEST_BASE16OBJ)
$(CC) $(CFLAGS) -o $@.tmp $^ && $(MV) $@.tmp $@
$(CC) $(CFLAGS) $(LDFLAGS) -o $@.tmp $^ && $(MV) $@.tmp $@
test_ed25519: $(TEST_ED25519OBJ)
$(CC) $(CFLAGS) -o $@.tmp $^ $(TEST_ED25519LIB) && $(MV) $@.tmp $@
$(CC) $(CFLAGS) $(LDFLAGS) -o $@.tmp $^ $(TEST_ED25519LIB) && $(MV) $@.tmp $@
%.c.o: %.c
$(CC) $(CFLAGS) -c -o $@.tmp $< && $(MV) $@.tmp $@
@ -349,12 +350,12 @@ ed25519/ref10/sign.c.o: ed25519/amd64-51-30k/crypto_hash_sha512.h
ed25519/ref10/sign.c.o: ed25519/ref10/ge.h ed25519/ref10/fe.h
ed25519/ref10/sign.c.o: ed25519/ref10/crypto_int32.h ed25519/ref10/sc.h
keccak.c.o: types.h keccak.h
main.c.o: ed25519/ed25519.h ed25519/ref10/ed25519.h ed25519/ref10/ge.h
main.c.o: ed25519/ref10/fe.h ed25519/ref10/crypto_int32.h
main.c.o: ed25519/amd64-51-30k/ed25519.h ed25519/amd64-51-30k/ge25519.h
main.c.o: ed25519/amd64-51-30k/fe25519.h ed25519/amd64-51-30k/sc25519.h
main.c.o: ed25519/amd64-64-24k/ed25519.h ed25519/amd64-64-24k/ge25519.h
main.c.o: types.h vec.h base32.h keccak.h
main.c.o: types.h likely.h vec.h base32.h ed25519/ed25519.h
main.c.o: ed25519/ref10/ed25519.h ed25519/ref10/ge.h ed25519/ref10/fe.h
main.c.o: ed25519/ref10/crypto_int32.h ed25519/amd64-51-30k/ed25519.h
main.c.o: ed25519/amd64-51-30k/ge25519.h ed25519/amd64-51-30k/fe25519.h
main.c.o: ed25519/amd64-51-30k/sc25519.h ed25519/amd64-64-24k/ed25519.h
main.c.o: ed25519/amd64-64-24k/ge25519.h keccak.h
test_base16.c.o: types.h base16.h
test_base32.c.o: types.h base32.h
test_ed25519.c.o: types.h base16.h ed25519/ed25519.h ed25519/ref10/ed25519.h

7
likely.h Normal file
View file

@ -0,0 +1,7 @@
#ifdef __GNUC__
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif

14
main.c
View file

@ -6,14 +6,14 @@
#include <time.h>
#include <pthread.h>
#include <signal.h>
#include <sodium/randombytes.h>
#include "ed25519/ed25519.h"
#include <sys/stat.h>
#include <sodium/randombytes.h>
#include "types.h"
#include "likely.h"
#include "vec.h"
#include "base32.h"
#include "ed25519/ed25519.h"
#include "keccak.h"
// additional leading zero is added by C
@ -310,13 +310,13 @@ initseed:
randombytes(seed,sizeof(seed));
again:
if (endwork)
if (unlikely(endwork))
goto end;
ed25519_seckey_expand(sk,seed);
ed25519_pubkey(pk,sk);
FILTERFOR(i) {
if (MATCHFILTER(i,pk)) {
if (unlikely(MATCHFILTER(i,pk))) {
memcpy(&hashsrc[checksumstrlen], &pubonion[pkprefixlen], PUBLIC_LEN);
FIPS202_SHA3_256(hashsrc, sizeof(hashsrc), &pubonion[pkprefixlen + PUBLIC_LEN]);
pubonion[pkprefixlen + PUBLIC_LEN + 2] = 0x03; // version
@ -379,11 +379,11 @@ initseed:
for (counter = 0;counter < U64_MAX-8;counter += 8) {
ge_p1p1 sum;
if (endwork)
if (unlikely(endwork))
goto end;
FILTERFOR(i) {
if (MATCHFILTER(i,pk)) {
if (unlikely(MATCHFILTER(i,pk))) {
// found!
// update secret key with counter
addu64toscalar32(sk,counter);

View file

@ -48,13 +48,13 @@ int main()
base16_from(seedbuf, &mask, test0[i].seed);
base16_from(secretbuf1, &mask, test0[i].secret);
base16_from(publicbuf1, &mask, test0[i].public);
ed25519_ref10_seckey_expand(secretbuf2, seedbuf);
ed25519_seckey_expand(secretbuf2, seedbuf);
WARNF(memcmp(secretbuf1, secretbuf2, SECRETKEYBYTES) == 0) {
base16_to(str1, secretbuf1, sizeof(secretbuf1));
base16_to(str2, secretbuf2, sizeof(secretbuf2));
fprintf(stderr, "expected: %s got %s\n", str1, str2);
}
ed25519_ref10_pubkey(publicbuf2, secretbuf1);
ed25519_pubkey(publicbuf2, secretbuf1);
WARNF(memcmp(publicbuf1, publicbuf2, PUBLICKEYBYTES) == 0) {
base16_to(str1, publicbuf1, sizeof(publicbuf1));
base16_to(str2, publicbuf2, sizeof(publicbuf2));