From d50adb80862b656cb26db5e4de1d853d79c78458 Mon Sep 17 00:00:00 2001 From: Ramana Kumar Date: Wed, 5 Oct 2022 20:04:35 +0100 Subject: [PATCH] Add verify_kzg_proof and load_trusted_setup for C# --- min-bindings/C#/ckzg.c | 31 ++++++++++++++++++++++++++++--- min-bindings/C#/tests.cs | 37 ++++++++++++++++++++++++++----------- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/min-bindings/C#/ckzg.c b/min-bindings/C#/ckzg.c index 816e731..8177f35 100644 --- a/min-bindings/C#/ckzg.c +++ b/min-bindings/C#/ckzg.c @@ -3,9 +3,34 @@ #include #include "c_kzg_4844.h" -uint64_t hello(uint64_t a) { - printf("Hello World! %lu\n", a); - return 42; +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; + 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[]) { diff --git a/min-bindings/C#/tests.cs b/min-bindings/C#/tests.cs index 95f0066..6858461 100644 --- a/min-bindings/C#/tests.cs +++ b/min-bindings/C#/tests.cs @@ -4,22 +4,24 @@ using System.Text; using System.Runtime.InteropServices; class ckzg { - [DllImport("ckzg.dll", EntryPoint="hello")] - public static extern uint hello(uint a); - [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) { - return bytes_to_bls_field_wrap(bytes); - } + [DllImport("ckzg.dll", EntryPoint="verify_kzg_proof_wrap")] + 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")] - private static extern IntPtr uint64s_from_bls_field(IntPtr fr); + [DllImport("ckzg.dll", EntryPoint="load_trusted_setup_wrap")] + 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")] 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) { IntPtr uptr = uint64s_from_bls_field(fr); Int64[] int64s = new Int64[4]; @@ -38,8 +40,21 @@ class ckzg { class tests { private static void Main(string[] args) { - Console.WriteLine("OK"); - Console.WriteLine(ckzg.hello(32)); + IntPtr ts = ckzg.load_trusted_setup("../../src/trusted_setup.txt"); + 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]; b[0] = 11; b[8] = 1;