Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

Add control flow AST #2

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libsnark-frontend/depends/libsnark
Submodule libsnark updated 61 files
+1 −11 README.md
+1 −1 depends/libff
+1 −1 depends/libfqfft
+0 −2 libsnark/common/data_structures/merkle_tree.tcc
+0 −2 libsnark/common/routing_algorithms/as_waksman_routing_algorithm.cpp
+7 −14 libsnark/gadgetlib1/gadgets/cpu_checkers/tinyram/components/alu_control_flow.tcc
+24 −86 libsnark/gadgetlib1/gadgets/cpu_checkers/tinyram/tinyram_cpu_checker.tcc
+1 −1 libsnark/gadgetlib1/gadgets/fields/fp3_gadgets.tcc
+1 −1 libsnark/gadgetlib1/gadgets/fields/fp4_gadgets.tcc
+0 −7 libsnark/gadgetlib1/gadgets/hashes/knapsack/knapsack_gadget.tcc
+0 −4 libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_gadget.tcc
+0 −4 libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_read_gadget.tcc
+0 −4 libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.tcc
+1 −11 libsnark/gadgetlib1/gadgets/verifiers/tests/test_r1cs_ppzksnark_verifier_gadget.cpp
+2 −2 libsnark/gadgetlib2/infrastructure.cpp
+1 −1 libsnark/gadgetlib2/variable.cpp
+0 −1 libsnark/gadgetlib2/variable.hpp
+2 −9 libsnark/knowledge_commitment/kc_multiexp.tcc
+2 −7 libsnark/reductions/ram_to_r1cs/examples/demo_arithmetization.cpp
+3 −3 libsnark/reductions/ram_to_r1cs/gadgets/ram_universal_gadget.hpp
+7 −2 libsnark/reductions/ram_to_r1cs/gadgets/ram_universal_gadget.tcc
+20 −28 libsnark/relations/ram_computations/rams/examples/ram_examples.tcc
+2 −11 libsnark/relations/ram_computations/rams/fooram/fooram_aux.cpp
+1 −4 libsnark/relations/ram_computations/rams/fooram/fooram_aux.hpp
+1 −2 libsnark/relations/ram_computations/rams/ram_params.hpp
+13 −77 libsnark/relations/ram_computations/rams/tinyram/tinyram_aux.cpp
+1 −2 libsnark/relations/ram_computations/rams/tinyram/tinyram_aux.hpp
+0 −7 libsnark/zk_proof_systems/ppzksnark/bacs_ppzksnark/tests/test_bacs_ppzksnark.cpp
+0 −7 libsnark/zk_proof_systems/ppzksnark/r1cs_gg_ppzksnark/tests/test_r1cs_gg_ppzksnark.cpp
+1 −1 libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/examples/run_r1cs_ppzksnark.tcc
+0 −7 libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/tests/test_r1cs_ppzksnark.cpp
+0 −9 libsnark/zk_proof_systems/ppzksnark/r1cs_se_ppzksnark/tests/test_r1cs_se_ppzksnark.cpp
+1 −1 libsnark/zk_proof_systems/ppzksnark/ram_ppzksnark/examples/demo_ram_ppzksnark.cpp
+0 −4 libsnark/zk_proof_systems/ppzksnark/ram_ppzksnark/examples/demo_ram_ppzksnark_generator.cpp
+1 −5 libsnark/zk_proof_systems/ppzksnark/ram_ppzksnark/examples/demo_ram_ppzksnark_prover.cpp
+1 −5 libsnark/zk_proof_systems/ppzksnark/ram_ppzksnark/examples/demo_ram_ppzksnark_verifier.cpp
+0 −10 libsnark/zk_proof_systems/ppzksnark/ram_ppzksnark/tests/test_ram_ppzksnark.cpp
+0 −7 libsnark/zk_proof_systems/ppzksnark/tbcs_ppzksnark/tests/test_tbcs_ppzksnark.cpp
+0 −7 libsnark/zk_proof_systems/ppzksnark/uscs_ppzksnark/tests/test_uscs_ppzksnark.cpp
+0 −2 tinyram_examples/.gitignore
+0 −40 tinyram_examples/README.md
+0 −0 tinyram_examples/answer0/answer0-auxiliary_input.txt
+0 −1 tinyram_examples/answer0/answer0-computation_bounds.txt
+0 −0 tinyram_examples/answer0/answer0-primary_input.txt
+0 −10 tinyram_examples/answer0/answer0.s
+0 −0 tinyram_examples/answer1/answer1-auxiliary_input.txt
+0 −1 tinyram_examples/answer1/answer1-computation_bounds.txt
+0 −0 tinyram_examples/answer1/answer1-primary_input.txt
+0 −10 tinyram_examples/answer1/answer1.s
+0 −1 tinyram_examples/knapsack-indirect/knapsack-indirect-auxiliary_input.txt
+0 −1 tinyram_examples/knapsack-indirect/knapsack-indirect-computation_bounds.txt
+0 −1 tinyram_examples/knapsack-indirect/knapsack-indirect-primary_input.txt
+0 −38 tinyram_examples/knapsack-indirect/knapsack-indirect.s
+0 −1 tinyram_examples/knapsack/knapsack-auxiliary_input.txt
+0 −1 tinyram_examples/knapsack/knapsack-computation_bounds.txt
+0 −1 tinyram_examples/knapsack/knapsack-primary_input.txt
+0 −27 tinyram_examples/knapsack/knapsack.s
+0 −112 tinyram_examples/process_assembly
+0 −11 tinyram_examples/run_demo_arithmetization
+0 −11 tinyram_examples/run_demo_ram_ppzksnark
+0 −24 tinyram_examples/run_ram_ppzksnark_gpv
2 changes: 2 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ library:
- union-find >= 0.2 && < 0.3
- aeson >= 1.4.6 && < 1.5
- text >= 1.2.3 && < 1.3
- string-interpolate >= 0.3.0
- deepseq >= 1.4.4 && < 1.5
- natural-numbers >= 0.1.2
- unordered-containers >= 0.2.10 && < 0.3
- hashable >= 1.2.7 && < 1.3

Expand Down
59 changes: 59 additions & 0 deletions src/IR/Circify/Control.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveAnyClass #-}
module IR.Circify.Control where

import Data.Natural
import Data.String.Interpolate (i)
import Control.Monad.State.Lazy

-- Range of a for loop
data Range = Range { start :: Int, end :: Int, step :: Int }

-- Convenience function that errors on bad ranges
mkRange :: Int -> Int -> Int -> Range
mkRange start end step
| abs (end - start) >= step && step > 0 = Range start end step
| abs (end - start) >= (- step) && step < 0 = Range start end step
| otherwise = error [i| Bad loop bounds for(i = #{start}; i < #{end}; i = i + #{step})|]

-- Control flow syntax
data Control (term :: * -> *) (a :: *) where
For :: Range -> term Int -> Control term a -> Control term a
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the body be Control term a? Maybe we should do the loop flattening during Codegen and have nested loops be unrepresentable

While :: term Bool -> Natural -> Control term a -> Control term a
If :: term Bool -> Control term a -> Control term a -> Control term a
One :: term a -> Control term a
Seq :: term b -> Control term a -> Control term a

deriving instance Functor term => Functor (Control term)
deriving instance Applicative term => Applicative (Control term)
deriving instance Monad term => Monad (Control term)

-- Convenience constructors
forloop :: Range -> term Int -> Control term a -> Control term a
forloop range iterator body = For range iterator body

whileloop :: term Bool -> Natural -> Control term a -> Control term a
whileloop check maxIterations body = While check maxIterations body

ifstmt :: term Bool -> Control term a -> Control term a -> Control term a
ifstmt check left right = If check left right

-- Analysis state
data LoopAnalysis a term = LoopAnalysis { stack :: [Control term a] }

-- TODO: Implement analysis and loop flattening transformation
loopAnalysis :: Control term a -> State (LoopAnalysis a term) (Control term a)
loopAnalysis = undefined

loopFlatten :: Control term a -> Control term a
loopFlatten top = undefined


3 changes: 2 additions & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ extra-deps:
- ghc-typelits-natnormalise-0.7.2@sha256:0fc48a3744aa25e5e53a054a8bb1fe6410752e497f446d75db9bd67bb258d05e,3495
- integer-roots-1.0@sha256:5ea7ecb80633fb05a8a36acd40e9da6fe22a6cef320b1310cb9f0330801ac124,2271
- mod-0.1.2.0@sha256:4c331986c46808d89da590d791841f046509041c849c4254b0872c72284f73ad,2202
- natural-numbers-0.1.2.0@sha256:0fcbe1220979b83fa612fca73a1adb1a99ff43c84f9652b3ff32b2323309b43c,1407
- poly-0.4.0.0@sha256:75c243113712745dab5d2f4a52643705e146473770af212f9e0ce3d87a83f2ed,2123
- semirings-0.5.4@sha256:5bd49c52a22c876c93d030cafa96b1233e9e7c42dacbe9f49686574368578c9c,2998

- string-interpolate-0.3.0.2@sha256:0102ed1eac13b7f85c6d2d3da764ad178d98a23d74912f5d2898eb54af624651,4022
# Override default flag values for local packages and extra-deps
# flags: {}

Expand Down
14 changes: 14 additions & 0 deletions stack.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ packages:
sha256: e98a21bc2bdaba7dfba0aeb33d7b2ed5472cd496aea38098b40bac665171cefb
original:
hackage: mod-0.1.2.0@sha256:4c331986c46808d89da590d791841f046509041c849c4254b0872c72284f73ad,2202
- completed:
hackage: natural-numbers-0.1.2.0@sha256:0fcbe1220979b83fa612fca73a1adb1a99ff43c84f9652b3ff32b2323309b43c,1407
pantry-tree:
size: 220
sha256: bd4beb677280f4586265c5657ab4e4a599c16f9e624df43d8edda6bf1e46dcdf
original:
hackage: natural-numbers-0.1.2.0@sha256:0fcbe1220979b83fa612fca73a1adb1a99ff43c84f9652b3ff32b2323309b43c,1407
- completed:
hackage: poly-0.4.0.0@sha256:75c243113712745dab5d2f4a52643705e146473770af212f9e0ce3d87a83f2ed,2123
pantry-tree:
Expand All @@ -92,6 +99,13 @@ packages:
sha256: 83e891acded173adc21b1bcce537f70f82d4b835fb42a41838ddf58d7bde7c7b
original:
hackage: semirings-0.5.4@sha256:5bd49c52a22c876c93d030cafa96b1233e9e7c42dacbe9f49686574368578c9c,2998
- completed:
hackage: string-interpolate-0.3.0.2@sha256:0102ed1eac13b7f85c6d2d3da764ad178d98a23d74912f5d2898eb54af624651,4022
pantry-tree:
size: 1002
sha256: 073892b62ecdbee3138c572ede59b223433ac56795af96d2b18885de23b82f96
original:
hackage: string-interpolate-0.3.0.2@sha256:0102ed1eac13b7f85c6d2d3da764ad178d98a23d74912f5d2898eb54af624651,4022
snapshots:
- completed:
size: 524996
Expand Down