Skip to content

Commit 175028a

Browse files
authored
Merge pull request #704 from zkFold/689-utxo
feat(#689): updates to symbolic-ledger in sync with 2nd milestone
2 parents f61b46d + 0864a81 commit 175028a

File tree

24 files changed

+678
-785
lines changed

24 files changed

+678
-785
lines changed

symbolic-base/src/ZkFold/Data/Eq.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import qualified Data.Ord as Haskell
1414
import Data.Ratio (Rational)
1515
import Data.String (String)
1616
import Data.Type.Equality (type (~))
17+
import GHC.Generics ((:.:))
1718
import qualified GHC.Generics as G
1819
import Numeric.Natural (Natural)
1920
import Prelude (Integer)
@@ -28,14 +29,18 @@ class BoolType (BooleanOf a) => Eq a where
2829
(==) :: a -> a -> BooleanOf a
2930
default (==)
3031
:: (G.Generic a, GEq (G.Rep a), BooleanOf a ~ GBooleanOf (G.Rep a))
31-
=> a -> a -> BooleanOf a
32+
=> a
33+
-> a
34+
-> BooleanOf a
3235
x == y = geq (G.from x) (G.from y)
3336

3437
infix 4 /=
3538
(/=) :: a -> a -> BooleanOf a
3639
default (/=)
3740
:: (G.Generic a, GEq (G.Rep a), BooleanOf a ~ GBooleanOf (G.Rep a))
38-
=> a -> a -> BooleanOf a
41+
=> a
42+
-> a
43+
-> BooleanOf a
3944
x /= y = gneq (G.from x) (G.from y)
4045

4146
elem :: (Eq a, Foldable t) => a -> t a -> BooleanOf a
@@ -74,6 +79,8 @@ instance Eq (f a) => Eq (G.Rec1 f a)
7479

7580
instance Eq (f a) => Eq (G.M1 i c f a)
7681

82+
instance Eq (f (g a)) => Eq ((:.:) f g a)
83+
7784
instance {-# OVERLAPPING #-} Eq (f a) => Eq ((f G.:*: G.U1) a) where
7885
(x G.:*: _) == (y G.:*: _) = x == y
7986
(x G.:*: _) /= (y G.:*: _) = x /= y

symbolic-base/src/ZkFold/Symbolic/Data/MerkleTree.hs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module ZkFold.Symbolic.Data.MerkleTree (
77
emptyTree,
88
fromLeaves,
99
toLeaves,
10+
MerklePath,
1011
merklePath,
1112
rootOnReplace,
1213
MerkleEntry (..),
@@ -111,7 +112,9 @@ type Index d = Vector (d - 1) :.: Bool
111112

112113
merklePath
113114
:: (Symbolic c, KnownNat (d - 1))
114-
=> MerkleTree d c -> Index d c -> MerklePath d c
115+
=> MerkleTree d c
116+
-> Index d c
117+
-> MerklePath d c
115118
merklePath MerkleTree {..} position =
116119
let baseTree = Base.MerkleTree (toBaseHash mHash) (toBaseLeaves mLeaves)
117120
path = fromBaseHash <$> Base.merkleProve' baseTree (toBasePosition position)
@@ -130,7 +133,9 @@ data MerkleEntry d c = MerkleEntry
130133
contains
131134
:: forall d c
132135
. (Symbolic c, KnownNat (d - 1))
133-
=> MerkleTree d c -> MerkleEntry d c -> Bool c
136+
=> MerkleTree d c
137+
-> MerkleEntry d c
138+
-> Bool c
134139
tree `contains` MerkleEntry {..} =
135140
rootOnReplace (merklePath tree position) value == mHash tree
136141

@@ -139,7 +144,9 @@ type Bool' c = BooleanOf (IntegralOf (WitnessField c))
139144
(!!)
140145
:: forall d c
141146
. (Symbolic c, KnownNat (d - 1))
142-
=> MerkleTree d c -> Index d c -> FieldElement c
147+
=> MerkleTree d c
148+
-> Index d c
149+
-> FieldElement c
143150
tree !! position =
144151
assert (\value -> tree `contains` MerkleEntry {..}) $
145152
fromBaseHash $
@@ -172,7 +179,9 @@ search pred tree =
172179
recSearch
173180
:: forall n b a
174181
. (BoolType b, Conditional b a)
175-
=> (a -> b) -> Vector (2 ^ n) a -> (b, Vector n b, a)
182+
=> (a -> b)
183+
-> Vector (2 ^ n) a
184+
-> (b, Vector n b, a)
176185
recSearch p d =
177186
let (b, i, x) = doSearch (toV d)
178187
in (b, unsafeToVector i, x)
@@ -228,7 +237,9 @@ elemIndex elem = findIndex (== elem)
228237

229238
lookup
230239
:: (Symbolic c, KnownNat (d - 1))
231-
=> MerkleTree d c -> Index d c -> FieldElement c
240+
=> MerkleTree d c
241+
-> Index d c
242+
-> FieldElement c
232243
lookup = (!!)
233244

234245
search'
@@ -242,7 +253,9 @@ type KnownMerkleTree d = (KnownNat (d - 1), KnownNat (Base.MerkleTreeSize d))
242253

243254
replace
244255
:: (Symbolic c, KnownMerkleTree d)
245-
=> MerkleEntry d c -> MerkleTree d c -> MerkleTree d c
256+
=> MerkleEntry d c
257+
-> MerkleTree d c
258+
-> MerkleTree d c
246259
replace entry@MerkleEntry {..} =
247260
assert (`contains` entry)
248261
. unconstrainedFromLeaves
@@ -252,12 +265,18 @@ replace entry@MerkleEntry {..} =
252265
where
253266
replacer
254267
:: (FromConstant n i, Eq i, Conditional (BooleanOf i) a)
255-
=> (i, a) -> n -> a -> a
268+
=> (i, a)
269+
-> n
270+
-> a
271+
-> a
256272
replacer (i, a') n = ifThenElse (i == fromConstant n) a'
257273

258274
replaceAt
259275
:: (Symbolic c, KnownMerkleTree d)
260-
=> Index d c -> FieldElement c -> MerkleTree d c -> MerkleTree d c
276+
=> Index d c
277+
-> FieldElement c
278+
-> MerkleTree d c
279+
-> MerkleTree d c
261280
replaceAt position value = replace MerkleEntry {..}
262281

263282
---------------------------- conversion functions ------------------------------

symbolic-ledger/src/ZkFold/Symbolic/Ledger/Types.hs

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,54 @@
33
module ZkFold.Symbolic.Ledger.Types (
44
module ZkFold.Symbolic.Ledger.Types.Address,
55
module ZkFold.Symbolic.Ledger.Types.Hash,
6-
module ZkFold.Symbolic.Ledger.Types.Interval,
7-
module ZkFold.Symbolic.Ledger.Types.Output,
6+
module ZkFold.Symbolic.Ledger.Types.State,
87
module ZkFold.Symbolic.Ledger.Types.Transaction,
9-
module ZkFold.Symbolic.Ledger.Types.Root,
10-
module ZkFold.Symbolic.Ledger.Types.Datum,
118
module ZkFold.Symbolic.Ledger.Types.Value,
12-
module ZkFold.Symbolic.Ledger.Types.DataAvailability,
13-
module ZkFold.Symbolic.Ledger.Types.Circuit,
14-
Signature,
9+
SignatureTransaction,
10+
SignatureTransactionBatch,
11+
SignatureState,
1512
) where
1613

17-
-- Re-exports
18-
19-
import GHC.Generics ((:*:))
20-
import GHC.TypeLits (KnownNat)
14+
import GHC.Generics ((:.:))
15+
import GHC.TypeNats (KnownNat, type (-))
16+
import ZkFold.Data.MerkleTree (MerkleTreeSize)
17+
import ZkFold.Data.Vector (Vector)
2118
import ZkFold.Symbolic.Class (Symbolic (..))
22-
import ZkFold.Symbolic.Data.Combinators (
23-
Ceil,
24-
GetRegisterSize,
25-
KnownRegisters,
26-
RegisterSize (Auto),
27-
)
19+
import ZkFold.Symbolic.Data.FieldElement (FieldElement)
2820
import ZkFold.Symbolic.Data.Hash (Hashable)
29-
import ZkFold.Symbolic.Data.UInt (OrdWord)
30-
import ZkFold.Symbolic.Fold (SymbolicFold)
3121

3222
import ZkFold.Symbolic.Ledger.Types.Address
33-
import ZkFold.Symbolic.Ledger.Types.Circuit
34-
import ZkFold.Symbolic.Ledger.Types.DataAvailability
35-
import ZkFold.Symbolic.Ledger.Types.Datum
3623
import ZkFold.Symbolic.Ledger.Types.Hash
37-
import ZkFold.Symbolic.Ledger.Types.Interval
38-
import ZkFold.Symbolic.Ledger.Types.Output
39-
import ZkFold.Symbolic.Ledger.Types.Root
24+
import ZkFold.Symbolic.Ledger.Types.State
4025
import ZkFold.Symbolic.Ledger.Types.Transaction
4126
import ZkFold.Symbolic.Ledger.Types.Value
4227

43-
{-
44-
zkFold's ledger is a UTXO-based ledger. The architecture of the ledger is mostly similar to the Cardano ledger with some key differences:
45-
46-
- Some transaction data is private and is kept off-chain by the concerned parties.
47-
48-
- All UTXOs are locked by contracts.
28+
type SignatureTransaction ud i o a context =
29+
( Symbolic context
30+
, KnownRegistersAssetQuantity context
31+
, KnownNat i
32+
, KnownNat a
33+
, KnownNat (ud - 1)
34+
, KnownNat (MerkleTreeSize ud)
35+
, Hashable (HashSimple context) (Transaction i o a context)
36+
, forall s. Hashable (HashSimple s) (Transaction i o a s)
37+
, Hashable (HashSimple context) (UTxO a context)
38+
, forall s. Hashable (HashSimple s) (UTxO a s)
39+
)
4940

50-
- Stake delegation and governance is implemented through contracts.
51-
-}
41+
type SignatureTransactionBatch ud i o a t context =
42+
( SignatureTransaction ud i o a context
43+
, Hashable (HashSimple context) (TransactionBatch i o a t context)
44+
, forall s. Hashable (HashSimple s) (TransactionBatch i o a t s)
45+
)
5246

53-
type Signature context =
54-
( KnownRegistersAssetQuantity context
55-
, KnownRegistersOutputIndex context
56-
, KnownRegisters context 11 Auto
57-
, SymbolicFold context
58-
, KnownNat (Ceil (GetRegisterSize (BaseField context) 11 Auto) OrdWord)
59-
, -- TODO: Can we derive 'Hashable h' based on constituents (using generic)?
60-
-- TODO: Remove @ImpredicativeTypes@ extension from symbolic-ledger once above 'Hashable' issue is sorted.
61-
Hashable (HashSimple context) (AssetValues context)
62-
, Hashable (HashSimple context) (Transaction context)
63-
, Hashable (HashSimple context) (TransactionBatch context)
64-
, Hashable (HashSimple context) (TransactionBatchData context)
65-
, Hashable (HashSimple context) ((Circuit :*: DAIndex :*: DAType) context)
66-
, forall s. Hashable (HashSimple s) (AssetValues s)
67-
, forall s. Hashable (HashSimple s) (Transaction s)
68-
, forall s. Hashable (HashSimple s) (TransactionBatch s)
69-
, forall s. Hashable (HashSimple s) (TransactionBatchData s)
70-
, forall s. Hashable (HashSimple s) ((Circuit :*: DAIndex :*: DAType) s)
47+
type SignatureState bi bo ud a context =
48+
( Symbolic context
49+
, KnownRegistersAssetQuantity context
50+
, Hashable (HashSimple context) (State bi bo ud a context)
51+
, forall s. Hashable (HashSimple s) (State bi bo ud a s)
52+
, Hashable (HashSimple context) ((Vector bi :.: Output a) context)
53+
, Hashable (HashSimple context) ((Vector bo :.: Output a) context)
54+
, Hashable (HashSimple context) (FieldElement context)
55+
, forall s. Hashable (HashSimple s) (FieldElement s)
7156
)
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
module ZkFold.Symbolic.Ledger.Types.Address (
22
Address,
3+
nullAddress,
34
) where
45

5-
import GHC.Generics ((:*:))
6-
7-
import ZkFold.Symbolic.Ledger.Types.Circuit (Circuit)
8-
import ZkFold.Symbolic.Ledger.Types.DataAvailability
9-
import ZkFold.Symbolic.Ledger.Types.Hash (Hash)
6+
import ZkFold.Algebra.Class (Zero (..))
7+
import ZkFold.Symbolic.Class (Symbolic)
8+
import ZkFold.Symbolic.Data.FieldElement (FieldElement)
9+
import Prelude hiding (Bool, Eq, length, splitAt, (*), (+))
1010

1111
-- | Address on the zkFold ledger.
12-
--
13-
-- Circuit describes the smart contract that locks funds at this address.
14-
--
15-
-- Data availability index points to a data availability source that posts data about the address's transactions. The data availability type indicates whether the address is associated with online or offline transactions.
16-
type Address = Hash (Circuit :*: DAIndex :*: DAType)
12+
type Address = FieldElement
13+
14+
-- | Null address.
15+
nullAddress :: Symbolic context => Address context
16+
nullAddress = zero

symbolic-ledger/src/ZkFold/Symbolic/Ledger/Types/Circuit.hs

Lines changed: 0 additions & 13 deletions
This file was deleted.

symbolic-ledger/src/ZkFold/Symbolic/Ledger/Types/DataAvailability.hs

Lines changed: 0 additions & 25 deletions
This file was deleted.

symbolic-ledger/src/ZkFold/Symbolic/Ledger/Types/Datum.hs

Lines changed: 0 additions & 9 deletions
This file was deleted.

symbolic-ledger/src/ZkFold/Symbolic/Ledger/Types/Hash.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ZkFold.Symbolic.Data.FieldElement (FieldElement)
77
import qualified ZkFold.Symbolic.Data.Hash as Symbolic.Hash
88

99
-- | Hash type used in the zkFold ledger.
10-
type Hash = Symbolic.Hash.Hash FieldElement
10+
type Hash = Symbolic.Hash.Hash HashSimple
1111

1212
-- TODO: Introduce a phantom type to track hash relation? Also should likely add strong typing than synonyms.
1313

symbolic-ledger/src/ZkFold/Symbolic/Ledger/Types/Interval.hs

Lines changed: 0 additions & 32 deletions
This file was deleted.

symbolic-ledger/src/ZkFold/Symbolic/Ledger/Types/Output.hs

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)