Merge pull request #2 from flcl42/feature/kzg-test

Sync C# wrapper changes, update tests, minor changes for c-kzg
This commit is contained in:
Ramana Kumar 2022-11-22 20:53:38 +00:00 committed by GitHub
commit 4b658698cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 164 additions and 4341 deletions

View File

@ -16,12 +16,12 @@ jobs:
- clang: x86_64-linux
native: linux-x64
host: ubuntu-latest
#- clang: arm64-linux
# native: linux-arm64
# host: ubuntu-latest
#- clang: arm64-darwin
# native: osx-arm64
# host: macos-latest
- clang: arm64-linux
native: linux-arm64
host: ubuntu-latest
- clang: arm64-darwin
native: osx-arm64
host: macos-latest
- clang: x86_64-darwin
native: osx-x64
host: macos-latest
@ -34,7 +34,7 @@ jobs:
with:
submodules: recursive
- 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 }} CLANG_PLATFORM=${{ matrix.target.clang }}
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
@ -58,6 +58,14 @@ jobs:
with:
name: ckzg-library-win-x64
path: bindings/csharp/Ckzg.Bindings/runtimes/win-x64/native
- uses: actions/download-artifact@v3
with:
name: ckzg-library-osx-arm64
path: bindings/csharp/Ckzg.Bindings/runtimes/osx-arm64/native
- uses: actions/download-artifact@v3
with:
name: ckzg-library-linux-arm64
path: bindings/csharp/Ckzg.Bindings/runtimes/linux-arm64/native
- name: Setup .NET Core SDK ${{ matrix.dotnet-version }}
uses: actions/setup-dotnet@v3
@ -70,5 +78,5 @@ jobs:
- name: Upload package
uses: actions/upload-artifact@v3
with:
name: Ckzg.Bindings.nuget
path: bindings/csharp/build/Ckzg.Bindings.1.0.0.nupkg
name: Ckzg.Bindings
path: bindings/csharp/build/Ckzg.Bindings.*.nupkg

View File

@ -1,14 +0,0 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Launch",
"preLaunchTask": "build",
"program": "${fileDirname}/tests.exe",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

View File

@ -1,12 +0,0 @@
{
"files.associations": {
"stdbool.h": "c",
"c_kzg_4844.h": "c",
"stdint.h": "c",
"string.h": "c",
"inttypes.h": "c",
"stdlib.h": "c",
"sha256.h": "c",
"ckzg.h": "c"
}
}

View File

@ -1,35 +0,0 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "build",
"command": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\Llvm\\x64\\bin\\clang.exe",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-w",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I../../src",
"-I../../blst/bindings",
"-I../../blst/src",
"../../blst/blst.lib",
"../../src/c_kzg_4844.c",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

View File

@ -5,13 +5,13 @@
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Ckzg</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10</LangVersion>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>Ckzg</Title>
<Title>Ckzg.Bindings</Title>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>0.1.0.4</Version>
</PropertyGroup>
<ItemGroup>
@ -22,16 +22,11 @@
</ItemGroup>
<ItemGroup>
<Content Include="runtimes\*\native\*.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)\runtimes</PackagePath>
</Content>
<Content Include="runtimes\*\native\*.so">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)\runtimes</PackagePath>
</Content>
<Content CopyToOutputDirectory="PreserveNewest" Include="runtimes/win-x64/native/ckzg.dll" Pack="true" PackagePath="runtimes/win-x64/native/ckzg.dll" />
<Content CopyToOutputDirectory="PreserveNewest" Include="runtimes/linux-x64/native/ckzg.so" Pack="true" PackagePath="runtimes/linux-x64/native/ckzg.so" />
<Content CopyToOutputDirectory="PreserveNewest" Include="runtimes/linux-arm64/native/ckzg.so" Pack="true" PackagePath="runtimes/linux-arm64/native/ckzg.so" />
<Content CopyToOutputDirectory="PreserveNewest" Include="runtimes/osx-x64/native/ckzg.so" Pack="true" PackagePath="runtimes/osx-x64/native/ckzg.so" />
<Content CopyToOutputDirectory="PreserveNewest" Include="runtimes/osx-arm64/native/ckzg.so" Pack="true" PackagePath="runtimes/osx-arm64/native/ckzg.so" />
</ItemGroup>
</Project>

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,28 +18,67 @@ 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 static unsafe extern void blob_to_kzg_commitment(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 static unsafe extern int compute_aggregate_kzg_proof(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 static unsafe extern int verify_aggregate_kzg_proof(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="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)] // returns 0 on success
public static extern int verify_kzg_proof(byte[/*48*/] commitment, byte[/*32*/] z, byte[/*32*/] y, byte[/*48*/] proof, IntPtr ts);
public unsafe static extern int VerifyKzgProof(byte* commitment, byte* z, 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 load_trusted_setup(string filename);
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 free_trusted_setup(IntPtr ts);
public static extern void FreeTrustedSetup(IntPtr ts);
}

View File

@ -4,14 +4,10 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<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;

View File

@ -20,6 +20,8 @@ else
CKZG_LIBRARY_PATH=Ckzg.Bindings/runtimes/$(CSHARP_PLATFORM)/native/ckzg.so
endif
TARGETS=ckzg.c ../../src/c_kzg_4844.c ../../blst/$(BLST_OBJ)
.blst:
cd ../../blst &&\
git apply ../blst_sha.patch &&\
@ -27,8 +29,8 @@ endif
git apply -R ../blst_sha.patch &&\
cd ../bindings/csharp
.ckzg: ckzg.c ../../src/c_kzg_4844.c ../../blst/$(BLST_OBJ)
$(CLANG_EXECUTABLE) -O -Wall -shared $(CLANG_FLAGS) ${addprefix -I,${INCLUDE_DIRS}} -o $(CKZG_LIBRARY_PATH) $^
.ckzg:
$(CLANG_EXECUTABLE) -O -Wall -shared $(CLANG_FLAGS) ${addprefix -I,${INCLUDE_DIRS}} -o $(CKZG_LIBRARY_PATH) $(TARGETS)
# Ckzg library
ckzg:
@ -36,9 +38,9 @@ ckzg:
@make .ckzg
# E2e tests as an executable
test: tests.c ckzg.c ../../src/c_kzg_4844.c ../../blst/$(BLST_OBJ)
test:
@make .blst
$(CLANG_EXECUTABLE) -O -w -Wall $(CLANG_FLAGS) ${addprefix -I,${INCLUDE_DIRS}} -o $(TESTS_EXECUTABLE) $^
$(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:

View File

@ -13,8 +13,9 @@ KZGSettings* load_trusted_setup_wrap(const char* file) {
if (f == NULL) { free(out); return NULL; }
if (load_trusted_setup(out, f) != C_KZG_OK) { free(out); return NULL; }
if (load_trusted_setup(out, f) != C_KZG_OK) { free(out); fclose(f); return NULL; }
fclose(f);
return out;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,45 @@
// RUN: make run-test
#include "ckzg.h"
#include <stdio.h>
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);
uint8_t *blob = (uint8_t *)calloc(4096, 32);
uint8_t *blobHash = (uint8_t *)calloc(32, 1);
n = 0;
for(int i = 0; i < 5875; i++){
if((n + 1) % 32 == 0)n++;
blob[n] = i % 250;
n++;
}
int res0 = compute_aggregate_kzg_proof_wrap(proof, blob, 1, s);
blob_to_kzg_commitment_wrap(commitment, blob, s);
FILE *f = fopen("output.txt", "wt");
// commitment
for(int i = 0; i< 48; i++){
fprintf(f, "%02x", commitment[i]);
}
fprintf(f, "\n");
// proof
for(int i = 0; i< 48; i++){
fprintf(f, "%02x", proof[i]);
}
fprintf(f, "\n");
fclose(f);
free(blob);
free(commitment);
free(proof);
free_trusted_setup_wrap(s);
}
int main() {
calculate_proof_and_commitment("../../src/trusted_setup.txt");
return 0;
}

File diff suppressed because one or more lines are too long