Skip to content

Commit c2728b9

Browse files
authored
Update resource type and fix NullifierKeyCommitment.fromByteArray (#100)
1 parent 782d703 commit c2728b9

File tree

7 files changed

+68
-26
lines changed

7 files changed

+68
-26
lines changed

Anoma/Builtin/System.juvix

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,7 @@ axiom pseudoRandomNumberGeneratorSplit : PRNG -> Pair PRNG PRNG;
9999
--- @param The generator to use.
100100
--- @return A pair containing the random number and the advanced PRNG.
101101
builtin anoma-random-next-bytes
102-
axiom pseudoRandomNumberGeneratorNextBits : Nat -> PRNG -> Pair ByteArray PRNG;
103-
104-
--- Generates a random number and returns the a
105-
--- @param The number of output bytes to generate.
106-
--- @param The generator to use.
107-
--- @return A pair containing the random number and the advanced PRNG.
108-
pseudoRandomNumberGeneratorNextBytes
109-
(bytesSize : Nat) (prng : PRNG) : Pair ByteArray PRNG :=
110-
pseudoRandomNumberGeneratorNextBits (bytesSize * 8) prng;
102+
axiom pseudoRandomNumberGeneratorNextBytes : (bytesSize : Nat) -> PRNG -> Pair ByteArray PRNG;
111103

112104
builtin anoma-is-nullifier
113105
axiom isNullifier : Nat -> Bool;

Applib.juvix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Anoma.Builtin.System open using {
1212
} public;
1313

1414
import BaseLayer open public;
15-
15+
import Applib.Show open public;
1616
import Applib.Helpers open public;
1717
import Applib.Identities open public;
1818
import Applib.Authorization as Authorization public;

Applib/Identities.juvix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module ExternalIdentity;
1616
(externalIdentity : ExternalIdentity) : NullifierKeyCommitment :=
1717
externalIdentity
1818
|> ExternalIdentity.toByteArray
19-
|> NullifierKeyCommitment.mk;
19+
|> NullifierKeyCommitment.fromByteArray;
2020
end;
2121

2222
toNullifierKey (identity : Identity) : NullifierKey :=
@@ -88,7 +88,7 @@ module Universal;
8888
nullifierKeyCommitment : NullifierKeyCommitment :=
8989
externalIdentity
9090
|> ExternalIdentity.toByteArray
91-
|> NullifierKeyCommitment.mk;
91+
|> NullifierKeyCommitment.fromByteArray;
9292
end;
9393

9494
module Alice;

Applib/Show.juvix

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Applib.Show;
2+
3+
import Stdlib.Prelude open;
4+
import Stdlib.Debug.Fail open;
5+
import Stdlib.Math open;
6+
7+
--- non-builtin equivalent of ;natToString;
8+
terminating
9+
recursiveNatToString (n : Nat) : String :=
10+
let
11+
showDigit (d : Nat) : String :=
12+
if
13+
| d == 0 := "0"
14+
| d == 1 := "1"
15+
| d == 2 := "2"
16+
| d == 3 := "3"
17+
| d == 4 := "4"
18+
| d == 5 := "5"
19+
| d == 6 := "6"
20+
| d == 7 := "7"
21+
| d == 8 := "8"
22+
| d == 9 := "9"
23+
| else := failwith "impossible";
24+
in if
25+
| n < 10 := showDigit n
26+
| else := recursiveNatToString (div n 10) ++ showDigit (mod n 10);

BaseLayer/ResourceMachine.juvix

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
module BaseLayer.ResourceMachine;
22

33
import BaseLayer.AnomaAtom open public;
4+
import Applib.Show open;
45
import Anoma.Encode open;
5-
import Stdlib.Debug.Fail open;
66
import Anoma.Builtin.System open;
77
import Anoma.Builtin.ByteArray open;
88
import Stdlib.Prelude open;
9+
import Stdlib.Debug.Fail open;
910
import Stdlib.Data.Map as Map open;
1011
import Anoma.Primitives.FixedSize open;
1112
import Anoma.Data.Json open;
@@ -207,9 +208,18 @@ with
207208

208209
size : Nat := 32;
209210

210-
fromAnomaAtom : AnomaAtom -> Nonce := fromAnomaContents size >> privateMk;
211+
fromAnomaAtom : AnomaAtom -> Nonce := fromAnomaContents size >> fromByteArray;
212+
213+
fromByteArray (b : ByteArray) : Nonce :=
214+
if
215+
| ByteArray.size b == size := privateMk b
216+
| else :=
217+
failwith
218+
("Nonce.fromByteArray incorrect size. Was passed a bytearray with size "
219+
++ recursiveNatToString (ByteArray.size b)
220+
++ ".");
211221

212-
from32SizedByteArray : ByteArray -> Nonce := privateMk;
222+
from32SizedByteArray : ByteArray -> Nonce := fromByteArray;
213223

214224
toAnomaAtom : Nonce -> AnomaAtom
215225
| (privateMk nonce) := toAnomaContents nonce;
@@ -230,19 +240,32 @@ end;
230240

231241
--- The nullifier key type describing a secret required to compute the ;Nullifier; of a resource
232242
-- dev: if this has to interact with anoma, replace ByteArray with AnomaAtom
233-
type NullifierKeyCommitment := mk ByteArray
243+
type NullifierKeyCommitment := privateMk ByteArray
234244
with
235245
-- TODO use universal instead of zero
236-
zero : NullifierKeyCommitment :=
237-
NullifierKeyCommitment.mk (ByteArray.zero size);
238-
239-
fromByteArray : ByteArray -> NullifierKeyCommitment := mk;
246+
zero : NullifierKeyCommitment := privateMk (ByteArray.zero size);
247+
248+
--- 𝒪(1) Converts a ;ByteArray; into a ;NullifierKeyCommitment;.
249+
--- It will crash if the ;ByteArray; has more than 32 (;NullifierKeyCommitment.size;) bytes.
250+
fromByteArray (b : ByteArray) : NullifierKeyCommitment :=
251+
let
252+
numBytes := ByteArray.size b;
253+
in if
254+
| numBytes == size := privateMk b
255+
| numBytes < size :=
256+
privateMk
257+
(ByteArray.fromList
258+
(ByteArray.toList b ++ replicate (sub size numBytes) 0))
259+
| else :=
260+
failwith
261+
("NullifierKeyCommitment.fromByteArray expects a ByteArray of at most 32 bytes. Was given a ByteArray of length " ++ recursiveNatToString numBytes);
240262

241263
toAnomaAtom : NullifierKeyCommitment -> AnomaAtom :=
242264
toByteArray >> toAnomaContents;
243265

266+
--- 𝒪(1) The returned ;ByteArray; has always 32 (;NullifierKeyCommitment.size;) bytes
244267
toByteArray : NullifierKeyCommitment -> ByteArray
245-
| (mk b) := b;
268+
| (privateMk b) := b;
246269

247270
size : Nat := 32;
248271

@@ -260,7 +283,7 @@ with
260283
NullifierKeyCommitment-ToJson : ToJson NullifierKeyCommitment :=
261284
ToJson.mk@{
262285
toJson : NullifierKeyCommitment -> Json
263-
| (mk b) := ToJson.toJson b;
286+
| (privateMk b) := ToJson.toJson b;
264287
};
265288
end;
266289

@@ -273,7 +296,7 @@ type Resource :=
273296
quantity : Nat;
274297
ephemeral : Bool;
275298
nonce : Nonce;
276-
nullifierKeyCommitment : AnomaAtom;
299+
nullifierKeyCommitment : NullifierKeyCommitment;
277300
--- Currently unused. It is safe to set it to 0
278301
unusedRandSeed : Nat;
279302
}
@@ -283,7 +306,8 @@ with
283306

284307
module Transparent;
285308
nullifier : Resource -> Nullifier :=
286-
NullifiableResource.Transparent.mk >> NullifiableResource.nullifier NullifierKey.Transparent.mk;
309+
NullifiableResource.Transparent.mk
310+
>> NullifiableResource.nullifier NullifierKey.Transparent.mk;
287311
end;
288312
end;
289313

Package.juvix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package : Package :=
88
version :=
99
mkVersion@{
1010
major := 0;
11-
minor := 12;
11+
minor := 13;
1212
patch := 0;
1313
};
1414
dependencies :=

juvix.lock.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Do not edit this file manually.
33

44
version: 2
5-
checksum: 5c11471c6f7b80ce9559cafebe1537073dc5cb6bba44bbf364862c2a821bef49
5+
checksum: 7a1c0a81eaeccccd15bd8132ec0eb5238f610b3aec8ac13993ab49331c8d3d0e
66
dependencies:
77
- git:
88
name: anoma_juvix-stdlib

0 commit comments

Comments
 (0)