From d3931808f33a02e2a043028b33b29328855d9572 Mon Sep 17 00:00:00 2001 From: Lars Kuhtz Date: Mon, 16 Sep 2024 11:39:39 -0700 Subject: [PATCH] remove hsbindgen and fix dynamic linking for macos (#14) * remove hsbindgen and fix dynamic linking for macos * remove outdated comment * create additional dynamic library for local template haskell use --- Cargo.lock | 109 ++------------------------------------------- Cargo.toml | 1 - Setup.hs | 74 +++++++++++++++--------------- hsbindgen.toml | 13 ------ plonk-verify.cabal | 1 - src/lib.rs | 25 ++++++++++- 6 files changed, 65 insertions(+), 158 deletions(-) delete mode 100644 hsbindgen.toml diff --git a/Cargo.lock b/Cargo.lock index 9c867dd..82d6041 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -101,7 +101,7 @@ dependencies = [ "num-bigint 0.4.6", "num-traits", "paste", - "rustc_version 0.4.1", + "rustc_version", "zeroize", ] @@ -488,7 +488,7 @@ dependencies = [ "cpufeatures", "curve25519-dalek-derive", "fiat-crypto", - "rustc_version 0.4.1", + "rustc_version", "subtle", "zeroize", ] @@ -582,17 +582,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "displaydoc" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.77", -] - [[package]] name = "downcast-rs" version = "1.2.1" @@ -941,54 +930,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "hs-bindgen" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25586ce217507e994a21a92efbc56456e024aa61969ba9b0f91698d1e7b06376" -dependencies = [ - "hs-bindgen-attribute", - "hs-bindgen-traits", -] - -[[package]] -name = "hs-bindgen-attribute" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf0c7ca61a6f04e432eb50b569d5322a3a34ba4f61e6a84503d9a3f25e623098" -dependencies = [ - "displaydoc", - "hs-bindgen-types", - "lazy_static", - "quote", - "reflexive", - "rustc_version 0.2.3", - "semver 1.0.23", - "serde", - "syn 1.0.109", - "thiserror", - "toml", -] - -[[package]] -name = "hs-bindgen-traits" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac4ec8936241746802cfe99813764d0ab06f023a7350df3967e287be456f29fc" - -[[package]] -name = "hs-bindgen-types" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aef6c1c28eb3c9a7fd73cb380e2a5114d511ef44b5ffaf5e76559669563abb05" -dependencies = [ - "cfg-if", - "displaydoc", - "proc-macro2", - "quote", - "thiserror", -] - [[package]] name = "hybrid-array" version = "0.2.0-rc.9" @@ -1831,7 +1772,6 @@ dependencies = [ "getrandom", "hex", "home", - "hs-bindgen", "num-bigint 0.4.6", "p3-baby-bear 0.1.0 (git+https://github.com/argumentcomputer/Plonky3.git?branch=sp1-new)", "p3-field 0.1.0 (git+https://github.com/argumentcomputer/Plonky3.git?branch=sp1-new)", @@ -1968,16 +1908,6 @@ dependencies = [ "bitflags", ] -[[package]] -name = "reflexive" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a88ef05e7d63adad0f2c5f5873903226c03b7f27d219df7f37fb05ab98b5d420" -dependencies = [ - "proc-macro2", - "quote", -] - [[package]] name = "regex" version = "1.10.6" @@ -2054,22 +1984,13 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" -[[package]] -name = "rustc_version" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" -dependencies = [ - "semver 0.9.0", -] - [[package]] name = "rustc_version" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" dependencies = [ - "semver 1.0.23", + "semver", ] [[package]] @@ -2132,27 +2053,12 @@ dependencies = [ "zeroize", ] -[[package]] -name = "semver" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" -dependencies = [ - "semver-parser", -] - [[package]] name = "semver" version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" -[[package]] -name = "semver-parser" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" - [[package]] name = "serde" version = "1.0.210" @@ -2664,15 +2570,6 @@ dependencies = [ "crunchy", ] -[[package]] -name = "toml" -version = "0.5.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" -dependencies = [ - "serde", -] - [[package]] name = "toml_datetime" version = "0.6.8" diff --git a/Cargo.toml b/Cargo.toml index 5bd62e9..9313b60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,6 @@ license = "MIT OR Apache-2.0" repository = "https://github.com/argumentcomputer/lurk-hs" [dependencies] -hs-bindgen = { version = "0.9.0", features = ["full"] } # sphinx-recursion-gnark-ffi = { path = "../sphinx/recursion/gnark-ffi", features = ["native"]} sphinx-recursion-gnark-ffi = { git = "https://github.com/argumentcomputer/sphinx.git", branch = "dev", features = ["native"]} getrandom = "=0.2.14" diff --git a/Setup.hs b/Setup.hs index 734fd52..191c891 100644 --- a/Setup.hs +++ b/Setup.hs @@ -1,9 +1,4 @@ --- This file was generated by `cargo-cabal`, its goal is to define few hooks to --- call `cargo` on the fly and link correctly the generated library. --- --- While it's an acceptable hack as this project is currently a prototype, this --- should be removed before `cargo-cabal` stable release. - +import Control.Monad import Data.Maybe import qualified Distribution.PackageDescription as PD import Distribution.Simple @@ -29,13 +24,20 @@ import Distribution.Simple.Setup import Distribution.Simple.UserHooks ( UserHooks (buildHook, confHook, cleanHook) ) -import Distribution.Simple.Utils (rawSystemExit) +import Distribution.Simple.Utils + ( notice + , copyFileVerbose + , createDirectoryIfMissingVerbose + , rawSystemExit + , info + ) import System.Directory (getCurrentDirectory) import Distribution.Text (display) import Distribution.Simple.Compiler -import Distribution.System (buildPlatform) +import Distribution.System (buildPlatform, buildOS, OS(..)) import Distribution.Simple.BuildPaths ( mkGenericSharedBundledLibName + , mkGenericSharedLibName , mkGenericStaticLibName , dllExtension ) @@ -43,22 +45,7 @@ import Distribution.Simple.BuildPaths main :: IO () main = defaultMainWithHooks - simpleUserHooks - { buildHook = rustBuildHook - , cleanHook = rustCleanHook - } - -rustCleanHook - :: PD.PackageDescription - -> () - -> UserHooks - -> CleanFlags - -> IO () -rustCleanHook desc () hooks flags = do - pwd <- getCurrentDirectory - putStrLn $ "cleanup rust build artifacts: delete " <> pwd <> "/target" - rawSystemExit (fromFlag $ cleanVerbosity flags) "rm" ["-r", "./target"] - cleanHook simpleUserHooks desc () hooks flags + simpleUserHooks { buildHook = rustBuildHook } rustBuildHook :: PD.PackageDescription @@ -70,27 +57,44 @@ rustBuildHook description localBuildInfo hooks flags = do -- run Rust build -- FIXME add cargo and rust to program db during configure step -- FIXME: add `--target $TARGET` flag to support cross-compiling to $TARGET - putStrLn "Call `cargo build --release` to build a dependency written in Rust" - rawSystemExit (fromFlag $ buildVerbosity flags) "cargo" ["build","--release"] + notice verbosity "Call `cargo build --release` to build a dependency written in Rust" + rawSystemExit (fromFlag $ buildVerbosity flags) "cargo" + [ "build" + , "--release" + , "--target-dir" + , rustTargetDir + ] + + -- Install build results into cabal build directory + createDirectoryIfMissingVerbose verbosity True targetBuildDir + copyFileVerbose verbosity staticSource staticTarget + copyFileVerbose verbosity dynSource dynTarget - -- copy static lib - putStrLn $ "Copy libplonk_verify.a to " <> staticTarget - rawSystemExit (fromFlag $ buildVerbosity flags) "cp" [staticSource, staticTarget] + -- seems to be needed for local use of template haskell + copyFileVerbose verbosity dynSource dynTarget_ - -- copy dyn lib - putStrLn $ "Copy libplonk_verify.dylib to " <> dynTarget - rawSystemExit (fromFlag $ buildVerbosity flags) "cp" [dynSource, dynTarget] + when (buildOS == OSX) $ + rawSystemExit verbosity "install_name_tool" + [ "-id" + , "@rpath/" <> mkGenericSharedBundledLibName buildPlatform (compilerId c) targetLibname + , dynTarget + ] - putStrLn "rustc compilation succeeded" + info verbosity "rustc compilation succeeded" buildHook simpleUserHooks description localBuildInfo hooks flags where + verbosity = fromFlag $ buildVerbosity flags c = compiler localBuildInfo + sourceLibname = "plonk_verify" - targetLibname = "Cplonk_verify" - sourceBuildDir = "target/release" + targetLibname = "C" <> sourceLibname + rustTargetDir = buildDir localBuildInfo <> "/rust-target" + sourceBuildDir = rustTargetDir <> "/release" targetBuildDir = buildDir localBuildInfo staticSource = sourceBuildDir <> "/" <> mkGenericStaticLibName sourceLibname staticTarget = targetBuildDir <> "/" <> mkGenericStaticLibName targetLibname dynSource = sourceBuildDir <> "/" <> "lib" <> sourceLibname <> "." <> dllExtension buildPlatform dynTarget = targetBuildDir <> "/" <> mkGenericSharedBundledLibName buildPlatform (compilerId c) targetLibname + dynTarget_ = targetBuildDir <> "/" <> mkGenericSharedLibName buildPlatform (compilerId c) targetLibname + diff --git a/hsbindgen.toml b/hsbindgen.toml deleted file mode 100644 index 5aa43ec..0000000 --- a/hsbindgen.toml +++ /dev/null @@ -1,13 +0,0 @@ -# Since the only `.cabal` format parser implementation and specification live -# in Cabal itself ... this deadly simple config file is used by `hs-bindgen` -# Rust crate to get needed data (like default exposed module name). - -default = "PlonkVerify" - -# There is an unlikely future where instead we have a Rust `.cabal` parser, -# that most likely would rely under the hood on a Haskell static lib wrapper -# of `Cabal.Parse` or https://hackage.haskell.org/package/Cabal-syntax library. -# But even in this case, it would be nice to know the `cargo-cabal` version that -# generated the `.cabal` file used. - -version = "0.8.0" \ No newline at end of file diff --git a/plonk-verify.cabal b/plonk-verify.cabal index c975ab8..133b866 100644 --- a/plonk-verify.cabal +++ b/plonk-verify.cabal @@ -26,7 +26,6 @@ extra-source-files: Cargo.toml Cargo.lock src/lib.rs - hsbindgen.toml data-files: verifier-assets/v1.0.8-testnet/vk.bin diff --git a/src/lib.rs b/src/lib.rs index 615da79..b5d9809 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,9 @@ use hex; -use hs_bindgen::*; use num_bigint::BigUint; use sha2::{Digest as _, Sha256}; +use std::ffi::CStr; + use sphinx_recursion_gnark_ffi::ffi; // Helper function to remove "0x" if present @@ -10,8 +11,28 @@ fn strip_hex_prefix(hex: &str) -> &str { hex.strip_prefix("0x").unwrap_or(hex) } +#[no_mangle] +pub extern "C" fn __c_verify_plonk_bn254( + cvkeydir: *const core::ffi::c_char, + cproof: *const core::ffi::c_char, + cpkey: *const core::ffi::c_char, + cparams: *const core::ffi::c_char, +) -> core::ffi::c_uint +{ + let vkeydir = unsafe { CStr::from_ptr(cvkeydir) }; + let proof = unsafe { CStr::from_ptr(cproof) }; + let pkey = unsafe { CStr::from_ptr(cpkey) }; + let params = unsafe { CStr::from_ptr(cparams) }; + + return verify_plonk_bn254( + vkeydir.to_str().expect("verifying key directory name is not a valid string"), + proof.to_str().expect("proof is not a value string"), + pkey.to_str().expect("program key is not a valid string"), + params.to_str().expect("public params is not a valid string"), + ); +} + // Facade exposing a bindgen anchor -#[hs_bindgen] pub fn verify_plonk_bn254( build_dir_str: &str, proof_str: &str,