Skip to content

Commit d32754e

Browse files
committed
better exporting for libzkp
1 parent f4a47f8 commit d32754e

File tree

12 files changed

+201
-92
lines changed

12 files changed

+201
-92
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/libzkp/impl/Cargo.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,3 @@ serde_stacker = "0.1"
2121
regex = "1.11"
2222
c-kzg = { version = "1.0", features = ["serde"] }
2323

24-
[profile.test]
25-
opt-level = 3
26-
27-
[profile.release]
28-
opt-level = 3
29-

common/libzkp/impl/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ pub fn gen_wrapped_proof(proof_json: &str, metadata: &str, vk: &[u8]) -> eyre::R
9595
Ok(ret)
9696
}
9797

98+
/// init verifier
99+
pub fn verifier_init(config: &str) -> eyre::Result<()> {
100+
let cfg: VerifierConfig = serde_json::from_str(config)?;
101+
verifier::init(cfg);
102+
Ok(())
103+
}
104+
98105
/// verify proof
99106
pub fn verify_proof(proof: Vec<u8>, fork_name: &str, task_type: TaskType) -> eyre::Result<bool> {
100107
let verifier = verifier::get_verifier(fork_name)?;

common/libzkp/interface/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,4 @@ crate-type = ["cdylib"]
1111
[dependencies]
1212
libzkp = { path = "../impl" }
1313
l2geth = { path = "../l2geth"}
14-
serde.workspace = true
15-
serde_json.workspace = true
1614
tracing.workspace = true

common/libzkp/interface/libzkp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
#include <stddef.h> // For size_t
1010

1111
// Initialize the verifier with configuration
12-
void init(char* config);
12+
void init_verifier(char* config);
13+
14+
// Initialize the l2geth with configuration
15+
void init_l2geth(char* config);
1316

1417
// Verify proofs - returns non-zero for success, zero for failure
1518
char verify_batch_proof(char* proof, char* fork_name);

common/libzkp/interface/src/lib.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@ use utils::{c_char_to_str, c_char_to_vec};
77

88
/// # Safety
99
#[no_mangle]
10-
pub unsafe extern "C" fn init(config: *const c_char) {
10+
pub unsafe extern "C" fn init_verifier(config: *const c_char) {
1111
let config_str = c_char_to_str(config);
12+
libzkp::verifier_init(config_str).unwrap();
13+
}
1214

13-
#[derive(serde::Deserialize)]
14-
struct Config {
15-
verifier: libzkp::VerifierConfig,
16-
l2geth: l2geth::RpcConfig,
17-
}
18-
19-
let cfg: Config = serde_json::from_str(config_str).unwrap();
20-
21-
l2geth::init(cfg.l2geth);
22-
libzkp::verifier::init(cfg.verifier);
15+
/// # Safety
16+
#[no_mangle]
17+
pub unsafe extern "C" fn init_l2geth(config: *const c_char) {
18+
let config_str = c_char_to_str(config);
19+
l2geth::init(config_str).unwrap();
2320
}
2421

2522
fn verify_proof(proof: *const c_char, fork_name: *const c_char, task_type: TaskType) -> c_char {

common/libzkp/l2geth/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ use std::sync::{Arc, OnceLock};
55

66
static GLOBAL_L2GETH_CLI: OnceLock<Arc<rpc_client::RpcClientCore>> = OnceLock::new();
77

8-
pub fn init(config: RpcConfig) {
9-
GLOBAL_L2GETH_CLI.get_or_init(|| Arc::new(rpc_client::RpcClientCore::create(&config).unwrap()));
8+
pub fn init(config: &str) -> eyre::Result<()> {
9+
let cfg: RpcConfig = serde_json::from_str(config)?;
10+
GLOBAL_L2GETH_CLI.get_or_init(|| Arc::new(rpc_client::RpcClientCore::create(&cfg).unwrap()));
11+
Ok(())
1012
}
1113

1214
pub fn get_client() -> rpc_client::RpcClient<'static> {

coordinator/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/build/bin
22
.idea
33
internal/logic/verifier/lib
4+
internal/libzkp/lib/libzkp.so

coordinator/Makefile

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@ ZK_VERSION=${ZKEVM_VERSION}-${HALO2_VERSION}
1616
test:
1717
go test -v -race -coverprofile=coverage.txt -covermode=atomic -p 1 $(PWD)/...
1818

19-
libzkp:
20-
cd ../common/libzkp/impl && cargo clean && cargo build --release && cp ./target/release/libzkp.so ../interface/
21-
rm -rf ./internal/logic/verifier/lib && cp -r ../common/libzkp/interface ./internal/logic/verifier/lib
19+
internal/libzkp/lib/libzkp.so:
20+
cd ../common/libzkp/impl && cargo clean --release -p libzkp
21+
cd ../common/libzkp/l2geth && cargo clean --release -p l2geth
22+
cd ../common/libzkp/interface && cargo clean --release -p libzkp-c && cargo build --release
23+
mkdir -p ./internal/libzkp/lib
24+
cp -f ../target/release/libzkp.so ./internal/libzkp/lib
25+
26+
clean_libzkp:
27+
rm ./internal/libzkp/lib/libzkp.so
28+
29+
libzkp: clean_libzkp internal/libzkp/lib/libzkp.so
2230

2331
coordinator_api: libzkp ## Builds the Coordinator api instance.
2432
go build -ldflags "-X scroll-tech/common/version.ZkVersion=${ZK_VERSION}" -o $(PWD)/build/bin/coordinator_api ./cmd/api
@@ -38,14 +46,14 @@ mock_coordinator_api: ## Builds the mocked Coordinator instance.
3846
mock_coordinator_cron: ## Builds the mocked Coordinator instance.
3947
go build -tags="mock_prover mock_verifier" -o $(PWD)/build/bin/coordinator_cron ./cmd/cron
4048

41-
test-verifier: libzkp
49+
test-verifier: internal/libzkp/lib/libzkp.so
4250
go test -tags ffi -timeout 0 -v ./internal/logic/verifier
4351

44-
test-gpu-verifier: libzkp
52+
test-gpu-verifier: internal/libzkp/lib/libzkp.so
4553
go test -tags="gpu ffi" -timeout 0 -v ./internal/logic/verifier
4654

4755
lint: ## Lint the files - used for CI
48-
cp -r ../common/libzkp/interface ./internal/logic/verifier/lib
56+
# cp -r ../common/libzkp/interface ./internal/logic/verifier/lib
4957
GOBIN=$(PWD)/build/bin go run ../build/lint.go
5058

5159
clean: ## Empty out the bin folder

coordinator/internal/libzkp/api.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package libzkp
2+
3+
/*
4+
#cgo LDFLAGS: -lzkp -lm -ldl -L${SRCDIR}/lib -Wl,-rpath=${SRCDIR}/lib
5+
#cgo gpu LDFLAGS: -lzkp -lm -ldl -lgmp -lstdc++ -lprocps -L/usr/local/cuda/lib64/ -lcudart -L${SRCDIR}/lib/ -Wl,-rpath=${SRCDIR}/lib
6+
#include <stdlib.h>
7+
#include "./../../../common/libzkp/interface/libzkp.h"
8+
*/
9+
import "C" //nolint:typecheck
10+
11+
import (
12+
"fmt"
13+
"os"
14+
"unsafe"
15+
)
16+
17+
// Helper function to convert Go string to C string and handle cleanup
18+
func goToCString(s string) *C.char {
19+
return C.CString(s)
20+
}
21+
22+
// Helper function to free C string
23+
func freeCString(s *C.char) {
24+
C.free(unsafe.Pointer(s))
25+
}
26+
27+
// Initialize the verifier
28+
func InitVerifier(configJSON string) {
29+
cConfig := goToCString(configJSON)
30+
defer freeCString(cConfig)
31+
32+
C.init_verifier(cConfig)
33+
}
34+
35+
// Initialize the verifier
36+
func InitL2geth(configJSON string) {
37+
cConfig := goToCString(configJSON)
38+
defer freeCString(cConfig)
39+
40+
C.init_l2geth(cConfig)
41+
}
42+
43+
// Verify a chunk proof
44+
func VerifyChunkProof(proofData, forkName string) bool {
45+
cProof := goToCString(proofData)
46+
cForkName := goToCString(forkName)
47+
defer freeCString(cProof)
48+
defer freeCString(cForkName)
49+
50+
result := C.verify_chunk_proof(cProof, cForkName)
51+
return result != 0
52+
}
53+
54+
// Verify a batch proof
55+
func VerifyBatchProof(proofData, forkName string) bool {
56+
cProof := goToCString(proofData)
57+
cForkName := goToCString(forkName)
58+
defer freeCString(cProof)
59+
defer freeCString(cForkName)
60+
61+
result := C.verify_batch_proof(cProof, cForkName)
62+
return result != 0
63+
}
64+
65+
// Verify a bundle proof
66+
func VerifyBundleProof(proofData, forkName string) bool {
67+
cProof := goToCString(proofData)
68+
cForkName := goToCString(forkName)
69+
defer freeCString(cProof)
70+
defer freeCString(cForkName)
71+
72+
result := C.verify_bundle_proof(cProof, cForkName)
73+
return result != 0
74+
}
75+
76+
// Generate a universal task
77+
func GenerateUniversalTask(taskType int, taskJSON, forkName string) (bool, string, string, []byte) {
78+
cTask := goToCString(taskJSON)
79+
cForkName := goToCString(forkName)
80+
defer freeCString(cTask)
81+
defer freeCString(cForkName)
82+
83+
result := C.gen_universal_task(C.int(taskType), cTask, cForkName)
84+
defer C.release_task_result(result)
85+
86+
// Check if the operation was successful
87+
if result.ok == 0 {
88+
return false, "", "", nil
89+
}
90+
91+
// Convert C strings to Go strings
92+
universalTask := C.GoString(result.universal_task)
93+
metadata := C.GoString(result.metadata)
94+
95+
// Convert C array to Go slice
96+
piHash := make([]byte, 32)
97+
for i := 0; i < 32; i++ {
98+
piHash[i] = byte(result.expected_pi_hash[i])
99+
}
100+
101+
return true, universalTask, metadata, piHash
102+
}
103+
104+
// Generate wrapped proof
105+
func GenerateWrappedProof(proofJSON, metadata string, vkData []byte) string {
106+
cProofJSON := goToCString(proofJSON)
107+
cMetadata := goToCString(metadata)
108+
defer freeCString(cProofJSON)
109+
defer freeCString(cMetadata)
110+
111+
// Create a C array from Go slice
112+
cVkData := (*C.char)(unsafe.Pointer(&vkData[0]))
113+
114+
resultPtr := C.gen_wrapped_proof(cProofJSON, cMetadata, cVkData, C.size_t(len(vkData)))
115+
if resultPtr == nil {
116+
return ""
117+
}
118+
119+
// Convert result to Go string and free C memory
120+
result := C.GoString(resultPtr)
121+
C.release_string(resultPtr)
122+
123+
return result
124+
}
125+
126+
// Dumps a verification key to a file
127+
func DumpVk(forkName, filePath string) error {
128+
cForkName := goToCString(forkName)
129+
cFilePath := goToCString(filePath)
130+
defer freeCString(cForkName)
131+
defer freeCString(cFilePath)
132+
133+
// Call the C function to dump the verification key
134+
C.dump_vk(cForkName, cFilePath)
135+
136+
// Check if the file was created successfully
137+
// Note: The C function doesn't return an error code, so we check if the file exists
138+
if _, err := os.Stat(filePath); os.IsNotExist(err) {
139+
return fmt.Errorf("failed to dump verification key: file %s was not created", filePath)
140+
}
141+
142+
return nil
143+
}

0 commit comments

Comments
 (0)