Update Compute*KZGProof in c# bindings (#187)

This commit is contained in:
Justin Traglia 2023-03-08 15:13:48 -07:00 committed by GitHub
parent 6f3751d97b
commit ff4e99579d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 32 deletions

View File

@ -31,10 +31,10 @@ public static partial class Ckzg
private static extern unsafe KzgResult BlobToKzgCommitment(byte* commitment, byte* blob, IntPtr ts);
[DllImport("ckzg", EntryPoint = "compute_kzg_proof", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe KzgResult ComputeKzgProof(byte* proof, byte* blob, byte* z, IntPtr ts);
private static extern unsafe KzgResult ComputeKzgProof(byte* proof_out, byte* y_out, byte* blob, byte* z, IntPtr ts);
[DllImport("ckzg", EntryPoint = "compute_blob_kzg_proof", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe KzgResult ComputeBlobKzgProof(byte* proof, byte* blob, IntPtr ts);
private static extern unsafe KzgResult ComputeBlobKzgProof(byte* proof, byte* blob, byte* commitment, IntPtr ts);
[DllImport("ckzg", EntryPoint = "verify_kzg_proof", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe KzgResult VerifyKzgProof(out bool result, byte* commitment, byte* z,

View File

@ -12,7 +12,7 @@ public static partial class Ckzg
/// Loads trusted setup settings from file.
/// </summary>
/// <param name="filename">Settings file path</param>
/// <exception cref="ArgumentException">Thrown when the file path is not correct </exception>
/// <exception cref="ArgumentException">Thrown when the file path is not correct</exception>
/// <exception cref="InvalidOperationException">Thrown when unable to load the setup</exception>
/// <returns>Trusted setup settings as a pointer</returns>
public static IntPtr LoadTrustedSetup(string filepath)
@ -40,7 +40,7 @@ public static partial class Ckzg
/// <summary>
/// Calculates commitment for the blob.
/// </summary>
/// <param name="commitment">Preallocated buffer of <inheritdoc cref="CommitmentLength" /> bytes to receive the commitment</param>
/// <param name="commitment">Preallocated buffer of <inheritdoc cref="BytesPerCommitment"/> bytes to receive the commitment</param>
/// <param name="blob">Flatten array of blob elements</param>
/// <param name="ckzgSetup">Trusted setup settings</param>
/// <exception cref="ArgumentException">Thrown when length of an argument is not correct or settings are not correct</exception>
@ -62,26 +62,26 @@ public static partial class Ckzg
/// <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="proof">Preallocated buffer of <inheritdoc cref="BytesPerProof"/> bytes to receive the proof</param>
/// <param name="y">Preallocated buffer of <inheritdoc cref="BytesPerFieldElement"/> bytes to receive y</param>
/// <param name="blob">Blob bytes</param>
/// <param name="z">Z point</param>
/// <param name="ckzgSetup">Trusted setup settings</param>
/// <exception cref="ArgumentException">Thrown when length of an argument is not correct or settings are not correct</exception>
/// <exception cref="ApplicationException">Thrown when the library returns unexpected Error code</exception>
/// <exception cref="InsufficientMemoryException">Thrown when the library has no enough memory to process</exception>
public static unsafe void ComputeKzgProof(Span<byte> proof, ReadOnlySpan<byte> blob, ReadOnlySpan<byte> z,
IntPtr ckzgSetup)
public static unsafe void ComputeKzgProof(Span<byte> proof, Span<byte> y, ReadOnlySpan<byte> blob,
ReadOnlySpan<byte> z, IntPtr ckzgSetup)
{
ThrowOnUninitializedTrustedSetup(ckzgSetup);
ThrowOnInvalidLength(proof, nameof(proof), BytesPerProof);
ThrowOnInvalidLength(y, nameof(y), BytesPerFieldElement);
ThrowOnInvalidLength(blob, nameof(blob), BytesPerBlob);
ThrowOnInvalidLength(z, nameof(z), BytesPerFieldElement);
if (z.Length != BytesPerFieldElement) throw new ArgumentException("Invalid z size", nameof(z));
fixed (byte* proofPtr = proof, blobPtr = blob, zPtr = z)
fixed (byte* proofPtr = proof, yPtr = y, blobPtr = blob, zPtr = z)
{
KzgResult result = ComputeKzgProof(proofPtr, blobPtr, zPtr, ckzgSetup);
KzgResult result = ComputeKzgProof(proofPtr, yPtr, blobPtr, zPtr, ckzgSetup);
ThrowOnError(result);
}
}
@ -89,21 +89,23 @@ public static partial class Ckzg
/// <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="proof">Preallocated buffer of <inheritdoc cref="BytesPerProof"/> bytes to receive the proof</param>
/// <param name="blob">Blob bytes</param>
/// <param name="commitment">Commitment bytes</param>
/// <param name="ckzgSetup">Trusted setup settings</param>
/// <exception cref="ArgumentException">Thrown when length of an argument is not correct or settings are not correct</exception>
/// <exception cref="ApplicationException">Thrown when the library returns unexpected Error code</exception>
/// <exception cref="InsufficientMemoryException">Thrown when the library has no enough memory to process</exception>
public static unsafe void ComputeBlobKzgProof(Span<byte> proof, ReadOnlySpan<byte> blob, IntPtr ckzgSetup)
public static unsafe void ComputeBlobKzgProof(Span<byte> proof, ReadOnlySpan<byte> blob,
ReadOnlySpan<byte> commitment, IntPtr ckzgSetup)
{
ThrowOnUninitializedTrustedSetup(ckzgSetup);
ThrowOnInvalidLength(proof, nameof(proof), BytesPerProof);
ThrowOnInvalidLength(blob, nameof(blob), BytesPerBlob);
fixed (byte* proofPtr = proof, blobPtr = blob)
fixed (byte* proofPtr = proof, blobPtr = blob, commitmentPtr = commitment)
{
KzgResult result = ComputeBlobKzgProof(proofPtr, blobPtr, ckzgSetup);
KzgResult result = ComputeBlobKzgProof(proofPtr, blobPtr, commitmentPtr, ckzgSetup);
ThrowOnError(result);
}
}
@ -111,9 +113,10 @@ public static partial class Ckzg
/// <summary>
/// Given a blob and a KZG proof, verify that the blob data corresponds to the provided commitment.
/// </summary>
/// <param name="blob"></param>
/// <param name="commitment"></param>
/// <param name="proof"></param>
/// <param name="commitment">Commitment bytes</param>
/// <param name="z">Z bytes</param>
/// <param name="y">Y bytes</param>
/// <param name="proof">Proof bytes</param>
/// <param name="ckzgSetup">Trusted setup settings</param>
/// <exception cref="ArgumentException">Thrown when length of an argument is not correct or settings are not correct</exception>
/// <exception cref="ApplicationException">Thrown when the library returns unexpected Error code</exception>

View File

@ -92,9 +92,8 @@ public class ReferenceTests
try
{
Ckzg.BlobToKzgCommitment(commitment, blob, _ts);
string? commitmentStr = test.Output;
Assert.That(commitmentStr, Is.Not.EqualTo(null));
byte[] expectedCommitment = GetBytes(commitmentStr);
Assert.That(test.Output, Is.Not.EqualTo(null));
byte[] expectedCommitment = GetBytes(test.Output);
Assert.That(commitment, Is.EqualTo(expectedCommitment));
}
catch
@ -117,7 +116,7 @@ public class ReferenceTests
private class ComputeKzgProofTest
{
public ComputeKzgProofInput Input { get; set; } = null!;
public string? Output { get; set; } = null!;
public List<string>? Output { get; set; } = null!;
}
[TestCase]
@ -133,16 +132,18 @@ public class ReferenceTests
Assert.That(test, Is.Not.EqualTo(null));
byte[] proof = new byte[48];
byte[] y = new byte[32];
byte[] blob = GetBytes(test.Input.Blob);
byte[] z = GetBytes(test.Input.Z);
try
{
Ckzg.ComputeKzgProof(proof, blob, z, _ts);
string? proofStr = test.Output;
Assert.That(proofStr, Is.Not.EqualTo(null));
byte[] expectedProof = GetBytes(proofStr);
Ckzg.ComputeKzgProof(proof, y, blob, z, _ts);
Assert.That(test.Output, Is.Not.EqualTo(null));
byte[] expectedProof = GetBytes(test.Output.ElementAt(0));
Assert.That(proof, Is.EqualTo(expectedProof));
byte[] expectedY = GetBytes(test.Output.ElementAt(1));
Assert.That(y, Is.EqualTo(expectedY));
}
catch
{
@ -158,6 +159,7 @@ public class ReferenceTests
private class ComputeBlobKzgProofInput
{
public string Blob { get; set; } = null!;
public string Commitment { get; set; } = null!;
}
private class ComputeBlobKzgProofTest
@ -180,13 +182,13 @@ public class ReferenceTests
byte[] proof = new byte[48];
byte[] blob = GetBytes(test.Input.Blob);
byte[] commitment = GetBytes(test.Input.Commitment);
try
{
Ckzg.ComputeBlobKzgProof(proof, blob, _ts);
string? proofStr = test.Output;
Assert.That(proofStr, Is.Not.EqualTo(null));
byte[] expectedProof = GetBytes(proofStr);
Ckzg.ComputeBlobKzgProof(proof, blob, commitment, _ts);
Assert.That(test.Output, Is.Not.EqualTo(null));
byte[] expectedProof = GetBytes(test.Output);
Assert.That(proof, Is.EqualTo(expectedProof));
}
catch

View File

@ -15,9 +15,9 @@ 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 compute_kzg_proof(KZGProof *out, const Blob *blob, const Bytes32 *z_bytes, const KZGSettings *s);
DLLEXPORT C_KZG_RET compute_kzg_proof(KZGProof *proof_out, Bytes32 *y_out, const Blob *blob, const Bytes32 *z_bytes, const KZGSettings *s);
DLLEXPORT C_KZG_RET compute_blob_kzg_proof(KZGProof *out, const Blob *blob, const KZGSettings *s);
DLLEXPORT C_KZG_RET compute_blob_kzg_proof(KZGProof *out, const Blob *blob, const Bytes48 *commitment, const 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);