Skip to content

Commit ecb603a

Browse files
authored
Cairo shielded backend integration (#3357)
This PR adds complier support for the builtins needed to create shielded transactions. * Adds the `Json` builtin type with appropriate translation to Nockma. In Nock, a term/noun has type `json` if it has one of the following form: - `["a" p]` with `p` of type `list json`, - `["b" p]` with `p` a boolean atom, - `["o" p]` with `p` of type `list (pair string json)`, - `["n" p]` with `p` a number atom, - `["s" p]` with `p` a string atom. * Adds the `ComplianceInputs` and `ShieldedTransaction` builtin types, and the `createFromComplianceInputs` builtin function.
1 parent 350e5ea commit ecb603a

File tree

28 files changed

+291
-24
lines changed

28 files changed

+291
-24
lines changed

runtime/c/src/juvix/limits.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
// have the value MAX_UIDS.
1919
#define MAX_UIDS 1048576U
2020

21-
#define BUILTIN_UIDS_NUM 8U
21+
// Make sure this corresponds to the number of built-in UIDs in
22+
// Juvix/Compiler/Backend.hs.
23+
#define BUILTIN_UIDS_NUM 13U
2224

2325
// Maximum number of different user constructors (globally).
2426
#define MAX_CONSTR_TAGS (MAX_UIDS - BUILTIN_UIDS_NUM)

runtime/c/src/juvix/object/object.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,14 @@ STATIC_ASSERT(BUILTIN_UIDS_NUM > UID_READLN);
154154

155155
#define FIRST_USER_UID BUILTIN_UIDS_NUM
156156

157-
#define BUILTIN_UIDS_INFO \
158-
{"false", 0, APP_FIXITY}, {"true", 0, APP_FIXITY}, \
159-
{"unit", 0, APP_FIXITY}, {"void", 0, APP_FIXITY}, \
160-
{"return", 0, APP_FIXITY}, {"bind", 0, APP_FIXITY}, \
161-
{"write", 0, APP_FIXITY}, { \
162-
"readln", 0, APP_FIXITY \
157+
#define BUILTIN_UIDS_INFO \
158+
{"false", 0, APP_FIXITY}, {"true", 0, APP_FIXITY}, \
159+
{"unit", 0, APP_FIXITY}, {"void", 0, APP_FIXITY}, \
160+
{"return", 0, APP_FIXITY}, {"bind", 0, APP_FIXITY}, \
161+
{"write", 0, APP_FIXITY}, {"readln", 0, APP_FIXITY}, \
162+
{"Json.array", 0, APP_FIXITY}, {"Json.bool", 0, APP_FIXITY}, \
163+
{"Json.object", 0, APP_FIXITY}, {"Json.number", 0, APP_FIXITY}, { \
164+
"Json.string", 0, APP_FIXITY \
163165
}
164166

165167
/*************************************************/

src/Juvix/Compiler/Backend.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ getLimits tgt debug = case tgt of
5555
_limitsMaxStackDelta = 16368,
5656
_limitsMaxFunctionAlloc = 16368,
5757
_limitsDispatchStackSize = 4,
58-
_limitsBuiltinUIDsNum = 8,
58+
_limitsBuiltinUIDsNum = 13,
5959
_limitsSpecialisedApply = 3
6060
}
6161
TargetCNative64 ->
@@ -70,7 +70,7 @@ getLimits tgt debug = case tgt of
7070
_limitsMaxStackDelta = 8184,
7171
_limitsMaxFunctionAlloc = 8184,
7272
_limitsDispatchStackSize = 4,
73-
_limitsBuiltinUIDsNum = 8,
73+
_limitsBuiltinUIDsNum = 13,
7474
_limitsSpecialisedApply = 3
7575
}
7676
TargetCore ->
@@ -93,7 +93,7 @@ getLimits tgt debug = case tgt of
9393
_limitsMaxStackDelta = 16368,
9494
_limitsMaxFunctionAlloc = 16368,
9595
_limitsDispatchStackSize = 4,
96-
_limitsBuiltinUIDsNum = 8,
96+
_limitsBuiltinUIDsNum = 13,
9797
_limitsSpecialisedApply = 3
9898
}
9999
TargetAnoma ->
@@ -110,7 +110,7 @@ getLimits tgt debug = case tgt of
110110
_limitsMaxStackDelta = 0, -- irrelevant
111111
_limitsMaxFunctionAlloc = 0, -- irrelevant
112112
_limitsDispatchStackSize = 0, -- irrelevant
113-
_limitsBuiltinUIDsNum = 8,
113+
_limitsBuiltinUIDsNum = 13,
114114
_limitsSpecialisedApply = 3
115115
}
116116
TargetRust ->

src/Juvix/Compiler/Backend/C/Translation/FromReg/Base.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ getBuiltinUID = \case
2323
Reg.TagBind -> 5
2424
Reg.TagWrite -> 6
2525
Reg.TagReadLn -> 7
26+
Reg.TagJsonArray -> 8
27+
Reg.TagJsonBool -> 9
28+
Reg.TagJsonObject -> 10
29+
Reg.TagJsonNumber -> 11
30+
Reg.TagJsonString -> 12
2631

2732
getUID :: Reg.ExtraInfo -> Reg.Tag -> Int
2833
getUID info tag = case tag of

src/Juvix/Compiler/Backend/Rust/Translation/FromReg/Base.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ getBuiltinUID = \case
2222
Reg.TagBind -> impossible
2323
Reg.TagWrite -> impossible
2424
Reg.TagReadLn -> impossible
25+
Reg.TagJsonArray -> impossible
26+
Reg.TagJsonBool -> impossible
27+
Reg.TagJsonObject -> impossible
28+
Reg.TagJsonNumber -> impossible
29+
Reg.TagJsonString -> impossible
2530

2631
getUID :: Reg.ExtraInfo -> Reg.Tag -> Int
2732
getUID info tag = case tag of

src/Juvix/Compiler/Builtins/Anoma.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ checkAction d = do
132132
unless (isSmallUniverse' (d ^. inductiveType)) (err "AnomaAction should be in the small universe")
133133
unless (length (d ^. inductiveConstructors) == 1) (err "AnomaAction should have exactly one constructor")
134134

135+
checkComplianceInputs :: (Members '[Error ScoperError] r) => InductiveDef -> Sem r ()
136+
checkComplianceInputs d = do
137+
let err = builtinsErrorText (getLoc d)
138+
unless (null (d ^. inductiveParameters)) (err "ComplianceInputs should have no type parameters")
139+
unless (isSmallUniverse' (d ^. inductiveType)) (err "ComplianceInputs should be in the small universe")
140+
unless (length (d ^. inductiveConstructors) == 1) (err "ComplianceInputs should have exactly one constructor")
141+
142+
checkShieldedTransaction :: (Members '[Error ScoperError] r) => InductiveDef -> Sem r ()
143+
checkShieldedTransaction d = do
144+
let err = builtinsErrorText (getLoc d)
145+
unless (null (d ^. inductiveParameters)) (err "ShieldedTransaction should have no type parameters")
146+
unless (isSmallUniverse' (d ^. inductiveType)) (err "ShieldedTransaction should be in the small universe")
147+
unless (length (d ^. inductiveConstructors) == 1) (err "ShieldedTransaction should have exactly one constructor")
148+
135149
checkDelta :: (Members '[Error ScoperError] r) => AxiomDef -> Sem r ()
136150
checkDelta d =
137151
unless (isSmallUniverse' (d ^. axiomType)) $
@@ -268,6 +282,16 @@ checkAnomaIsNullifier f = do
268282
unless (f ^. axiomType === (nat_ --> bool_)) $
269283
builtinsErrorText l "isNullifier must be of type Nat -> Bool"
270284

285+
checkAnomaCreateFromComplianceInputs :: (Members '[Reader BuiltinsTable, Error ScoperError] r) => AxiomDef -> Sem r ()
286+
checkAnomaCreateFromComplianceInputs f = do
287+
let l = getLoc f
288+
json_ <- getBuiltinNameScoper l BuiltinJson
289+
list_ <- getBuiltinNameScoper l BuiltinList
290+
byteArray <- getBuiltinNameScoper l BuiltinByteArray
291+
shieldedTransaction <- getBuiltinNameScoper l BuiltinAnomaShieldedTransaction
292+
unless (f ^. axiomType === (list_ @@ json_ --> list_ @@ byteArray --> list_ @@ json_ --> list_ @@ byteArray --> list_ @@ json_ --> shieldedTransaction)) $
293+
builtinsErrorText l "createFromComplianceInputs must be of type: List Json -> List ByteArray -> List Json -> List ByteArray -> List Json -> ShieldedTransaction"
294+
271295
checkAnomaSet :: (Members '[Error ScoperError] r) => AxiomDef -> Sem r ()
272296
checkAnomaSet t = do
273297
let ty = t ^. axiomType
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module Juvix.Compiler.Builtins.Json where
2+
3+
import Juvix.Compiler.Internal.Builtins
4+
import Juvix.Compiler.Internal.Extra
5+
import Juvix.Prelude
6+
7+
checkJsonDef :: forall r. (Members '[Reader BuiltinsTable, Error ScoperError] r) => InductiveDef -> Sem r ()
8+
checkJsonDef d = do
9+
let err :: forall a. Text -> Sem r a
10+
err = builtinsErrorText (getLoc d)
11+
unless (isSmallUniverse' (d ^. inductiveType)) (err "Json should be in the small universe")
12+
unless (null (d ^. inductiveParameters)) (err "Json should have no type parameters")
13+
case d ^. inductiveConstructors of
14+
[constrJsonArray, constrJsonBool, constrJsonObject, constrJsonNumber, constrJsonString] ->
15+
checkJsonArray constrJsonArray
16+
>> checkJsonBool constrJsonBool
17+
>> checkJsonObject constrJsonObject
18+
>> checkJsonNumber constrJsonNumber
19+
>> checkJsonString constrJsonString
20+
_ -> err "Json should have exactly five constructors"
21+
22+
checkJsonArray :: (Members '[Reader BuiltinsTable, Error ScoperError] r) => ConstructorDef -> Sem r ()
23+
checkJsonArray d@ConstructorDef {..} = do
24+
let ty = _inductiveConstructorType
25+
json_ <- getBuiltinNameScoper (getLoc d) BuiltinJson
26+
list_ <- getBuiltinNameScoper (getLoc d) BuiltinList
27+
let cty = list_ @@ json_ --> json_
28+
unless (ty === cty) $
29+
builtinsErrorText (getLoc d) "json array constructor has the wrong type"
30+
31+
checkJsonBool :: (Members '[Reader BuiltinsTable, Error ScoperError] r) => ConstructorDef -> Sem r ()
32+
checkJsonBool d@ConstructorDef {..} = do
33+
let ty = _inductiveConstructorType
34+
json_ <- getBuiltinNameScoper (getLoc d) BuiltinJson
35+
bool_ <- getBuiltinNameScoper (getLoc d) BuiltinBool
36+
let cty = bool_ --> json_
37+
unless (ty === cty) $
38+
builtinsErrorText (getLoc d) "json bool constructor has the wrong type"
39+
40+
checkJsonObject :: (Members '[Reader BuiltinsTable, Error ScoperError] r) => ConstructorDef -> Sem r ()
41+
checkJsonObject d@ConstructorDef {..} = do
42+
let ty = _inductiveConstructorType
43+
json_ <- getBuiltinNameScoper (getLoc d) BuiltinJson
44+
list_ <- getBuiltinNameScoper (getLoc d) BuiltinList
45+
pair_ <- getBuiltinNameScoper (getLoc d) BuiltinPair
46+
string_ <- getBuiltinNameScoper (getLoc d) BuiltinString
47+
let cty = list_ @@ (pair_ @@ string_ @@ json_) --> json_
48+
unless (ty === cty) $
49+
builtinsErrorText (getLoc d) "json object constructor has the wrong type"
50+
51+
checkJsonNumber :: (Members '[Reader BuiltinsTable, Error ScoperError] r) => ConstructorDef -> Sem r ()
52+
checkJsonNumber d@ConstructorDef {..} = do
53+
let ty = _inductiveConstructorType
54+
json_ <- getBuiltinNameScoper (getLoc d) BuiltinJson
55+
nat_ <- getBuiltinNameScoper (getLoc d) BuiltinNat
56+
let cty = nat_ --> json_
57+
unless (ty === cty) $
58+
builtinsErrorText (getLoc d) "json number constructor has the wrong type"
59+
60+
checkJsonString :: (Members '[Reader BuiltinsTable, Error ScoperError] r) => ConstructorDef -> Sem r ()
61+
checkJsonString d@ConstructorDef {..} = do
62+
let ty = _inductiveConstructorType
63+
json_ <- getBuiltinNameScoper (getLoc d) BuiltinJson
64+
string_ <- getBuiltinNameScoper (getLoc d) BuiltinString
65+
let cty = string_ --> json_
66+
unless (ty === cty) $
67+
builtinsErrorText (getLoc d) "json string constructor has the wrong type"

src/Juvix/Compiler/Builtins/List.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ checkListDef d = do
1313
param :: VarName <- case d ^. inductiveParameters of
1414
[v] -> return (v ^. inductiveParamName)
1515
_ -> err "List should have exactly one type parameter"
16-
1716
case d ^. inductiveConstructors of
1817
[c1, c2] -> checkNil param c1 >> checkCons param c2
1918
_ -> err "List should have exactly two constructors"

src/Juvix/Compiler/Concrete/Data/Builtins.hs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ builtinConstructors = \case
5555
BuiltinList -> [BuiltinListNil, BuiltinListCons]
5656
BuiltinMaybe -> [BuiltinMaybeNothing, BuiltinMaybeJust]
5757
BuiltinPair -> [BuiltinPairConstr]
58+
BuiltinJson -> [BuiltinJsonArray, BuiltinJsonBool, BuiltinJsonObject, BuiltinJsonNumber, BuiltinJsonString]
5859
BuiltinPoseidonState -> [BuiltinMkPoseidonState]
5960
BuiltinEcPoint -> [BuiltinMkEcPoint]
6061
BuiltinAnomaResource -> [BuiltinMkAnomaResource]
6162
BuiltinAnomaAction -> [BuiltinMkAnomaAction]
63+
BuiltinAnomaComplianceInputs -> [BuiltinMkAnomaComplianceInputs]
64+
BuiltinAnomaShieldedTransaction -> [BuiltinMkAnomaShieldedTransaction]
6265
BuiltinEq -> [BuiltinMkEq]
6366
BuiltinOrd -> [BuiltinMkOrd]
6467
BuiltinOrdering -> [BuiltinOrderingLT, BuiltinOrderingEQ, BuiltinOrderingGT]
@@ -70,13 +73,16 @@ data BuiltinInductive
7073
| BuiltinList
7174
| BuiltinMaybe
7275
| BuiltinPair
76+
| BuiltinJson
7377
| BuiltinEq
7478
| BuiltinOrd
7579
| BuiltinOrdering
7680
| BuiltinPoseidonState
7781
| BuiltinEcPoint
7882
| BuiltinAnomaResource
7983
| BuiltinAnomaAction
84+
| BuiltinAnomaComplianceInputs
85+
| BuiltinAnomaShieldedTransaction
8086
deriving stock (Show, Eq, Ord, Enum, Bounded, Generic, Data)
8187

8288
instance Hashable BuiltinInductive
@@ -93,13 +99,16 @@ instance Pretty BuiltinInductive where
9399
BuiltinList -> Str.list
94100
BuiltinMaybe -> Str.maybe_
95101
BuiltinPair -> Str.pair
102+
BuiltinJson -> Str.json
96103
BuiltinEq -> Str.eq
97104
BuiltinOrd -> Str.ord
98105
BuiltinOrdering -> Str.ordering
99106
BuiltinPoseidonState -> Str.cairoPoseidonState
100107
BuiltinEcPoint -> Str.cairoEcPoint
101108
BuiltinAnomaResource -> Str.anomaResource
102109
BuiltinAnomaAction -> Str.anomaAction
110+
BuiltinAnomaComplianceInputs -> Str.anomaComplianceInputs
111+
BuiltinAnomaShieldedTransaction -> Str.anomaShieldedTransaction
103112

104113
instance Pretty BuiltinConstructor where
105114
pretty = \case
@@ -114,10 +123,17 @@ instance Pretty BuiltinConstructor where
114123
BuiltinMaybeNothing -> Str.nothing
115124
BuiltinMaybeJust -> Str.just
116125
BuiltinPairConstr -> Str.pair
126+
BuiltinJsonArray -> Str.jsonArray
127+
BuiltinJsonBool -> Str.jsonBool
128+
BuiltinJsonObject -> Str.jsonObject
129+
BuiltinJsonNumber -> Str.jsonNumber
130+
BuiltinJsonString -> Str.jsonString
117131
BuiltinMkPoseidonState -> Str.cairoMkPoseidonState
118132
BuiltinMkEcPoint -> Str.cairoMkEcPoint
119133
BuiltinMkAnomaResource -> Str.anomaMkResource
120134
BuiltinMkAnomaAction -> Str.anomaMkAction
135+
BuiltinMkAnomaComplianceInputs -> Str.anomaMkComplianceInputs
136+
BuiltinMkAnomaShieldedTransaction -> Str.anomaMkShieldedTransaction
121137
BuiltinMkEq -> Str.mkEq
122138
BuiltinMkOrd -> Str.mkOrd
123139
BuiltinOrderingLT -> Str.lt
@@ -141,10 +157,17 @@ data BuiltinConstructor
141157
| BuiltinMaybeNothing
142158
| BuiltinMaybeJust
143159
| BuiltinPairConstr
160+
| BuiltinJsonArray
161+
| BuiltinJsonBool
162+
| BuiltinJsonObject
163+
| BuiltinJsonNumber
164+
| BuiltinJsonString
144165
| BuiltinMkPoseidonState
145166
| BuiltinMkEcPoint
146167
| BuiltinMkAnomaResource
147168
| BuiltinMkAnomaAction
169+
| BuiltinMkAnomaComplianceInputs
170+
| BuiltinMkAnomaShieldedTransaction
148171
deriving stock (Show, Eq, Ord, Generic, Data)
149172

150173
instance Hashable BuiltinConstructor
@@ -280,6 +303,7 @@ data BuiltinAxiom
280303
| BuiltinAnomaRandomSplit
281304
| BuiltinAnomaIsCommitment
282305
| BuiltinAnomaIsNullifier
306+
| BuiltinAnomaCreateFromComplianceInputs
283307
| BuiltinAnomaSet
284308
| BuiltinAnomaSetToList
285309
| BuiltinAnomaSetFromList
@@ -350,6 +374,7 @@ instance HasNameKind BuiltinAxiom where
350374
BuiltinAnomaRandomSplit -> KNameFunction
351375
BuiltinAnomaIsCommitment -> KNameFunction
352376
BuiltinAnomaIsNullifier -> KNameFunction
377+
BuiltinAnomaCreateFromComplianceInputs -> KNameFunction
353378
BuiltinPoseidon -> KNameFunction
354379
BuiltinEcOp -> KNameFunction
355380
BuiltinRandomEcPoint -> KNameFunction
@@ -427,6 +452,7 @@ instance Pretty BuiltinAxiom where
427452
BuiltinAnomaRandomSplit -> Str.anomaRandomSplit
428453
BuiltinAnomaIsCommitment -> Str.anomaIsCommitment
429454
BuiltinAnomaIsNullifier -> Str.anomaIsNullifier
455+
BuiltinAnomaCreateFromComplianceInputs -> Str.anomaCreateFromComplianceInputs
430456
BuiltinAnomaSet -> Str.anomaSet
431457
BuiltinAnomaSetToList -> Str.anomaSetToList
432458
BuiltinAnomaSetFromList -> Str.anomaSetFromList

src/Juvix/Compiler/Core/Evaluator.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ geval opts herr tab env0 = eval' env0
245245
OpAnomaRandomSplit -> normalizeOrUnsupported opcode
246246
OpAnomaIsCommitment -> normalizeOrUnsupported opcode
247247
OpAnomaIsNullifier -> normalizeOrUnsupported opcode
248+
OpAnomaCreateFromComplianceInputs -> normalizeOrUnsupported opcode
248249
OpAnomaSetToList -> normalizeOrUnsupported opcode
249250
OpAnomaSetFromList -> normalizeOrUnsupported opcode
250251
OpPoseidonHash -> poseidonHashOp

0 commit comments

Comments
 (0)