#include "slip39.h"

uint8_t test_round_function(void);
uint8_t test_encrypt_function(void);


uint8_t test_round_function(void) {
    uint8_t fail = 0;

    // data from this test was taken from running the round function
    // on pass one of encrypting the string "abcd" with the empty
    // password, and a group identifier of 1234
    uint8_t salt[] = { 's', 'h', 'a', 'm', 'i', 'r', 4, 210 };
    uint8_t r[] = { 'c', 'd' };
    uint8_t d[2];

    round_function(0,"",0,salt,8,r,2,d,2);

    if(!(d[0] == 183 && d[1] == 32)) {
        fail = 1;
    }

    round_function(0,"TREZOR",0,salt,8,r,2,d,2);
    if(!(d[0] == 156 && d[1] == 169)) {
        fail = 1;
    }

    return fail;
}

uint8_t test_encrypt_function(void) {
    uint8_t fail = 0;

    // Data for this test was generated by running the equivalent
    // _encrypt() function in the python reference code
    char *input = "abcd";
    uint8_t output[4];

    slip39_encrypt((uint8_t *)input, 4, "", 0, 1234, output);

    if(!(output[0] == 167 && output[1] == 251 && output[2]==61 && output[3] == 147)) {
        fail = 1;
    }


    slip39_encrypt((uint8_t *)input, 4, "TREZOR", 0, 1234, output);
    if( !(output[0] == 41 && output[1] == 155 && output[2]==1 && output[3] == 50) ) {
        fail = 1;
    }

    return fail;
}

int main(void) {
    uint8_t fail  = 0;
    uint8_t t;

    t=test_round_function();
    fail = fail || t;
    printf("test round function: %s\n", t ? "fail" : "pass");

    t=test_encrypt_function();
    fail = fail || t;
    printf("test encrypt function: %s\n", t ? "fail" : "pass");

    return fail;
}
