samedi 18 février 2023

how to correctly replace bytes with memset?

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 of which i am expecting 46 zeroes. Now i am wondering if there is something like zfill that can correctly fill 23 bytes with zeros.

 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;
}

how can i correctly patch first 23 bytes?




Aucun commentaire:

Enregistrer un commentaire