#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "ecc.h"
#include "test_helper.h"
#ifdef CONTIKI
#include "contiki.h"
#endif
uint32_t null[8] = { 0x00000000,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000};
uint32_t null64[16] = { 0x00000000,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000};
uint32_t one[8] = { 0x00000001,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000};
uint32_t one64[16] = { 0x00000001,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000};
uint32_t two[8] = { 0x00000002,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000};
uint32_t two64[16] = { 0x00000002,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000};
uint32_t three[8] = { 0x00000003,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000};
uint32_t four[8] = {0x00000004,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000};
uint32_t four64[16] = { 0x00000004,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000};
uint32_t six[8] = { 0x00000006,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000};
uint32_t eight[8] = { 0x00000008,0x00000000,0x00000000,0x00000000,
0x00000000,0x00000000,0x00000000,0x00000000};
uint32_t full[8] = { 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
uint32_t resultFullAdd[8] = { 0x00000001,0x00000000,0x00000000,0xFFFFFFFF,
0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFE,0x00000000};
uint32_t primeMinusOne[8]= { 0xfffffffe,0xffffffff,0xffffffff,0x00000000,
0x00000000,0x00000000,0x00000001,0xffffffff};
uint32_t resultDoubleMod[8] = { 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
0x00000000,0x00000000,0x00000001,0xffffffff};
uint32_t resultQuadMod[16] = { 0x00000004,0x00000000,0x00000000,0xFFFFFFFC,
0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFC,0x00000003,
0xFFFFFFFC,0x00000001,0xFFFFFFFE,0x00000001,
0x00000001,0xFFFFFFFE,0x00000002,0xFFFFFFFE};
uint32_t resultFullMod[8] = { 0x00000002,0x00000000,0xFFFFFFFF,0xFFFFFFFD,
0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFF,0x00000002};
static const uint32_t orderMinusOne[8] = {0xFC632550, 0xF3B9CAC2, 0xA7179E84, 0xBCE6FAAD,
0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF};
static const uint32_t orderResultDoubleMod[8] = {0xFC63254F, 0xF3B9CAC2, 0xA7179E84, 0xBCE6FAAD, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF};
uint32_t temp[8];
uint32_t temp2[16];
static void
nullEverything(void){
memset(temp, 0, sizeof(temp));
memset(temp2, 0, sizeof(temp));
}
static void
fieldAddTest(void){
assert(ecc_isSame(one, one, arrayLength));
ecc_fieldAdd(one, null, ecc_prime_r, temp);
assert(ecc_isSame(temp, one, arrayLength));
nullEverything();
ecc_fieldAdd(one, one, ecc_prime_r, temp);
assert(ecc_isSame(temp, two, arrayLength));
nullEverything();
ecc_add(full, one, temp, 32);
assert(ecc_isSame(null, temp, arrayLength));
nullEverything();
ecc_fieldAdd(full, one, ecc_prime_r, temp);
assert(ecc_isSame(temp, resultFullAdd, arrayLength));
}
static void
fieldSubTest(void){
assert(ecc_isSame(one, one, arrayLength));
ecc_fieldSub(one, null, ecc_prime_m, temp);
assert(ecc_isSame(one, temp, arrayLength));
nullEverything();
ecc_fieldSub(one, one, ecc_prime_m, temp);
assert(ecc_isSame(null, temp, arrayLength));
nullEverything();
ecc_fieldSub(null, one, ecc_prime_m, temp);
assert(ecc_isSame(primeMinusOne, temp, arrayLength));
}
static void
fieldMultTest(void){
ecc_fieldMult(one, null, temp2, arrayLength);
assert(ecc_isSame(temp2, null64, arrayLength * 2));
nullEverything();
ecc_fieldMult(one, two, temp2, arrayLength);
assert(ecc_isSame(temp2, two64, arrayLength * 2));
nullEverything();
ecc_fieldMult(two, two, temp2, arrayLength);
assert(ecc_isSame(temp2, four64, arrayLength * 2));
nullEverything();
ecc_fieldMult(primeMinusOne, primeMinusOne, temp2, arrayLength);
assert(ecc_isSame(temp2, resultQuadMod, arrayLength * 2));
nullEverything();
ecc_fieldInv(two, ecc_prime_m, ecc_prime_r, temp);
ecc_fieldMult(temp, two, temp2, arrayLength);
ecc_fieldModP(temp, temp2);
assert(ecc_isSame(temp, one, arrayLength));
}
static void
fieldModPTest(void){
ecc_fieldMult(primeMinusOne, primeMinusOne, temp2, arrayLength);
ecc_fieldModP(temp, temp2);
assert(ecc_isSame(temp, one, arrayLength));
nullEverything();
ecc_fieldModP(temp, one64);
assert(ecc_isSame(temp, one, arrayLength));
nullEverything();
ecc_fieldMult(two, primeMinusOne, temp2, arrayLength);
ecc_fieldModP(temp, temp2);
assert(ecc_isSame(temp, resultDoubleMod, arrayLength));
nullEverything();
}
static void
fieldModOTest(void){
ecc_fieldMult(orderMinusOne, orderMinusOne, temp2, arrayLength);
ecc_fieldModO(temp2, temp, arrayLength * 2);
assert(ecc_isSame(temp, one, arrayLength));
nullEverything();
ecc_fieldModO(one64, temp, arrayLength * 2);
assert(ecc_isSame(temp, one, arrayLength));
nullEverything();
ecc_fieldMult(two, orderMinusOne, temp2, arrayLength);
ecc_fieldModO(temp2, temp, arrayLength * 2);
assert(ecc_isSame(temp, orderResultDoubleMod, arrayLength));
nullEverything();
}
static void
fieldInvTest(void){
nullEverything();
ecc_fieldInv(two, ecc_prime_m, ecc_prime_r, temp);
ecc_fieldMult(temp, two, temp2, arrayLength);
ecc_fieldModP(temp, temp2);
assert(ecc_isSame(one, temp, arrayLength));
nullEverything();
ecc_fieldInv(eight, ecc_prime_m, ecc_prime_r, temp);
ecc_fieldMult(temp, eight, temp2, arrayLength);
ecc_fieldModP(temp, temp2);
assert(ecc_isSame(one, temp, arrayLength));
nullEverything();
ecc_fieldInv(three, ecc_prime_m, ecc_prime_r, temp);
ecc_fieldMult(temp, three, temp2, arrayLength);
ecc_fieldModP(temp, temp2);
assert(ecc_isSame(one, temp, arrayLength));
nullEverything();
ecc_fieldInv(six, ecc_prime_m, ecc_prime_r, temp);
ecc_fieldMult(temp, six, temp2, arrayLength);
ecc_fieldModP(temp, temp2);
assert(ecc_isSame(one, temp, arrayLength));
nullEverything();
ecc_fieldInv(primeMinusOne, ecc_prime_m, ecc_prime_r, temp);
ecc_fieldMult(temp, primeMinusOne, temp2, arrayLength);
ecc_fieldModP(temp, temp2);
assert(ecc_isSame(one, temp, arrayLength));
}
#ifdef CONTIKI
PROCESS(ecc_field_test, "ECC field test");
AUTOSTART_PROCESSES(&ecc_field_test);
PROCESS_THREAD(ecc_field_test, ev, d)
{
PROCESS_BEGIN();
nullEverything();
nullEverything();
fieldAddTest();
nullEverything();
fieldSubTest();
nullEverything();
fieldMultTest();
nullEverything();
fieldModPTest();
nullEverything();
fieldModOTest();
nullEverything();
fieldInvTest();
nullEverything();
printf("%s\n", "All Tests succesfull!");
PROCESS_END();
}
#else
int main(int argc, char const *argv[])
{
(void)argc;
(void)argv;
nullEverything();
nullEverything();
fieldAddTest();
nullEverything();
fieldSubTest();
nullEverything();
fieldMultTest();
nullEverything();
fieldModPTest();
nullEverything();
fieldModOTest();
nullEverything();
fieldInvTest();
nullEverything();
printf("%s\n", "All Tests succesfull!");
return 0;
}
#endif