Skip to content

Commit daed67f

Browse files
authored
Resource Machine v0.2 (#87)
* mk refactor * style * error message * sync * wip * more types * rc1 * update Package version * update Helpers * update juvix lock * fixes * add FixedSize Nonce * v0.10.0-rc3 * Resource.mk * rc 4 * rc 5 * rc 6 * use AnomaAtom rc7 * rc8 * push missing file * rc9 * lock * rc10 * selftag rc11 * appdata list rc12 * fixed size identities * rc13 * update dependencies rc14
1 parent f85dadd commit daed67f

32 files changed

+814
-636
lines changed

Anoma.juvix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module Anoma;
22

3-
import Anoma.Resource open public;
43
import Anoma.Identity open public;
4+
import Anoma.Data open public;
55
import Anoma.Builtin.ByteArray open public;
66
import Anoma.Delta as Delta public;
77
import Anoma.Delta open using {Delta} public;
88
import Anoma.Random open public;
99
import Anoma.Utils as Utils public;
1010
import Anoma.Encode open public;
11-
import Anoma.State.CommitmentTree open public;
11+
import BaseLayer.ResourceMachine open public;
1212
import Anoma.Builtin.System open using {isNullifier; isCommitment} public;

Anoma/Builtin/ByteArray.juvix

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
11
module Anoma.Builtin.ByteArray;
22

33
import Stdlib.Prelude open;
4+
import BaseLayer.AnomaAtom open;
45
import Stdlib.Trait.Ord.Eq open using {fromOrdToEq};
56

6-
builtin bytearray
7-
axiom ByteArray : Type;
7+
module ByteArray;
8+
builtin bytearray
9+
axiom ByteArray : Type;
810

9-
builtin bytearray-from-list-byte
10-
axiom mkByteArray : List Byte -> ByteArray;
11+
builtin bytearray-from-list-byte
12+
axiom mk : List Byte -> ByteArray;
1113

12-
builtin bytearray-length
13-
axiom size : ByteArray -> Nat;
14+
zero (length : Nat) : ByteArray := mk (replicate length 0x0);
1415

15-
builtin anoma-bytearray-to-anoma-contents
16-
axiom toAnomaContents : ByteArray -> Nat;
16+
--- The number of bytes in the ;ByteArray;
17+
builtin bytearray-length
18+
axiom size : ByteArray -> Nat;
1719

18-
builtin anoma-bytearray-from-anoma-contents
19-
axiom fromAnomaContents : Nat -> Nat -> ByteArray;
20+
builtin anoma-bytearray-to-anoma-contents
21+
axiom rawToAnomaContents : ByteArray -> Nat;
22+
23+
toAnomaContents : ByteArray -> AnomaAtom :=
24+
rawToAnomaContents >> AnomaAtom.fromNat;
25+
26+
builtin anoma-bytearray-from-anoma-contents
27+
axiom rawFromAnomaContents (size : Nat) (contents : Nat) : ByteArray;
28+
29+
fromAnomaContents (size : Nat) (contents : AnomaAtom) : ByteArray :=
30+
rawFromAnomaContents@{
31+
size;
32+
contents := AnomaAtom.toNat contents;
33+
};
34+
end;
35+
36+
open ByteArray using {ByteArray; fromAnomaContents; toAnomaContents} public;
2037

2138
instance
2239
ByteArray-Ord : Ord ByteArray :=
2340
let
24-
prod (b : ByteArray) : _ := size b, toAnomaContents b;
25-
in mkOrd@{
26-
cmp (lhs rhs : ByteArray) : Ordering := Ord.cmp (prod lhs) (prod rhs);
41+
prod (b : ByteArray) : _ := ByteArray.size b, toAnomaContents b;
42+
in Ord.mk@{
43+
compare (lhs rhs : ByteArray) : Ordering :=
44+
Ord.compare (prod lhs) (prod rhs);
2745
};
2846

2947
instance
3048
ByteArray-Eq : Eq ByteArray := fromOrdToEq;
31-
32-
zero (length : Nat) : ByteArray := mkByteArray (replicate length 0x0);

Anoma/Data.juvix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Anoma.Data;
2+
3+
import Anoma.Data.RandSeed open public;

Anoma/Data/RandSeed.juvix

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module Anoma.Data.RandSeed;
2+
3+
import Stdlib.Prelude open;
4+
import Anoma.Builtin.System open;
5+
import Anoma.Builtin.ByteArray open;
6+
import BaseLayer.AnomaAtom open;
7+
import Anoma.Primitives.FixedSize open;
8+
9+
--- A fixed-size data type encoding a randomness seed.
10+
--- NOTE: This seed provides pseudo randomness and cannot be expected to provide true randomness.
11+
type RandSeed :=
12+
internalMk@{
13+
unRandSeed : ByteArray;
14+
} with
15+
toByteArray : RandSeed -> ByteArray := unRandSeed;
16+
fromByteArray : ByteArray -> RandSeed := internalMk;
17+
end;
18+
19+
instance
20+
RandSeed-FixedSize : FixedSize RandSeed := FixedSize.mk 32;
21+
22+
instance
23+
RandSeetFromNaturalI : FromNatural RandSeed :=
24+
mkFromNatural@{
25+
fromNat (n : Nat) : RandSeed :=
26+
RandSeed.fromByteArray (fromAnomaContents (FixedSize.byteSize {RandSeed}) (AnomaAtom.fromNat n));
27+
};
28+
29+
deriving instance
30+
RandSeed-Ord : Ord RandSeed;
31+
32+
deriving instance
33+
RandSeed-Eq : Eq RandSeed;

Anoma/Delta/Types.juvix

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import BaseLayer.ResourceMachine as B;
99
open B using {Delta} public;
1010

1111
type VerifyingKey :=
12-
mkVerifyingKey@{
12+
mk@{
1313
unVerifyingKey : UNUSED;
1414
};
1515

1616
type ProvingKey :=
17-
mkProvingKey@{
17+
mk@{
1818
unProvingKey : UNUSED;
1919
};
2020

2121
type Instance :=
22-
mkInstance@{
22+
mk@{
2323
unInstance : UNUSED;
2424
};
2525

2626
type Witness :=
27-
mkWitness@{
27+
mk@{
2828
unWitness : UNUSED;
2929
};
3030

Anoma/Encode.juvix

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,20 @@ module Anoma.Encode;
33
import Stdlib.Prelude open;
44
import Anoma.Builtin.System open;
55

6-
module Encode;
7-
module Internal;
8-
type Encoded A := mkEncoded Nat;
9-
end;
10-
11-
open Internal using {Encoded} public;
6+
type Encoded A := internalMk Nat
7+
with
8+
coerceFromNat {A} : Nat -> Encoded A := internalMk;
129

1310
decode {A} : (encoded : Encoded A) -> A
14-
| (Internal.mkEncoded n) := builtinAnomaDecode n;
11+
| (internalMk n) := builtinAnomaDecode n;
1512

16-
encode {A} (obj : A) : Encoded A :=
17-
Internal.mkEncoded (builtinAnomaEncode obj);
13+
encode {A} (obj : A) : Encoded A := internalMk (builtinAnomaEncode obj);
1814

1915
deriving instance
2016
Encoded-Ord {A} : Ord (Encoded A);
2117

2218
deriving instance
2319
Encoded-Eq {A} : Eq (Encoded A);
24-
2520
end;
2621

27-
open Encode using {Encoded; decode; encode} public;
22+
open Encoded using {decode; encode} public;

Anoma/Identity/External.juvix

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
module Anoma.Identity.External;
22

33
import Stdlib.Prelude open;
4-
import Anoma.Builtin.ByteArray open using {ByteArray; toAnomaContents};
4+
import BaseLayer.AnomaAtom open;
5+
import Anoma.Primitives.FixedSize open;
6+
import Anoma.Builtin.ByteArray open using {
7+
ByteArray;
8+
toAnomaContents;
9+
fromAnomaContents;
10+
};
11+
12+
--- A fixed-size data type describing an external identity.
13+
type ExternalIdentity :=
14+
mk@{
15+
unExternalIdentity : ByteArray;
16+
}
17+
with
18+
fromByteArray : ByteArray -> ExternalIdentity := ExternalIdentity.mk;
19+
20+
fromAnomaAtom : AnomaAtom -> ExternalIdentity :=
21+
fromAnomaContents size >> fromByteArray;
522

6-
module ExternalIdentity;
7-
--- A fixed-size data type describing an external identity.
8-
type ExternalIdentity :=
9-
mkExternalIdentity@{
10-
unExternalIdentity : ByteArray;
11-
};
23+
instance
24+
ExternalIdentity-FixedSize : FixedSize ExternalIdentity := FixedSize.mk size;
1225

13-
fromByteArray : ByteArray -> ExternalIdentity := mkExternalIdentity;
26+
size : Nat := 32;
1427

1528
toByteArray : ExternalIdentity -> ByteArray :=
1629
ExternalIdentity.unExternalIdentity;
1730

18-
toNat : ExternalIdentity -> Nat := toByteArray >> toAnomaContents;
31+
toAnomaAtom : ExternalIdentity -> AnomaAtom := toByteArray >> toAnomaContents;
32+
33+
toNat : ExternalIdentity -> Nat := toAnomaAtom >> AnomaAtom.toNat;
1934

2035
deriving instance
2136
ExternalIdentity-Ord : Ord ExternalIdentity;
@@ -25,7 +40,5 @@ module ExternalIdentity;
2540

2641
instance
2742
ExternalIdentity-Show : Show ExternalIdentity :=
28-
mkShow \{iden := "<<ExternalIdentity>>"};
43+
Show.mk \{iden := "<<ExternalIdentity>>"};
2944
end;
30-
31-
open ExternalIdentity using {ExternalIdentity} public;

Anoma/Identity/Internal.juvix

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,54 @@
11
module Anoma.Identity.Internal;
22

33
import Stdlib.Prelude open;
4+
import BaseLayer.AnomaAtom open;
45
import Stdlib.Trait.Ord.Eq open using {fromOrdToEq};
5-
import Anoma.Builtin.ByteArray open using {ByteArray; toAnomaContents};
6+
import Anoma.Primitives.FixedSize open;
7+
import Anoma.Builtin.ByteArray open using {
8+
ByteArray;
9+
fromAnomaContents;
10+
toAnomaContents;
11+
};
12+
13+
--- A fixed-size data type describing an internal identity.
14+
type InternalIdentity :=
15+
mk@{
16+
unInternalIdentity : ByteArray;
17+
}
18+
with
19+
fromByteArray : ByteArray -> InternalIdentity := InternalIdentity.mk;
20+
21+
fromAnomaAtom : AnomaAtom -> InternalIdentity :=
22+
fromAnomaContents size >> fromByteArray;
623

7-
module InternalIdentity;
8-
--- A fixed-size data type describing an internal identity.
9-
type InternalIdentity :=
10-
mkInternalIdentity@{
11-
unInternalIdentity : ByteArray;
12-
};
24+
instance
25+
InternalIdentity-FixedSize : FixedSize InternalIdentity := FixedSize.mk size;
1326

14-
fromByteArray : ByteArray -> InternalIdentity := mkInternalIdentity;
27+
size : Nat := 32;
1528

1629
toByteArray : InternalIdentity -> ByteArray :=
1730
InternalIdentity.unInternalIdentity;
1831

19-
toNat : InternalIdentity -> Nat := toByteArray >> toAnomaContents;
32+
toAnomaAtom : InternalIdentity -> AnomaAtom := toByteArray >> toAnomaContents;
33+
34+
toNat : InternalIdentity -> Nat := toAnomaAtom >> AnomaAtom.toNat;
2035

2136
--- Compares two ;InternalIdentity; objects.
2237
compare (lhs rhs : InternalIdentity) : Ordering :=
23-
Ord.cmp
38+
Ord.compare
2439
(InternalIdentity.unInternalIdentity lhs)
2540
(InternalIdentity.unInternalIdentity rhs);
2641

2742
--- Implements the ;Ord; trait for ;InternalIdentity;.
2843
instance
29-
InternalIdentity-Ord : Ord InternalIdentity := mkOrd compare;
44+
InternalIdentity-Ord : Ord InternalIdentity := Ord.mk compare;
3045

3146
--- Implements the ;Eq; trait for ;InternalIdentity;.
3247
instance
3348
InternalIdentity-Eq : Eq InternalIdentity := fromOrdToEq;
3449
end;
3550

36-
open InternalIdentity using {InternalIdentity} public;
37-
3851
--- The Anoma backend does not support converting natural numbers to strings
3952
instance
4053
InternalIdentity-Show : Show InternalIdentity :=
41-
mkShow \{id := "<<InternalIdentity>>"};
54+
Show.mk \{id := "<<InternalIdentity>>"};

Anoma/Identity/Object.juvix

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,29 @@ import Anoma.Identity.Internal open;
88
--- A record describing an identity.
99
--- NOTE: For the private testnet, this deviates from the specs https://specs.anoma.net/v2/system_architecture/identity/identity.html.
1010
type Identity :=
11-
mkIdentity@{
11+
mk@{
1212
external : ExternalIdentity;
1313
internal : InternalIdentity;
14-
};
14+
}
15+
with
16+
module Internal;
17+
--- Compares two ;Identity; objects.
18+
compare (lhs rhs : Identity) : Ordering :=
19+
Ord.compare (Identity.external lhs) (Identity.external rhs);
1520

16-
module IdentityInternal;
17-
--- Compares two ;Identity; objects.
18-
compare (lhs rhs : Identity) : Ordering :=
19-
Ord.cmp (Identity.external lhs) (Identity.external rhs);
21+
--- Implements the ;Ord; trait for ;Identity;.
22+
instance
23+
Identity-Ord : Ord Identity := Ord.mk compare;
2024

21-
--- Implements the ;Ord; trait for ;Identity;.
22-
instance
23-
Identity-Ord : Ord Identity := mkOrd compare;
24-
25-
--- Implements the ;Eq; trait for ;Identity;.
26-
instance
27-
Identity-Eq : Eq Identity := fromOrdToEq;
25+
--- Implements the ;Eq; trait for ;Identity;.
26+
instance
27+
Identity-Eq : Eq Identity := fromOrdToEq;
28+
end;
2829
end;
2930

3031
instance
3132
Identity-Show : Show Identity :=
32-
mkShow
33+
Show.mk
3334
\{id :=
3435
"{"
3536
++str "external : "

Anoma/Identity/Signing/Types.juvix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Anoma.Builtin.ByteArray as ByteArray open;
77
import Anoma.Builtin.System as SystemBuiltins open;
88

99
type Signature :=
10-
mkSignature@{
10+
mk@{
1111
unSignature : ByteArray;
1212
};
1313

@@ -24,4 +24,4 @@ sign
2424
SystemBuiltins.anomaSignDetached
2525
message
2626
(InternalIdentity.toByteArray internalIdentity)
27-
|> mkSignature;
27+
|> Signature.mk;

0 commit comments

Comments
 (0)