Bring compute_powers() closer to the spec

This commit is contained in:
George Kadianakis 2022-11-29 19:03:19 +02:00
parent 94aa388055
commit 80fe1cb3e0
1 changed files with 14 additions and 10 deletions

View File

@ -825,10 +825,12 @@ void free_trusted_setup(KZGSettings *s) {
free_kzg_settings(s); free_kzg_settings(s);
} }
static void compute_powers(fr_t out[], uint64_t n) { static void compute_powers(fr_t out[], BLSFieldElement *x, uint64_t n) {
out[0] = fr_one; BLSFieldElement current_power = fr_one;
for (uint64_t i = 2; i < n; i++) for (uint64_t i = 0; i < n; i++) {
fr_mul(&out[i], &out[i-1], &out[1]); out[i] = current_power;
fr_mul(&current_power, &current_power, x);
}
} }
void bytes_to_bls_field(BLSFieldElement *out, const uint8_t bytes[32]) { void bytes_to_bls_field(BLSFieldElement *out, const uint8_t bytes[32]) {
@ -1119,17 +1121,19 @@ static C_KZG_RET compute_challenges(BLSFieldElement *out, BLSFieldElement r_powe
uint8_t hashed_data[32] = {0}; uint8_t hashed_data[32] = {0};
hash(hashed_data, bytes, nb); hash(hashed_data, bytes, nb);
uint8_t r[32] = {0}; /* Compute r */
uint8_t r_bytes[32] = {0};
uint8_t hash_input_0[33] = {0}; // hashed_data + b'\x00' uint8_t hash_input_0[33] = {0}; // hashed_data + b'\x00'
memcpy(hash_input_0, hashed_data, 32); memcpy(hash_input_0, hashed_data, 32);
hash_input_0[32] = 0x0; hash_input_0[32] = 0x0;
hash(r, hash_input_0, 33); hash(r_bytes, hash_input_0, 33);
if (n > 0) { /* Compute r_powers */
if (n > 1) bytes_to_bls_field(&r_powers[1], r); BLSFieldElement r;
compute_powers(r_powers, n); bytes_to_bls_field(&r, r_bytes);
} compute_powers(r_powers, &r, n);
/* Compute eval_challenge */
uint8_t eval_challenge[32] = {0}; uint8_t eval_challenge[32] = {0};
uint8_t hash_input_1[33] = {0}; // hashed_data + b'\x01' uint8_t hash_input_1[33] = {0}; // hashed_data + b'\x01'
memcpy(hash_input_1, hashed_data, 32); memcpy(hash_input_1, hashed_data, 32);