Add verify_kzg_proof and load_trusted_setup for C#

This commit is contained in:
Ramana Kumar 2022-10-05 20:04:35 +01:00
parent 989797c254
commit d50adb8086
No known key found for this signature in database
GPG Key ID: ED471C788B900433
2 changed files with 54 additions and 14 deletions

View File

@ -3,9 +3,34 @@
#include <stdlib.h> #include <stdlib.h>
#include "c_kzg_4844.h" #include "c_kzg_4844.h"
uint64_t hello(uint64_t a) { 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) {
printf("Hello World! %lu\n", a); KZGCommitment commitment;
return 42; BLSFieldElement px, py;
KZGProof proof;
bool out;
if (bytes_to_g1(&commitment, c) != C_KZG_OK) return -1;
bytes_to_bls_field(&px, x);
bytes_to_bls_field(&py, y);
if (bytes_to_g1(&proof, p) != C_KZG_OK) return -1;
if (verify_kzg_proof(&out, &commitment, &px, &py, &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));
if (out == NULL) return NULL;
FILE* f = fopen(file, "r");
if (f == NULL) return NULL;
if (load_trusted_setup(out, f) != C_KZG_OK) return NULL;
return out;
} }
BLSFieldElement* bytes_to_bls_field_wrap(const uint8_t bytes[]) { BLSFieldElement* bytes_to_bls_field_wrap(const uint8_t bytes[]) {

View File

@ -4,22 +4,24 @@ using System.Text;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
class ckzg { class ckzg {
[DllImport("ckzg.dll", EntryPoint="hello")]
public static extern uint hello(uint a);
[DllImport("ckzg.dll", EntryPoint="bytes_to_bls_field_wrap")] [DllImport("ckzg.dll", EntryPoint="bytes_to_bls_field_wrap")]
private static extern IntPtr bytes_to_bls_field_wrap(byte[] bytes); public static extern IntPtr bytes_to_bls_field(byte[] bytes);
public static IntPtr bytes_to_bls_field(byte[] bytes) { [DllImport("ckzg.dll", EntryPoint="verify_kzg_proof_wrap")]
return bytes_to_bls_field_wrap(bytes); public static extern int verify_kzg_proof(byte[] c, byte[] x, byte[] y, byte[] p, IntPtr ts);
}
[DllImport("ckzg.dll", EntryPoint="uint64s_from_bls_field")] [DllImport("ckzg.dll", EntryPoint="load_trusted_setup_wrap")]
private static extern IntPtr uint64s_from_bls_field(IntPtr fr); public static extern IntPtr load_trusted_setup(string filename);
[DllImport("ckzg.dll", EntryPoint="free_trusted_setup")]
public static extern void free_trusted_setup(IntPtr ts);
[DllImport("ckzg.dll", EntryPoint="free")] [DllImport("ckzg.dll", EntryPoint="free")]
private static extern void free(IntPtr p); private static extern void free(IntPtr p);
[DllImport("ckzg.dll", EntryPoint="uint64s_from_bls_field")]
private static extern IntPtr uint64s_from_bls_field(IntPtr fr);
public static BigInteger int_from_bls_field(IntPtr fr) { public static BigInteger int_from_bls_field(IntPtr fr) {
IntPtr uptr = uint64s_from_bls_field(fr); IntPtr uptr = uint64s_from_bls_field(fr);
Int64[] int64s = new Int64[4]; Int64[] int64s = new Int64[4];
@ -38,8 +40,21 @@ class ckzg {
class tests { class tests {
private static void Main(string[] args) private static void Main(string[] args)
{ {
Console.WriteLine("OK"); IntPtr ts = ckzg.load_trusted_setup("../../src/trusted_setup.txt");
Console.WriteLine(ckzg.hello(32)); if (ts == IntPtr.Zero) {
Console.WriteLine("Failed to load trusted setup.");
return;
}
byte[] c = new byte[48];
byte[] x = new byte[32];
byte[] y = new byte[32];
byte[] p = new byte[48];
int result = ckzg.verify_kzg_proof(c, x, y, p, ts);
Console.WriteLine(string.Format("Verification result: {0}", result));
ckzg.free_trusted_setup(ts);
byte[] b = new byte[32]; byte[] b = new byte[32];
b[0] = 11; b[0] = 11;
b[8] = 1; b[8] = 1;