Add a preliminary test for core_ed25519

This commit is contained in:
Frank Denis 2017-11-15 01:34:43 +01:00
parent d5634850e4
commit 1df2285362
5 changed files with 88 additions and 0 deletions

1
.gitignore vendored
View File

@ -101,6 +101,7 @@ test/default/box_seal
test/default/box_seed
test/default/chacha20
test/default/codecs
test/default/core_ed25519
test/default/core1
test/default/core2
test/default/core3

View File

@ -8,6 +8,9 @@
extern "C" {
#endif
#define crypto_core_ed25519_BYTES 32
#define crypto_core_ed25519_UNIFORMBYTES 32
SODIUM_EXPORT
int crypto_core_ed25519_is_valid_point(const unsigned char *p);

View File

@ -22,6 +22,7 @@ EXTRA_DIST = \
box_seed.exp \
chacha20.exp \
codecs.exp \
core_ed25519.exp \
core1.exp \
core2.exp \
core3.exp \
@ -95,6 +96,7 @@ DISTCLEANFILES = \
box_seed.res \
chacha20.res \
codecs.res \
core_ed25519.res \
core1.res \
core2.res \
core3.res \
@ -169,6 +171,7 @@ CLEANFILES = \
box_seed.final \
chacha20.final \
codecs.final \
core_ed25519.final \
core1.final \
core2.final \
core3.final \
@ -238,6 +241,7 @@ CLEANFILES = \
box_seed.nexe \
chacha20.nexe \
codecs.nexe \
core_ed25519.nexe \
core1.nexe \
core2.nexe \
core3.nexe \
@ -438,6 +442,9 @@ chacha20_LDADD = $(TESTS_LDADD)
codecs_SOURCE = cmptest.h codecs.c
codecs_LDADD = $(TESTS_LDADD)
core_ed25519_SOURCE = cmptest.h core_ed25519.c
core_ed25519_LDADD = $(TESTS_LDADD)
core1_SOURCE = cmptest.h core1.c
core1_LDADD = $(TESTS_LDADD)
@ -593,6 +600,7 @@ xchacha20_LDADD = $(TESTS_LDADD)
if !MINIMAL
TESTS_TARGETS += \
core_ed25519 \
pwhash_scrypt \
pwhash_scrypt_ll \
siphashx24 \

View File

@ -0,0 +1,75 @@
#define TEST_NAME "core_ed25519"
#include "cmptest.h"
int
main(void)
{
unsigned char *h;
unsigned char *p, *p2, *p3;
unsigned char *sc;
int i, j;
h = sodium_malloc(crypto_core_ed25519_UNIFORMBYTES);
p = sodium_malloc(crypto_core_ed25519_BYTES);
for (i = 0; i < 1000; i++) {
randombytes_buf(h, crypto_core_ed25519_UNIFORMBYTES);
if (crypto_core_ed25519_from_uniform(p, h) != 0) {
printf("crypto_core_ed25519_from_uniform() failed\n");
}
if (crypto_core_ed25519_is_valid_point(p) == 0) {
printf("crypto_core_ed25519_from_uniform() returned an invalid point\n");
}
}
p2 = sodium_malloc(crypto_core_ed25519_BYTES);
p3 = sodium_malloc(crypto_core_ed25519_BYTES);
randombytes_buf(h, crypto_core_ed25519_UNIFORMBYTES);
crypto_core_ed25519_from_uniform(p2, h);
j = 1 + (int) randombytes_uniform(100);
memcpy(p3, p, crypto_core_ed25519_BYTES);
for (i = 0; i < j; i++) {
crypto_core_ed25519_add(p, p, p2);
if (crypto_core_ed25519_is_valid_point(p) != 1) {
printf("crypto_core_add() returned an invalid point\n");
}
}
if (memcmp(p, p3, crypto_core_ed25519_BYTES) == 0) {
printf("crypto_core_add() failed\n");
}
for (i = 0; i < j; i++) {
crypto_core_ed25519_sub(p, p, p2);
}
if (memcmp(p, p3, crypto_core_ed25519_BYTES) != 0) {
printf("crypto_core_add() or crypto_core_sub() failed\n");
}
sc = sodium_malloc(crypto_scalarmult_ed25519_SCALARBYTES);
memset(sc, 0, crypto_scalarmult_ed25519_SCALARBYTES);
sc[0] = 8;
memcpy(p2, p, crypto_core_ed25519_BYTES);
memcpy(p3, p, crypto_core_ed25519_BYTES);
for (i = 0; i < 254; i++) {
crypto_core_ed25519_add(p2, p2, p2);
}
for (i = 0; i < 8; i++) {
crypto_core_ed25519_add(p2, p2, p);
}
if (crypto_scalarmult_ed25519(p3, sc, p) != 0) {
printf("crypto_scalarmult_ed25519() failed\n");
}
if (memcmp(p2, p3, crypto_core_ed25519_BYTES) != 0) {
printf("crypto_scalarmult_ed25519() is inconsistent with crypto_core_ed25519_add()\n");
}
sodium_free(sc);
sodium_free(p3);
sodium_free(p2);
sodium_free(p);
sodium_free(h);
printf("OK\n");
return 0;
}

View File

@ -0,0 +1 @@
OK