From af4832c585fa051f979575b2f4bb07546ed5d4ac Mon Sep 17 00:00:00 2001 From: Ramana Kumar Date: Mon, 10 Oct 2022 20:12:31 +0100 Subject: [PATCH] Update C# interface not fully implemented or tested yet --- min-bindings/C#/ckzg.c | 61 +++++++++++++++++++++++++++------------- min-bindings/C#/tests.cs | 39 +++++++++++++------------ 2 files changed, 61 insertions(+), 39 deletions(-) diff --git a/min-bindings/C#/ckzg.c b/min-bindings/C#/ckzg.c index 00a2d9c..966d846 100644 --- a/min-bindings/C#/ckzg.c +++ b/min-bindings/C#/ckzg.c @@ -3,19 +3,54 @@ #include #include "c_kzg_4844.h" -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; +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 vectors[], const BLSFieldElement scalars[], uint64_t num_vectors, uint64_t vector_len) { + return NULL; // TODO +} + +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(&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) + if (verify_kzg_proof(&out, c, x, y, &proof, s) != C_KZG_OK) return -2; + return out ? 1 : 0; } @@ -61,15 +96,3 @@ void free_trusted_setup_wrap(KZGSettings* s) { free_trusted_setup(s); free(s); } - -BLSFieldElement* bytes_to_bls_field_wrap(const uint8_t bytes[]) { - BLSFieldElement* out = (BLSFieldElement*)malloc(sizeof(BLSFieldElement)); - bytes_to_bls_field(out, bytes); - return out; -} - -uint64_t* uint64s_from_bls_field(BLSFieldElement *fr) { - uint64_t *r = (uint64_t*)calloc(4, sizeof(uint64_t)); - uint64s_from_BLSFieldElement(r, fr); - return r; -} diff --git a/min-bindings/C#/tests.cs b/min-bindings/C#/tests.cs index 9deca0c..756d186 100644 --- a/min-bindings/C#/tests.cs +++ b/min-bindings/C#/tests.cs @@ -4,40 +4,35 @@ using System.Text; using System.Runtime.InteropServices; class ckzg { - [DllImport("ckzg.dll", EntryPoint="bytes_to_bls_field_wrap")] + [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="compute_powers_wrap")] // free result with free() + public static extern IntPtr compute_powers(IntPtr r, UInt64 n); + + [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(byte[] c, byte[] x, byte[] y, byte[] p, IntPtr ts); + public static extern int verify_kzg_proof(IntPtr c, IntPtr x, IntPtr y, byte[] p, IntPtr ts); [DllImport("ckzg.dll", EntryPoint="evaluate_polynomial_wrap")] public static extern int evaluate_polynomial_in_evaluation_form(byte[] result, byte[] p, UInt64 n, byte[] z, IntPtr ts); - [DllImport("ckzg.dll", EntryPoint="load_trusted_setup_wrap")] + [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")] + public static extern void free_polynomial(IntPtr p); + [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]; - Marshal.Copy(uptr, int64s, 0, 4); - free(uptr); - BigInteger result = new BigInteger(0); - BigInteger mult = new BigInteger(1); - for (int i = 0; i < 4; i++) { - result += Convert.ToUInt64(int64s[i]) * mult; - mult *= BigInteger.Pow(2, 64); - } - return result; - } } class tests { @@ -54,6 +49,7 @@ class tests { private static void Main(string[] args) { + /* TODO: update for new interface Console.WriteLine("Test 1: verify_kzg_proof"); IntPtr ts = ckzg.load_trusted_setup("../../src/trusted_setup.txt"); @@ -71,7 +67,9 @@ class tests { System.Diagnostics.Trace.Assert(result == 0, "Verification succeeded incorrectly"); ckzg.free_trusted_setup(ts); + */ + /* TODO: update for new interface Console.WriteLine("Test 2: evaluate_polynomial_in_evaluation_form"); ts = ckzg.load_trusted_setup("../python/tiny_trusted_setup.txt"); @@ -92,6 +90,7 @@ class tests { "Second evaluation produced incorrect value"); ckzg.free_trusted_setup(ts); + */ Console.WriteLine("Tests passed"); }