Sketch update to csharp interface

This commit is contained in:
Ramana Kumar 2022-10-20 19:29:36 +01:00
parent 800f450e5b
commit 140ee4c187
No known key found for this signature in database
GPG Key ID: ED471C788B900433
2 changed files with 64 additions and 110 deletions

View File

@ -3,99 +3,16 @@
#include <stdlib.h>
#include "c_kzg_4844.h"
BLSFieldElement* bytes_to_bls_field_wrap(const uint8_t bytes[]) {
BLSFieldElement* out = (BLSFieldElement*)malloc(sizeof(BLSFieldElement));
if (out != NULL) bytes_to_bls_field(out, bytes);
return out;
}
BLSFieldElement* compute_powers_wrap(const BLSFieldElement *r, uint64_t n) {
BLSFieldElement* out = (BLSFieldElement*)calloc(n, sizeof(BLSFieldElement));
if (out != NULL) compute_powers(out, r, n);
return out;
}
PolynomialEvalForm* vector_lincomb_wrap(const uint8_t bytes[], const BLSFieldElement scalars[], uint64_t num_vectors, uint64_t vector_len) {
PolynomialEvalForm *p = (PolynomialEvalForm*)malloc(sizeof(PolynomialEvalForm));
if (p == NULL) return NULL;
if (alloc_polynomial(p, vector_len) != C_KZG_OK) {
free(p);
return NULL;
}
BLSFieldElement *vectors = (BLSFieldElement*)calloc(num_vectors * vector_len, sizeof(BLSFieldElement));
if (vectors == NULL) {
free_polynomial(p);
free(p);
return NULL;
}
for (uint64_t i = 0; i < num_vectors; i++)
for (uint64_t j = 0; j < vector_len; j++)
bytes_to_bls_field(&vectors[i * vector_len + j], &bytes[(i * vector_len + j) * 32]);
vector_lincomb(p->values, vectors, scalars, num_vectors, vector_len);
free(vectors);
return p;
}
KZGCommitment* g1_lincomb_wrap(const uint8_t bytes[], const BLSFieldElement scalars[], uint64_t num_points) {
KZGCommitment* points = (KZGCommitment*)calloc(num_points, sizeof(KZGCommitment));
if (points == NULL) return NULL;
for (uint64_t i = 0; i < num_points; i++) {
if (bytes_to_g1(&points[i], &bytes[i * 48]) != C_KZG_OK) {
free(points);
return NULL;
}
}
KZGCommitment* out = (KZGCommitment*)malloc(sizeof(KZGCommitment));
if (out == NULL) {
free(points);
return NULL;
}
g1_lincomb(out, points, scalars, num_points);
free(points);
return out;
}
int verify_kzg_proof_wrap(const KZGCommitment* c, const BLSFieldElement* x, const BLSFieldElement* y, const uint8_t p[48], KZGSettings *s) {
KZGProof proof;
bool out;
if (bytes_to_g1(&proof, p) != C_KZG_OK) return -1;
if (verify_kzg_proof(&out, c, x, y, &proof, s) != C_KZG_OK)
return -2;
return out ? 1 : 0;
}
KZGSettings* load_trusted_setup_wrap(const char* file) {
KZGSettings* out = (KZGSettings*)malloc(sizeof(KZGSettings));
KZGSettings* out = malloc(sizeof(KZGSettings));
if (out == NULL) return NULL;
FILE* f = fopen(file, "r");
if (f == NULL) return NULL;
if (f == NULL) { free(out); return NULL; }
if (load_trusted_setup(out, f) != C_KZG_OK) return NULL;
return out;
}
BLSFieldElement* evaluate_polynomial_wrap(const PolynomialEvalForm* p, const BLSFieldElement* z, const KZGSettings *s) {
BLSFieldElement *out = (BLSFieldElement*)malloc(sizeof(BLSFieldElement));
if (out == NULL) return NULL;
if (evaluate_polynomial_in_evaluation_form(out, p, z, s) != C_KZG_OK)
return NULL;
if (load_trusted_setup(out, f) != C_KZG_OK) { free(out); return NULL; }
return out;
}
@ -105,7 +22,59 @@ void free_trusted_setup_wrap(KZGSettings *s) {
free(s);
}
void free_polynomial_wrap(PolynomialEvalForm *p) {
free_polynomial(p);
free(p);
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]);
KZGCommitment c;
blob_to_kzg_commitment(&c, p, s);
bytes_from_g1(out, &c);
}
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;
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 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;
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;
}

View File

@ -6,35 +6,20 @@ using System.Runtime.InteropServices;
class ckzg
{
[DllImport("ckzg.dll", EntryPoint = "bytes_to_bls_field_wrap")] // free result with free()
public static extern IntPtr bytes_to_bls_field(byte[] bytes);
[DllImport("ckzg.dll", EntryPoint = "blob_to_kzg_commitment_wrap")]
public static extern void blob_to_kzg_commitment(byte[/*48*/] retval, byte[/*4096 * 32*/] blob, IntPtr ts);
[DllImport("ckzg.dll", EntryPoint = "compute_powers_wrap")] // free result with free()
public static extern IntPtr compute_powers(IntPtr r, UInt64 n);
[DllImport("ckzg.dll", EntryPoint = "compute_aggregate_kzg_proof_wrap")] // returns 0 on success
public static extern int compute_aggregate_kzg_proof(byte[/*48*/] retval, byte[] blobs, int n, IntPtr ts);
[DllImport("ckzg.dll", EntryPoint = "vector_lincomb_wrap")] // free result with free_polynomial()
public static extern IntPtr vector_lincomb(byte[] vectors, IntPtr scalars, UInt64 num_vectors, UInt64 vector_len);
[DllImport("ckzg.dll", EntryPoint = "g1_lincomb_wrap")] // free result with free()
public static extern IntPtr g1_lincomb(byte[] points, IntPtr scalars, UInt64 num_points);
[DllImport("ckzg.dll", EntryPoint = "verify_kzg_proof_wrap")]
public static extern int verify_kzg_proof(IntPtr c, IntPtr x, IntPtr y, byte[] p, IntPtr ts);
[DllImport("ckzg.dll", EntryPoint = "evaluate_polynomial_wrap")] // free result with free()
public static extern IntPtr evaluate_polynomial_in_evaluation_form(IntPtr p, IntPtr z, IntPtr ts);
[DllImport("ckzg.dll", EntryPoint = "verify_aggregate_kzg_proof_wrap")] // returns 0 on success
public static extern int verify_aggregate_kzg_proof(byte[] blobs, byte[] commitments, int n, byte[/*48*/] proof, IntPtr ts);
[DllImport("ckzg.dll", EntryPoint = "load_trusted_setup_wrap")] // free result with free_trusted_setup()
public static extern IntPtr load_trusted_setup(string filename);
[DllImport("ckzg.dll", EntryPoint = "free_trusted_setup_wrap")]
public static extern void free_trusted_setup(IntPtr ts);
[DllImport("ckzg.dll", EntryPoint = "free_polynomial_wrap")]
public static extern void free_polynomial(IntPtr p);
[DllImport("ckzg.dll", EntryPoint = "free")]
private static extern void free(IntPtr p);
}
class tests