Skip to content

Commit 1ecab29

Browse files
committed
Refactor and add SecP256k1 functions
1 parent c101661 commit 1ecab29

File tree

6 files changed

+88
-65
lines changed

6 files changed

+88
-65
lines changed

tools/Stateless/Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ build-zisk: build-dotnet-zisk build-docker-bflat
9999
$(BFLAT_BUILD) --libc zisk $(BFLAT_REFS) \
100100
--extlib https://github.com/NethermindEth/bflat-libziskos:pre-develop-0.16.0 \
101101
$(SRC_DIR)/Program.cs \
102-
$(SRC_DIR)/Zisk.Memory.cs \
103-
$(SRC_DIR)/Zisk.Precompiles.cs
102+
$(SRC_DIR)/Zisk/Crypto.cs \
103+
$(SRC_DIR)/Zisk/IO.cs \
104+
$(SRC_DIR)/Zisk/Precompiles.cs
104105
mkdir -p $(ZISK_DIR)/bin && \
105-
mv -f $(ZISK_DIR)/Program.patched $(ZISK_DIR)/bin/nethermind
106+
mv -f $(ZISK_DIR)/Program.patched $(ZISK_DIR)/bin/nethermind && \
107+
rm -f $(ZISK_DIR)/Program
106108

107109
run-tester: build-tester
108110
docker build --platform linux/amd64 -t zkvm-tester $(TESTER_DIR)

tools/Stateless/ZiskGuest/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ class Program
1212
{
1313
static int Main()
1414
{
15-
byte[] input = Zisk.ReadInput();
15+
byte[] input = Zisk.IO.ReadInput();
1616

1717
Block block = StatelessExecutor.Execute(input);
1818

1919
Span<byte> hash = block.Hash!.Bytes;
2020
var size = sizeof(uint);
2121

2222
for (int i = 0, count = hash.Length / size; i < count; i++)
23-
Zisk.SetOutput(i, BinaryPrimitives.ReadUInt32BigEndian(hash[(i * size)..]));
23+
Zisk.IO.SetOutput(i, BinaryPrimitives.ReadUInt32BigEndian(hash[(i * size)..]));
2424

2525
return 0;
2626
}

tools/Stateless/ZiskGuest/Zisk.Precompiles.cs

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-FileCopyrightText: 2026 Demerzel Solutions Limited
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
4+
#pragma warning disable CA1401 // P/Invokes should not be visible
5+
#pragma warning disable SYSLIB1054 // Use 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
6+
7+
using System.Runtime.InteropServices;
8+
9+
namespace Nethermind.Stateless.ZiskGuest.Zisk;
10+
11+
public static unsafe class Crypto
12+
{
13+
[DllImport("__Internal")]
14+
public static extern byte secp256k1_ecdsa_address_recover_c(
15+
byte* sig,
16+
byte* recid,
17+
byte* msg,
18+
byte* output);
19+
20+
[DllImport("__Internal")]
21+
public static extern byte secp256k1_ecdsa_verify_and_address_recover_c(
22+
byte* sig,
23+
byte* msg,
24+
byte* pk,
25+
byte* output);
26+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
using System.Runtime.CompilerServices;
55

6-
namespace Nethermind.Stateless.ZiskGuest;
6+
namespace Nethermind.Stateless.ZiskGuest.Zisk;
77

8-
public static unsafe partial class Zisk
8+
public static unsafe partial class IO
99
{
1010
private const ulong INPUT_ADDR = 0x90000000UL;
1111
private const ulong OUTPUT_ADDR = 0xa0010000UL;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-FileCopyrightText: 2026 Demerzel Solutions Limited
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
4+
#pragma warning disable CA1401 // P/Invokes should not be visible
5+
#pragma warning disable SYSLIB1054 // Use 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
6+
7+
using System.Runtime.InteropServices;
8+
9+
namespace Nethermind.Stateless.ZiskGuest.Zisk;
10+
11+
public static unsafe class Precompiles
12+
{
13+
[DllImport("__Internal")]
14+
public static extern byte bls12_381_fp_to_g1_c(byte* ret, byte* fp);
15+
16+
[DllImport("__Internal")]
17+
public static extern byte bls12_381_fp2_to_g2_c(byte* ret, byte* fp2);
18+
19+
[DllImport("__Internal")]
20+
public static extern byte bls12_381_g1_add_c(byte* ret, byte* a, byte* b);
21+
22+
[DllImport("__Internal")]
23+
public static extern byte bls12_381_g1_msm_c(byte* ret, byte* pairs, nuint num_pairs);
24+
25+
[DllImport("__Internal")]
26+
public static extern byte bls12_381_g2_add_c(byte* ret, byte* a, byte* b);
27+
28+
[DllImport("__Internal")]
29+
public static extern byte bls12_381_g2_msm_c(byte* ret, byte* pairs, nuint num_pairs);
30+
31+
[DllImport("__Internal")]
32+
public static extern byte bls12_381_pairing_check_c(byte* pairs, nuint num_pairs);
33+
34+
[DllImport("__Internal")]
35+
public static extern byte bn254_g1_add_c(byte* p1, byte* p2, byte* ret);
36+
37+
[DllImport("__Internal")]
38+
public static extern byte bn254_g1_mul_c(byte* point, byte* scalar, byte* ret);
39+
40+
[DllImport("__Internal")]
41+
public static extern byte bn254_pairing_check_c(byte* pairs, nuint num_pairs);
42+
43+
[DllImport("__Internal")]
44+
public static extern nuint modexp_bytes_c(
45+
byte* base_ptr,
46+
nuint base_len,
47+
byte* exp_ptr,
48+
nuint exp_len,
49+
byte* modulus_ptr,
50+
nuint modulus_len,
51+
byte* result_ptr
52+
);
53+
}

0 commit comments

Comments
 (0)