Merge pull request #15 from asn-d6/compute_challenges_spec
Bring compute_challenges() and compute_powers() closer to the spec
This commit is contained in:
commit
7b3477c6e9
|
@ -825,10 +825,12 @@ void free_trusted_setup(KZGSettings *s) {
|
|||
free_kzg_settings(s);
|
||||
}
|
||||
|
||||
static void compute_powers(fr_t out[], uint64_t n) {
|
||||
out[0] = fr_one;
|
||||
for (uint64_t i = 2; i < n; i++)
|
||||
fr_mul(&out[i], &out[i-1], &out[1]);
|
||||
static void compute_powers(BLSFieldElement out[], BLSFieldElement *x, uint64_t n) {
|
||||
BLSFieldElement current_power = fr_one;
|
||||
for (uint64_t i = 0; i < n; i++) {
|
||||
out[i] = current_power;
|
||||
fr_mul(¤t_power, ¤t_power, x);
|
||||
}
|
||||
}
|
||||
|
||||
void bytes_to_bls_field(BLSFieldElement *out, const uint8_t bytes[32]) {
|
||||
|
@ -1098,34 +1100,46 @@ static C_KZG_RET compute_challenges(BLSFieldElement *out, BLSFieldElement r_powe
|
|||
const size_t np = ni + n * BYTES_PER_BLOB;
|
||||
const size_t nb = np + n * 48;
|
||||
|
||||
uint8_t* bytes = calloc(nb + (n == 0), sizeof(uint8_t)); // need at least 1 byte more than ni for hash later
|
||||
uint8_t* bytes = calloc(nb, sizeof(uint8_t));
|
||||
if (bytes == NULL) return C_KZG_MALLOC;
|
||||
|
||||
/* Copy domain seperator */
|
||||
memcpy(bytes, FIAT_SHAMIR_PROTOCOL_DOMAIN, 16);
|
||||
bytes_of_uint64(&bytes[16], FIELD_ELEMENTS_PER_BLOB);
|
||||
bytes_of_uint64(&bytes[16 + 8], n);
|
||||
|
||||
/* Copy polynomials */
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < FIELD_ELEMENTS_PER_BLOB; j++)
|
||||
bytes_from_bls_field(&bytes[ni + BYTES_PER_FIELD_ELEMENT * (i * FIELD_ELEMENTS_PER_BLOB + j)], &polys[i][j]);
|
||||
|
||||
/* Copy commitments */
|
||||
for (i = 0; i < n; i++)
|
||||
bytes_from_g1(&bytes[np + i * 48], &comms[i]);
|
||||
|
||||
uint8_t hash_output[32] = {0};
|
||||
hash(hash_output, bytes, nb);
|
||||
memcpy(bytes, hash_output, 32);
|
||||
bytes[32] = 0x0;
|
||||
hash(hash_output, bytes, 33);
|
||||
/* Now let's create challenges! */
|
||||
uint8_t hashed_data[32] = {0};
|
||||
hash(hashed_data, bytes, nb);
|
||||
|
||||
if (n > 0) {
|
||||
if (n > 1) bytes_to_bls_field(&r_powers[1], hash_output);
|
||||
compute_powers(r_powers, n);
|
||||
}
|
||||
/* We will use hash_input in the computation of both challenges */
|
||||
uint8_t hash_input[33];
|
||||
|
||||
bytes[32] = 0x1;
|
||||
hash(hash_output, bytes, 33);
|
||||
bytes_to_bls_field(out, hash_output);
|
||||
/* Compute r */
|
||||
uint8_t r_bytes[32] = {0};
|
||||
memcpy(hash_input, hashed_data, 32);
|
||||
hash_input[32] = 0x0;
|
||||
hash(r_bytes, hash_input, 33);
|
||||
|
||||
/* Compute r_powers */
|
||||
BLSFieldElement r;
|
||||
bytes_to_bls_field(&r, r_bytes);
|
||||
compute_powers(r_powers, &r, n);
|
||||
|
||||
/* Compute eval_challenge */
|
||||
uint8_t eval_challenge[32] = {0};
|
||||
hash_input[32] = 0x1;
|
||||
hash(eval_challenge, hash_input, 33);
|
||||
bytes_to_bls_field(out, eval_challenge);
|
||||
|
||||
free(bytes);
|
||||
return C_KZG_OK;
|
||||
|
|
Loading…
Reference in New Issue