Add evaluate_polynomial_in_evaluation_form to C#

This commit is contained in:
Ramana Kumar 2022-10-07 00:45:42 +01:00
parent 6083d09d29
commit 61a43c95a2
No known key found for this signature in database
GPG Key ID: ED471C788B900433
2 changed files with 43 additions and 4 deletions

View File

@ -33,6 +33,30 @@ KZGSettings* load_trusted_setup_wrap(const char* file) {
return out; return out;
} }
int evaluate_polynomial_wrap(uint8_t out[32], const uint8_t pvals[], size_t n, const uint8_t point[32], const KZGSettings *s) {
PolynomialEvalForm p;
if (alloc_polynomial(&p, n) != C_KZG_OK)
return -1;
for (size_t i = 0; i < n; i++)
bytes_to_bls_field(&p.values[i], &pvals[i * 32]);
BLSFieldElement z;
bytes_to_bls_field(&z, point);
BLSFieldElement r;
if (evaluate_polynomial_in_evaluation_form(&r, &p, &z, s) != C_KZG_OK) {
free_polynomial(&p);
return -1;
}
bytes_from_bls_field(out, &r);
return 0;
}
void free_trusted_setup_wrap(KZGSettings* s) { void free_trusted_setup_wrap(KZGSettings* s) {
free_trusted_setup(s); free_trusted_setup(s);
free(s); free(s);

View File

@ -10,6 +10,9 @@ class ckzg {
[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(byte[] c, byte[] x, byte[] 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, byte[] z, IntPtr ts);
[DllImport("ckzg.dll", EntryPoint="load_trusted_setup_wrap")] [DllImport("ckzg.dll", EntryPoint="load_trusted_setup_wrap")]
public static extern IntPtr load_trusted_setup(string filename); public static extern IntPtr load_trusted_setup(string filename);
@ -51,11 +54,10 @@ class tests {
private static void Main(string[] args) private static void Main(string[] args)
{ {
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");
if (ts == IntPtr.Zero) { System.Diagnostics.Trace.Assert(ts != IntPtr.Zero, "Failed to load trusted setup.");
Console.WriteLine("Failed to load trusted setup.");
return;
}
byte[] c = HexadecimalStringToByteArray("b91c022acf7bd3b63be69a4c19b781ea7a3d5df1cd66ceb7dd0f399610f0ee04695dace82e04bfb83af2b17d7319f87f"); byte[] c = HexadecimalStringToByteArray("b91c022acf7bd3b63be69a4c19b781ea7a3d5df1cd66ceb7dd0f399610f0ee04695dace82e04bfb83af2b17d7319f87f");
byte[] x = HexadecimalStringToByteArray("0345f802a75a6c0d9cc5b8a1e71642b8fa80b0a78938edc6da1e591149578d1a"); byte[] x = HexadecimalStringToByteArray("0345f802a75a6c0d9cc5b8a1e71642b8fa80b0a78938edc6da1e591149578d1a");
@ -70,6 +72,19 @@ class tests {
ckzg.free_trusted_setup(ts); ckzg.free_trusted_setup(ts);
Console.WriteLine("Test 2: evaluate_polynomial_in_evaluation_form");
ts = ckzg.load_trusted_setup("../python/tiny_trusted_setup.txt");
System.Diagnostics.Trace.Assert(ts != IntPtr.Zero, "Failed to load trusted setup.");
p = HexadecimalStringToByteArray("10000000000000000d00000000000000000000000000000000000000000000000a000000000000000d00000000000000000000000000000000000000000000000b000000000001000d000376020003ecd0040376cecc518d00000000000000000c000000fffffeff0b5cfb8900a4ba6734d39e93390be8a5477d9d2953a7ed73");
x = HexadecimalStringToByteArray("0200000000000000000000000000000000000000000000000000000000000000");
result = ckzg.evaluate_polynomial_in_evaluation_form(y, p, x, ts);
System.Diagnostics.Trace.Assert(result == 0, "Evaluation failed");
System.Diagnostics.Trace.Assert(y == HexadecimalStringToByteArray("1c000000000000000d0000000000000000000000000000000000000000000000"),
"Evaluation produced incorrect value");
ckzg.free_trusted_setup(ts);
Console.WriteLine("Tests passed"); Console.WriteLine("Tests passed");
} }
} }