From what i heard or read, segmentation fault occurs when we try to write on read only memory or writing on unallocated memory. I searched the whole code putting a lot of printf to see where it happens and why but i got no better results.
See the full source HERE
Below is sample code which i suspect has something to do with segfault.
static bool init_rand(void *data, size_t size)
{
FILE *stream = fopen("/dev/urandom", "r");
if (stream == NULL)
return false;
bool ok = (fread(data, sizeof(uint8_t), size, stream) == size);
for (int i = 0; i < size; i++) {
printf("%02x", ((unsigned char *) data) [i]);
}
fclose(stream);
return ok;
}
union uint256_s
{
uint8_t i8[32];
uint16_t i16[16];
uint32_t i32[8];
uint64_t i64[4];
};
typedef union uint256_s uint256_t;
static inline uint256_t sha256(const void *data, size_t len)
{
secp256k1_sha256_t cxt;
secp256k1_sha256_initialize(&cxt);
secp256k1_sha256_write(&cxt, (uint8_t *)data, (int)len);
uint256_t res;
secp256k1_sha256_finalize(&cxt, (uint8_t *)&res);
return res;
}
static uint256_t sha256d(const void *data, size_t len)
{
uint256_t hsh = sha256(data, len);
hsh = sha256(&hsh, sizeof(hsh));
return hsh;
}
struct starp
{
uint256_t starp;
uint128_t counter;
};
static struct starp *make_starp(void)
{
struct starp *starp = (struct starp *)malloc(sizeof(struct starp));
assert(starp != NULL);
starp->counter = 0;
if (!init_rand(starp, sizeof(struct starp)))
{
fprintf(stderr, "error: failed to init random starp\n");
exit(EXIT_FAILURE);
}
if (starp->counter == 0) // Sanity check...
{
fprintf(stderr, "error: random starp initialization failed\n");
exit(EXIT_FAILURE);
}
return starp;
}
static uint256_t rand256(struct starp *starp)
{
starp->counter++;
return sha256(starp,sizeof(struct starp));
}
static uint160_t gen_hash160(const uint8_t *pub_key)
{
uint160_t hsh160 = hash160(pub_key);
return hsh160;
}
#define BASE_MAX 0xFFFF
static secp256k1_gej_t bases[BASE_MAX];
static secp256k1_scalar_t priv_bases[BASE_MAX];
#define OFFSET_MAX_COL 9
#define OFFSET_MAX_ROW BASE_MAX
#define BITS 16
#define NUM_PARTS(n) (((n) - 1) / 16 + 1)
#define NUM_INIT_WORKERS 4
static secp256k1_ge_t offsets[OFFSET_MAX_ROW][OFFSET_MAX_COL];
static secp256k1_scalar_t priv_offsets[OFFSET_MAX_ROW][OFFSET_MAX_COL];
static bool read_pub_table(FILE *stream);
static bool write_pub_table(FILE *stream);
static bool read_priv_table(FILE *stream);
static bool write_priv_table(FILE *stream);
static void *init_worker(void *arg)
{
struct starp *starp = make_starp();
size_t i = (size_t)arg;
for (size_t j = 0; j < OFFSET_MAX_ROW; j++)
{
int overflow = 0;
do
{
uint256_t x = rand256(starp);
secp256k1_scalar_set_b32(&priv_offsets[j][i], x.i8, &overflow);
}
while (overflow);
secp256k1_gej_t tmp;
secp256k1_ecmult_gen(&cxt->ecmult_gen_ctx, &tmp, &priv_offsets[j][i]);
secp256k1_ge_set_gej(&offsets[j][i], &tmp);
}
free(starp);
putchar('.');
printf(" Init Worker executes\n");
fflush(stdout);
return NULL;
}
When i replace all occurrences of sizeof(struct starp)
with other number like 12 segmentation fault occurs but if i put 48 it works.
HERE is my output and i need the program to read 12 bytes instead of 32 bytes priv key.