Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Encoded #77

Merged
merged 1 commit into from
Jan 20, 2025
Merged
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
3 changes: 1 addition & 2 deletions Anoma.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ import Anoma.Delta as Delta public;
import Anoma.Delta open using {Delta} public;
import Anoma.Random open public;
import Anoma.Utils as Utils public;

import Anoma.Builtin.System open using {anomaEncode; anomaDecode} public;
import Anoma.Encode open public;
4 changes: 2 additions & 2 deletions Anoma/Builtin/System.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end;

--- Encodes a value into a natural number.
builtin anoma-encode
axiom anomaEncode
axiom builtinAnomaEncode
: {Value : Type}
-- | The value to encode.
-> Value
Expand All @@ -22,7 +22,7 @@ axiom anomaEncode

--- Decodes a value from a natural number.
builtin anoma-decode
axiom anomaDecode
axiom builtinAnomaDecode
: {Value : Type}
-- | The natural number to decode.
-> Nat
Expand Down
20 changes: 20 additions & 0 deletions Anoma/Encode.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Anoma.Encode;

import Stdlib.Prelude open;
import Anoma.Builtin.System open;

module Encode;
module Internal;
type Encoded A := mkEncoded Nat;
end;

open Internal using {Encoded} public;

decode {A} : (encoded : Encoded A) -> A
| (Internal.mkEncoded n) := builtinAnomaDecode n;

encode {A} (obj : A) : Encoded A :=
Internal.mkEncoded (builtinAnomaEncode obj);
end;

open Encode using {Encoded; decode; encode} public;
2 changes: 1 addition & 1 deletion Anoma/Resource/Types.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ open B using {Kind} public;
instance
KindEq : Eq Kind :=
mkEq@{
eq (a b : Kind) : Bool := anomaEncode a == anomaEncode b;
eq (a b : Kind) : Bool := builtinAnomaEncode a == builtinAnomaEncode b;
};

--- A fixed-size data type encoding the public commitment to the private nullifier key.
Expand Down
18 changes: 12 additions & 6 deletions Applib/Data/AnomaMap.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ module Applib.Data.AnomaMap;

import Stdlib.Prelude open;
import Stdlib.Data.Map as Map open using {Map};
import Anoma.Builtin.System open using {anomaEncode; anomaDecode};

-- TODO add size
import Anoma.Builtin.System open using {builtinAnomaEncode; builtinAnomaDecode};
import Anoma.Encode open;

module Private;
type AnomaMap := mkAnoma (Map Nat Nat);
Expand All @@ -14,10 +13,17 @@ open Private using {AnomaMap} public;

empty : AnomaMap := Private.mkAnoma Map.empty;

lookup {Key Value : Type} : Key -> AnomaMap -> Maybe Value
size : AnomaMap -> Nat
| (Private.mkAnoma m) := Map.size m;

lookup {Key Value : Type} : Key -> AnomaMap -> Maybe (Encoded Value)
| key (Private.mkAnoma a) :=
Map.lookup (anomaEncode key) a |> map anomaDecode;
Map.lookup (builtinAnomaEncode key) a |> map Encode.Internal.mkEncoded;

lookupDecode {Key Value : Type} (k : Key) (m : AnomaMap) : Maybe Value :=
map decode (lookup k m);

insert {Key Value : Type} : Key -> Value -> AnomaMap -> AnomaMap
| k v (Private.mkAnoma a) :=
Private.mkAnoma (Map.insert (anomaEncode k) (anomaEncode v) a);
Private.mkAnoma
(Map.insert (builtinAnomaEncode k) (builtinAnomaEncode v) a);
Loading