Add some more to the csharp interface
This commit is contained in:
parent
f5e95c497c
commit
b3d550d1a3
|
@ -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
|
||||
|
|
|
@ -1,8 +1,21 @@
|
|||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue