Add some more to the csharp interface

This commit is contained in:
Ramana Kumar 2022-10-04 22:40:18 +01:00
parent f5e95c497c
commit b3d550d1a3
No known key found for this signature in database
GPG Key ID: ED471C788B900433
3 changed files with 49 additions and 4 deletions

View File

@ -1,7 +1,7 @@
INCLUDE_DIRS = .. ../../min-src ../../inc INCLUDE_DIRS = .. ../../min-src ../../inc
test: tests.cs ckzg.dll test: tests.cs ckzg.dll
mcs tests.cs mcs tests.cs -r:System.Numerics
./tests.exe ./tests.exe
ckzg.dll: ckzg.c ../../min-src/c_kzg_4844.o ../../lib/libblst.a ckzg.dll: ckzg.c ../../min-src/c_kzg_4844.o ../../lib/libblst.a

View File

@ -1,8 +1,21 @@
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "c_kzg_4844.h" #include "c_kzg_4844.h"
uint64_t hello(uint64_t a) { uint64_t hello(uint64_t a) {
printf("Hello World! %lu\n", a); printf("Hello World! %lu\n", a);
return 42; 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;
}

View File

@ -1,11 +1,38 @@
using System; using System;
using System.Numerics;
using System.Text; using System.Text;
using System.Runtime.InteropServices;
class ckzg { class ckzg {
[global::System.Runtime.InteropServices.DllImport("ckzg.dll", EntryPoint="hello")] [DllImport("ckzg.dll", EntryPoint="hello")]
public static extern uint hello(uint a); 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 { class tests {
@ -13,5 +40,10 @@ class tests {
{ {
Console.WriteLine("OK"); Console.WriteLine("OK");
Console.WriteLine(ckzg.hello(32)); 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));
} }
} }