Skip to content

Commit fd7b975

Browse files
authored
feat(ffi): ffi refactors (#1092)
1 parent decd1c7 commit fd7b975

File tree

19 files changed

+143
-205
lines changed

19 files changed

+143
-205
lines changed

batcher/aligned-batcher/gnark/verifier.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ import "C"
1515

1616
import (
1717
"bytes"
18+
"log"
19+
"unsafe"
20+
1821
"github.com/consensys/gnark-crypto/ecc"
1922
"github.com/consensys/gnark/backend/groth16"
2023
"github.com/consensys/gnark/backend/plonk"
2124
"github.com/consensys/gnark/backend/witness"
22-
"log"
23-
"unsafe"
2425
)
2526

2627
func listRefToBytes(listRef C.ListRef) []byte {
28+
29+
if listRef.len == 0 {
30+
return []byte{}
31+
}
32+
2733
return C.GoBytes(unsafe.Pointer(listRef.ptr), C.int(listRef.len))
2834
}
2935

operator/halo2ipa/halo2ipa.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import "C"
1010
import "unsafe"
1111

1212
func VerifyHalo2IpaProof(
13-
proofBuffer []byte, proofLen uint32,
14-
paramsBuffer []byte, paramsLen uint32,
15-
publicInputBuffer []byte, publicInputLen uint32,
13+
proofBuffer []byte,
14+
paramsBuffer []byte,
15+
publicInputBuffer []byte,
1616
) bool {
1717
/*
1818
For Halo2 the `paramsBuffer` contains the serialized cs, vk, and params with there respective sizes serialized as u32 values (4 bytes) => 3 * 4 bytes = 12 followed by the concatenated variable length buffers:
@@ -28,8 +28,8 @@ func VerifyHalo2IpaProof(
2828
publicInputPtr := (*C.uchar)(unsafe.Pointer(&publicInputBuffer[0]))
2929

3030
return (bool)(C.verify_halo2_ipa_proof_ffi(
31-
proofPtr, (C.uint32_t)(proofLen),
32-
paramsPtr, (C.uint32_t)(paramsLen),
33-
publicInputPtr, (C.uint32_t)(publicInputLen),
31+
proofPtr, (C.uint32_t)(len(proofBuffer)),
32+
paramsPtr, (C.uint32_t)(len(paramsBuffer)),
33+
publicInputPtr, (C.uint32_t)(len(publicInputBuffer)),
3434
))
3535
}

operator/halo2ipa/halo2ipa_test.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,32 @@ import (
77
"github.com/yetanotherco/aligned_layer/operator/halo2ipa"
88
)
99

10-
const MaxProofSize = 8 * 1024
11-
12-
const MaxParamsSize = 8 * 1024
13-
14-
const MaxPublicInputSize = 4 * 1024
15-
1610
const ProofFilePath = "../../scripts/test_files/halo2_ipa/proof.bin"
1711

1812
const PublicInputPath = "../../scripts/test_files/halo2_ipa/pub_input.bin"
1913

2014
const ParamsFilePath = "../../scripts/test_files/halo2_ipa/params.bin"
2115

2216
func TestHalo2IpaProofVerifies(t *testing.T) {
23-
proofFile, err := os.Open(ProofFilePath)
17+
proofBytes, err := os.ReadFile(ProofFilePath)
2418
if err != nil {
2519
t.Errorf("could not open proof file: %s", err)
2620
}
27-
proofBytes := make([]byte, MaxProofSize)
28-
nReadProofBytes, err := proofFile.Read(proofBytes)
29-
if err != nil {
30-
t.Errorf("could not read bytes from file")
31-
}
32-
defer proofFile.Close()
3321

34-
paramsFile, err := os.Open(ParamsFilePath)
22+
paramsBytes, err := os.ReadFile(ParamsFilePath)
3523
if err != nil {
36-
t.Errorf("could not open proof file: %s", err)
24+
t.Errorf("could not open params file: %s", err)
3725
}
38-
paramsBytes := make([]byte, MaxParamsSize)
39-
nReadParamsBytes, err := paramsFile.Read(paramsBytes)
40-
if err != nil {
41-
t.Errorf("could not read bytes from file")
42-
}
43-
defer paramsFile.Close()
4426

45-
publicInputFile, err := os.Open(PublicInputPath)
46-
if err != nil {
47-
t.Errorf("could not open proof file: %s", err)
48-
}
49-
publicInputBytes := make([]byte, MaxPublicInputSize)
50-
nReadPublicInputBytes, err := publicInputFile.Read(publicInputBytes)
27+
publicInputBytes, err := os.ReadFile(PublicInputPath)
5128
if err != nil {
52-
t.Errorf("could not read bytes from file")
29+
t.Errorf("could not open public input file: %s", err)
5330
}
5431

5532
if !halo2ipa.VerifyHalo2IpaProof(
56-
([]byte)(proofBytes), uint32(nReadProofBytes),
57-
([]byte)(paramsBytes), uint32(nReadParamsBytes),
58-
([]byte)(publicInputBytes), uint32(nReadPublicInputBytes),
33+
([]byte)(proofBytes),
34+
([]byte)(paramsBytes),
35+
([]byte)(publicInputBytes),
5936
) {
6037
t.Errorf("proof did not verify")
6138
}

operator/halo2kzg/halo2kzg.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import "C"
99
import "unsafe"
1010

1111
func VerifyHalo2KzgProof(
12-
proofBuffer []byte, proofLen uint32,
13-
paramsBuffer []byte, paramsLen uint32,
14-
publicInputBuffer []byte, publicInputLen uint32,
12+
proofBuffer []byte,
13+
paramsBuffer []byte,
14+
publicInputBuffer []byte,
1515
) bool {
1616
/*
1717
For Halo2 the `paramsBuffer` contains the serialized cs, vk, and params with there respective sizes serialized as u32 values (4 bytes) => 3 * 4 bytes = 12 followed by the concatenated variable length buffers:
@@ -27,8 +27,8 @@ func VerifyHalo2KzgProof(
2727
publicInputPtr := (*C.uchar)(unsafe.Pointer(&publicInputBuffer[0]))
2828

2929
return (bool)(C.verify_halo2_kzg_proof_ffi(
30-
proofPtr, (C.uint32_t)(proofLen),
31-
paramsPtr, (C.uint32_t)(paramsLen),
32-
publicInputPtr, (C.uint32_t)(publicInputLen),
30+
proofPtr, (C.uint32_t)(len(proofBuffer)),
31+
paramsPtr, (C.uint32_t)(len(paramsBuffer)),
32+
publicInputPtr, (C.uint32_t)(len(publicInputBuffer)),
3333
))
3434
}

operator/halo2kzg/halo2kzg_test.go

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,32 @@ import (
77
"github.com/yetanotherco/aligned_layer/operator/halo2kzg"
88
)
99

10-
// MaxProofSize 4KB
11-
const MaxProofSize = 8 * 1024
12-
13-
// MaxProofSize 4KB
14-
const MaxParamsSize = 8 * 1024
15-
16-
// MaxPublicInputSize 4KB
17-
const MaxPublicInputSize = 4 * 1024
18-
1910
const ProofFilePath = "../../scripts/test_files/halo2_kzg/proof.bin"
2011

2112
const PublicInputPath = "../../scripts/test_files/halo2_kzg/pub_input.bin"
2213

2314
const ParamsFilePath = "../../scripts/test_files/halo2_kzg/params.bin"
2415

2516
func TestHalo2KzgProofVerifies(t *testing.T) {
26-
proofFile, err := os.Open(ProofFilePath)
17+
proofBytes, err := os.ReadFile(ProofFilePath)
2718
if err != nil {
2819
t.Errorf("could not open proof file: %s", err)
2920
}
30-
proofBytes := make([]byte, MaxProofSize)
31-
nReadProofBytes, err := proofFile.Read(proofBytes)
32-
if err != nil {
33-
t.Errorf("could not read bytes from file")
34-
}
35-
defer proofFile.Close()
3621

37-
paramsFile, err := os.Open(ParamsFilePath)
22+
paramsBytes, err := os.ReadFile(ParamsFilePath)
3823
if err != nil {
39-
t.Errorf("could not open proof file: %s", err)
24+
t.Errorf("could not open params file: %s", err)
4025
}
41-
paramsBytes := make([]byte, MaxParamsSize)
42-
nReadParamsBytes, err := paramsFile.Read(paramsBytes)
43-
if err != nil {
44-
t.Errorf("could not read bytes from file")
45-
}
46-
defer paramsFile.Close()
4726

48-
publicInputFile, err := os.Open(PublicInputPath)
49-
if err != nil {
50-
t.Errorf("could not open proof file: %s", err)
51-
}
52-
publicInputBytes := make([]byte, MaxPublicInputSize)
53-
nReadPublicInputBytes, err := publicInputFile.Read(publicInputBytes)
27+
publicInputBytes, err := os.ReadFile(PublicInputPath)
5428
if err != nil {
55-
t.Errorf("could not read bytes from file")
29+
t.Errorf("could not open public input file: %s", err)
5630
}
5731

5832
if !halo2kzg.VerifyHalo2KzgProof(
59-
([]byte)(proofBytes), uint32(nReadProofBytes),
60-
([]byte)(paramsBytes), uint32(nReadParamsBytes),
61-
([]byte)(publicInputBytes), uint32(nReadPublicInputBytes),
33+
([]byte)(proofBytes),
34+
([]byte)(paramsBytes),
35+
([]byte)(publicInputBytes),
6236
) {
6337
t.Errorf("proof did not verify")
6438
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e0a3761a514a2a7873350869e699bbd87c9cbf53ba963caae0a232cb6d698b1b
1+
715181f01c095618472a72cd06e384d92f02eadc4ea28bf097181b17fdc57f28
-10.6 MB
Binary file not shown.

operator/merkle_tree/merkle_tree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ package merkle_tree
99
import "C"
1010
import "unsafe"
1111

12-
func VerifyMerkleTreeBatch(batchBuffer []byte, batchLen uint, merkleRootBuffer [32]byte) bool {
12+
func VerifyMerkleTreeBatch(batchBuffer []byte, merkleRootBuffer [32]byte) bool {
1313
if len(batchBuffer) == 0 {
1414
return false
1515
}
1616

1717
batchPtr := (*C.uchar)(unsafe.Pointer(&batchBuffer[0]))
1818
merkleRootPtr := (*C.uchar)(unsafe.Pointer(&merkleRootBuffer[0]))
19-
return (bool)(C.verify_merkle_tree_batch_ffi(batchPtr, (C.uint)(batchLen), merkleRootPtr))
19+
return (bool)(C.verify_merkle_tree_batch_ffi(batchPtr, (C.uint)(len(batchBuffer)), merkleRootPtr))
2020
}

operator/merkle_tree/merkle_tree_test.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,21 @@ package merkle_tree
33
import (
44
"encoding/hex"
55
"fmt"
6-
"io"
76
"os"
87
"testing"
98
)
109

11-
func TestVerifyMerkleTreeBatch(t *testing.T) {
12-
batchFile, err := os.Open("lib/test_files/merkle_tree_batch.bin")
13-
if err != nil {
14-
t.Fatalf("Error opening batch file: %v", err)
15-
}
10+
const BatchFilePath = "lib/test_files/merkle_tree_batch.bin"
1611

17-
batchByteValue, err := io.ReadAll(batchFile)
18-
if err != nil {
19-
t.Fatalf("Error reading batch file: %v", err)
20-
}
12+
const RootFilePath = "lib/test_files/merkle_root.bin"
2113

22-
rootFile, err := os.Open("lib/test_files/merkle_root.bin")
14+
func TestVerifyMerkleTreeBatch(t *testing.T) {
15+
batchByteValue, err := os.ReadFile(BatchFilePath)
2316
if err != nil {
24-
t.Fatalf("Error opening batch file: %v", err)
17+
t.Fatalf("Error reading batch file: %v", err)
2518
}
2619

27-
rootByteValue, err := io.ReadAll(rootFile)
20+
rootByteValue, err := os.ReadFile(RootFilePath)
2821
if err != nil {
2922
t.Fatalf("Error reading batch file: %v", err)
3023
}
@@ -39,7 +32,7 @@ func TestVerifyMerkleTreeBatch(t *testing.T) {
3932
var merkleRoot [32]byte
4033
copy(merkleRoot[:], merkle_root)
4134

42-
if !VerifyMerkleTreeBatch(batchByteValue, uint(len(batchByteValue)), merkleRoot) {
35+
if !VerifyMerkleTreeBatch(batchByteValue, merkleRoot) {
4336
t.Errorf("Batch did not verify Merkle Root")
4437
}
4538

operator/merkle_tree_old/merkle_tree_old.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ package merkle_tree_old
99
import "C"
1010
import "unsafe"
1111

12-
func VerifyMerkleTreeBatchOld(batchBuffer []byte, batchLen uint, merkleRootBuffer [32]byte) bool {
12+
func VerifyMerkleTreeBatchOld(batchBuffer []byte, merkleRootBuffer [32]byte) bool {
1313
if len(batchBuffer) == 0 {
1414
return false
1515
}
1616

1717
batchPtr := (*C.uchar)(unsafe.Pointer(&batchBuffer[0]))
1818
merkleRootPtr := (*C.uchar)(unsafe.Pointer(&merkleRootBuffer[0]))
19-
return (bool)(C.verify_merkle_tree_batch_ffi_old(batchPtr, (C.uint)(batchLen), merkleRootPtr))
19+
return (bool)(C.verify_merkle_tree_batch_ffi_old(batchPtr, (C.uint)(len(batchBuffer)), merkleRootPtr))
2020
}

0 commit comments

Comments
 (0)