using System.Runtime.InteropServices; using System.Runtime.Loader; namespace Ckzg; public class Ckzg { 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/{( RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "linux" : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx" : RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" : "")}-{RuntimeInformation.ProcessArchitecture switch { Architecture.X64 => "x64", Architecture.Arm64 => "arm64", _ => "" }}/native/{path}.{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dll" : "so")}"); /// /// Calculates commitment for the blob /// /// Prealocated buffer of bytes to receive the commitment /// Flatten array of blob elements /// Trusted setup settings [DllImport("ckzg", EntryPoint = "blob_to_kzg_commitment_wrap", CallingConvention = CallingConvention.Cdecl)] public unsafe static extern void BlobToKzgCommitment(byte* commitment, byte* blob, IntPtr ts); /// /// Calculates aggregated proof for the blobs /// /// Prealocated buffer of bytes to receive the proof /// Blobs as a flatten byte array /// Blobs count /// Trusted setup settings /// Returns error code or 0 if succeed [DllImport("ckzg", EntryPoint = "compute_aggregate_kzg_proof_wrap", CallingConvention = CallingConvention.Cdecl)] // returns 0 on success public unsafe static extern int ComputeAggregatedKzgProof(byte* proof, byte* blobs, int count, IntPtr ts); /// /// Verify aggregated proof and commitments for the given blobs /// /// Blobs as a flatten byte array /// Commitments as a flatten byte array /// Blobs and commitments count /// /// Trusted setup settings /// Returns error code or 0 if the proof is correct [DllImport("ckzg", EntryPoint = "verify_aggregate_kzg_proof_wrap", CallingConvention = CallingConvention.Cdecl)] // returns 0 on success public unsafe static extern int VerifyAggregatedKzgProof(byte* blobs, byte* commitments, int count, byte* proof, IntPtr ts); /// /// Verify the proof by point evaluation for the given commitment /// /// Commitment /// Z /// Y /// Proof /// Trusted setup settings /// Returns error code or 0 if the proof is correct [DllImport("ckzg", EntryPoint = "verify_kzg_proof_wrap", CallingConvention = CallingConvention.Cdecl)] // returns 0 on success public unsafe static extern int VerifyKzgProof(byte* commitment, byte* z, byte* y, byte* proof, IntPtr ts); /// /// Load trusted setup settings from file /// /// Settings file path /// Trusted setup settings as a pointer or 0 in case of failure [DllImport("ckzg", EntryPoint = "load_trusted_setup_wrap")] // free result with free_trusted_setup() public static extern IntPtr LoadTrustedSetup(string filename); /// /// Frees memory allocated for trusted setup settings /// /// Trusted setup settings [DllImport("ckzg", EntryPoint = "free_trusted_setup_wrap", CallingConvention = CallingConvention.Cdecl)] public static extern void FreeTrustedSetup(IntPtr ts); }