diff options
Diffstat (limited to 'src/gen/seed.c')
| -rw-r--r-- | src/gen/seed.c | 119 |
1 files changed, 31 insertions, 88 deletions
diff --git a/src/gen/seed.c b/src/gen/seed.c index cc26cb4..1da7f2a 100644 --- a/src/gen/seed.c +++ b/src/gen/seed.c @@ -4,45 +4,32 @@ */ #include "seed.h" -#include <sha1/sha1.h> #include "io/output.h" -#include "util/binascii.h" +#include "util/bits.h" #include "util/memory.h" seed_t *seed_new(void) { return try_calloc(sizeof(seed_t)); } -static seed_t *seed_cpy(const seed_t *src, seed_t *dest) { - if (src->hex) { - dest->hex = try_strdup(src->hex); - dest->hex_len = src->hex_len; - } - if (src->raw) { - dest->raw = try_memdup(src->raw, src->raw_len); - dest->raw_len = src->raw_len; +seed_t *seed_copy(const seed_t *src, seed_t *dest) { + if (src->seed) { + dest->seed = bits_copy(src->seed); } if (src->hash20) { dest->hash20 = try_memdup(src->hash20, 20); } if (src->W) { - dest->W = try_memdup(src->W, src->W_len); - dest->W_len = src->W_len; + dest->W = bits_copy(src->W); } return dest; } -seed_t *seed_copy(const seed_t *src, seed_t *dest) { - if (src->seed) dest->seed = gcopy(src->seed); - return seed_cpy(src, dest); -} - seed_t *seed_new_copy(const seed_t *src) { seed_t *result = seed_new(); return seed_copy(src, result); } seed_t *seed_clone(const seed_t *src, seed_t *dest) { - if (src->seed) dest->seed = gclone(src->seed); - return seed_cpy(src, dest); + return seed_copy(src, dest); } seed_t *seed_new_clone(const seed_t *src) { @@ -52,95 +39,57 @@ seed_t *seed_new_clone(const seed_t *src) { void seed_free(seed_t **seed) { if (*seed) { - if ((*seed)->seed && isclone((*seed)->seed)) { - gunclone((*seed)->seed); - } - if ((*seed)->hex) { - try_free((*seed)->hex); - } - if ((*seed)->raw) { - try_free((*seed)->raw); + if ((*seed)->seed) { + bits_free(&(*seed)->seed); } if ((*seed)->hash20) { try_free((*seed)->hash20); } + if ((*seed)->W) { + bits_free(&(*seed)->W); + } try_free(*seed); *seed = NULL; } } -static GEN seed_stoi(const char *cstr) { - pari_sp ltop = avma; - - size_t len = strlen(cstr); - char *seed_str; - if (len <= 3 || !(cstr[0] == '0' && (cstr[1] == 'x' || cstr[1] == 'X'))) { - seed_str = try_malloc((size_t)(len + 3)); - strncpy(seed_str + 2, cstr, len); - seed_str[0] = '0'; - seed_str[1] = 'x'; - } else { - seed_str = try_malloc(len + 1); - strncpy(seed_str, cstr, len); +bool seed_valid(const char *hex_str) { + size_t len = strlen(hex_str); + if (len < 40) { + return false; } - GEN i = strtoi(seed_str); - - try_free(seed_str); - return gerepilecopy(ltop, i); -} - -static char *seed_itos(GEN seed) { - pari_sp ltop = avma; - char *result = pari_sprintf("%Px", seed); - char *seed_str = try_strdup(result); - - avma = ltop; - return seed_str; -} - -static char *seed_strip(const char *cstr) { - char *seed_str = try_malloc(strlen(cstr) + 1); - char *prefix = strstr(cstr, "0x"); - if (prefix != NULL) { - strcpy(seed_str, cstr + 2); - } else { - strcpy(seed_str, cstr); + const char *str_start = hex_str; + if (hex_str[0] == '0' && (hex_str[1] == 'x' || hex_str[1] == 'X')) { + str_start = hex_str + 2; + } + while (*str_start != 0) { + char c = *str_start++; + if (!isxdigit(c)) return false; } - return seed_str; + return true; } -static void seed_raw(seed_t *seed) { - seed->raw = binascii_itob(seed->seed, ENDIAN_BIG); - seed->raw_len = binascii_blen(seed->seed); +static bits_t *seed_stoi(const char *cstr) { + const char *seed_str = cstr; + const char *prefix = strstr(cstr, "0x"); + if (prefix != NULL) seed_str = prefix + 2; + return bits_from_hex(seed_str); } static void seed_hash(seed_t *seed) { seed->hash20 = try_malloc(20); - SHA_CTX ctx = {}; - SHA1_Init(&ctx); - SHA1_Update(&ctx, seed->raw, (int)seed->raw_len); - SHA1_Final(seed->hash20, &ctx); + bits_sha1(seed->seed, seed->hash20); } static void seed_W(seed_t *seed, const config_t *cfg) { GEN t = utoi(cfg->bits); GEN s = floorr(rdivii(subis(t, 1), stoi(160), DEFAULTPREC)); GEN h = subii(t, mulis(s, 160)); - GEN hash = binascii_btoi(seed->hash20, 20, ENDIAN_BIG); - GEN mask = subis(int2n(itos(h)), 1); - // TODO: what if I get zeros at the beginning? 0123 == 123 for PARI t_INT - // I should just convert to a t_VECSMALL of bits from the seed->hash and do - // everything with that. - // That's alot of custom code to handle bit strings. - GEN c0 = ibitand(hash, mask); } GENERATOR(seed_gen_random) { seed_t *seed = seed_new(); - seed->seed = random_int(160); - seed->hex = seed_itos(seed->seed); - seed->hex_len = strlen(seed->hex); - seed_raw(seed); + seed->seed = bits_from_i(random_int(160)); seed_hash(seed); seed_W(seed, cfg); curve->seed = seed; @@ -150,9 +99,6 @@ GENERATOR(seed_gen_random) { GENERATOR(seed_gen_argument) { seed_t *seed = seed_new(); seed->seed = seed_stoi(cfg->seed); - seed->hex = seed_strip(cfg->seed); - seed->hex_len = strlen(seed->hex); - seed_raw(seed); seed_hash(seed); seed_W(seed, cfg); curve->seed = seed; @@ -164,7 +110,7 @@ GENERATOR(seed_gen_input) { GEN str = input_string("seed:"); const char *cstr = GSTR(str); - if (strlen(cstr) < 40) { + if (!seed_valid(cstr)) { fprintf(err, "SEED must be at least 160 bits(40 hex characters).\n"); avma = ltop; return 0; @@ -172,9 +118,6 @@ GENERATOR(seed_gen_input) { seed_t *seed = seed_new(); seed->seed = seed_stoi(cstr); - seed->hex = seed_strip(cstr); - seed->hex_len = strlen(seed->hex); - seed_raw(seed); seed_hash(seed); seed_W(seed, cfg); curve->seed = seed; |
