Skip to content

Commit 9a6f151

Browse files
authored
Add keccak and secp256k1 (#103)
1 parent 7b9eb6b commit 9a6f151

File tree

6 files changed

+138
-2
lines changed

6 files changed

+138
-2
lines changed

Anoma/Builtin/ByteArray.juvix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ module ByteArray;
3434
builtin anoma-bytearray-to-anoma-contents
3535
axiom rawToAnomaContents : ByteArray -> Nat;
3636

37+
toNat : ByteArray -> Nat := toAnomaContents >> AnomaAtom.toNat;
38+
3739
toAnomaContents : ByteArray -> AnomaAtom :=
3840
rawToAnomaContents >> AnomaAtom.fromNat;
3941

Applib.juvix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ import Applib.Helpers open public;
1717
import Applib.Identities open public;
1818
import Applib.Authorization as Authorization public;
1919
import Applib.Trait.Tx open public;
20+
import Applib.Keccak256 open public;
21+
import Applib.Secp256k1 open public;

Applib/Keccak256.juvix

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Applib.Keccak256;
2+
3+
import Stdlib.Prelude open;
4+
import Anoma.Builtin.ByteArray open;
5+
import BaseLayer.AnomaAtom open;
6+
7+
module Keccak;
8+
hash : Nat -> ByteArray :=
9+
Private.primKeccak >> AnomaAtom.fromNat >> ByteArray.fromAnomaContents size;
10+
11+
size : Nat := 32;
12+
13+
module Private;
14+
builtin keccak256
15+
axiom primKeccak : Nat -> Nat;
16+
end;
17+
end;

Applib/Secp256k1.juvix

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
module Applib.Secp256k1;
2+
3+
import Stdlib.Prelude open;
4+
import Anoma.Builtin.ByteArray open;
5+
import BaseLayer.AnomaAtom open;
6+
import Anoma.Primitives.FixedSize open;
7+
import Anoma.Identity.Internal open;
8+
9+
--- Module for the Secp256k1 elliptic curve.
10+
---
11+
--- 1. To obtain a private key:
12+
--- priv := ;Secp256k1.PrivateKey.fromInternalIdentity; someIdentity
13+
--- 2. To obtain a public key:
14+
--- pub := ;Secp256k1.PublicKey.mk; priv
15+
--- 3. To sign a message:
16+
--- signature := ;sign; myMessage priv
17+
--- 4. To verify a message
18+
--- ;verify; myMessage signature pub
19+
module Secp256k1;
20+
type PrivateKey := privateMk ByteArray
21+
with
22+
--- Create a ;PrivateKey; from an ;InternalIdentity;
23+
mk : InternalIdentity -> PrivateKey := fromInternalIdentity;
24+
25+
fromInternalIdentity : InternalIdentity -> PrivateKey :=
26+
InternalIdentity.toByteArray >> fromByteArray;
27+
28+
toNat : PrivateKey -> Nat :=
29+
toByteArray >> ByteArray.toAnomaContents >> AnomaAtom.toNat;
30+
31+
fromByteArray : ByteArray -> PrivateKey := privateMk;
32+
33+
toByteArray : PrivateKey -> ByteArray
34+
| (privateMk b) := b;
35+
36+
size : Nat := 32;
37+
38+
instance
39+
PrivateKey-FixedSize : FixedSize PrivateKey := FixedSize.mk size;
40+
end;
41+
42+
type PublicKey := privateMk ByteArray
43+
with
44+
--- This is the main way to generate public keys.
45+
mk : PrivateKey -> PublicKey := fromPrivateKey;
46+
47+
--- Generate a ;PublicKey; from a ;PrivateKey;
48+
fromPrivateKey : PrivateKey -> PublicKey :=
49+
PrivateKey.toNat >> Private.pubKey >> fromNat;
50+
51+
toByteArray : PublicKey -> ByteArray
52+
| (privateMk b) := b;
53+
54+
fromNat : Nat -> PublicKey := AnomaAtom.fromNat >> fromAnomaAtom;
55+
56+
toNat : PublicKey -> Nat := toByteArray >> ByteArray.toNat;
57+
58+
instance
59+
PublicteKey-FixedSize : FixedSize PublicKey := FixedSize.mk size;
60+
61+
fromAnomaAtom : AnomaAtom -> PublicKey :=
62+
ByteArray.fromAnomaContents size >> privateMk;
63+
64+
--- The size of the produced public keys is always sixty-five bytes
65+
size : Nat := 65;
66+
end;
67+
68+
--- See ;sign; and ;verify;
69+
type Signature := privateMk ByteArray
70+
with
71+
instance
72+
PublicteKey-FixedSize : FixedSize Signature := FixedSize.mk size;
73+
74+
fromAnomaAtom : AnomaAtom -> Signature :=
75+
ByteArray.fromAnomaContents size >> privateMk;
76+
77+
fromNat : Nat -> Signature := AnomaAtom.fromNat >> fromAnomaAtom;
78+
79+
toByeArray : Signature -> ByteArray
80+
| (privateMk b) := b;
81+
82+
toNat : Signature -> Nat := toByeArray >> ByteArray.toNat;
83+
84+
size : Nat := 64;
85+
end;
86+
87+
sign (msg : AnomaAtom) (priv : PrivateKey) : Signature :=
88+
Private.sign (AnomaAtom.toNat msg) (PrivateKey.toNat priv)
89+
|> Private.PrimitiveSignature.bin
90+
|> Signature.fromNat;
91+
92+
verify (msg : AnomaAtom) (sig : Signature) (pub : PublicKey) : Bool :=
93+
Private.verify
94+
(AnomaAtom.toNat msg)
95+
(Signature.toNat sig)
96+
(PublicKey.toNat pub);
97+
98+
module Private;
99+
builtin secp256k1-public-key
100+
axiom pubKey (privKey : Nat) : Nat;
101+
102+
type PrimitiveSignature :=
103+
mk@{
104+
bin : Nat;
105+
--- currently never used
106+
recoveryId : Nat;
107+
};
108+
109+
builtin secp256k1-sign-compact
110+
axiom sign (msg privKey : Nat) : PrimitiveSignature;
111+
112+
builtin secp256k1-verify
113+
axiom verify (msg signature pubKey : Nat) : Bool;
114+
end;
115+
end;

Package.juvix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package : Package :=
99
mkVersion@{
1010
major := 0;
1111
minor := 13;
12-
patch := 0;
12+
patch := 1;
1313
};
1414
dependencies :=
1515
[

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: 7a1c0a81eaeccccd15bd8132ec0eb5238f610b3aec8ac13993ab49331c8d3d0e
5+
checksum: f2d0e43e920616ddf14f28e97493c325e76af638d0cc02f812494349edde3917
66
dependencies:
77
- git:
88
name: anoma_juvix-stdlib

0 commit comments

Comments
 (0)