Checkpoint

This commit is contained in:
dancoffman 2022-11-01 17:56:34 -07:00
parent 6ec86af547
commit 354281a9d8
No known key found for this signature in database
GPG Key ID: 47B1F53E36A9B3CC
7 changed files with 2322 additions and 35 deletions

View File

@ -3,8 +3,8 @@ clean:
rm -f ckzg.node rm -f ckzg.node
rm -f ckzg_wrap.cxx rm -f ckzg_wrap.cxx
build: ../../src/c_kzg_4844.c build: ckzg.c ckzg.h
swig -c++ -javascript -node ckzg.swg swig -javascript -node ckzg.swg
node-gyp rebuild node-gyp rebuild
cp build/Release/ckzg.node . cp build/Release/ckzg.node .

View File

@ -6,7 +6,7 @@
'ckzg_wrap.cxx', 'ckzg_wrap.cxx',
'../../src/c_kzg_4844.c', '../../src/c_kzg_4844.c',
], ],
'include_dirs': [ '../../inc' ], 'include_dirs': [ '../../inc' ]
}, },
] ]
} }

97
bindings/node.js/ckzg.c Normal file
View File

@ -0,0 +1,97 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include "c_kzg_4844.h"
KZGSettings* loadTrustSetup(const char* file) {
KZGSettings* out = malloc(sizeof(KZGSettings));
if (out == NULL) return NULL;
FILE* f = fopen(file, "r");
if (f == NULL) { free(out); return NULL; }
if (load_trusted_setup(out, f) != C_KZG_OK) { free(out); return NULL; }
return out;
}
void freeTrustedSetup(KZGSettings *s) {
free_trusted_setup(s);
free(s);
}
void blobToKzgCommitment(uint8_t out[48], const uint8_t blob[FIELD_ELEMENTS_PER_BLOB * 32], const KZGSettings *s) {
Polynomial p;
for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++)
bytes_to_bls_field(&p[i], &blob[i * 32]);
KZGCommitment c;
blob_to_kzg_commitment(&c, p, s);
bytes_from_g1(out, &c);
}
int verifyAggregateKzgProof(const uint8_t blobs[], const uint8_t commitments[], size_t n, const uint8_t proof[48], const KZGSettings *s) {
Polynomial* p = calloc(n, sizeof(Polynomial));
if (p == NULL) return -1;
KZGCommitment* c = calloc(n, sizeof(KZGCommitment));
if (c == NULL) { free(p); return -1; }
C_KZG_RET ret;
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < FIELD_ELEMENTS_PER_BLOB; j++)
bytes_to_bls_field(&p[i][j], &blobs[i * FIELD_ELEMENTS_PER_BLOB * 32 + j * 32]);
ret = bytes_to_g1(&c[i], &commitments[i * 48]);
if (ret != C_KZG_OK) { free(c); free(p); return -1; }
}
KZGProof f;
ret = bytes_to_g1(&f, proof);
if (ret != C_KZG_OK) { free(c); free(p); return -1; }
bool b;
ret = verify_aggregate_kzg_proof(&b, p, c, n, &f, s);
if (ret != C_KZG_OK) { free(c); free(p); return -1; }
free(c); free(p);
return b ? 0 : 1;
}
C_KZG_RET computeAggregateKzgProof(uint8_t out[48], const uint8_t blobs[], size_t n, const KZGSettings *s) {
Polynomial* p = calloc(n, sizeof(Polynomial));
if (p == NULL) return -1;
for (size_t i = 0; i < n; i++)
for (size_t j = 0; j < FIELD_ELEMENTS_PER_BLOB; j++)
bytes_to_bls_field(&p[i][j], &blobs[i * FIELD_ELEMENTS_PER_BLOB * 32 + j * 32]);
KZGProof f;
C_KZG_RET ret = compute_aggregate_kzg_proof(&f, p, n, s);
free(p);
if (ret != C_KZG_OK) return ret;
bytes_from_g1(out, &f);
return C_KZG_OK;
}
int verifyKzgProof(const uint8_t c[48], const uint8_t x[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s) {
KZGCommitment commitment;
KZGProof proof;
BLSFieldElement fx, fy;
bool out;
bytes_to_bls_field(&fx, x);
bytes_to_bls_field(&fy, y);
if (bytes_to_g1(&commitment, c) != C_KZG_OK) return -1;
if (bytes_to_g1(&proof, p) != C_KZG_OK) return -1;
if (verify_kzg_proof(&out, &commitment, &fx, &fy, &proof, s) != C_KZG_OK)
return -2;
return out ? 0 : 1;
}

16
bindings/node.js/ckzg.h Normal file
View File

@ -0,0 +1,16 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include "c_kzg_4844.h"
KZGSettings* loadTrustSetup(const char* file);
void freeTrustedSetup(KZGSettings *s);
void blobToKzgCommitment(uint8_t out[48], const uint8_t blob[FIELD_ELEMENTS_PER_BLOB * 32], const KZGSettings *s);
int verifyKzgProof(const uint8_t c[48], const uint8_t x[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s);
int verifyAggregateKzgProof(const uint8_t blobs[], const uint8_t commitments[], size_t n, const uint8_t proof[48], const KZGSettings *s);
C_KZG_RET computeAggregateKzgProof(uint8_t out[48], const uint8_t blobs[], size_t n, const KZGSettings *s);

View File

@ -2,34 +2,6 @@
%module ckzg %module ckzg
%{ %{
#include "../../src/c_kzg_4844.h" #include "ckzg.h"
%} %}
%include "ckzg.h"
extern C_KZG_RET load_trusted_setup(KZGSettings *out,
FILE *in);
extern void free_trusted_setup(
KZGSettings *s);
extern C_KZG_RET compute_aggregate_kzg_proof(KZGProof *out,
const Polynomial blobs[],
size_t n,
const KZGSettings *s);
extern C_KZG_RET verify_aggregate_kzg_proof(bool *out,
const Polynomial blobs[],
const KZGCommitment expected_kzg_commitments[],
size_t n,
const KZGProof *kzg_aggregated_proof,
const KZGSettings *s);
extern void blob_to_kzg_commitment(KZGCommitment *out,
const BLSFieldElement blob[FIELD_ELEMENTS_PER_BLOB],
const KZGSettings *s);
extern C_KZG_RET verify_kzg_proof(bool *out,
const KZGCommitment *polynomial_kzg,
const BLSFieldElement *z,
const BLSFieldElement *y,
const KZGProof *kzg_proof,
const KZGSettings *s);

2200
bindings/node.js/ckzg_wrap.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,8 @@
console.log('testing...'); console.log('testing...');
const blst = require('ckzg'); const kzg = require('ckzg');
console.log(blst); console.log(kzg);
kzg.testFunction();