-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from zkFold/vks4git/protostar
Vks4git/protostar
- Loading branch information
Showing
41 changed files
with
589 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE NoGeneralisedNewtypeDeriving #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
{-# OPTIONS_GHC -freduction-depth=0 #-} | ||
|
||
module Main where | ||
|
||
import Control.DeepSeq (force) | ||
import Control.Exception (evaluate) | ||
import qualified Data.Map as M | ||
import Data.Time.Clock (getCurrentTime) | ||
import Prelude hiding (divMod, not, sum, (&&), (*), (+), (-), (/), (^), | ||
(||)) | ||
import System.Random (randomIO) | ||
import Test.Tasty.Bench | ||
|
||
import ZkFold.Base.Algebra.Basic.Class | ||
import ZkFold.Base.Algebra.Basic.Field | ||
import ZkFold.Base.Algebra.Basic.Number | ||
import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 | ||
import ZkFold.Base.Data.Vector | ||
import ZkFold.Symbolic.Compiler | ||
import ZkFold.Symbolic.Data.Combinators | ||
import ZkFold.Symbolic.Data.UInt | ||
|
||
evalUInt :: forall a n . UInt n ArithmeticCircuit a -> Vector (NumberOfRegisters a n) a | ||
evalUInt (UInt xs) = eval xs M.empty | ||
|
||
-- | Generate random addition circuit of given size | ||
-- | ||
divisionCircuit | ||
:: forall n p r | ||
. KnownNat n | ||
=> PrimeField (Zp p) | ||
=> r ~ NumberOfRegisters (Zp p) n | ||
=> KnownNat r | ||
=> KnownNat (r - 1) | ||
=> KnownNat (r + r) | ||
=> 1 + (r - 1) ~ r | ||
=> 1 <= r | ||
=> IO (UInt n ArithmeticCircuit (Zp p), UInt n ArithmeticCircuit (Zp p)) | ||
divisionCircuit = do | ||
x <- randomIO | ||
y <- randomIO | ||
let acX = fromConstant (x :: Integer) :: UInt n ArithmeticCircuit (Zp p) | ||
acY = fromConstant (y :: Integer) :: UInt n ArithmeticCircuit (Zp p) | ||
|
||
acZ = acX `divMod` acY | ||
|
||
evaluate . force $ acZ | ||
|
||
benchOps | ||
:: forall n p r | ||
. KnownNat n | ||
=> PrimeField (Zp p) | ||
=> r ~ NumberOfRegisters (Zp p) n | ||
=> KnownNat r | ||
=> KnownNat (r - 1) | ||
=> KnownNat (r + r) | ||
=> 1 + (r - 1) ~ r | ||
=> 1 <= r | ||
=> Benchmark | ||
benchOps = env (divisionCircuit @n @p) $ \ ~ac -> | ||
bench ("Dividing UInts of size " <> show (value @n)) $ nf (\(a, b) -> (evalUInt a, evalUInt b)) ac | ||
|
||
main :: IO () | ||
main = do | ||
getCurrentTime >>= print | ||
(UInt ac32q, UInt ac32r) <- divisionCircuit @32 @BLS12_381_Scalar | ||
getCurrentTime >>= print | ||
(UInt ac64q, UInt ac64r) <- divisionCircuit @64 @BLS12_381_Scalar | ||
getCurrentTime >>= print | ||
(UInt ac128q, UInt ac128r) <- divisionCircuit @128 @BLS12_381_Scalar | ||
getCurrentTime >>= print | ||
|
||
putStrLn "Sizes" | ||
|
||
print $ (acSizeM ac32q, acSizeM ac32r) | ||
getCurrentTime >>= print | ||
print $ (acSizeM ac64q, acSizeM ac64r) | ||
getCurrentTime >>= print | ||
print $ (acSizeM ac128q, acSizeM ac128r) | ||
getCurrentTime >>= print | ||
|
||
putStrLn "Evaluation" | ||
|
||
print $ (exec ac32q, exec ac32r) | ||
getCurrentTime >>= print | ||
print $ (exec ac64q, exec ac64r) | ||
getCurrentTime >>= print | ||
print $ (exec ac128q, exec ac128r) | ||
getCurrentTime >>= print | ||
|
||
defaultMain | ||
[ benchOps @32 @BLS12_381_Scalar | ||
, benchOps @64 @BLS12_381_Scalar | ||
, benchOps @128 @BLS12_381_Scalar | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,71 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
|
||
module ZkFold.Base.Protocol.ARK.Protostar where | ||
|
||
|
||
import Control.DeepSeq (NFData) | ||
import Data.Map.Strict (Map) | ||
import qualified Data.Map.Strict as M | ||
import GHC.Generics (Generic) | ||
import Prelude (($), (==)) | ||
import qualified Prelude as P | ||
|
||
import ZkFold.Base.Algebra.Basic.Number | ||
import qualified ZkFold.Base.Data.Vector as V | ||
import ZkFold.Base.Data.Vector (Vector) | ||
import ZkFold.Base.Protocol.ARK.Protostar.SpecialSound | ||
import ZkFold.Symbolic.Compiler.ArithmeticCircuit.Internal | ||
|
||
{-- | ||
1. Compress verification checks (Section 3.5; ) | ||
2. Commit (Section 3.2; ZkFold.Base.Protocol.ARK.Protostar.CommitOpen) | ||
3. Fiat-Shamir transform (Section 3.3; ZkFold.Base.Protocol.ARK.Protostar.FiatShamir) | ||
A technique for taking an interactive proof of knowledge and creating a digital signature based on it. | ||
This way, some fact (for example, knowledge of a certain secret number) can be publicly proven without revealing underlying information. | ||
4. Accumulation scheme (Section 3.4; ZkFold.Base.Protocol.ARK.Protostar.AccumulatorScheme) | ||
5. Obtain the IVC scheme (Theorem 1 from “Proof-Carrying Data Without Succinct Arguments”; ) | ||
--} | ||
-- | ||
-- | ||
-- To complete the protocol: | ||
-- 1. Finish AccumulatorScheme -- and Lookup | ||
-- 2. Implement IVC scheme | ||
-- 3. Put it all together | ||
-- 4. Input and output == AC | ||
|
||
|
||
-- | A data for recurcive computations. | ||
-- @circuit@ is an Arithmetic circuit with @n@ inputs and @n@ outputs applied to itself (i.e. outputs are fed as inputs at the next iteration) @iterations@ times. | ||
-- | ||
data RecursiveCircuit n a | ||
= RecursiveCircuit | ||
{ iterations :: Natural | ||
, circuit :: ArithmeticCircuit a (Vector n) | ||
} deriving (Generic, NFData) | ||
|
||
instance Arithmetic a => SpecialSoundProtocol a (RecursiveCircuit n a) where | ||
type Witness a (RecursiveCircuit n a) = Map Natural a | ||
type Input a (RecursiveCircuit n a) = Vector n a | ||
type ProverMessage a (RecursiveCircuit n a) = Vector n a | ||
type VerifierMessage a (RecursiveCircuit n a) = Vector n a | ||
type Degree (RecursiveCircuit n a) = 2 | ||
|
||
-- One round for Plonk | ||
rounds = P.const 1 | ||
|
||
outputLength (RecursiveCircuit _ c) = P.fromIntegral $ M.size $ constraintSystem c | ||
|
||
-- The transcript will be empty at this point, it is a one-round protocol | ||
-- | ||
prover rc _ i _ = eval (circuit rc) (M.fromList $ P.zip [1..] (V.fromVector i)) | ||
|
||
-- We can use the polynomial system from the circuit, no need to build it from scratch | ||
-- | ||
algebraicMap rc _ _ _ = M.elems $ constraintSystem (circuit rc) | ||
|
||
-- The transcript is only one prover message since this is a one-round protocol | ||
-- | ||
verifier rc i pm _ = eval (circuit rc) (M.fromList $ P.zip [1..] (V.fromVector i)) == P.head pm | ||
|
Oops, something went wrong.