I need to patch first 23 bytes of 32 bytes randomly generated by the rand256
function but it seems memset
isn't correctly patching.
0000000000000000000000C90F4094919B9FAE4616149C0FDC61E7C1F318E234
as you can see, memset
added only 23 zeroes whereas I am expecting 46 zeroes.
Now I am wondering if there is something like zfill
that can correctly fill 23 bytes with zeros.
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 uint256_t rand256(struct seed *seed)
{
seed->counter++;
return sha256(seed, sizeof(struct seed));
}
static uint256_t rand256_and_mask(struct seed *seed)
{
uint256_t r = rand256(seed);
memset(&r, 0x000, 23);
return r;
}
struct seed {
uint256_t seed;
uint128_t counter;
};
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 void *init_worker(void *arg)
{
struct seed *seed = make_seed();
size_t i = (size_t)arg;
for (size_t j = 0; j < OFFSET_MAX_ROW; j++)
{
int overflow = 0;
do
{
uint256_t x = rand256_and_mask(seed);
secp256k1_scalar_set_b32(&priv_offsets[j][i], x.i8 + 12, &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(seed);
putchar('.');
fflush(stdout);
return NULL;
}
how can I correctly patch first 23 bytes?