Skip to content

Commit

Permalink
remove hsbindgen and fix dynamic linking for macos (#14)
Browse files Browse the repository at this point in the history
* remove hsbindgen and fix dynamic linking for macos

* remove outdated comment

* create additional dynamic library for local template haskell use
  • Loading branch information
larskuhtz authored Sep 16, 2024
1 parent c29cfbc commit d393180
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 158 deletions.
109 changes: 3 additions & 106 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
74 changes: 39 additions & 35 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -29,36 +24,28 @@ 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
)

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
Expand All @@ -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

13 changes: 0 additions & 13 deletions hsbindgen.toml

This file was deleted.

1 change: 0 additions & 1 deletion plonk-verify.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
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
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,
Expand Down

0 comments on commit d393180

Please sign in to comment.