|
| 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; |
0 commit comments