Skip to content

Commit

Permalink
add plonk verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoeldner committed Sep 13, 2024
1 parent 23726e1 commit 54dc735
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
Binary file added assets/plonk/vk.bin
Binary file not shown.
13 changes: 13 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ source-repository-package
tag: 174af3523616c8fe01449da5ccbb9f16df097ac3
--sha256: sha256-kVFIy+Aj3TNJpsM1Cs/5uGmzeWwHKYWjjCQ+L1/XOj8=


-- Required for the plonk verifier
-- source-repository-package
-- type: git
-- location: https://github.com/argumentcomputer/lurk-hs
-- tag: fd4bca56d2a2b3f8a3339ba9c63a4f4f1a285095
-- --sha256: sha256-kVFIy+Aj3TNJpsM1Cs/5uGmzeWwHKYWjjCQ+L1/XOj8=

source-repository-package
type: git
location: https://github.com/larskuhtz/lurk-hs.git
tag: 1f1dd579efc9082211bdea4decc3218f8dd1441b

-- -------------------------------------------------------------------------- --
-- Relaxed Bounds

Expand Down
6 changes: 6 additions & 0 deletions chainweb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ extra-source-files:

rewards/miner_rewards.csv

-- verifier assets

assets/plonk/vk.bin

-- pact test data

pact/coin-contract/*.repl
Expand Down Expand Up @@ -264,6 +268,7 @@ library
, Chainweb.VerifierPlugin.Hyperlane.Message.After225
, Chainweb.VerifierPlugin.Hyperlane.Message.Before225
, Chainweb.VerifierPlugin.Hyperlane.Utils
, Chainweb.VerifierPlugin.Plonk
, Chainweb.Version
, Chainweb.Version.Development
, Chainweb.Version.Guards
Expand Down Expand Up @@ -462,6 +467,7 @@ library
, warp-tls >= 3.4
, yaml >= 0.11
, yet-another-logger >= 0.4.1
, plonk-verify

if flag(ed25519)
cpp-options: -DWITH_ED25519=1
Expand Down
49 changes: 49 additions & 0 deletions src/Chainweb/VerifierPlugin/Plonk.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
-- |

module Chainweb.VerifierPlugin.Plonk (plugin) where

import Chainweb.VerifierPlugin
import Control.Lens
import Control.Monad
import Control.Monad.Except
import Control.Monad.Trans.Class
import qualified Data.Text.Foreign as T
import Foreign.C.String
import GHC.IO
import Pact.Types.Exp
import Pact.Types.PactValue
import Pact.Types.Term.Internal

import qualified PlonkVerify

plonkVerifierPath :: CString
plonkVerifierPath = unsafePerformIO $ newCString "assets/plonk/"
{-# NOINLINE plonkVerifierPath #-}

plugin :: VerifierPlugin
plugin = VerifierPlugin $ \_ proofObj _caps gasRef -> do
chargeGas gasRef 100
(vk, pub, proof) <- case proofObj of
PObject (ObjectMap om) -> do
let getField k = case om ^? at (FieldKey k) . _Just . _PLiteral of
Just (LString str) -> pure str
_ -> throwError $ VerifierError $ k <> " is missing."

vk <- getField "vkey"
pub <- getField "pub"
proof <- getField "proof"

pure (vk, pub, proof)

_ -> throwError $ VerifierError "Expected object with keys: vkey, pub, proof."

pres <- lift $ unsafeIOToST $
T.withCString vk $ \cvk ->
T.withCString pub $ \cpub ->
T.withCString proof $ \cproof ->
PlonkVerify.verify_plonk_bn254 plonkVerifierPath cproof cvk cpub

-- https://github.com/argumentcomputer/lurk-hs/blob/main/src/lib.rs#L58-L63
unless (pres == 1) $ throwError $ VerifierError "Verification failed."
3 changes: 3 additions & 0 deletions src/Chainweb/Version/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import Chainweb.VerifierPlugin
import qualified Chainweb.VerifierPlugin.Allow
import qualified Chainweb.VerifierPlugin.Hyperlane.Announcement
import qualified Chainweb.VerifierPlugin.Hyperlane.Message
import qualified Chainweb.VerifierPlugin.Plonk

import Control.Lens
import Data.Foldable
Expand Down Expand Up @@ -478,4 +479,6 @@ allVerifierPlugins = M.fromList $ map (over _1 VerifierName)

, ("hyperlane_v3_announcement", Chainweb.VerifierPlugin.Hyperlane.Announcement.plugin)
, ("hyperlane_v3_message", Chainweb.VerifierPlugin.Hyperlane.Message.plugin)

, ("plonk", Chainweb.VerifierPlugin.Plonk.plugin)
]

0 comments on commit 54dc735

Please sign in to comment.