X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=shamirs;a=blobdiff_plain;f=shamirssecret.c;h=30115cc2d829203b17add93efa9b3d61db7d45cb;hp=cc3e71334a1277367c2beae82bd05f9496945287;hb=1e75ce31ed0f5451806e3c9716d1edd382afd2b4;hpb=7564918dc5ea2af16b99cac19bd158cf075ceded diff --git a/shamirssecret.c b/shamirssecret.c index cc3e713..30115cc 100644 --- a/shamirssecret.c +++ b/shamirssecret.c @@ -102,6 +102,11 @@ static uint8_t field_pow_ret(uint8_t calc, uint8_t a, uint8_t e) { return ret; } static uint8_t field_pow(uint8_t a, uint8_t e) { +#ifndef TEST + // Although this function works for a==0, its not trivially obvious why, + // and since we never call with a==0, we just assert a != 0 (except when testing) + assert(a != 0); +#endif return field_pow_ret(exp[(log[a] * e) % 255], a, e); } @@ -185,7 +190,7 @@ int main(int argc, char* argv[]) { assert(mlockall(MCL_CURRENT | MCL_FUTURE) == 0); char split = 0; - uint8_t n = 0, k = 0; + uint8_t total_shares = 0, k = 0; char* files[P]; uint8_t files_count = 0; char *in_file = (void*)0, *out_file_param = (void*)0; @@ -209,7 +214,7 @@ int main(int argc, char* argv[]) { if (t <= 0 || t >= P) ERROREXIT("n must be > 0 and < %u\n", P) else - n = t; + total_shares = t; break; } case 'k': { @@ -248,10 +253,10 @@ int main(int argc, char* argv[]) { ERROREXIT("Invalid argument\n") if (split) { - if (!n || !k) + if (!total_shares || !k) ERROREXIT("n and k must be set.\n") - if (k > n) + if (k > total_shares) ERROREXIT("k must be <= n\n") if (files_count != 0 || !in_file || !out_file_param) @@ -273,14 +278,14 @@ int main(int argc, char* argv[]) { fclose(secret_file); printf("Using secret of length %lu\n", secret_length); - uint8_t a[k], D[n][secret_length]; + uint8_t a[k], D[total_shares][secret_length]; for (uint32_t i = 0; i < secret_length; i++) { a[0] = secret[i]; for (uint8_t j = 1; j < k; j++) assert(fread(&a[j], sizeof(uint8_t), 1, random) == 1); - for (uint8_t j = 0; j < n; j++) + for (uint8_t j = 0; j < total_shares; j++) D[j][i] = calculateQ(a, k, j+1); if (i % 32 == 0 && i != 0) @@ -289,7 +294,7 @@ int main(int argc, char* argv[]) { char out_file_name_buf[strlen(out_file_param) + 4]; strcpy(out_file_name_buf, out_file_param); - for (uint8_t i = 0; i < n; i++) { + for (uint8_t i = 0; i < total_shares; i++) { /*printf("%u-", i); for (uint8_t j = 0; j < secret_length; j++) printf("%02x", D[i][j]);