-
Notifications
You must be signed in to change notification settings - Fork 51
/
Schnorr.purs
52 lines (45 loc) · 1.78 KB
/
Schnorr.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- | A module that implements crypto primitives that match CIP-49 SECP256k1
-- | Schnorr spec.
module Contract.Crypto.Secp256k1.Schnorr
( module X
, verifySchnorrSecp256k1Signature
, signSchnorrSecp256k1
, deriveSchnorrSecp256k1PublicKey
, mkSchnorrPublicKey
, unSchnorrPublicKey
) where
import Prelude
import Contract.Crypto.Secp256k1 (Secp256k1PrivateKey)
import Data.ByteArray (ByteArray)
import Data.Maybe (Maybe)
import Data.Newtype (unwrap)
import Effect.Aff (Aff)
import Noble.Secp256k1.Schnorr (SchnorrPublicKey, SchnorrSignature) as X
import Noble.Secp256k1.Schnorr
( SchnorrPublicKey
, SchnorrSignature
, getSchnorrPublicKey
, signSchnorr
, verifySchnorr
)
import Noble.Secp256k1.Schnorr (mkSchnorrPublicKey, unSchnorrPublicKey) as ECDSA
-- | Verify arbitrary binary messages signed using the Schnorr signature scheme
-- | on the Secp256k1 curve.
-- | Matches CIP-49 spec:
-- | https://github.com/cardano-foundation/CIPs/blob/master/CIP-0049/README.md
verifySchnorrSecp256k1Signature
:: SchnorrPublicKey -> ByteArray -> SchnorrSignature -> Aff Boolean
verifySchnorrSecp256k1Signature publicKey message signature =
verifySchnorr signature message publicKey
-- | Sign a message using Schnorr signature scheme.
signSchnorrSecp256k1 :: Secp256k1PrivateKey -> ByteArray -> Aff SchnorrSignature
signSchnorrSecp256k1 privateKey message =
signSchnorr message (unwrap privateKey)
deriveSchnorrSecp256k1PublicKey :: Secp256k1PrivateKey -> SchnorrPublicKey
deriveSchnorrSecp256k1PublicKey = unwrap >>> getSchnorrPublicKey
-- | Construct a public key from its byte representation.
mkSchnorrPublicKey
:: ByteArray -> Maybe SchnorrPublicKey
mkSchnorrPublicKey = ECDSA.mkSchnorrPublicKey
unSchnorrPublicKey :: SchnorrPublicKey -> ByteArray
unSchnorrPublicKey = ECDSA.unSchnorrPublicKey