Update csharp bindings (#146)
This commit is contained in:
parent
03b439ef2f
commit
0e6e23de65
|
@ -13,20 +13,15 @@ jobs:
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
target:
|
target:
|
||||||
- clang: x86_64-linux
|
- native: linux-x64
|
||||||
native: linux-x64
|
|
||||||
host: ubuntu-latest
|
host: ubuntu-latest
|
||||||
- clang: arm64-linux
|
- native: linux-arm64
|
||||||
native: linux-arm64
|
|
||||||
host: ubuntu-latest
|
host: ubuntu-latest
|
||||||
- clang: arm64-darwin
|
- native: osx-arm64
|
||||||
native: osx-arm64
|
|
||||||
host: macos-latest
|
host: macos-latest
|
||||||
- clang: x86_64-darwin
|
- native: osx-x64
|
||||||
native: osx-x64
|
|
||||||
host: macos-latest
|
host: macos-latest
|
||||||
- clang: x86_64-win
|
- native: win-x64
|
||||||
native: win-x64
|
|
||||||
host: windows-latest
|
host: windows-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: ilammy/msvc-dev-cmd@v1
|
- uses: ilammy/msvc-dev-cmd@v1
|
||||||
|
@ -34,7 +29,7 @@ jobs:
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: recursive
|
||||||
- name: Build native library for ${{ matrix.target.native }}
|
- name: Build native library for ${{ matrix.target.native }}
|
||||||
run: cd bindings/csharp && make -B ckzg CSHARP_PLATFORM=${{ matrix.target.native }} CLANG_PLATFORM=${{ matrix.target.clang }}
|
run: cd bindings/csharp && make -B ckzg CSHARP_PLATFORM=${{ matrix.target.native }}
|
||||||
- name: Upload artifacts
|
- name: Upload artifacts
|
||||||
uses: actions/upload-artifact@v3
|
uses: actions/upload-artifact@v3
|
||||||
with:
|
with:
|
||||||
|
@ -76,7 +71,7 @@ jobs:
|
||||||
- name: Test
|
- name: Test
|
||||||
run: cd bindings/csharp && dotnet test --configuration Release --no-restore
|
run: cd bindings/csharp && dotnet test --configuration Release --no-restore
|
||||||
- name: Build
|
- name: Build
|
||||||
run: cd bindings/csharp && dotnet build -p:Version=0.1.1.${{github.run_number}} --configuration Release --no-restore -o build
|
run: cd bindings/csharp && dotnet build -p:Version=0.1.1.${{github.run_number}} --configuration Release --no-restore
|
||||||
- name: Upload package
|
- name: Upload package
|
||||||
uses: actions/upload-artifact@v3
|
uses: actions/upload-artifact@v3
|
||||||
with:
|
with:
|
||||||
|
|
|
@ -5,10 +5,18 @@ namespace Ckzg;
|
||||||
|
|
||||||
public class Ckzg
|
public class Ckzg
|
||||||
{
|
{
|
||||||
public const int CommitmentLength = 48;
|
public const int BytesPerFieldElement = 32;
|
||||||
public const int BlobElementLength = 32;
|
public const int BytesPerBlob = BytesPerFieldElement * 4096;
|
||||||
public const int BlobLength = BlobElementLength * 4096;
|
public const int BytesPerCommitment = 48;
|
||||||
public const int ProofLength = 48;
|
public const int BytesPerProof = 48;
|
||||||
|
|
||||||
|
public enum Ret
|
||||||
|
{
|
||||||
|
Ok,
|
||||||
|
BadArgs,
|
||||||
|
Error,
|
||||||
|
Malloc
|
||||||
|
}
|
||||||
|
|
||||||
static Ckzg() => AssemblyLoadContext.Default.ResolvingUnmanagedDll += (assembly, path) => NativeLibrary.Load($"runtimes/{(
|
static Ckzg() => AssemblyLoadContext.Default.ResolvingUnmanagedDll += (assembly, path) => NativeLibrary.Load($"runtimes/{(
|
||||||
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
|
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
|
||||||
|
@ -20,59 +28,12 @@ public class Ckzg
|
||||||
_ => ""
|
_ => ""
|
||||||
}}/native/{path}.{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dll" : "so")}");
|
}}/native/{path}.{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dll" : "so")}");
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Calculates commitment for the blob
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="commitment">Preallocated buffer of <inheritdoc cref="CommitmentLength"/> bytes to receive the commitment</param>
|
|
||||||
/// <param name="blob">Flatten array of blob elements</param>
|
|
||||||
/// <param name="ts">Trusted setup settings</param>
|
|
||||||
/// <returns>Returns error code or <c>0</c> if successful</returns>
|
|
||||||
[DllImport("ckzg", EntryPoint = "blob_to_kzg_commitment", CallingConvention = CallingConvention.Cdecl)]
|
|
||||||
public unsafe static extern int BlobToKzgCommitment(byte* commitment, byte* blob, IntPtr ts);
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Calculates aggregated proof for the blobs
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="proof">Preallocated buffer of <inheritdoc cref="ProofLength"/> bytes to receive the proof</param>
|
|
||||||
/// <param name="blobs">Blobs as a flatten byte array</param>
|
|
||||||
/// <param name="count">Blobs count</param>
|
|
||||||
/// <param name="ts">Trusted setup settings</param>
|
|
||||||
/// <returns>Returns error code or <c>0</c> if successful</returns>
|
|
||||||
[DllImport("ckzg", EntryPoint = "compute_aggregate_kzg_proof", CallingConvention = CallingConvention.Cdecl)]
|
|
||||||
public unsafe static extern int ComputeAggregatedKzgProof(byte* proof, byte* blobs, int count, IntPtr ts);
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Verify aggregated proof and commitments for the given blobs
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="blobs">Blobs as a flatten byte array</param>
|
|
||||||
/// <param name="commitments">Commitments as a flatten byte array</param>
|
|
||||||
/// <param name="count">Blobs and commitments count</param>
|
|
||||||
/// <param name="proof"></param>
|
|
||||||
/// <param name="ts">Trusted setup settings</param>
|
|
||||||
/// <returns>Returns error code or <c>0</c> if the proof is correct</returns>
|
|
||||||
[DllImport("ckzg", EntryPoint = "verify_aggregate_kzg_proof_wrap", CallingConvention = CallingConvention.Cdecl)]
|
|
||||||
public unsafe static extern int VerifyAggregatedKzgProof(byte* blobs, byte* commitments_bytes, int count, byte* aggregated_proof_bytes, IntPtr ts);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Verify the proof by point evaluation for the given commitment
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="commitment">Commitment</param>
|
|
||||||
/// <param name="z">Z</param>
|
|
||||||
/// <param name="y">Y</param>
|
|
||||||
/// <param name="proof">Proof</param>
|
|
||||||
/// <param name="ts">Trusted setup settings</param>
|
|
||||||
/// <returns>Returns error code or <c>0</c> if the proof is correct</returns>
|
|
||||||
[DllImport("ckzg", EntryPoint = "verify_kzg_proof_wrap", CallingConvention = CallingConvention.Cdecl)]
|
|
||||||
public unsafe static extern int VerifyKzgProof(byte* commitment_bytes, byte* z_bytes, byte* y_bytes, byte* proof_bytes, IntPtr ts);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Load trusted setup settings from file
|
/// Load trusted setup settings from file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="filename">Settings file path</param>
|
/// <param name="filename">Settings file path</param>
|
||||||
/// <returns>Trusted setup settings as a pointer or <c>0</c> in case of failure</returns>
|
/// <returns>Trusted setup settings as a pointer or <c>0</c> in case of failure</returns>
|
||||||
[DllImport("ckzg", EntryPoint = "load_trusted_setup_wrap")] // free result with free_trusted_setup()
|
[DllImport("ckzg", EntryPoint = "load_trusted_setup_wrap")]
|
||||||
public static extern IntPtr LoadTrustedSetup(string filename);
|
public static extern IntPtr LoadTrustedSetup(string filename);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -81,5 +42,74 @@ public class Ckzg
|
||||||
/// <param name="ts">Trusted setup settings</param>
|
/// <param name="ts">Trusted setup settings</param>
|
||||||
[DllImport("ckzg", EntryPoint = "free_trusted_setup_wrap", CallingConvention = CallingConvention.Cdecl)]
|
[DllImport("ckzg", EntryPoint = "free_trusted_setup_wrap", CallingConvention = CallingConvention.Cdecl)]
|
||||||
public static extern void FreeTrustedSetup(IntPtr ts);
|
public static extern void FreeTrustedSetup(IntPtr ts);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Calculates commitment for the blob
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="commitment">Preallocated buffer of <inheritdoc cref="CommitmentLength"/> bytes to receive the commitment</param>
|
||||||
|
/// <param name="blob">Flatten array of blob elements</param>
|
||||||
|
/// <param name="ts">Trusted setup settings</param>
|
||||||
|
/// <returns>Returns error code or <c>0</c> if successful</returns>
|
||||||
|
[DllImport("ckzg", EntryPoint = "blob_to_kzg_commitment", CallingConvention = CallingConvention.Cdecl)]
|
||||||
|
public unsafe static extern Ret BlobToKzgCommitment(byte* commitment, byte* blob, IntPtr ts);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Compute KZG proof at point `z` for the polynomial represented by `blob`.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="proof">Preallocated buffer of <inheritdoc cref="ProofLength"/> bytes to receive the proof</param>
|
||||||
|
/// <param name="blob">Blob byte array</param>
|
||||||
|
/// <param name="z_bytes"></param>
|
||||||
|
/// <param name="ts">Trusted setup settings</param>
|
||||||
|
/// <returns>Returns error code or <c>0</c> if successful</returns>
|
||||||
|
[DllImport("ckzg", EntryPoint = "compute_kzg_proof", CallingConvention = CallingConvention.Cdecl)]
|
||||||
|
public unsafe static extern Ret ComputeKzgProof(byte* proof, byte* blob, byte* z_bytes, IntPtr ts);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Given a blob, return the KZG proof that is used to verify it against the commitment.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="proof">Preallocated buffer of <inheritdoc cref="ProofLength"/> bytes to receive the proof</param>
|
||||||
|
/// <param name="blob">Blob byte array</param>
|
||||||
|
/// <param name="ts">Trusted setup settings</param>
|
||||||
|
/// <returns>Returns error code or <c>0</c> if successful</returns>
|
||||||
|
[DllImport("ckzg", EntryPoint = "compute_blob_kzg_proof", CallingConvention = CallingConvention.Cdecl)]
|
||||||
|
public unsafe static extern Ret ComputeBlobKzgProof(byte* proof, byte* blob, IntPtr ts);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="result">True if the proof is valid</param>
|
||||||
|
/// <param name="commitment_bytes"></param>
|
||||||
|
/// <param name="z_bytes"></param>
|
||||||
|
/// <param name="y_bytes"></param>
|
||||||
|
/// <param name="proof_bytes"></param>
|
||||||
|
/// <param name="ts">Trusted setup settings</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[DllImport("ckzg", EntryPoint = "verify_kzg_proof", CallingConvention = CallingConvention.Cdecl)]
|
||||||
|
public unsafe static extern Ret VerifyKzgProof(bool* result, byte* commitment_bytes, byte* z_bytes, byte* y_bytes, byte* proof_bytes, IntPtr ts);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Given a blob and a KZG proof, verify that the blob data corresponds to the provided commitment.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="result">True if the proof is valid</param>
|
||||||
|
/// <param name="blob"></param>
|
||||||
|
/// <param name="commitment"></param>
|
||||||
|
/// <param name="proof"></param>
|
||||||
|
/// <param name="ts">Trusted setup settings</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[DllImport("ckzg", EntryPoint = "verify_blob_kzg_proof", CallingConvention = CallingConvention.Cdecl)]
|
||||||
|
public unsafe static extern Ret VerifyBlobKzgProof(bool* result, byte* blob, byte* commitment_bytes, byte* proof_bytes, IntPtr ts);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Given a list of blobs and blob KZG proofs, verify that they correspond to the provided commitments.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="result">True if the proofs are valid</param>
|
||||||
|
/// <param name="blobs">Blobs as a flattened byte array</param>
|
||||||
|
/// <param name="commitments">Commitments as a flattened byte array</param>
|
||||||
|
/// <param name="proofs">Proofs as a flattened byte array</param>
|
||||||
|
/// <param name="count">The number of blobs/commitments/proofs</param>
|
||||||
|
/// <param name="ts">Trusted setup settings</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[DllImport("ckzg", EntryPoint = "verify_blob_kzg_proof_batch", CallingConvention = CallingConvention.Cdecl)]
|
||||||
|
public unsafe static extern Ret VerifyBlobKzgProofBatch(bool* result, byte* blobs, byte* commitments_bytes, byte* proofs_bytes, int count, IntPtr ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,57 +1,218 @@
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace Ckzg.Test;
|
namespace Ckzg.Test;
|
||||||
|
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class BasicKzgTests
|
public class BasicKzgTests
|
||||||
{
|
{
|
||||||
private IntPtr _ts;
|
private IntPtr ts;
|
||||||
|
|
||||||
|
const string TestDir = "../../../../../../tests";
|
||||||
|
string BlobToKZGCommitmentTests = Path.Join(TestDir, "blob_to_kzg_commitment");
|
||||||
|
string ComputeKzgProofTests = Path.Join(TestDir, "compute_kzg_proof");
|
||||||
|
string ComputeBlobKzgProofTests = Path.Join(TestDir, "compute_blob_kzg_proof");
|
||||||
|
string VerifyKzgProofTests = Path.Join(TestDir, "verify_kzg_proof");
|
||||||
|
string VerifyBlobKzgProofTests = Path.Join(TestDir, "verify_blob_kzg_proof");
|
||||||
|
string VerifyBlobKzgProofBatchTests = Path.Join(TestDir, "verify_blob_kzg_proof_batch");
|
||||||
|
|
||||||
|
public static byte[] StringToByteArray(string hex)
|
||||||
|
{
|
||||||
|
return Enumerable.Range(0, hex.Length)
|
||||||
|
.Where(x => x % 2 == 0)
|
||||||
|
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] GetBytes(String path)
|
||||||
|
{
|
||||||
|
string hex = System.IO.File.ReadAllText(path);
|
||||||
|
return StringToByteArray(hex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] GetFlatBytes(String path)
|
||||||
|
{
|
||||||
|
List<String> files = Directory.GetFiles(path).ToList();
|
||||||
|
files.Sort();
|
||||||
|
List<byte[]> filesBytes = new List<byte[]>();
|
||||||
|
foreach (String file in files)
|
||||||
|
{
|
||||||
|
filesBytes.Add(GetBytes(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] flatBytes = new byte[filesBytes.Sum(b => b.Length)];
|
||||||
|
int offset = 0;
|
||||||
|
foreach (byte[] bytes in filesBytes)
|
||||||
|
{
|
||||||
|
System.Buffer.BlockCopy(bytes, 0, flatBytes, offset, bytes.Length);
|
||||||
|
offset += bytes.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
return flatBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool GetBoolean(String path)
|
||||||
|
{
|
||||||
|
return System.IO.File.ReadAllText(path).Contains("true");
|
||||||
|
}
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
{
|
{
|
||||||
_ts = Ckzg.LoadTrustedSetup("trusted_setup.txt");
|
ts = Ckzg.LoadTrustedSetup("trusted_setup.txt");
|
||||||
Assert.That(_ts, Is.Not.EqualTo(IntPtr.Zero));
|
Assert.That(ts, Is.Not.EqualTo(IntPtr.Zero));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase(0xff, 1, -1)]
|
[TearDown]
|
||||||
[TestCase(0x73, 1, -1)]
|
public void Teardown()
|
||||||
[TestCase(0x72, 0, 0)]
|
|
||||||
[TestCase(0x00, 0, 0)]
|
|
||||||
public unsafe void Test_Computes_And_Verifies(byte highByteValue, int expectedProofComputed, int expectedProofVerified)
|
|
||||||
{
|
{
|
||||||
byte[] blob = Enumerable.Range(0, 4096 * 32).Select(x => x % 32 == 31 ? highByteValue : (byte)(x % 256)).ToArray();
|
Ckzg.FreeTrustedSetup(ts);
|
||||||
|
}
|
||||||
|
|
||||||
byte[] proof = new byte[48];
|
[TestCase]
|
||||||
|
public unsafe void TestBlobToKzgCommitment()
|
||||||
|
{
|
||||||
|
foreach (String test in Directory.GetDirectories(BlobToKZGCommitmentTests))
|
||||||
|
{
|
||||||
byte[] commitment = new byte[48];
|
byte[] commitment = new byte[48];
|
||||||
fixed (byte* commitmentPtr = commitment, blobPtr = blob, proofPtr = proof)
|
byte[] blob = GetBytes(Path.Join(test, "blob.txt"));
|
||||||
|
fixed (byte *pCommitment = commitment, pBlob = blob)
|
||||||
{
|
{
|
||||||
int proofComputed = Ckzg.ComputeAggregatedKzgProof(proofPtr, blobPtr, 1, _ts);
|
Ckzg.Ret ret = Ckzg.BlobToKzgCommitment(pCommitment, pBlob, ts);
|
||||||
Assert.That(proofComputed, Is.EqualTo(expectedProofComputed));
|
if (ret == Ckzg.Ret.Ok)
|
||||||
|
{
|
||||||
Ckzg.BlobToKzgCommitment(commitmentPtr, blobPtr, _ts);
|
byte[] expectedCommitment = GetBytes(Path.Join(test, "commitment.txt"));
|
||||||
int proofVerified = Ckzg.VerifyAggregatedKzgProof(blobPtr, commitmentPtr, 1, proofPtr, _ts);
|
Assert.That(commitment, Is.EqualTo(expectedCommitment));
|
||||||
Assert.That(proofVerified, Is.EqualTo(expectedProofVerified));
|
}
|
||||||
|
else
|
||||||
Ckzg.FreeTrustedSetup(_ts);
|
{
|
||||||
|
Assert.False(System.IO.File.Exists(Path.Join(test, "commitment.txt")));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase]
|
[TestCase]
|
||||||
public unsafe void Test_PointEvaluationPrecompile_Verifies()
|
public unsafe void TestComputeKzgProof()
|
||||||
|
{
|
||||||
|
foreach (String test in Directory.GetDirectories(ComputeKzgProofTests))
|
||||||
{
|
{
|
||||||
byte[] commitment = new byte[48];
|
|
||||||
commitment[0] = 0xc0;
|
|
||||||
byte[] x = new byte[32];
|
|
||||||
byte[] y = new byte[32];
|
|
||||||
byte[] proof = new byte[48];
|
byte[] proof = new byte[48];
|
||||||
proof[0] = 0xc0;
|
byte[] blob = GetBytes(Path.Join(test, "blob.txt"));
|
||||||
|
byte[] inputPoint = GetBytes(Path.Join(test, "input_point.txt"));
|
||||||
fixed (byte* commitmentPtr = commitment, xPtr = x, yPtr = y, proofPtr = proof)
|
fixed (byte *pProof = proof, pBlob = blob, pInputPoint = inputPoint)
|
||||||
{
|
{
|
||||||
int result = Ckzg.VerifyKzgProof(commitmentPtr, xPtr, yPtr, proofPtr, _ts);
|
Ckzg.Ret ret = Ckzg.ComputeKzgProof(pProof, pBlob, pInputPoint, ts);
|
||||||
Ckzg.FreeTrustedSetup(_ts);
|
if (ret == Ckzg.Ret.Ok)
|
||||||
Assert.That(result, Is.EqualTo(0));
|
{
|
||||||
|
byte[] expectedProof = GetBytes(Path.Join(test, "proof.txt"));
|
||||||
|
Assert.That(proof, Is.EqualTo(expectedProof));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.False(System.IO.File.Exists(Path.Join(test, "proof.txt")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase]
|
||||||
|
public unsafe void TestComputeBlobKzgProof()
|
||||||
|
{
|
||||||
|
foreach (String test in Directory.GetDirectories(ComputeBlobKzgProofTests))
|
||||||
|
{
|
||||||
|
byte[] proof = new byte[48];
|
||||||
|
byte[] blob = GetBytes(Path.Join(test, "blob.txt"));
|
||||||
|
fixed (byte *pProof = proof, pBlob = blob)
|
||||||
|
{
|
||||||
|
Ckzg.Ret ret = Ckzg.ComputeBlobKzgProof(pProof, pBlob, ts);
|
||||||
|
if (ret == Ckzg.Ret.Ok)
|
||||||
|
{
|
||||||
|
byte[] expectedProof = GetBytes(Path.Join(test, "proof.txt"));
|
||||||
|
Assert.That(proof, Is.EqualTo(expectedProof));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.False(System.IO.File.Exists(Path.Join(test, "proof.txt")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase]
|
||||||
|
public unsafe void TestVerifyKzgProof()
|
||||||
|
{
|
||||||
|
foreach (String test in Directory.GetDirectories(VerifyKzgProofTests))
|
||||||
|
{
|
||||||
|
bool ok = false;
|
||||||
|
byte[] commitment = GetBytes(Path.Join(test, "commitment.txt"));
|
||||||
|
byte[] inputPoint = GetBytes(Path.Join(test, "input_point.txt"));
|
||||||
|
byte[] claimedValue = GetBytes(Path.Join(test, "claimed_value.txt"));
|
||||||
|
byte[] proof = GetBytes(Path.Join(test, "proof.txt"));
|
||||||
|
fixed (byte *pCommitment = commitment, pInputPoint = inputPoint, pClaimedValue = claimedValue, pProof = proof)
|
||||||
|
{
|
||||||
|
Ckzg.Ret ret = Ckzg.VerifyKzgProof(&ok, pCommitment, pInputPoint, pClaimedValue, pProof, ts);
|
||||||
|
if (ret == Ckzg.Ret.Ok)
|
||||||
|
{
|
||||||
|
bool expectedOk = GetBoolean(Path.Join(test, "ok.txt"));
|
||||||
|
Assert.That(ok, Is.EqualTo(expectedOk));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.False(System.IO.File.Exists(Path.Join(test, "ok.txt")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase]
|
||||||
|
public unsafe void TestVerifyBlobKzgProof()
|
||||||
|
{
|
||||||
|
foreach (String test in Directory.GetDirectories(VerifyBlobKzgProofTests))
|
||||||
|
{
|
||||||
|
bool ok = false;
|
||||||
|
byte[] blob = GetBytes(Path.Join(test, "blob.txt"));
|
||||||
|
byte[] commitment = GetBytes(Path.Join(test, "commitment.txt"));
|
||||||
|
byte[] proof = GetBytes(Path.Join(test, "proof.txt"));
|
||||||
|
fixed (byte *pBlob = blob, pCommitment = commitment, pProof = proof)
|
||||||
|
{
|
||||||
|
Ckzg.Ret ret = Ckzg.VerifyBlobKzgProof(&ok, pBlob, pCommitment, pProof, ts);
|
||||||
|
if (ret == Ckzg.Ret.Ok)
|
||||||
|
{
|
||||||
|
bool expectedOk = GetBoolean(Path.Join(test, "ok.txt"));
|
||||||
|
Assert.That(ok, Is.EqualTo(expectedOk));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.False(System.IO.File.Exists(Path.Join(test, "ok.txt")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase]
|
||||||
|
public unsafe void TestVerifyBlobKzgProofBatch()
|
||||||
|
{
|
||||||
|
foreach (String test in Directory.GetDirectories(VerifyBlobKzgProofBatchTests))
|
||||||
|
{
|
||||||
|
bool ok = false;
|
||||||
|
byte[] blobs = GetFlatBytes(Path.Join(test, "blobs"));
|
||||||
|
byte[] commitments = GetFlatBytes(Path.Join(test, "commitments"));
|
||||||
|
byte[] proofs = GetFlatBytes(Path.Join(test, "proofs"));
|
||||||
|
int count = blobs.Length / Ckzg.BytesPerBlob;
|
||||||
|
fixed (byte *pBlobs = blobs, pCommitments = commitments, pProofs = proofs)
|
||||||
|
{
|
||||||
|
Ckzg.Ret ret = Ckzg.VerifyBlobKzgProofBatch(&ok, pBlobs, pCommitments, pProofs, count, ts);
|
||||||
|
if (ret == Ckzg.Ret.Ok)
|
||||||
|
{
|
||||||
|
bool expectedOk = GetBoolean(Path.Join(test, "ok.txt"));
|
||||||
|
Assert.That(ok, Is.EqualTo(expectedOk));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.False(System.IO.File.Exists(Path.Join(test, "ok.txt")));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,54 +1,54 @@
|
||||||
INCLUDE_DIRS = ../../src ../../blst/bindings
|
|
||||||
|
|
||||||
FIELD_ELEMENTS_PER_BLOB ?= 4096
|
|
||||||
|
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
BLST_BUILDSCRIPT = ./build.bat
|
BLST_BUILDSCRIPT = ./build.bat
|
||||||
BLST_OBJ = blst.lib
|
BLST_OBJ = blst.lib
|
||||||
TESTS_EXECUTABLE=test_ckzg.exe
|
|
||||||
CSHARP_PLATFORM ?= win-x64
|
CSHARP_PLATFORM ?= win-x64
|
||||||
CLANG_PLATFROM?=x86_64-win
|
|
||||||
CLANG_FLAGS=
|
|
||||||
CLANG_EXECUTABLE = clang
|
CLANG_EXECUTABLE = clang
|
||||||
CKZG_LIBRARY_PATH = Ckzg.Bindings\runtimes\$(CSHARP_PLATFORM)\native\ckzg.dll
|
CKZG_LIBRARY_PATH = Ckzg.Bindings\runtimes\$(CSHARP_PLATFORM)\native\ckzg.dll
|
||||||
else
|
else
|
||||||
BLST_BUILDSCRIPT = ./build.sh
|
BLST_BUILDSCRIPT = ./build.sh
|
||||||
BLST_OBJ = libblst.a
|
BLST_OBJ = libblst.a
|
||||||
TESTS_EXECUTABLE=./test_ckzg
|
CLANG_EXECUTABLE = clang
|
||||||
|
|
||||||
|
UNAME_S := $(shell uname -s)
|
||||||
|
UNAME_M := $(shell uname -m)
|
||||||
|
ifeq ($(UNAME_S),Linux)
|
||||||
|
ifeq ($(UNAME_M),x86_64)
|
||||||
CSHARP_PLATFORM ?= linux-x64
|
CSHARP_PLATFORM ?= linux-x64
|
||||||
CLANG_PLATFORM?=x86_64-linux
|
else
|
||||||
CLANG_FLAGS=-fPIC
|
CSHARP_PLATFORM ?= linux-arm64
|
||||||
CLANG_EXECUTABLE=cc
|
endif
|
||||||
|
endif
|
||||||
|
ifeq ($(UNAME_S),Darwin)
|
||||||
|
ifeq ($(UNAME_M),arm64)
|
||||||
|
CSHARP_PLATFORM ?= osx-arm64
|
||||||
|
else
|
||||||
|
CSHARP_PLATFORM ?= osx-x64
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
CKZG_LIBRARY_PATH = Ckzg.Bindings/runtimes/$(CSHARP_PLATFORM)/native/ckzg.so
|
CKZG_LIBRARY_PATH = Ckzg.Bindings/runtimes/$(CSHARP_PLATFORM)/native/ckzg.so
|
||||||
endif
|
endif
|
||||||
|
|
||||||
CLANG_FLAGS += -DFIELD_ELEMENTS_PER_BLOB=$(FIELD_ELEMENTS_PER_BLOB)
|
FIELD_ELEMENTS_PER_BLOB ?= 4096
|
||||||
|
INCLUDE_DIRS = ../../src ../../blst/bindings
|
||||||
TARGETS = ckzg.c ../../src/c_kzg_4844.c ../../blst/$(BLST_OBJ)
|
TARGETS = ckzg.c ../../src/c_kzg_4844.c ../../blst/$(BLST_OBJ)
|
||||||
|
|
||||||
.blst:
|
CFLAGS += -O2 -Wall -Wextra -shared
|
||||||
|
CFLAGS += -DFIELD_ELEMENTS_PER_BLOB=$(FIELD_ELEMENTS_PER_BLOB)
|
||||||
|
CFLAGS += ${addprefix -I,${INCLUDE_DIRS}}
|
||||||
|
|
||||||
|
.PHONY: all
|
||||||
|
all: blst ckzg ckzg-dotnet
|
||||||
|
|
||||||
|
.PHONY: blst
|
||||||
|
blst:
|
||||||
cd ../../blst && $(BLST_BUILDSCRIPT)
|
cd ../../blst && $(BLST_BUILDSCRIPT)
|
||||||
|
|
||||||
.ckzg:
|
.PHONY: ckzg
|
||||||
$(CLANG_EXECUTABLE) -O -Wall -shared $(CLANG_FLAGS) ${addprefix -I,${INCLUDE_DIRS}} -o $(CKZG_LIBRARY_PATH) $(TARGETS)
|
ckzg: blst
|
||||||
|
$(CLANG_EXECUTABLE) $(CFLAGS) -o $(CKZG_LIBRARY_PATH) $(TARGETS)
|
||||||
|
|
||||||
# Ckzg library
|
.PHONY: ckzg-dotnet
|
||||||
ckzg:
|
ckzg-dotnet: ckzg
|
||||||
@make .blst
|
|
||||||
@make .ckzg
|
|
||||||
|
|
||||||
# E2e tests as an executable
|
|
||||||
test:
|
|
||||||
@make .blst
|
|
||||||
$(CLANG_EXECUTABLE) -O -w -Wall $(CLANG_FLAGS) ${addprefix -I,${INCLUDE_DIRS}} -o $(TESTS_EXECUTABLE) kzg_tests.c $(TARGETS)
|
|
||||||
|
|
||||||
# E2e tests are built and run
|
|
||||||
run-test:
|
|
||||||
@make test
|
|
||||||
$(TESTS_EXECUTABLE)
|
|
||||||
|
|
||||||
# Makes a build and a nuget package just for windows or linux
|
|
||||||
# To build full package - use ckzg command on every plaform and dotnet build
|
|
||||||
ckzg-dotnet:
|
|
||||||
@make ckzg
|
|
||||||
dotnet build
|
dotnet build
|
||||||
|
dotnet test
|
||||||
|
|
|
@ -23,19 +23,3 @@ void free_trusted_setup_wrap(KZGSettings *s) {
|
||||||
free_trusted_setup(s);
|
free_trusted_setup(s);
|
||||||
free(s);
|
free(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
int verify_aggregate_kzg_proof_wrap(const Blob *blobs, const Bytes48 *commitments_bytes, size_t n, const Bytes48 *aggregated_proof_bytes, const KZGSettings *s) {
|
|
||||||
bool b;
|
|
||||||
C_KZG_RET ret = verify_aggregate_kzg_proof(&b, blobs, commitments_bytes, n, aggregated_proof_bytes, s);
|
|
||||||
if (ret != C_KZG_OK) return -1;
|
|
||||||
|
|
||||||
return b ? 0 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int verify_kzg_proof_wrap(const Bytes48 *commitment_bytes, const Bytes32 *z_bytes, const Bytes32 *y_bytes, const Bytes48 *proof_bytes, KZGSettings *s) {
|
|
||||||
bool out;
|
|
||||||
if (verify_kzg_proof(&out, commitment_bytes, z_bytes, y_bytes, proof_bytes, s) != C_KZG_OK)
|
|
||||||
return -2;
|
|
||||||
|
|
||||||
return out ? 0 : 1;
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,8 +15,12 @@ DLLEXPORT void free_trusted_setup_wrap(KZGSettings *s);
|
||||||
|
|
||||||
DLLEXPORT C_KZG_RET blob_to_kzg_commitment(KZGCommitment *out, const Blob *blob, const KZGSettings *s);
|
DLLEXPORT C_KZG_RET blob_to_kzg_commitment(KZGCommitment *out, const Blob *blob, const KZGSettings *s);
|
||||||
|
|
||||||
DLLEXPORT int verify_aggregate_kzg_proof_wrap(const Blob blobs[], const Bytes48 *commitments_bytes, size_t n, const Bytes48 *aggregated_proof_bytes, const KZGSettings *s);
|
DLLEXPORT C_KZG_RET compute_kzg_proof(KZGProof *out, const Blob *blob, const Bytes32 *z_bytes, const KZGSettings *s);
|
||||||
|
|
||||||
DLLEXPORT C_KZG_RET compute_aggregate_kzg_proof(KZGProof *out, const Blob blobs[], size_t n, const KZGSettings *s);
|
DLLEXPORT C_KZG_RET compute_blob_kzg_proof(KZGProof *out, const Blob *blob, const KZGSettings *s);
|
||||||
|
|
||||||
DLLEXPORT int verify_kzg_proof_wrap(const Bytes48 *commitment_bytes, const Bytes32 *z_bytes, const Bytes32 *y_bytes, const Bytes48 *proof_bytes, KZGSettings *s);
|
DLLEXPORT C_KZG_RET verify_kzg_proof(bool *result, const Bytes48 *commitments_bytes, const Bytes32 *z_bytes, const Bytes32 *y_bytes, const Bytes48 *proof_bytes, const KZGSettings *s);
|
||||||
|
|
||||||
|
DLLEXPORT C_KZG_RET verify_blob_kzg_proof(bool *result, const Blob *blob, const Bytes48 *commitment_bytes, const Bytes48 *proof_bytes, const KZGSettings *s);
|
||||||
|
|
||||||
|
DLLEXPORT C_KZG_RET verify_blob_kzg_proof_batch(bool *result, const Blob *blobs, const Bytes48 *commitments_bytes, const Bytes48 *proofs_bytes, size_t count, const KZGSettings *s);
|
||||||
|
|
Loading…
Reference in New Issue