From b3d550d1a3f7a25db59a4f4bc0a1abf28a842635 Mon Sep 17 00:00:00 2001 From: Ramana Kumar Date: Tue, 4 Oct 2022 22:40:18 +0100 Subject: [PATCH] Add some more to the csharp interface --- min-bindings/C#/Makefile | 2 +- min-bindings/C#/ckzg.c | 13 +++++++++++++ min-bindings/C#/tests.cs | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/min-bindings/C#/Makefile b/min-bindings/C#/Makefile index 5ea1905..8bd3a9a 100644 --- a/min-bindings/C#/Makefile +++ b/min-bindings/C#/Makefile @@ -1,7 +1,7 @@ INCLUDE_DIRS = .. ../../min-src ../../inc test: tests.cs ckzg.dll - mcs tests.cs + mcs tests.cs -r:System.Numerics ./tests.exe ckzg.dll: ckzg.c ../../min-src/c_kzg_4844.o ../../lib/libblst.a diff --git a/min-bindings/C#/ckzg.c b/min-bindings/C#/ckzg.c index 4ade739..816e731 100644 --- a/min-bindings/C#/ckzg.c +++ b/min-bindings/C#/ckzg.c @@ -1,8 +1,21 @@ #include #include +#include #include "c_kzg_4844.h" uint64_t hello(uint64_t a) { printf("Hello World! %lu\n", a); return 42; } + +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 fe23744..95f0066 100644 --- a/min-bindings/C#/tests.cs +++ b/min-bindings/C#/tests.cs @@ -1,11 +1,38 @@ using System; +using System.Numerics; using System.Text; +using System.Runtime.InteropServices; class ckzg { - [global::System.Runtime.InteropServices.DllImport("ckzg.dll", EntryPoint="hello")] + [DllImport("ckzg.dll", EntryPoint="hello")] public static extern uint hello(uint a); - [global::System.Runtime.InteropServices.DllImport("ckzg.dll", EntryPoint="bytes_to_bls_field_wrap")] - public static extern void bytes_to_bls_field(byte[] b); + + [DllImport("ckzg.dll", EntryPoint="bytes_to_bls_field_wrap")] + private static extern IntPtr bytes_to_bls_field_wrap(byte[] bytes); + + public static IntPtr bytes_to_bls_field(byte[] bytes) { + return bytes_to_bls_field_wrap(bytes); + } + + [DllImport("ckzg.dll", EntryPoint="uint64s_from_bls_field")] + private static extern IntPtr uint64s_from_bls_field(IntPtr fr); + + [DllImport("ckzg.dll", EntryPoint="free")] + private static extern void free(IntPtr p); + + 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 { @@ -13,5 +40,10 @@ class tests { { Console.WriteLine("OK"); Console.WriteLine(ckzg.hello(32)); + byte[] b = new byte[32]; + b[0] = 11; + b[8] = 1; + IntPtr fr = ckzg.bytes_to_bls_field(b); + Console.WriteLine(ckzg.int_from_bls_field(fr)); } }