Rollback hash usage, clean up code

This commit is contained in:
Alexey Osipov 2022-11-22 10:44:34 +03:00
parent 853c7aee95
commit b7a4f7ccea
9 changed files with 69 additions and 4231 deletions

View File

@ -11,7 +11,7 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>Ckzg.Bindings</Title>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>0.1.0.3</Version>
<Version>0.1.0.4</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -5,11 +5,12 @@ namespace Ckzg;
public class Ckzg
{
static Ckzg()
{
AssemblyLoadContext.Default.ResolvingUnmanagedDll += (assembly, path) =>
{
var a = $"runtimes/{(
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
@ -17,27 +18,66 @@ public class Ckzg
Architecture.X64 => "x64",
Architecture.Arm64 => "arm64",
_ => ""
}}/native/{path}.{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dll" : "so")}";
return NativeLibrary.Load(a);
};
}
}}/native/{path}.{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dll" : "so")}");
/// <summary>
/// Calculates commitment for the blob
/// </summary>
/// <param name="commitment">Prealocated 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>
[DllImport("ckzg", EntryPoint = "blob_to_kzg_commitment_wrap", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern void BlobToKzgCommitment(byte* retval, byte* blob, IntPtr ts);
public unsafe static extern void BlobToKzgCommitment(byte* commitment, byte* blob, IntPtr ts);
/// <summary>
/// Calculates aggregated proof for the blobs
/// </summary>
/// <param name="proof">Prealocated 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 succeed</returns>
[DllImport("ckzg", EntryPoint = "compute_aggregate_kzg_proof_wrap", CallingConvention = CallingConvention.Cdecl)] // returns 0 on success
public unsafe static extern int ComputeAggregateKzgProof(byte* retval, byte* blobs, int n, IntPtr ts);
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)] // returns 0 on success
public unsafe static extern int VerifyAggregateKzgProof(byte* blobs, byte* commitments, int blobCount, byte* proof, IntPtr ts);
public unsafe static extern int VerifyAggregatedKzgProof(byte* blobs, byte* commitments, int count, byte* proof, IntPtr ts);
/// <summary>
/// Verify the proof by point evaluation for the given commitment
/// </summary>
/// <param name="commitment">Commitment</param>
/// <param name="x">X</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)] // returns 0 on success
public unsafe static extern int VerifyKzgProof(byte* commitment, byte* x, byte* y, byte* proof, IntPtr ts);
/// <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);
/// <summary>
/// Frees memory allocated for trusted setup settings
/// </summary>
/// <param name="ts">Trusted setup settings</param>
[DllImport("ckzg", EntryPoint = "free_trusted_setup_wrap", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreeTrustedSetup(IntPtr ts);
}

View File

@ -8,10 +8,6 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<None Remove="devnetv2-geth.txt" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
@ -25,9 +21,6 @@
</ItemGroup>
<ItemGroup>
<Content Include="..\devnetv2-geth.txt" Link="devnetv2-geth.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="..\..\..\src\trusted_setup.txt" Link="trusted_setup.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

File diff suppressed because one or more lines are too long

View File

@ -1 +0,0 @@
global using NUnit.Framework;

File diff suppressed because it is too large Load Diff

View File

@ -2,8 +2,8 @@
#include "ckzg.h"
#include <stdio.h>
void TestProofs(char * path){
KZGSettings *s = load_trusted_setup_wrap(path);
void calculate_proof_and_commitment(char * trusted_setup_path){
KZGSettings *s = load_trusted_setup_wrap(trusted_setup_path);
size_t n = 1;
uint8_t *commitment = (uint8_t *)calloc(48, 1);
uint8_t *proof = (uint8_t *)calloc(48, 1);
@ -19,26 +19,12 @@ void TestProofs(char * path){
blob_to_kzg_commitment_wrap(commitment, blob, s);
FILE *f = fopen("output.txt", "wt");
// blob
for(int i = 0; i< 4096*32; i++){
fprintf(f, "%02x", blob[i]);
}
fprintf(f, "\n");
// commitment
for(int i = 0; i< 48; i++){
fprintf(f, "%02x", commitment[i]);
}
fprintf(f, "\n");
// hash
hash(blobHash, commitment, 48);
blobHash[0] = 1;
for(int i = 0; i< 32; i++){
fprintf(f, "%02x", blobHash[i]);
}
fprintf(f, "\n");
// proof
for(int i = 0; i< 48; i++){
fprintf(f, "%02x", proof[i]);
@ -54,6 +40,6 @@ void TestProofs(char * path){
int main() {
TestProofs("devnetv3-geth.txt");
calculate_proof_and_commitment("../../src/trusted_setup.txt");
return 0;
}

View File

@ -1065,7 +1065,7 @@ void sha256_init(SHA256_CTX *ctx);
void sha256_update(SHA256_CTX *ctx, const void *_inp, size_t len);
void sha256_final(unsigned char md[32], SHA256_CTX *ctx);
void hash(uint8_t md[32], const uint8_t input[], size_t n) {
static void hash(uint8_t md[32], const uint8_t input[], size_t n) {
SHA256_CTX ctx;
sha256_init(&ctx);
sha256_update(&ctx, input, n);

View File

@ -121,8 +121,6 @@ C_KZG_RET verify_kzg_proof(bool *out,
const KZGProof *kzg_proof,
const KZGSettings *s);
void hash(uint8_t md[32], const uint8_t input[], size_t n);
#ifdef __cplusplus
}
#endif