c-kzg-4844/bindings/csharp/ckzg.c

98 lines
2.7 KiB
C
Raw Normal View History

2022-10-04 18:45:12 +00:00
#include <inttypes.h>
#include <stdio.h>
2022-10-04 21:40:18 +00:00
#include <stdlib.h>
2022-10-04 18:45:12 +00:00
#include "c_kzg_4844.h"
2022-10-20 18:29:36 +00:00
KZGSettings* load_trusted_setup_wrap(const char* file) {
KZGSettings* out = malloc(sizeof(KZGSettings));
2022-10-20 18:29:36 +00:00
if (out == NULL) return NULL;
2022-10-20 18:29:36 +00:00
FILE* f = fopen(file, "r");
2022-10-20 18:29:36 +00:00
if (f == NULL) { free(out); return NULL; }
2022-10-20 18:29:36 +00:00
if (load_trusted_setup(out, f) != C_KZG_OK) { free(out); return NULL; }
return out;
}
2022-10-20 18:29:36 +00:00
void free_trusted_setup_wrap(KZGSettings *s) {
free_trusted_setup(s);
free(s);
}
2022-10-20 18:29:36 +00:00
void blob_to_kzg_commitment_wrap(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]);
2022-10-20 18:29:36 +00:00
KZGCommitment c;
blob_to_kzg_commitment(&c, p, s);
2022-10-20 18:29:36 +00:00
bytes_from_g1(out, &c);
}
2022-10-20 18:29:36 +00:00
int verify_aggregate_kzg_proof_wrap(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;
2022-10-20 18:29:36 +00:00
KZGCommitment* c = calloc(n, sizeof(KZGCommitment));
if (c == NULL) { free(p); return -1; }
2022-10-20 18:29:36 +00:00
C_KZG_RET ret;
2022-10-20 18:29:36 +00:00
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; }
}
2022-10-20 18:29:36 +00:00
KZGProof f;
ret = bytes_to_g1(&f, proof);
if (ret != C_KZG_OK) { free(c); free(p); return -1; }
2022-10-20 18:29:36 +00:00
bool b;
ret = verify_aggregate_kzg_proof(&b, p, c, n, &f, s);
if (ret != C_KZG_OK) { free(c); free(p); return -1; }
2022-10-04 21:40:18 +00:00
2022-10-20 18:29:36 +00:00
free(c); free(p);
return b ? 0 : 1;
}
2022-10-20 18:29:36 +00:00
C_KZG_RET compute_aggregate_kzg_proof_wrap(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;
2022-10-20 18:29:36 +00:00
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]);
2022-10-20 18:29:36 +00:00
KZGProof f;
C_KZG_RET ret = compute_aggregate_kzg_proof(&f, p, n, s);
free(p);
2022-10-20 18:29:36 +00:00
if (ret != C_KZG_OK) return ret;
bytes_from_g1(out, &f);
return C_KZG_OK;
}
int verify_kzg_proof_wrap(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;
}