c-kzg-4844/bindings/csharp/Ckzg.Bindings/Ckzg.cs

86 lines
4.3 KiB
C#
Raw Normal View History

2022-11-03 13:08:17 +00:00
using System.Runtime.InteropServices;
using System.Runtime.Loader;
namespace Ckzg;
public class Ckzg
{
2022-11-22 07:44:34 +00:00
public const int CommitmentLength = 48;
public const int BlobElementLength = 32;
public const int BlobLength = BlobElementLength * 4096;
public const int ProofLength = 48;
static Ckzg() => AssemblyLoadContext.Default.ResolvingUnmanagedDll += (assembly, path) => NativeLibrary.Load($"runtimes/{(
2022-11-03 13:08:17 +00:00
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" :
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" : "")}-{RuntimeInformation.ProcessArchitecture switch
{
Architecture.X64 => "x64",
Architecture.Arm64 => "arm64",
_ => ""
2022-11-22 07:44:34 +00:00
}}/native/{path}.{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dll" : "so")}");
2022-11-03 13:08:17 +00:00
2022-11-22 07:44:34 +00:00
/// <summary>
/// Calculates commitment for the blob
/// </summary>
2023-01-09 22:15:01 +00:00
/// <param name="commitment">Preallocated buffer of <inheritdoc cref="CommitmentLength"/> bytes to receive the commitment</param>
2022-11-22 07:44:34 +00:00
/// <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);
2022-11-03 13:08:17 +00:00
2022-11-22 07:44:34 +00:00
/// <summary>
/// Calculates aggregated proof for the blobs
/// </summary>
2023-01-09 22:15:01 +00:00
/// <param name="proof">Preallocated buffer of <inheritdoc cref="ProofLength"/> bytes to receive the proof</param>
2022-11-22 07:44:34 +00:00
/// <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)]
2022-11-22 07:44:34 +00:00
public unsafe static extern int ComputeAggregatedKzgProof(byte* proof, byte* blobs, int count, IntPtr ts);
2022-11-03 13:08:17 +00:00
2022-11-22 07:44:34 +00:00
/// <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);
2022-11-03 13:08:17 +00:00
2022-11-22 07:44:34 +00:00
/// <summary>
/// Verify the proof by point evaluation for the given commitment
/// </summary>
/// <param name="commitment">Commitment</param>
/// <param name="z">Z</param>
2022-11-22 07:44:34 +00:00
/// <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);
2022-11-03 13:08:17 +00:00
2022-11-22 07:44:34 +00:00
/// <summary>
/// Load trusted setup settings from file
/// </summary>
/// <param name="filename">Settings file path</param>
/// <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()
public static extern IntPtr LoadTrustedSetup(string filename);
2022-11-03 13:08:17 +00:00
2022-11-22 07:44:34 +00:00
/// <summary>
/// Frees memory allocated for trusted setup settings
/// </summary>
/// <param name="ts">Trusted setup settings</param>
2022-11-03 13:08:17 +00:00
[DllImport("ckzg", EntryPoint = "free_trusted_setup_wrap", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreeTrustedSetup(IntPtr ts);
2022-11-03 13:08:17 +00:00
}