From a074bdd2c57b3a509cf6ca9fe4ab9d4c36df75f7 Mon Sep 17 00:00:00 2001 From: Evgeniy Samodelov Date: Wed, 9 Oct 2024 16:11:20 +0300 Subject: [PATCH 01/10] feat: configure build --- .gitignore | 2 +- Setup.hs | 48 +++++++++++++------- haskell-wrapper/src/BN254/BN254.hs | 26 ----------- haskell-wrapper/src/Functions.chs | 50 --------------------- haskell-wrapper/src/RustFunctions.hs | 67 ++++++++++++---------------- haskell-wrapper/tests/Main.hs | 2 - haskell-wrapper/tests/ScalarSum.hs | 36 --------------- run.sh | 8 ---- rust-wrapper/Cargo.toml | 9 ---- rust-wrapper/src/fft.rs | 3 +- rust-wrapper/src/lib.rs | 1 - rust-wrapper/src/msm.rs | 31 ++----------- rust-wrapper/src/sum.rs | 25 ----------- rust-wrapper/src/utils.rs | 67 ++-------------------------- zkfold-prover.cabal | 14 ++---- 15 files changed, 71 insertions(+), 318 deletions(-) delete mode 100644 haskell-wrapper/src/BN254/BN254.hs delete mode 100644 haskell-wrapper/src/Functions.chs delete mode 100644 haskell-wrapper/tests/ScalarSum.hs delete mode 100755 run.sh delete mode 100644 rust-wrapper/src/sum.rs diff --git a/.gitignore b/.gitignore index 84669b2..d100958 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,6 @@ cabal.project.local~ .ghc.environment.* rust-wrapper/target/ rust-wrapper/Cargo.lock -rust-wrapper/haskell-wrapper/cbits/rust-wrapper.h +lib.so *.data *.svg diff --git a/Setup.hs b/Setup.hs index e1a84e2..542afbf 100644 --- a/Setup.hs +++ b/Setup.hs @@ -1,22 +1,36 @@ -import Distribution.Simple -import System.Process -import Control.Monad -import Distribution.Types.HookedBuildInfo -import System.Exit +import Control.Exception (throwIO) +import Control.Monad +import Data.Char (isSpace) +import Data.List (dropWhile, isPrefixOf) +import Distribution.Simple +import Distribution.Types.HookedBuildInfo +import System.Directory +import System.Exit +import System.Process (readProcess, system) main :: IO () main = defaultMainWithHooks simpleUserHooks - { preConf = runMyScript - + { preConf = buildRustLib } -runMyScript :: Args -> a -> IO HookedBuildInfo -runMyScript args flags = do - putStrLn "Running pre-script..." - executeShellCommand "source ./run.sh" - return $ emptyHookedBuildInfo - -executeShellCommand cmd = putStrLn ("EXEC: " ++ cmd) >> system cmd >>= check - where - check ExitSuccess = return () - check (ExitFailure n) = error $ "cmd: " ++ cmd ++ " failure code " ++ show n \ No newline at end of file +buildRustLib :: Args -> a -> IO HookedBuildInfo +buildRustLib _ flags = do + + buildResult <- system "cargo +nightly cbuild --release --manifest-path rust-wrapper/Cargo.toml" + case buildResult of + ExitSuccess -> return () + ExitFailure exitCode -> throwIO $ userError $ "Build rust library failed with exit code " <> show exitCode + + output <- readProcess "rustc" ["--version", "--verbose"] "" + case filter ("host: " `isPrefixOf`) (lines output) of + [line] -> do + let host = dropWhile isSpace $ drop 5 line + pathToLib = "rust-wrapper/target/" <> host <> "/release/librust_wrapper.so" + + libExist <- doesFileExist pathToLib + unless libExist $ throwIO $ userError "Can't find rust library" + + copyFile pathToLib "./lib.so" + + _ -> throwIO $ userError "Can't find default rust target" + return emptyHookedBuildInfo diff --git a/haskell-wrapper/src/BN254/BN254.hs b/haskell-wrapper/src/BN254/BN254.hs deleted file mode 100644 index 6054fdf..0000000 --- a/haskell-wrapper/src/BN254/BN254.hs +++ /dev/null @@ -1,26 +0,0 @@ -{-# OPTIONS_GHC -Wno-orphans #-} - -module BN254.BN254 where - -import ZkFold.Base.Algebra.Basic.Field (Zp) -import ZkFold.Base.Algebra.Basic.Number (Prime) -import ZkFold.Base.Algebra.EllipticCurve.Class (EllipticCurve (..), Point (Inf, Point), pointAdd, pointMul) - -type BN254_Scalar = 21888242871839275222246405745257275088548364400416034343698204186575808495617 -instance Prime BN254_Scalar - -type BN254_Base = 21888242871839275222246405745257275088696311157297823662689037894645226208583 -instance Prime BN254_Base - -type Fr = Zp BN254_Scalar -type Fp = Zp BN254_Base - -data BN254_G1 - -instance EllipticCurve BN254_G1 where - type ScalarField BN254_G1 = Fr - type BaseField BN254_G1 = Fp - inf = Inf - gen = Point 1 2 - add = pointAdd - mul = pointMul diff --git a/haskell-wrapper/src/Functions.chs b/haskell-wrapper/src/Functions.chs deleted file mode 100644 index e42b098..0000000 --- a/haskell-wrapper/src/Functions.chs +++ /dev/null @@ -1,50 +0,0 @@ -module Functions where - -#include "rust_wrapper.h" -import Foreign.C.String -import Prelude -import Data.ByteString (ByteString) -import Foreign.Rust.Marshall.Variable - -{# fun unsafe rust_wrapper_scalar_sum as rustWrapperScalarSum - { toBorshVar* `ByteString'& - , toBorshVar* `ByteString'& - , getVarBuffer `Buffer ByteString'& - } - -> `()' -#} - -{# fun unsafe rust_wrapper_multi_scalar_multiplication as rustWrapperMultiScalarMultiplication - { toBorshVar* `ByteString'& - , toBorshVar* `ByteString'& - , getVarBuffer `Buffer ByteString'& - } - -> `()' -#} - -{# fun pure unsafe rust_wrapper_multi_scalar_multiplication_without_serialization as ^ - { `CString' - , `Int' - - , `CString' - , `Int' - - , `Int' - , `CString' - } - -> `()' -#} - - -{# fun pure unsafe rust_wrapper_mul_fft as rustWrapperMulFFT - { `CString' - , `Int' - - , `CString' - , `Int' - - , `Int' - , `CString' - } - -> `()' -#} diff --git a/haskell-wrapper/src/RustFunctions.hs b/haskell-wrapper/src/RustFunctions.hs index f91940b..30dbe7c 100644 --- a/haskell-wrapper/src/RustFunctions.hs +++ b/haskell-wrapper/src/RustFunctions.hs @@ -4,11 +4,8 @@ {-# LANGUAGE UndecidableInstances #-} {-# OPTIONS_GHC -Wno-orphans #-} - module RustFunctions - ( rustScalarSum - , rustMultiScalarMultiplication - , rustMultiScalarMultiplicationWithoutSerialization + ( rustMultiScalarMultiplicationWithoutSerialization , rustMulFft , RustCore ) where @@ -17,8 +14,7 @@ import qualified Data.ByteString as BS import Data.Maybe (fromJust) import qualified Data.Vector as V import Foreign -import Foreign.Rust.Marshall.Variable (withPureBorshVarBuffer) -import Functions +import Foreign.C.String import GHC.Base import GHC.IO (unsafePerformIO) import GHC.Natural (Natural) @@ -27,6 +23,7 @@ import GHC.Num.Natural (naturalFromAddr, n import GHC.Ptr (Ptr (..)) import GHC.TypeNats (KnownNat) import Prelude hiding (sum) +import System.Posix.DynamicLinker import ZkFold.Base.Algebra.Basic.Field import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 @@ -35,39 +32,20 @@ import ZkFold.Base.Algebra.Polynomials.Univariate (fromPolyVec) import ZkFold.Base.Data.ByteString import ZkFold.Base.Protocol.NonInteractiveProof (CoreFunction (..), msm) -rustScalarSum - :: forall - (a :: Type) - (b :: Natural) - . ( ScalarField a ~ Zp b - , KnownNat b - ) - => ScalarField a - -> ScalarField a - -> ScalarField a -rustScalarSum a b = fromJust $ fromByteString @(ScalarField a) sum' - where - a' = toByteString a - b' = toByteString b - sum' = withPureBorshVarBuffer $ rustWrapperScalarSum a' b' +libPath :: FilePath +libPath = "./lib.so" -rustMultiScalarMultiplication - :: forall - (a :: Type) - (b :: Natural) - . ( ScalarField a ~ Zp b - , KnownNat b - , Binary (Point a) - ) - => [Point a] - -> [ScalarField a] - -> Point a -rustMultiScalarMultiplication points scalars = fromJust $ fromByteString @(Point a) res' - where - scalars' = mconcat $ toByteString <$> scalars - points' = mconcat $ toByteString <$> points - res' = withPureBorshVarBuffer $ rustWrapperMultiScalarMultiplication points' scalars' +type FunFFT = + CString -> Int -> CString -> Int -> Int -> CString -> IO () +foreign import ccall "dynamic" + mkFunFFT :: FunPtr FunFFT -> FunFFT + +type FunMSM = + CString -> Int -> CString -> Int -> Int -> CString -> IO () + +foreign import ccall "dynamic" + mkFunMSM :: FunPtr FunMSM -> FunMSM rustMultiScalarMultiplicationWithoutSerialization :: forall @@ -93,6 +71,10 @@ rustMultiScalarMultiplicationWithoutSerialization points scalars = unsafePerform runMSM :: IO (Point a) runMSM = do + dl <- dlopen libPath [RTLD_NOW] + msmPtr <- dlsym dl "rust_wrapper_multi_scalar_multiplication_without_serialization" + let !msmf = mkFunMSM $ castFunPtr msmPtr + ptrScalars <- callocBytes @(ScalarField a) scalarsByteLength ptrPoints <- callocBytes @(Point a) pointsByteLength @@ -101,11 +83,13 @@ rustMultiScalarMultiplicationWithoutSerialization points scalars = unsafePerform out <- mallocBytes pointSize - let !_ = rustWrapperMultiScalarMultiplicationWithoutSerialization + !_ <- msmf (castPtr ptrPoints) pointsByteLength (castPtr ptrScalars) scalarsByteLength pointSize out + dlclose dl + res <- BS.packCStringLen (out, pointSize) free ptrScalars @@ -208,6 +192,10 @@ rustMulFft l r = if lByteLength * rByteLength == 0 then V.empty else unsafePerfo runFFT :: IO (V.Vector f) runFFT = do + dl <- dlopen libPath [RTLD_NOW] + fftPtr <- dlsym dl "rust_wrapper_mul_fft" + let !fft = mkFunFFT $ castFunPtr fftPtr + ptrL <- callocBytes @f lByteLength ptrR <- callocBytes @f rByteLength @@ -217,9 +205,10 @@ rustMulFft l r = if lByteLength * rByteLength == 0 then V.empty else unsafePerfo let outLen = lByteLength + rByteLength - scalarSize out <- callocBytes outLen - let !_ = rustWrapperMulFFT + !_ <- fft (castPtr ptrL) lByteLength (castPtr ptrR) rByteLength outLen out + dlclose dl V.fromList <$> peekArray @f (V.length l + V.length r - 1) (castPtr out) diff --git a/haskell-wrapper/tests/Main.hs b/haskell-wrapper/tests/Main.hs index 5fd53d4..df78042 100644 --- a/haskell-wrapper/tests/Main.hs +++ b/haskell-wrapper/tests/Main.hs @@ -3,10 +3,8 @@ module Main (main) where import FFT (testFFT) import MultiScalarMultiplication (testMultiScalarMultiplication) import Prelude -import ScalarSum (testScalarSum) main :: IO () main = do - testScalarSum testFFT testMultiScalarMultiplication diff --git a/haskell-wrapper/tests/ScalarSum.hs b/haskell-wrapper/tests/ScalarSum.hs deleted file mode 100644 index 9cabe38..0000000 --- a/haskell-wrapper/tests/ScalarSum.hs +++ /dev/null @@ -1,36 +0,0 @@ -{-# LANGUAGE AllowAmbiguousTypes #-} -{-# LANGUAGE TypeOperators #-} -module ScalarSum (testScalarSum) where - -import BN254.BN254 (BN254_G1) -import Data.Kind (Type) -import GHC.TypeNats (KnownNat) -import Numeric.Natural (Natural) -import Prelude hiding (Num (..)) -import RustFunctions (rustScalarSum) -import Test.Hspec (describe, hspec, it, shouldBe) -import Test.QuickCheck (Testable (property)) - -import ZkFold.Base.Algebra.Basic.Class (AdditiveSemigroup ((+))) -import ZkFold.Base.Algebra.Basic.Field (Zp) -import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 (BLS12_381_G1) -import ZkFold.Base.Algebra.EllipticCurve.Class (EllipticCurve (ScalarField)) - -specScalarSum - :: forall - (a :: Type) - (b :: Natural) - . ( ScalarField a ~ Zp b - , KnownNat b - ) - => String - -> IO () -specScalarSum name = hspec $ do - describe ("Rust add specification for " <> name) $ do - it "should be equal to haskell" $ do - property $ \(x :: ScalarField a) (y :: ScalarField a) -> rustScalarSum @a @b x y `shouldBe` (x + y) - -testScalarSum :: IO () -testScalarSum = do - specScalarSum @BLS12_381_G1 "BLS12_381_G1" - specScalarSum @BN254_G1 "BN254" diff --git a/run.sh b/run.sh deleted file mode 100755 index 1f53454..0000000 --- a/run.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -PATH_TO_PROJECT=$(pwd) -export PKG_CONFIG_PATH=$PATH_TO_PROJECT/rust-wrapper/target/x86_64-unknown-linux-gnu/release -export LD_LIBRARY_PATH=$PATH_TO_PROJECT/rust-wrapper/target/x86_64-unknown-linux-gnu/release -cd $PATH_TO_PROJECT/rust-wrapper -cbindgen --lang=c -o haskell-wrapper/cbits/rust-wrapper.h -cargo cbuild --release -cd $PATH_TO_PROJECT \ No newline at end of file diff --git a/rust-wrapper/Cargo.toml b/rust-wrapper/Cargo.toml index 2f4c791..1d9b4aa 100644 --- a/rust-wrapper/Cargo.toml +++ b/rust-wrapper/Cargo.toml @@ -23,12 +23,6 @@ ark-poly = "0.4.2" ark-test-curves = "0.4.2" criterion = { version = "0.5", features = ["html_reports"] } -haskell-ffi.git = "https://github.com/BeFunctional/haskell-rust-ffi.git" -haskell-ffi.rev = "2bf292e2e56eac8e9fb0fb2e1450cf4a4bd01274" - -[profile.bench] -opt-level = 2 - [[bench]] name = "msm_bench" harness = false @@ -39,6 +33,3 @@ harness = false [features] capi = [] - -[package.metadata.capi.library] -versioning = false diff --git a/rust-wrapper/src/fft.rs b/rust-wrapper/src/fft.rs index 34a1900..4ff82c9 100644 --- a/rust-wrapper/src/fft.rs +++ b/rust-wrapper/src/fft.rs @@ -37,11 +37,10 @@ pub unsafe extern "C" fn rust_wrapper_mul_fft( out_len: usize, out: *mut libc::c_char, ) { - let l = slice::from_raw_parts(l_var as *const u8, l_len); let r = slice::from_raw_parts(r_var as *const u8, r_len); let res = mul_fft(l, r); - + std::ptr::copy(res.as_ptr(), out as *mut u8, out_len); } diff --git a/rust-wrapper/src/lib.rs b/rust-wrapper/src/lib.rs index ba8900e..0d661c5 100644 --- a/rust-wrapper/src/lib.rs +++ b/rust-wrapper/src/lib.rs @@ -1,4 +1,3 @@ pub mod fft; pub mod msm; -pub mod sum; mod utils; diff --git a/rust-wrapper/src/msm.rs b/rust-wrapper/src/msm.rs index f3d99d1..796399a 100644 --- a/rust-wrapper/src/msm.rs +++ b/rust-wrapper/src/msm.rs @@ -1,38 +1,13 @@ use std::slice; -use ark_bls12_381::{Fr as ScalarField, G1Affine as GAffine, G1Projective as G}; -use ark_ec::VariableBaseMSM as BaselineVariableBaseMSM; +use ark_bls12_381::G1Affine as GAffine; use ark_ff::PrimeField; use ark_msm::msm::VariableBaseMSM; use ark_serialize::CanonicalSerialize; use ark_std::log2; -use haskell_ffi::{from_haskell::marshall_from_haskell_var, to_haskell::marshall_to_haskell_var}; -use utils::{Buffer, RW}; -use crate::utils::{self, deserialize_vector_points, deserialize_vector_scalar_field}; +use crate::utils::{deserialize_vector_points, deserialize_vector_scalar_field}; -/// -/// # Safety -/// The caller must make sure that correctly serialized scalars are passed. -/// Correct serialization/deserialization is described in ToHaskell/FromHaskell trait. -/// . -#[no_mangle] -pub unsafe extern "C" fn rust_wrapper_multi_scalar_multiplication( - points_var: *const u8, - points_len: usize, - scalars_var: *const u8, - scalars_len: usize, - out: *mut u8, - out_len: &mut usize, -) { - let scalars = - marshall_from_haskell_var::>(scalars_var, scalars_len, RW).0; - let points = marshall_from_haskell_var::>(points_var, points_len, RW).0; - - let r: GAffine = G::msm(&points, &scalars).unwrap().into(); - - marshall_to_haskell_var(&r, out, out_len, RW); -} // This is inner function from arkmsm crate, derived from benchmark results. May not be optimal for all configurations // https://github.com/snarkify/arkmsm/blob/main/src/msm.rs const fn get_opt_window_size(k: u32) -> u32 { @@ -90,6 +65,6 @@ pub unsafe extern "C" fn rust_wrapper_multi_scalar_multiplication_without_serial let point_buffer = slice::from_raw_parts(points_var as *const u8, points_len); let res = multi_scalar_multiplication_without_serialization(scalar_buffer, point_buffer); - + std::ptr::copy(res.as_ptr(), out as *mut u8, out_len); } diff --git a/rust-wrapper/src/sum.rs b/rust-wrapper/src/sum.rs deleted file mode 100644 index bcb4f15..0000000 --- a/rust-wrapper/src/sum.rs +++ /dev/null @@ -1,25 +0,0 @@ -use ark_bls12_381::Fr as ScalarField; -use haskell_ffi::{from_haskell::marshall_from_haskell_var, to_haskell::marshall_to_haskell_var}; -use utils::RW; - -use crate::utils; - -/// -/// # Safety -/// The caller must make sure that correctly serialized scalars are passed. -/// Correct serialization/deserialization is described in ToHaskell/FromHaskell trait. -/// . -#[no_mangle] -pub unsafe extern "C" fn rust_wrapper_scalar_sum( - a_var: *const u8, - a_len: usize, - b_var: *const u8, - b_len: usize, - out: *mut u8, - out_len: &mut usize, -) { - let a: ScalarField = marshall_from_haskell_var(a_var, a_len, RW); - let b: ScalarField = marshall_from_haskell_var(b_var, b_len, RW); - let r: ScalarField = a + b; - marshall_to_haskell_var(&r, out, out_len, RW); -} diff --git a/rust-wrapper/src/utils.rs b/rust-wrapper/src/utils.rs index 2ef96b3..c4bfced 100644 --- a/rust-wrapper/src/utils.rs +++ b/rust-wrapper/src/utils.rs @@ -1,22 +1,13 @@ use ark_bls12_381::{Fr as ScalarField, G1Affine as GAffine}; use ark_ff::PrimeField; -use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; -use haskell_ffi::error::Result; -use haskell_ffi::{FromHaskell, ToHaskell}; -use num_bigint::BigUint; -use std::io::Write; -use std::marker::PhantomData; - -pub enum RW {} - -pub const RW: PhantomData = PhantomData; +use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError}; pub struct Buffer(pub Vec); pub fn deserialize_vector( vector: &[u8], object_size: usize, - deserialize: fn(&[u8]) -> Result, + deserialize: fn(&[u8]) -> Result, ) -> Buffer { Buffer( vector @@ -39,59 +30,7 @@ pub fn deserialize_vector_points(buffer: &[u8]) -> Vec { let points_size = bytes.len(); bytes[0..(points_size >> 1)].reverse(); bytes[(points_size >> 1)..points_size].reverse(); - Ok(GAffine::deserialize_uncompressed_unchecked(&*bytes)?) + GAffine::deserialize_uncompressed_unchecked(&*bytes) }) .0 } - -impl FromHaskell for ScalarField { - fn from_haskell(buf: &mut &[u8], tag: PhantomData) -> Result { - let x = >::from_haskell(buf, tag)?; - let s1: ScalarField = PrimeField::from_le_bytes_mod_order(&x); - Ok(s1) - } -} - -impl ToHaskell for ScalarField { - fn to_haskell(&self, writer: &mut W, tag: PhantomData) -> Result<()> { - BigUint::from(self.into_bigint()) - .to_bytes_le() - .to_haskell(writer, tag) - } -} - -impl FromHaskell for GAffine { - fn from_haskell(buf: &mut &[u8], tag: PhantomData) -> Result { - let _a = >::from_haskell(buf, tag)?; - let a = GAffine::deserialize_uncompressed(&*_a)?; - Ok(a) - } -} - -impl ToHaskell for GAffine { - fn to_haskell(&self, writer: &mut W, tag: PhantomData) -> Result<()> { - let mut res = Vec::new(); - self.serialize_uncompressed(&mut res)?; - res.to_haskell(writer, tag) - } -} - -impl FromHaskell for Buffer { - fn from_haskell(buf: &mut &[u8], tag: PhantomData) -> Result { - let buffer = >::from_haskell(buf, tag)?; - let res = deserialize_vector(&buffer, std::mem::size_of::(), |bytes| { - Ok(PrimeField::from_le_bytes_mod_order(bytes)) - }); - Ok(res) - } -} - -impl FromHaskell for Buffer { - fn from_haskell(buf: &mut &[u8], tag: PhantomData) -> Result { - let buffer = >::from_haskell(buf, tag)?; - let res = deserialize_vector(&buffer, GAffine::identity().uncompressed_size(), |bytes| { - Ok(GAffine::deserialize_uncompressed_unchecked(bytes)?) - }); - Ok(res) - } -} diff --git a/zkfold-prover.cabal b/zkfold-prover.cabal index cabe470..747e41e 100644 --- a/zkfold-prover.cabal +++ b/zkfold-prover.cabal @@ -6,7 +6,7 @@ license-file: LICENSE author: ZkFold maintainer: info@zkfold.io category: Network -build-type: Simple +build-type: Custom extra-doc-files: CHANGELOG.md custom-setup @@ -14,6 +14,7 @@ custom-setup base , Cabal , process + , directory Flag Pedantic Description: Enable pedantic build with -Werror @@ -80,24 +81,18 @@ common options library import: options exposed-modules: - Functions - , RustFunctions - , BN254.BN254 + RustFunctions hs-source-dirs: haskell-wrapper/src build-depends: , base >= 4.16 , bytestring - , foreign-rust , zkfold-base , vector , binary + , unix ghc-options: -O2 - build-tool-depends: - c2hs:c2hs - pkgconfig-depends: - rust_wrapper-uninstalled test-suite wrapper-test import: options @@ -105,7 +100,6 @@ test-suite wrapper-test type: exitcode-stdio-1.0 hs-source-dirs: haskell-wrapper/tests other-modules: - ScalarSum MultiScalarMultiplication FFT build-depends: From 00a4e148d1e9cf95aaccd46ad59cf7df1af17c7f Mon Sep 17 00:00:00 2001 From: Evgeniy Samodelov Date: Wed, 9 Oct 2024 17:23:18 +0300 Subject: [PATCH 02/10] feat: add ci --- .github/workflows/main-pull.yml | 105 ++++++++++++++++++++++++++++++++ .github/workflows/main-push.yml | 56 +++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 .github/workflows/main-pull.yml create mode 100644 .github/workflows/main-push.yml diff --git a/.github/workflows/main-pull.yml b/.github/workflows/main-pull.yml new file mode 100644 index 0000000..5dbb93c --- /dev/null +++ b/.github/workflows/main-pull.yml @@ -0,0 +1,105 @@ +name: CI Pull Request + +on: + pull_request: + branches: [main] + +permissions: + contents: write + +jobs: + on-main-pull-request: + + strategy: + matrix: + os: [ubuntu-latest] + cabal: [3.10.2.1] + ghc: [9.6.3] + + runs-on: ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v4.1.1 + + - name: Set up Haskell + uses: haskell-actions/setup@v2.6.2 + id: setup + with: + ghc-version: ${{ matrix.ghc }} + cabal-version: ${{ matrix.cabal }} + cabal-update: true + + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 + + - name: Set up cargo-c + run: | + cargo install cargo-c + + - name: Configure Cabal + run: | + cabal update + cabal configure --enable-tests --enable-benchmarks --enable-documentation + + - name: Generate cache key + # Creates plan.json file + run: | + cabal build all --dry-run + + - name: Restore cached dependencies + uses: actions/cache/restore@v4.0.1 + id: cache + env: + key: ${{ matrix.os }}-ghc-${{ matrix.ghc }}-cabal-${{ matrix.cabal }} + with: + path: ${{ steps.setup.outputs.cabal-store }} + key: ${{ env.key }}-plan-${{ hashFiles('**/plan.json') }} + restore-keys: ${{ env.key }}- + + - name: Install stylish-haskell + run: | + cabal install stylish-haskell + + - name: Lint Haskell + run: | + find . -name '*.hs' -exec sh -c 'for file do stylish-haskell --inplace "$file"; done' sh {} + + + - name: Auto-commit lint + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: stylish-haskell auto-commit + commit_user_name: GitHub Action + commit_user_email: action@github.com + branch: ${{ github.head_ref }} + + - name: Install dependencies + # If we had an exact cache hit, the dependencies will be up to date. + if: steps.cache.outputs.cache-hit != 'true' + run: | + cabal build all --only-dependencies + + # Cache dependencies already here, so that we do not have to rebuild them should the subsequent steps fail. + - name: Save cached dependencies + uses: actions/cache/save@v4.0.1 + # If we had an exact cache hit, trying to save the cache would error because of key clash. + if: steps.cache.outputs.cache-hit != 'true' + with: + path: ${{ steps.setup.outputs.cabal-store }} + key: ${{ steps.cache.outputs.cache-primary-key }} + + - name: Build + run: | + cabal build all -f Pedantic + + # - name: Test + # run: cabal test all + + # - name: Check cabal file + # run: cabal check + + # - name: Document package + # run: cabal haddock all + + # - name: Prepare package for publishing + # run: cabal sdist all \ No newline at end of file diff --git a/.github/workflows/main-push.yml b/.github/workflows/main-push.yml new file mode 100644 index 0000000..1feaaf5 --- /dev/null +++ b/.github/workflows/main-push.yml @@ -0,0 +1,56 @@ +name: CI Push + +on: + push: + branches: [ main ] + + workflow_dispatch: + +permissions: + contents: read + +jobs: + on-main-update: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4.1.1 + - uses: haskell-actions/setup@v2.6.2 + with: + ghc-version: '9.6.3' + cabal-version: '3.10.2.1' + - uses: actions-rs/toolchain@v1 + + - name: Set up cargo-c + run: | + cargo install cargo-c + + - name: Cache + uses: actions/cache@v4.0.1 + env: + cache-name: cache-cabal + with: + path: ~/.cabal + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/*.cabal') }}-${{ hashFiles('**/cabal.project') }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- + + - name: Build package + shell: bash + run: | + cabal update + cabal build all -f Pedantic + + - name: Upload package + env: + HACKAGE_PASSWORD: ${{ secrets.HACKAGE_PASSWORD }} + shell: bash + run: | + cabal sdist symbolic-base + cabal upload --username=VladimirSinyakov --password="$HACKAGE_PASSWORD" dist-newstyle/sdist/*.tar.gz + + cabal v2-haddock symbolic-base --haddock-for-hackage --enable-doc + cabal upload --documentation --username=VladimirSinyakov --password="$HACKAGE_PASSWORD" dist-newstyle/*-docs.tar.gz \ No newline at end of file From a56595b2e30a0e32621be6f958fbe08d38d6b264 Mon Sep 17 00:00:00 2001 From: Evgeniy Samodelov Date: Wed, 9 Oct 2024 17:40:32 +0300 Subject: [PATCH 03/10] fix: ci --- .github/workflows/main-pull.yml | 6 +++--- .github/workflows/main-push.yml | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main-pull.yml b/.github/workflows/main-pull.yml index 5dbb93c..9062ed2 100644 --- a/.github/workflows/main-pull.yml +++ b/.github/workflows/main-pull.yml @@ -7,6 +7,9 @@ on: permissions: contents: write +env: + CARGO_TERM_COLOR: always + jobs: on-main-pull-request: @@ -30,9 +33,6 @@ jobs: cabal-version: ${{ matrix.cabal }} cabal-update: true - - name: Install Rust toolchain - uses: actions-rs/toolchain@v1 - - name: Set up cargo-c run: | cargo install cargo-c diff --git a/.github/workflows/main-push.yml b/.github/workflows/main-push.yml index 1feaaf5..db2c112 100644 --- a/.github/workflows/main-push.yml +++ b/.github/workflows/main-push.yml @@ -6,6 +6,9 @@ on: workflow_dispatch: +env: + CARGO_TERM_COLOR: always + permissions: contents: read @@ -20,8 +23,7 @@ jobs: with: ghc-version: '9.6.3' cabal-version: '3.10.2.1' - - uses: actions-rs/toolchain@v1 - + - name: Set up cargo-c run: | cargo install cargo-c From 6f64089e75ad5d2397fbcc2d7e82adaf0c1730a5 Mon Sep 17 00:00:00 2001 From: Evgeniy Samodelov Date: Wed, 9 Oct 2024 18:57:43 +0300 Subject: [PATCH 04/10] fix: ci --- .github/workflows/main-pull.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main-pull.yml b/.github/workflows/main-pull.yml index 9062ed2..77bb3cb 100644 --- a/.github/workflows/main-pull.yml +++ b/.github/workflows/main-pull.yml @@ -59,7 +59,7 @@ jobs: - name: Install stylish-haskell run: | - cabal install stylish-haskell + cabal install stylish-haskell-0.14.5.0 - name: Lint Haskell run: | From 07c09192e98be3b6747b4a12de5a31a587aa552c Mon Sep 17 00:00:00 2001 From: Evgeniy Samodelov Date: Wed, 9 Oct 2024 20:08:00 +0300 Subject: [PATCH 05/10] fix: add nightly channel in ci --- .github/workflows/main-pull.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/main-pull.yml b/.github/workflows/main-pull.yml index 77bb3cb..4aec0c5 100644 --- a/.github/workflows/main-pull.yml +++ b/.github/workflows/main-pull.yml @@ -33,6 +33,10 @@ jobs: cabal-version: ${{ matrix.cabal }} cabal-update: true + - name: Set up rustup nightly + run: | + rustup toolchain install nightly + - name: Set up cargo-c run: | cargo install cargo-c From dc0610b29930aba9dbfb962b9f2badf9210291e2 Mon Sep 17 00:00:00 2001 From: Evgeniy Samodelov Date: Wed, 9 Oct 2024 21:12:34 +0300 Subject: [PATCH 06/10] fix: add cargo clippy and fmt --- .github/workflows/main-pull.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/main-pull.yml b/.github/workflows/main-pull.yml index 4aec0c5..d0a92e8 100644 --- a/.github/workflows/main-pull.yml +++ b/.github/workflows/main-pull.yml @@ -65,6 +65,14 @@ jobs: run: | cabal install stylish-haskell-0.14.5.0 + - name: Cargo clippy + run: | + cargo clippy --manifest-path rust-wrapper/Cargo.toml -- -D warnings + + - name: Rust format + run: | + cargo fmt --manifest-path rust-wrapper/Cargo.toml + - name: Lint Haskell run: | find . -name '*.hs' -exec sh -c 'for file do stylish-haskell --inplace "$file"; done' sh {} + From fac83e78b119a39a7ccdad53a8108202014b6b39 Mon Sep 17 00:00:00 2001 From: Evgeniy Samodelov Date: Wed, 9 Oct 2024 21:29:45 +0300 Subject: [PATCH 07/10] fix: check auto-commit for code style --- haskell-wrapper/src/RustFunctions.hs | 2 +- rust-wrapper/src/msm.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/haskell-wrapper/src/RustFunctions.hs b/haskell-wrapper/src/RustFunctions.hs index 30dbe7c..2bedd94 100644 --- a/haskell-wrapper/src/RustFunctions.hs +++ b/haskell-wrapper/src/RustFunctions.hs @@ -23,7 +23,7 @@ import GHC.Num.Natural (naturalFromAddr, n import GHC.Ptr (Ptr (..)) import GHC.TypeNats (KnownNat) import Prelude hiding (sum) -import System.Posix.DynamicLinker +import System.Posix.DynamicLinker import ZkFold.Base.Algebra.Basic.Field import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 diff --git a/rust-wrapper/src/msm.rs b/rust-wrapper/src/msm.rs index 796399a..cd377ba 100644 --- a/rust-wrapper/src/msm.rs +++ b/rust-wrapper/src/msm.rs @@ -61,6 +61,7 @@ pub unsafe extern "C" fn rust_wrapper_multi_scalar_multiplication_without_serial out_len: usize, out: *mut libc::c_char, ) { + let scalar_buffer = slice::from_raw_parts(scalars_var as *const u8, scalars_len); let point_buffer = slice::from_raw_parts(points_var as *const u8, points_len); From 87561bdccf36171bb8df83858d08a7c05b717a38 Mon Sep 17 00:00:00 2001 From: diS3e Date: Wed, 9 Oct 2024 18:37:45 +0000 Subject: [PATCH 08/10] stylish-haskell auto-commit --- haskell-wrapper/src/RustFunctions.hs | 2 +- rust-wrapper/src/msm.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/haskell-wrapper/src/RustFunctions.hs b/haskell-wrapper/src/RustFunctions.hs index 2bedd94..30dbe7c 100644 --- a/haskell-wrapper/src/RustFunctions.hs +++ b/haskell-wrapper/src/RustFunctions.hs @@ -23,7 +23,7 @@ import GHC.Num.Natural (naturalFromAddr, n import GHC.Ptr (Ptr (..)) import GHC.TypeNats (KnownNat) import Prelude hiding (sum) -import System.Posix.DynamicLinker +import System.Posix.DynamicLinker import ZkFold.Base.Algebra.Basic.Field import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 diff --git a/rust-wrapper/src/msm.rs b/rust-wrapper/src/msm.rs index cd377ba..796399a 100644 --- a/rust-wrapper/src/msm.rs +++ b/rust-wrapper/src/msm.rs @@ -61,7 +61,6 @@ pub unsafe extern "C" fn rust_wrapper_multi_scalar_multiplication_without_serial out_len: usize, out: *mut libc::c_char, ) { - let scalar_buffer = slice::from_raw_parts(scalars_var as *const u8, scalars_len); let point_buffer = slice::from_raw_parts(points_var as *const u8, points_len); From f6b3511eefb0aaedad75c14601a19822e57beb6e Mon Sep 17 00:00:00 2001 From: Evgeniy Samodelov Date: Wed, 9 Oct 2024 22:05:48 +0300 Subject: [PATCH 09/10] fix: change README --- README.md | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1c2a2f4..69ad357 100644 --- a/README.md +++ b/README.md @@ -15,15 +15,16 @@ Install rustup and cargo (require `curl`): curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh ``` +Install nightly: +```bash +rustup toolchain install nightly +``` + -Install cbindgen (require `gcc`): -```bash -cargo install --force cbindgen -```