Merge pull request #24 from ethereum/no_file

Load trusted setup from arguments
This commit is contained in:
Ramana Kumar 2022-12-11 18:23:38 +00:00 committed by GitHub
commit 0f54189400
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 28 deletions

View File

@ -13,7 +13,7 @@ KZGSettings* load_trusted_setup_wrap(const char* file) {
if (f == NULL) { free(out); return NULL; } if (f == NULL) { free(out); return NULL; }
if (load_trusted_setup(out, f) != C_KZG_OK) { free(out); fclose(f); return NULL; } if (load_trusted_setup_file(out, f) != C_KZG_OK) { free(out); fclose(f); return NULL; }
fclose(f); fclose(f);
return out; return out;

View File

@ -48,7 +48,7 @@ JNIEXPORT void JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_loadTrustedSetup(JNIEn
return; return;
} }
C_KZG_RET ret = load_trusted_setup(settings, f); C_KZG_RET ret = load_trusted_setup_file(settings, f);
if (ret != C_KZG_OK) if (ret != C_KZG_OK)
{ {

View File

@ -90,7 +90,7 @@ Napi::Value LoadTrustedSetup(const Napi::CallbackInfo& info) {
return env.Null(); return env.Null();
} }
if (load_trusted_setup(kzg_settings, f) != C_KZG_OK) { if (load_trusted_setup_file(kzg_settings, f) != C_KZG_OK) {
free(kzg_settings); free(kzg_settings);
Napi::Error::New(env, "Error loading trusted setup file").ThrowAsJavaScriptException(); Napi::Error::New(env, "Error loading trusted setup file").ThrowAsJavaScriptException();
return env.Null(); return env.Null();

View File

@ -22,7 +22,7 @@ static PyObject* load_trusted_setup_wrap(PyObject *self, PyObject *args) {
if (s == NULL) return PyErr_NoMemory(); if (s == NULL) return PyErr_NoMemory();
if (load_trusted_setup(s, fopen(PyUnicode_AsUTF8(f), "r")) != C_KZG_OK) { if (load_trusted_setup_file(s, fopen(PyUnicode_AsUTF8(f), "r")) != C_KZG_OK) {
free(s); free(s);
return PyErr_Format(PyExc_RuntimeError, "error loading trusted setup"); return PyErr_Format(PyExc_RuntimeError, "error loading trusted setup");
} }

View File

@ -770,39 +770,26 @@ static void bytes_from_bls_field(uint8_t out[32], const BLSFieldElement *in) {
blst_scalar_from_fr((blst_scalar*)out, in); blst_scalar_from_fr((blst_scalar*)out, in);
} }
C_KZG_RET load_trusted_setup(KZGSettings *out, FILE *in) { C_KZG_RET load_trusted_setup(KZGSettings *out, const uint8_t g1_bytes[], size_t n1, const uint8_t g2_bytes[], size_t n2) {
uint64_t i; uint64_t i;
int j; uint8_t c[96];
blst_p2_affine g2_affine; blst_p2_affine g2_affine;
g1_t *g1_projective; g1_t *g1_projective;
fscanf(in, "%" SCNu64, &i); TRY(new_g1_array(&out->g1_values, n1));
CHECK(i == FIELD_ELEMENTS_PER_BLOB); TRY(new_g2_array(&out->g2_values, n2));
fscanf(in, "%" SCNu64, &i);
CHECK(i == 65);
TRY(new_g1_array(&out->g1_values, FIELD_ELEMENTS_PER_BLOB)); TRY(new_g1_array(&g1_projective, n1));
TRY(new_g2_array(&out->g2_values, 65));
TRY(new_g1_array(&g1_projective, FIELD_ELEMENTS_PER_BLOB)); for (i = 0; i < n1; i++)
bytes_to_g1(&g1_projective[i], &g1_bytes[48 * i]);
for (i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++) { for (i = 0; i < n2; i++) {
for (j = 0; j < 48; j++) { blst_p2_uncompress(&g2_affine, &g2_bytes[96 * i]);
fscanf(in, "%2hhx", &c[j]);
}
bytes_to_g1(&g1_projective[i], c);
}
for (i = 0; i < 65; i++) {
for (j = 0; j < 96; j++) {
fscanf(in, "%2hhx", &c[j]);
}
blst_p2_uncompress(&g2_affine, c);
blst_p2_from_affine(&out->g2_values[i], &g2_affine); blst_p2_from_affine(&out->g2_values[i], &g2_affine);
} }
unsigned int max_scale = 0; unsigned int max_scale = 0;
while (((uint64_t)1 << max_scale) < FIELD_ELEMENTS_PER_BLOB) max_scale++; while (((uint64_t)1 << max_scale) < n1) max_scale++;
out->fs = (FFTSettings*)malloc(sizeof(FFTSettings)); out->fs = (FFTSettings*)malloc(sizeof(FFTSettings));
if (out->fs == NULL) { free(g1_projective); return C_KZG_MALLOC; } if (out->fs == NULL) { free(g1_projective); return C_KZG_MALLOC; }
@ -811,15 +798,35 @@ C_KZG_RET load_trusted_setup(KZGSettings *out, FILE *in) {
ret = new_fft_settings((FFTSettings*)out->fs, max_scale); ret = new_fft_settings((FFTSettings*)out->fs, max_scale);
if (ret != C_KZG_OK) { free(g1_projective); return ret; } if (ret != C_KZG_OK) { free(g1_projective); return ret; }
ret = fft_g1(out->g1_values, g1_projective, true, FIELD_ELEMENTS_PER_BLOB, out->fs); ret = fft_g1(out->g1_values, g1_projective, true, n1, out->fs);
free(g1_projective); free(g1_projective);
if (ret != C_KZG_OK) return ret; if (ret != C_KZG_OK) return ret;
TRY(reverse_bit_order(out->g1_values, sizeof(g1_t), FIELD_ELEMENTS_PER_BLOB)); TRY(reverse_bit_order(out->g1_values, sizeof(g1_t), n1));
return C_KZG_OK; return C_KZG_OK;
} }
C_KZG_RET load_trusted_setup_file(KZGSettings *out, FILE *in) {
uint64_t i;
fscanf(in, "%" SCNu64, &i);
CHECK(i == FIELD_ELEMENTS_PER_BLOB);
fscanf(in, "%" SCNu64, &i);
CHECK(i == 65);
uint8_t g1_bytes[FIELD_ELEMENTS_PER_BLOB * 48];
uint8_t g2_bytes[65 * 96];
for (i = 0; i < FIELD_ELEMENTS_PER_BLOB * 48; i++)
fscanf(in, "%2hhx", &g1_bytes[i]);
for (i = 0; i < 65 * 96; i++)
fscanf(in, "%2hhx", &g2_bytes[i]);
return load_trusted_setup(out, g1_bytes, FIELD_ELEMENTS_PER_BLOB, g2_bytes, 65);
}
void free_trusted_setup(KZGSettings *s) { void free_trusted_setup(KZGSettings *s) {
free_fft_settings((FFTSettings*)s->fs); free_fft_settings((FFTSettings*)s->fs);
free_kzg_settings(s); free_kzg_settings(s);

View File

@ -92,6 +92,12 @@ void bytes_from_g1(uint8_t out[48], const g1_t *in);
void bytes_to_bls_field(BLSFieldElement *out, const uint8_t in[BYTES_PER_FIELD_ELEMENT]); void bytes_to_bls_field(BLSFieldElement *out, const uint8_t in[BYTES_PER_FIELD_ELEMENT]);
C_KZG_RET load_trusted_setup(KZGSettings *out, C_KZG_RET load_trusted_setup(KZGSettings *out,
const uint8_t g1_bytes[], /* n1 * 48 bytes */
size_t n1,
const uint8_t g2_bytes[], /* n2 * 96 bytes */
size_t n2);
C_KZG_RET load_trusted_setup_file(KZGSettings *out,
FILE *in); FILE *in);
void free_trusted_setup( void free_trusted_setup(