Skip to content
Permalink
Browse files

function documentation and add test runner to test.sh

  • Loading branch information...
enzoc4 committed Mar 12, 2019
1 parent e9962ed commit b1621bfecd4c26f622c60f0060b55fa0482ed2ab
Showing with 48 additions and 19 deletions.
  1. +33 −11 cardano-c/cardano.h
  2. +1 −1 cardano-c/src/bip39.rs
  3. +8 −0 cardano-c/test.sh
  4. +6 −7 cardano-c/test/test_bip39_entropy.c
@@ -17,39 +17,61 @@ typedef int cardano_result;
/* bip39 error definitions */
typedef enum _bip39_config_error
{
SUCCESS = 0,
INVALID_MNEMONIC = 1,
INVALID_CHECKSUM = 2,
INVALID_WORD_COUNT = 3
BIP39_SUCCESS = 0,
BIP39_INVALID_MNEMONIC = 1,
BIP39_INVALID_CHECKSUM = 2,
BIP39_INVALID_WORD_COUNT = 3
} cardano_bip39_error_t;


/* Error descriptions */
struct _errordesc {
int code;
char *message;
} errordesc[] = {
{ SUCCESS, "No error" },
{ INVALID_MNEMONIC, "Invalid mnemonic word" },
{ INVALID_CHECKSUM, "Invalid checksum" },
{ INVALID_WORD_COUNT, "The word count should be one of: 9, 12, 15, 18, 21, 24"}
{ BIP39_SUCCESS, "No error" },
{ BIP39_INVALID_MNEMONIC, "Invalid mnemonic word" },
{ BIP39_INVALID_CHECKSUM, "Invalid checksum" },
{ BIP39_INVALID_WORD_COUNT, "The word count should be one of: 9, 12, 15, 18, 21, 24"}
};

typedef uint8_t* cardano_entropy;

/*!
* \brief get entropy array from the given english mnemonics
* \param [in] mnemonics a string consisting of 9, 12, 15, 18, 21 or 24 english words
* \param [out] entropy the returned entropy array
* \param [out] entropy_size the size of the the returned array
* \returns BIP39_SUCCESS or either BIP39_INVALID_MNEMONIC or BIP39_INVALID_CHECKSUM
*/
cardano_bip39_error_t cardano_entropy_from_english_mnemonics(
const char *mnemonics,
cardano_entropy *entropy,
uint32_t *entropy_size
);
cardano_bip39_error_t cardano_generate_random_entropy(

/*!
* \brief encode a entropy into its equivalent words represented by their index (0 to 2047) in the BIP39 dictionary
* \param [in] number_of_words one of 9, 12, 15, 18, 21 or 24 representing the number of words of the equivalent mnemonic
* \param [in] random_generator a function that generates random bytes
* \param [out] entropy the returned entropy array
* \param [out] entropy_size the size of the the returned array
* \returns BIP39_SUCCESS or BIP39_INVALID_WORD_COUNT
*/
cardano_bip39_error_t cardano_entropy_from_random(
uint8_t number_of_words,
uint8_t (*random_generator)(),
cardano_entropy *entropy,
uint32_t *entropy_size
);

void cardano_delete_entropy_array(uint8_t *entropy, uint32_t bytes);
/*!
* delete the allocated memory of entropy byte array
* \param [in] entropy the entropy array
* \param [in] entropy_size the length of the entropy array
* \sa cardano_entropy_from_random()
* \sa cardano_entropy_from_english_mnemonics()
*/
void cardano_delete_entropy_array(uint8_t *entropy, uint32_t entropy_size);

cardano_result cardano_bip39_encode(const char * const entropy_raw, unsigned long entropy_size, unsigned short *mnemonic_index, unsigned long mnemonic_size);

@@ -60,7 +60,7 @@ pub extern "C" fn cardano_entropy_from_english_mnemonics(

///generate entropy from the given random generator
#[no_mangle]
pub extern "C" fn cardano_generate_random_entropy(
pub extern "C" fn cardano_entropy_from_random(
words: u8,
gen: extern "C" fn() -> c_uchar,
entropy_ptr: *mut *const c_uchar,
@@ -24,3 +24,11 @@ echo "######################################################################"
echo ""
echo "######################################################################"
rm test-cardano-c.$$


gcc -o test-cardano-c.$$ -I "${C_ROOT}" "${C_ROOT}test/test_bip39_entropy.c" "${C_ROOT}test/unity/unity.c" "${PROJECT_ROOT}target/debug/libcardano_c.a" -lpthread -lm -ldl
echo "######################################################################"
./test-cardano-c.$$
echo ""
echo "######################################################################"
rm test-cardano-c.$$
@@ -15,13 +15,13 @@ void test_generate_entropy_from_mnemonics(void) {
}

void test_generate_entropy_from_mnemonics_error_code_invalid_word(void) {
static const char *mnemonics = "termo abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
static const char *mnemonics = "notaword abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";

cardano_entropy entropy;
uint32_t bytes;
int error = cardano_entropy_from_english_mnemonics(mnemonics, &entropy, &bytes);

TEST_ASSERT_EQUAL_HEX32(INVALID_MNEMONIC, error);
TEST_ASSERT_EQUAL_HEX32(BIP39_INVALID_MNEMONIC, error);
}

void test_generate_entropy_from_mnemonics_invalid_checksum(void) {
@@ -31,7 +31,7 @@ void test_generate_entropy_from_mnemonics_invalid_checksum(void) {
uint32_t bytes;
int error = cardano_entropy_from_english_mnemonics(mnemonics, &entropy, &bytes);

TEST_ASSERT_EQUAL_HEX32(INVALID_CHECKSUM, error);
TEST_ASSERT_EQUAL_HEX32(BIP39_INVALID_CHECKSUM, error);
}

uint8_t gen() {
@@ -42,7 +42,7 @@ void test_generate_entropy_from_random_generator(void) {
const uint8_t NUMBER_OF_WORDS = 12;
cardano_entropy entropy;
uint32_t bytes;
cardano_bip39_error_t error = cardano_generate_random_entropy(NUMBER_OF_WORDS, gen, &entropy, &bytes);
cardano_bip39_error_t error = cardano_entropy_from_random(NUMBER_OF_WORDS, gen, &entropy, &bytes);

uint8_t expected[16] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

@@ -52,14 +52,13 @@ void test_generate_entropy_from_random_generator(void) {
cardano_delete_entropy_array(entropy, bytes);
}


void test_generate_entropy_from_random_generator_word_count_error(void) {
const uint8_t NUMBER_OF_WORDS = 13;
cardano_entropy entropy;
uint32_t bytes;
cardano_bip39_error_t error = cardano_generate_random_entropy(NUMBER_OF_WORDS, gen, &entropy, &bytes);
cardano_bip39_error_t error = cardano_entropy_from_random(NUMBER_OF_WORDS, gen, &entropy, &bytes);

TEST_ASSERT_EQUAL_HEX32(INVALID_WORD_COUNT, error);
TEST_ASSERT_EQUAL_HEX32(BIP39_INVALID_WORD_COUNT, error);
}

int main(void) {

0 comments on commit b1621bf

Please sign in to comment.
You can’t perform that action at this time.