Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Vardominator committed Jun 5, 2024
0 parents commit a9dd31d
Show file tree
Hide file tree
Showing 16 changed files with 315 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dist-newstyle/
.dockerignore
.gitignore
CHANGELOG.md
Dockerfile
LICENSE
README.md
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
dist
dist-*
cabal-dev
*.o
*.hi
*.hie
*.chi
*.chs.h
*.dyn_o
*.dyn_hi
.hpc
.hsenv
.cabal-sandbox/
cabal.sandbox.config
*.prof
*.aux
*.hp
*.eventlog
.stack-work/
cabal.project.local
cabal.project.local~
.HTF/
.ghc.environment.*
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Revision history for zkfold-prover-api

## 0.1.0.0 -- YYYY-mm-dd

* First version. Released on an unsuspecting world.
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM haskell:9.6.3 as build

WORKDIR /app

COPY zkfold-prover-api.cabal cabal.project /app/

RUN cabal update

COPY . /app

RUN cabal build --enable-executable-static --libexecdir=/usr/local/bin

FROM debian:bullseye-slim

RUN apt-get update && apt-get install -y libgmp10 && rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY --from=build /usr/local/bin/zkfold-prover-api /app/zkfold-prover-api

EXPOSE 8080
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2024 Vardominator

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
APP_NAME=zkfold-prover-api
DOCKER_IMAGE=$(APP_NAME):latest
PORT ?= 8080

all: build

build:
cabal update
cabal build

run: build
PORT=$(PORT) cabal run

docker-build:
docker build -t $(DOCKER_IMAGE) .

docker-run: docker-build
docker run -e PORT=$(PORT) -p $(PORT):$(PORT) $(DOCKER_IMAGE)

clean:
cabal clean

.PHONY: all build run docker-build docker-run clean
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# zkFold Prover API
REST API server for zkFold proof creation and verification. Leverages the [zkFold Base](https://github.com/zkFold/zkfold-base/tree/main) building blocks.

In collaboration with [Maestro](https://github.com/maestro-org)!

<img src="media/cover.png" alt="Cover Image" width="400">

# Build
## Source
The package compiles with GHC 9.6.3 and Cabal 3.10.2.1.
```
make build
```
## Docker
```
make docker-build
```
# Run
## Host
```
make run
```
## Docker
```
make docker-run
```

# Tests
On the way!
13 changes: 13 additions & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Main where

import Network.Wai.Handler.Warp (run)
import System.Environment (lookupEnv)
import Text.Read (readMaybe)
import Server (app)

main :: IO ()
main = do
portEnv <- lookupEnv "PORT"
let port = maybe 8080 id (portEnv >>= readMaybe)
putStrLn $ "Running server on port " ++ show port
run port app
7 changes: 7 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
packages: .

source-repository-package
type: git
location: https://github.com/zkFold/zkfold-base
tag: main
subdir: .
Binary file added media/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/API/Prove.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}

module API.Prove where

import Servant
import Data.ByteString (ByteString)
import ZkFold.Base.Protocol.NonInteractiveProof (ProveAPIResult)

type ProveAPI = "prove" :> ReqBody '[OctetStream] ByteString :> ReqBody '[OctetStream] ByteString :> Post '[JSON] ProveAPIResult
18 changes: 18 additions & 0 deletions src/Handlers/ProveHandler.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Handlers.ProveHandler where

import Servant
import Data.ByteString (ByteString)
import ZkFold.Base.Protocol.NonInteractiveProof (proveAPI, ProveAPIResult(..))
import ZkFold.Base.Data.ByteString (fromByteString)
import Data.Maybe (fromJust)
import Types.ZkProof (ZkProof)
import Types.Instances ()

proveHandler :: ByteString -> ByteString -> Handler ProveAPIResult
proveHandler bsS bsW = do
let setup = fromJust (fromByteString bsS)
witness = fromJust (fromByteString bsW)
return $ proveAPI @ZkProof setup witness
17 changes: 17 additions & 0 deletions src/Server.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}

module Server (app) where

import Servant
import API.Prove (ProveAPI)
import Handlers.ProveHandler (proveHandler)
import Types.Instances () -- Import the instances

type API = ProveAPI

server :: Server API
server = proveHandler

app :: Application
app = serve (Proxy :: Proxy API) server
47 changes: 47 additions & 0 deletions src/Types/Instances.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}

module Types.Instances where

import GHC.Generics (Generic)
import Data.Aeson (ToJSON, FromJSON, toJSON, parseJSON, withObject, (.:), (.=), object)
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.Char8 as BS
import qualified Data.Text as T
import qualified Data.Aeson.Key as Key
import ZkFold.Base.Protocol.NonInteractiveProof (ProveAPIResult(..))

-- Define ToJSON and FromJSON instances for ByteString
instance ToJSON BS.ByteString where
toJSON = Aeson.String . T.pack . BS.unpack . B64.encode

instance FromJSON BS.ByteString where
parseJSON = Aeson.withText "ByteString" $ \t ->
case B64.decode (BS.pack (T.unpack t)) of
Left err -> fail err
Right bs -> return bs

-- Define ToJSON and FromJSON instances for ProveAPIResult
instance ToJSON ProveAPIResult where
toJSON (ProveAPISuccess bs) =
object [Key.fromString "status" .= ("success" :: String), Key.fromString "data" .= bs]
toJSON ProveAPIErrorSetup =
object [Key.fromString "status" .= ("error" :: String), Key.fromString "message" .= ("Setup error" :: String)]
toJSON ProveAPIErrorWitness =
object [Key.fromString "status" .= ("error" :: String), Key.fromString "message" .= ("Witness error" :: String)]

instance FromJSON ProveAPIResult where
parseJSON = withObject "ProveAPIResult" $ \v -> do
status <- v .: Key.fromString "status"
case (status :: String) of
"success" -> do
dataStr <- v .: Key.fromString "data"
return $ ProveAPISuccess dataStr
"error" -> do
message <- v .: Key.fromString "message"
case (message :: String) of
"Setup error" -> return ProveAPIErrorSetup
"Witness error" -> return ProveAPIErrorWitness
_ -> fail "Unknown error message"
_ -> fail "Unknown status"
26 changes: 26 additions & 0 deletions src/Types/ZkProof.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TypeFamilies #-}

module Types.ZkProof where

import GHC.Generics (Generic)
import Control.DeepSeq (NFData)
import ZkFold.Base.Protocol.NonInteractiveProof
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS (empty)

-- Define your proof type
data ZkProof = ZkProof
deriving (Show, Eq, Generic, NFData)

instance NonInteractiveProof ZkProof where
type Transcript ZkProof = ByteString
type Setup ZkProof = ByteString
type Witness ZkProof = ByteString
type Input ZkProof = ByteString
type Proof ZkProof = ByteString

setup _ = BS.empty
prove _ _ = (BS.empty, BS.empty)
verify _ _ _ = True
49 changes: 49 additions & 0 deletions zkfold-prover-api.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
cabal-version: 3.0
name: zkfold-prover-api
version: 0.1.0.0
synopsis: ZkFold's prover backend API for constructing ZK proofs
homepage: https://zkfold.io/
license: MIT
license-file: LICENSE
author: Vardominator
maintainer: [email protected]
build-type: Simple
extra-doc-files: CHANGELOG.md

common warnings
ghc-options: -Wall

library
hs-source-dirs: src
exposed-modules: Server
, API.Prove
, Handlers.ProveHandler
, Types.ZkProof
, Types.Instances
build-depends: base ^>=4.18.1.0 && <5
, servant
, servant-server
, aeson
, bytestring
, text
, deepseq
, generic-deriving
, QuickCheck
, cryptohash-sha256
, base64-bytestring
, warp
, zkfold-base
default-language: Haskell2010

executable zkfold-prover-api
import: warnings
main-is: Main.hs
hs-source-dirs: app
build-depends: base ^>=4.18.1.0 && <5
, zkfold-prover-api
, aeson
, bytestring
, base64-bytestring
, text
, warp
default-language: Haskell2010

0 comments on commit a9dd31d

Please sign in to comment.