Update C# interface

not fully implemented or tested yet
This commit is contained in:
Ramana Kumar 2022-10-10 20:12:31 +01:00
parent dc152f4238
commit af4832c585
No known key found for this signature in database
GPG Key ID: ED471C788B900433
2 changed files with 61 additions and 39 deletions

View File

@ -3,19 +3,54 @@
#include <stdlib.h> #include <stdlib.h>
#include "c_kzg_4844.h" #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) { BLSFieldElement* bytes_to_bls_field_wrap(const uint8_t bytes[]) {
KZGCommitment commitment; BLSFieldElement* out = (BLSFieldElement*)malloc(sizeof(BLSFieldElement));
BLSFieldElement px, py; 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; KZGProof proof;
bool out; 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 (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 -2;
return out ? 1 : 0; return out ? 1 : 0;
} }
@ -61,15 +96,3 @@ void free_trusted_setup_wrap(KZGSettings* s) {
free_trusted_setup(s); free_trusted_setup(s);
free(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;
}

View File

@ -4,40 +4,35 @@ using System.Text;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
class ckzg { 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); 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")] [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")] [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); 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); public static extern IntPtr load_trusted_setup(string filename);
[DllImport("ckzg.dll", EntryPoint="free_trusted_setup_wrap")] [DllImport("ckzg.dll", EntryPoint="free_trusted_setup_wrap")]
public static extern void free_trusted_setup(IntPtr ts); 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")] [DllImport("ckzg.dll", EntryPoint="free")]
private static extern void free(IntPtr p); 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 { class tests {
@ -54,6 +49,7 @@ class tests {
private static void Main(string[] args) private static void Main(string[] args)
{ {
/* TODO: update for new interface
Console.WriteLine("Test 1: verify_kzg_proof"); Console.WriteLine("Test 1: verify_kzg_proof");
IntPtr ts = ckzg.load_trusted_setup("../../src/trusted_setup.txt"); 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"); System.Diagnostics.Trace.Assert(result == 0, "Verification succeeded incorrectly");
ckzg.free_trusted_setup(ts); ckzg.free_trusted_setup(ts);
*/
/* TODO: update for new interface
Console.WriteLine("Test 2: evaluate_polynomial_in_evaluation_form"); Console.WriteLine("Test 2: evaluate_polynomial_in_evaluation_form");
ts = ckzg.load_trusted_setup("../python/tiny_trusted_setup.txt"); ts = ckzg.load_trusted_setup("../python/tiny_trusted_setup.txt");
@ -92,6 +90,7 @@ class tests {
"Second evaluation produced incorrect value"); "Second evaluation produced incorrect value");
ckzg.free_trusted_setup(ts); ckzg.free_trusted_setup(ts);
*/
Console.WriteLine("Tests passed"); Console.WriteLine("Tests passed");
} }