-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add trinary and input validators. (#13)
* added trits/trytes validations. * Added input validators. * fixed code style and typo. * Added test_is_address_with_checksum.
- Loading branch information
Sam Chen
authored
Feb 3, 2020
1 parent
50aeee0
commit f0afbcb
Showing
14 changed files
with
471 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2020 IOTA Stiftung | ||
* https://github.com/iotaledger/iota_common | ||
* | ||
* Refer to the LICENSE file for licensing information | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <unity/unity.h> | ||
|
||
#include "common/trinary/trits.h" | ||
|
||
void test_is_trits(void) { | ||
const trit_t tryte_LUT[27][3] = { | ||
{0, 0, 0}, {1, 0, 0}, {-1, 1, 0}, {0, 1, 0}, {1, 1, 0}, {-1, -1, 1}, {0, -1, 1}, {1, -1, 1}, {-1, 0, 1}, | ||
{0, 0, 1}, {1, 0, 1}, {-1, 1, 1}, {0, 1, 1}, {1, 1, 1}, {-1, -1, -1}, {0, -1, -1}, {1, -1, -1}, {-1, 0, -1}, | ||
{0, 0, -1}, {1, 0, -1}, {-1, 1, -1}, {0, 1, -1}, {1, 1, -1}, {-1, -1, 0}, {0, -1, 0}, {1, -1, 0}, {-1, 0, 0}}; | ||
|
||
for (size_t i = 0; i < 27; i++) { | ||
printf("tryte LUT checking %ld\n", i); | ||
TEST_ASSERT(is_trits(tryte_LUT[i], 3) == true); | ||
} | ||
|
||
const trit_t ng_tryte[6][3] = {{2, 0, 0}, {1, 3, 0}, {-1, 1, 4}, {-2, 1, 0}, {1, -3, 0}, {-1, -1, -4}}; | ||
for (size_t i = 0; i < 6; i++) { | ||
printf("ng_tryte checking %ld\n", i); | ||
TEST_ASSERT(is_trits(ng_tryte[i], 3) == false); | ||
} | ||
|
||
const trit_t trytes_LUT[14][6] = {{0, 0, 0, 1, 0, 0}, {-1, 1, 0, 0, 1, 0}, {1, 1, 0, -1, -1, 1}, | ||
{0, -1, 1, 1, -1, 1}, {-1, 0, 1, 0, 0, 1}, {1, 0, 1, -1, 1, 1}, | ||
{0, 1, 1, 1, 1, 1}, {-1, -1, -1, 0, -1, -1}, {1, -1, -1, -1, 0, -1}, | ||
{0, 0, -1, 1, 0, -1}, {-1, 1, -1, 0, 1, -1}, {1, 1, -1, -1, -1, 0}, | ||
{0, -1, 0, 1, -1, 0}, {-1, 0, 0, 1, 1, 1}}; | ||
for (size_t i = 0; i < 14; i++) { | ||
printf("trytes LUT checking %ld\n", i); | ||
TEST_ASSERT(is_trits(trytes_LUT[i], 6) == true); | ||
} | ||
|
||
const trit_t ng_trytes[14][6] = {{2, 0, 0, 1, 0, 0}, {-1, 3, 0, 0, 1, 0}, {1, 1, 4, -1, -1, 1}, | ||
{0, -1, 1, 5, -1, 1}, {-1, 0, 1, 0, 6, 1}, {1, 0, 1, -1, 1, 7}, | ||
{-2, 1, 1, 1, 1, 1}, {-1, -3, -1, 0, -1, -1}, {1, -1, -4, -1, 0, -1}, | ||
{0, 0, -1, -5, 0, -1}, {-1, 1, -1, 0, -6, -1}, {1, 1, -1, -1, -1, -7}, | ||
{0, -1, 0, 1, -10, 0}, {-1, 10, 0, 1, 1, 1}}; | ||
for (size_t i = 0; i < 14; i++) { | ||
printf("ng_trytes LUT checking %ld\n", i); | ||
TEST_ASSERT(is_trits(ng_trytes[i], 6) == false); | ||
} | ||
} | ||
|
||
int main(void) { | ||
UNITY_BEGIN(); | ||
|
||
RUN_TEST(test_is_trits); | ||
|
||
return UNITY_END(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2020 IOTA Stiftung | ||
* https://github.com/iotaledger/iota_common | ||
* | ||
* Refer to the LICENSE file for licensing information | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <unity/unity.h> | ||
|
||
#include "common/trinary/tryte.h" | ||
|
||
void test_is_trytes() { | ||
tryte_t *hash0 = (tryte_t *)"XUERGHWTYRTFUYKFKXURKHMFEVLOIFTTCNTXOGLDPCZ9CJLKHROOPGNAQYFJEPGK9OKUQROUECBAVNXRX"; | ||
TEST_ASSERT(is_trytes(hash0, 81) == true); | ||
// length is unmatched | ||
TEST_ASSERT(is_trytes(hash0, 20) == false); | ||
|
||
// hash with lowercases | ||
tryte_t *hash1 = (tryte_t *)"XUERGHWTYRTFUYKFKXURKHMFEVLOIFTTCNTXOGLDPCZ9cjlkhroopgnaqyfjepgk9okuqrouecbavnxrx"; | ||
TEST_ASSERT(is_trytes(hash1, 81) == false); | ||
|
||
// hash with invalid tryte | ||
tryte_t *hash2 = (tryte_t *)"XUERGH3TYRTFUYKFKXURKHMF5VLOIFTTCNTXOGLDPCZ9CJLKHR0OPGNAQYFJEPGK9OKUQROUECBAVNXRX"; | ||
TEST_ASSERT(is_trytes(hash2, 81) == false); | ||
} | ||
|
||
int main(void) { | ||
UNITY_BEGIN(); | ||
|
||
RUN_TEST(test_is_trytes); | ||
|
||
return UNITY_END(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright (c) 2020 IOTA Stiftung | ||
* https://github.com/iotaledger/iota_common | ||
* | ||
* Refer to the LICENSE file for licensing information | ||
*/ | ||
|
||
#include "common/trinary/trits.h" | ||
|
||
bool is_trits(trit_t const *const trits, size_t len) { | ||
for (size_t i = 0; i < len; i++) { | ||
if (trits[i] < -1 || trits[i] > 1) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) 2020 IOTA Stiftung | ||
* https://github.com/iotaledger/iota_common | ||
* | ||
* Refer to the LICENSE file for licensing information | ||
*/ | ||
|
||
#include <string.h> | ||
|
||
#include "common/trinary/tryte.h" | ||
|
||
bool is_trytes(tryte_t const *const trytes, size_t len) { | ||
size_t trytes_cnt = strlen((char *)trytes); | ||
if (trytes_cnt != len) { | ||
// length does not match. | ||
return false; | ||
} | ||
|
||
for (size_t i = 0; i < len; i++) { | ||
if (trytes[i] != '9' && (trytes[i] < 'A' || trytes[i] > 'Z')) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2020 IOTA Stiftung | ||
* https://github.com/iotaledger/iota_common | ||
* | ||
* Refer to the LICENSE file for licensing information | ||
*/ | ||
|
||
#include "utils/input_validators.h" | ||
#include "common/model/transaction.h" | ||
|
||
bool is_tryte_with_trailing_zero(tryte_t const tryte) { | ||
if (tryte == '9' || (tryte >= 'A' && tryte <= 'D') || (tryte >= 'W' && tryte <= 'Z')) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool is_address(tryte_t const *const address) { | ||
if (is_trytes(address, NUM_TRYTES_ADDRESS)) { | ||
return is_tryte_with_trailing_zero(address[NUM_TRYTES_ADDRESS - 1]); | ||
} | ||
return false; | ||
} | ||
|
||
bool is_address_with_checksum(tryte_t const *const address) { | ||
if (is_trytes(address, NUM_TRYTES_ADDRESS + NUM_TRYTES_CHECKSUM)) { | ||
return is_tryte_with_trailing_zero(address[NUM_TRYTES_ADDRESS - 1]); | ||
} | ||
return false; | ||
} | ||
|
||
bool is_tag(tryte_t const *const tag) { return is_trytes(tag, NUM_TRYTES_TAG); } | ||
|
||
bool is_seed(tryte_t const *const seed) { return is_trytes(seed, NUM_TRYTES_HASH); } | ||
|
||
bool is_security_level(uint8_t level) { return (level >= 1 && level <= 3); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (c) 2020 IOTA Stiftung | ||
* https://github.com/iotaledger/iota_common | ||
* | ||
* Refer to the LICENSE file for licensing information | ||
*/ | ||
|
||
#ifndef __INPUT_VALIDATORS_H__ | ||
#define __INPUT_VALIDATORS_H__ | ||
|
||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "common/trinary/trits.h" | ||
#include "common/trinary/tryte.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* @brief Checks if a tryte with a trailing zero trit. | ||
* | ||
* @param[in] tryte A tryte | ||
* @return boolean | ||
*/ | ||
bool is_tryte_with_trailing_zero(tryte_t const tryte); | ||
|
||
/** | ||
* @brief Checks if the given address is valid. | ||
* | ||
* An address should be 81 trytes with a zero-trit at tail. | ||
* | ||
* @param[in] address A hash string. | ||
* @return boolean | ||
*/ | ||
bool is_address(tryte_t const* const address); | ||
|
||
/** | ||
* @brief Checks if address hash contains checksum | ||
* | ||
* It won't validate checksum, checks the hash length only. | ||
* | ||
* @param[in] address A hash string. | ||
* @return boolean | ||
*/ | ||
bool is_address_with_checksum(tryte_t const* const address); | ||
|
||
/** | ||
* @brief Checks if the given hash is a tag. | ||
* | ||
* @param[in] tag A hash string. | ||
* @return boolean | ||
*/ | ||
bool is_tag(tryte_t const* const tag); | ||
|
||
/** | ||
* @brief Checks if the given hash is a seed. | ||
* | ||
* @param[in] seed A hash string. | ||
* @return boolean | ||
*/ | ||
bool is_seed(tryte_t const* const seed); | ||
|
||
/** | ||
* @brief Checks if the given number is a security level. | ||
* | ||
* @param[in] level An integer. | ||
* @return boolean | ||
*/ | ||
bool is_security_level(uint8_t level); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // __INPUT_VALIDATORS_H__ |
Oops, something went wrong.