morley-1.16.1: Developer tools for the Michelson Language
Safe HaskellNone
LanguageHaskell2010

Morley.Tezos.Crypto

Description

Cryptographic primitives used in Tezos.

WARNING: some functions may be vulnerable to timing attacks. Also, this code was not reviewed by cryptography/security experts. Do not use it with secret keys that have some value. We provide SecretKey type and (limited) signing functionality only for testing. If you need to sign something in production, use something else (e. g. `tezos-client`).

Tezos supports 3 cryptographic curves that are denoted by the number after tz in the public key hash: tz1, tz2 or tz3. • tz1 — ed25519 • tz2 — secp256k1 • tz3 — P256 We have Morley.Tezos.Crypto.Curve module for each of these curves. They expose very similar functionality and their main purpose is to hide implementation details for each curve as well as some other specifics (e. g. prefixes that are used for human-readable representation).

This module serves two purposes: 1. It is an umbrella module that re-exports some stuff from other modules. 2. Michelson types such as key and signature may store primitive of any curve, so we need "union" types in Haskell as well.

During conversion to human-readable representation usually some magical prefix is used. They have been found in source code in some repos (e. g. https://gitlab.com/tezos/tezos/blob/c52ee69231c5ae4d9cec1f3c8aba0c3573922e2a/src/lib_crypto/base58.ml) and checked manually. Existing tests confirm they are correct.

Synopsis

Cryptographic primitive types

data PublicKey Source #

Public cryptographic key used by Tezos. There are three cryptographic curves each represented by its own constructor.

Constructors

PublicKeyEd25519 PublicKey

Public key that uses the ed25519 cryptographic curve.

PublicKeySecp256k1 PublicKey

Public key that uses the secp256k1 cryptographic curve.

PublicKeyP256 PublicKey

Public key that uses the NIST P-256 cryptographic curve.

Instances

Instances details
Eq PublicKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

Ord PublicKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

Show PublicKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

Generic PublicKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

Associated Types

type Rep PublicKey :: Type -> Type #

ToJSON PublicKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

FromJSON PublicKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

NFData PublicKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

Methods

rnf :: PublicKey -> () #

Buildable PublicKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

Methods

build :: PublicKey -> Builder #

IsoValue PublicKey Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

Associated Types

type ToT PublicKey :: T Source #

TypeHasDoc PublicKey Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

type Rep PublicKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

type ToT PublicKey Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

type TypeDocFieldDescriptions PublicKey Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

data SecretKey Source #

Secret cryptographic key used by Tezos. Constructors correspond to PublicKey constructors.

Constructors

SecretKeyEd25519 SecretKey

Secret key that uses the ed25519 cryptographic curve.

SecretKeySecp256k1 SecretKey

Secret key that uses the secp256k1 cryptographic curve.

SecretKeyP256 SecretKey

Secret key that uses the NIST P-256 cryptographic curve.

Instances

Instances details
Eq SecretKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

Show SecretKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

Generic SecretKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

Associated Types

type Rep SecretKey :: Type -> Type #

NFData SecretKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

Methods

rnf :: SecretKey -> () #

Buildable SecretKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

Methods

build :: SecretKey -> Builder #

HasCLReader SecretKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

type Rep SecretKey Source # 
Instance details

Defined in Morley.Tezos.Crypto

data Signature Source #

Cryptographic signatures used by Tezos. Constructors correspond to PublicKey constructors.

Tezos distinguishes signatures for different curves. For instance, ed25519 signatures and secp256k1 signatures are printed differently (have different prefix). However, signatures are packed without information about the curve. For this purpose there is a generic signature which only stores bytes and doesn't carry information about the curve. Apparently unpacking from bytes always produces such signature. Unpacking from string produces a signature with curve information.

Constructors

SignatureEd25519 Signature

Signature that uses the ed25519 cryptographic curve.

SignatureSecp256k1 Signature

Siganture that uses the secp256k1 cryptographic curve.

SignatureP256 Signature

Signature that uses the NIST P-256 cryptographic curve.

SignatureGeneric ByteString

Generic signature for which curve is unknown.

Instances

Instances details
Eq Signature Source # 
Instance details

Defined in Morley.Tezos.Crypto

Ord Signature Source # 
Instance details

Defined in Morley.Tezos.Crypto

Show Signature Source # 
Instance details

Defined in Morley.Tezos.Crypto

Generic Signature Source # 
Instance details

Defined in Morley.Tezos.Crypto

Associated Types

type Rep Signature :: Type -> Type #

ToJSON Signature Source # 
Instance details

Defined in Morley.Tezos.Crypto

FromJSON Signature Source # 
Instance details

Defined in Morley.Tezos.Crypto

NFData Signature Source # 
Instance details

Defined in Morley.Tezos.Crypto

Methods

rnf :: Signature -> () #

Buildable Signature Source # 
Instance details

Defined in Morley.Tezos.Crypto

Methods

build :: Signature -> Builder #

IsoValue Signature Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

Associated Types

type ToT Signature :: T Source #

TypeHasDoc Signature Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

type Rep Signature Source # 
Instance details

Defined in Morley.Tezos.Crypto

type ToT Signature Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

type TypeDocFieldDescriptions Signature Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

data KeyHashTag Source #

Which curve was used for the hashed public key inside KeyHash.

Instances

Instances details
Bounded KeyHashTag Source # 
Instance details

Defined in Morley.Tezos.Crypto

Enum KeyHashTag Source # 
Instance details

Defined in Morley.Tezos.Crypto

Eq KeyHashTag Source # 
Instance details

Defined in Morley.Tezos.Crypto

Ord KeyHashTag Source # 
Instance details

Defined in Morley.Tezos.Crypto

Show KeyHashTag Source # 
Instance details

Defined in Morley.Tezos.Crypto

Generic KeyHashTag Source # 
Instance details

Defined in Morley.Tezos.Crypto

Associated Types

type Rep KeyHashTag :: Type -> Type #

NFData KeyHashTag Source # 
Instance details

Defined in Morley.Tezos.Crypto

Methods

rnf :: KeyHashTag -> () #

Lift KeyHashTag Source # 
Instance details

Defined in Morley.Tezos.Crypto

type Rep KeyHashTag Source # 
Instance details

Defined in Morley.Tezos.Crypto

type Rep KeyHashTag = D1 ('MetaData "KeyHashTag" "Morley.Tezos.Crypto" "morley-1.16.1-inplace" 'False) (C1 ('MetaCons "KeyHashEd25519" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "KeyHashSecp256k1" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "KeyHashP256" 'PrefixI 'False) (U1 :: Type -> Type)))

data KeyHash Source #

Blake2b_160 hash of a public key.

Constructors

KeyHash 

Fields

Instances

Instances details
Eq KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

Methods

(==) :: KeyHash -> KeyHash -> Bool #

(/=) :: KeyHash -> KeyHash -> Bool #

Ord KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

Show KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

Generic KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

Associated Types

type Rep KeyHash :: Type -> Type #

Methods

from :: KeyHash -> Rep KeyHash x #

to :: Rep KeyHash x -> KeyHash #

ToJSON KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

ToJSONKey KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

FromJSON KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

FromJSONKey KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

NFData KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

Methods

rnf :: KeyHash -> () #

Buildable KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

Methods

build :: KeyHash -> Builder #

HasCLReader KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

IsoValue KeyHash Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

Associated Types

type ToT KeyHash :: T Source #

TypeHasDoc KeyHash Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

Lift KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

Methods

lift :: KeyHash -> Q Exp #

liftTyped :: KeyHash -> Q (TExp KeyHash) #

type Rep KeyHash Source # 
Instance details

Defined in Morley.Tezos.Crypto

type Rep KeyHash = D1 ('MetaData "KeyHash" "Morley.Tezos.Crypto" "morley-1.16.1-inplace" 'False) (C1 ('MetaCons "KeyHash" 'PrefixI 'True) (S1 ('MetaSel ('Just "khTag") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 KeyHashTag) :*: S1 ('MetaSel ('Just "khBytes") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 ByteString)))
type ToT KeyHash Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

type TypeDocFieldDescriptions KeyHash Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

data Bls12381Fr Source #

An element of an algebraic number field (scalar), used for multiplying Bls12381G1 and Bls12381G2.

Instances

Instances details
Bounded Bls12381Fr Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Enum Bls12381Fr Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Eq Bls12381Fr Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Fractional Bls12381Fr Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Integral Bls12381Fr Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Num Bls12381Fr Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Ord Bls12381Fr Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Real Bls12381Fr Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Show Bls12381Fr Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

NFData Bls12381Fr Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Methods

rnf :: Bls12381Fr -> () #

CurveObject Bls12381Fr Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

IsoValue Bls12381Fr Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

Associated Types

type ToT Bls12381Fr :: T Source #

MultiplyPoint Bls12381Fr Bls12381G2 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

MultiplyPoint Bls12381Fr Bls12381G1 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

type ToT Bls12381Fr Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

data Bls12381G1 Source #

G1 point on the curve.

Instances

Instances details
Eq Bls12381G1 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Show Bls12381G1 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

NFData Bls12381G1 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Methods

rnf :: Bls12381G1 -> () #

CurveObject Bls12381G1 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

IsoValue Bls12381G1 Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

Associated Types

type ToT Bls12381G1 :: T Source #

MultiplyPoint Integer Bls12381G1 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

MultiplyPoint Bls12381Fr Bls12381G1 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

type ToT Bls12381G1 Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

data Bls12381G2 Source #

G2 point on the curve.

Instances

Instances details
Eq Bls12381G2 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Show Bls12381G2 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

NFData Bls12381G2 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

Methods

rnf :: Bls12381G2 -> () #

CurveObject Bls12381G2 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

IsoValue Bls12381G2 Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

Associated Types

type ToT Bls12381G2 :: T Source #

MultiplyPoint Integer Bls12381G2 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

MultiplyPoint Bls12381Fr Bls12381G2 Source # 
Instance details

Defined in Morley.Tezos.Crypto.BLS12381

type ToT Bls12381G2 Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

Public/secret key functions

detSecretKey :: HasCallStack => ByteString -> SecretKey Source #

Deterministicaly generate a secret key from seed. Type of the key depends on seed length.

toPublic :: SecretKey -> PublicKey Source #

Create a public key from a secret key.

Signature

signatureToBytes :: ByteArray ba => Signature -> ba Source #

Convert a Signature to raw bytes.

mkSignature :: ByteArray ba => ba -> Maybe Signature Source #

Make a Signature from raw bytes. Can return only generic signature.

parseSignatureRaw :: ByteString -> Either ParseSignatureRawError Signature Source #

checkSignature :: PublicKey -> Signature -> ByteString -> Bool Source #

Check that a sequence of bytes has been signed with a given key.

Formatting

keyHashLengthBytes :: Integral n => n Source #

Length of key hash in bytes (only hash itself, no tags, checksums or anything).

parseSecretKey :: Text -> Either CryptoParseError SecretKey Source #

Parse unencrypted secret key. It accepts formats containing either with or without the unecrypted prefix.

Hashing

hashKey :: PublicKey -> KeyHash Source #

Compute the b58check of a public key hash.

blake2b :: ByteString -> ByteString Source #

Compute a cryptographic hash of a bytestring using the Blake2b_256 cryptographic hash function. It's used by the BLAKE2B instruction in Michelson.

blake2b160 :: ByteString -> ByteString Source #

Compute a cryptographic hash of a bytestring using the Blake2b_160 cryptographic hash function.

keccak :: ByteString -> ByteString Source #

Compute a cryptographic hash of a bytestring using the Keccak_256 cryptographic hash function. It is used by the KECCAK Michelson instruction.

sha256 :: ByteString -> ByteString Source #

Compute a cryptographic hash of a bytestring using the Sha256 cryptographic hash function.

sha3 :: ByteString -> ByteString Source #

Compute a cryptographic hash of a bytestring using the Sha3_256 cryptographic hash function. It is used by the SHA3 Michelson instruction.

sha512 :: ByteString -> ByteString Source #

Compute a cryptographic hash of a bytestring using the Sha512 cryptographic hash function.

Timelock puzzle

data Chest Source #

A locked chest

Instances

Instances details
Eq Chest Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Methods

(==) :: Chest -> Chest -> Bool #

(/=) :: Chest -> Chest -> Bool #

Show Chest Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Methods

showsPrec :: Int -> Chest -> ShowS #

show :: Chest -> String #

showList :: [Chest] -> ShowS #

Generic Chest Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Associated Types

type Rep Chest :: Type -> Type #

Methods

from :: Chest -> Rep Chest x #

to :: Rep Chest x -> Chest #

Binary Chest Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Methods

put :: Chest -> Put #

get :: Get Chest #

putList :: [Chest] -> Put #

NFData Chest Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Methods

rnf :: Chest -> () #

IsoValue Chest Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

Associated Types

type ToT Chest :: T Source #

TypeHasDoc Chest Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

type Rep Chest Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

type Rep Chest
type ToT Chest Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

type ToT Chest = 'TChest
type TypeDocFieldDescriptions Chest Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

data ChestKey Source #

A chest "key" with proof that it was indeed opened fairly.

Instances

Instances details
Eq ChestKey Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Show ChestKey Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Generic ChestKey Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Associated Types

type Rep ChestKey :: Type -> Type #

Methods

from :: ChestKey -> Rep ChestKey x #

to :: Rep ChestKey x -> ChestKey #

Binary ChestKey Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Methods

put :: ChestKey -> Put #

get :: Get ChestKey #

putList :: [ChestKey] -> Put #

NFData ChestKey Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Methods

rnf :: ChestKey -> () #

IsoValue ChestKey Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

Associated Types

type ToT ChestKey :: T Source #

TypeHasDoc ChestKey Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

type Rep ChestKey Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

type ToT ChestKey Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

type TypeDocFieldDescriptions ChestKey Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

data OpeningResult Source #

The result of opening the chest.

Constructors

Correct ByteString

The chest was opened correctly.

BogusCipher

The chest was opened correctly, but the contents do not decode with the given symmetric key.

BogusOpening

The chest was not opened correctly, i.e. proof verification failed.

newtype TLTime Source #

Number of steps a timelock needs to be opened without knowing a "secret", i.e. modulo factorization.

The reference implementation uses OCaml int, and it can only be positive, so on 64-bit architecture it's actually a 62-bit natural. We use Word64 to represent it.

Constructors

UnsafeTLTime 

Fields

Bundled Patterns

pattern TLTime :: Word64 -> TLTime 

Instances

Instances details
Bounded TLTime Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Eq TLTime Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Methods

(==) :: TLTime -> TLTime -> Bool #

(/=) :: TLTime -> TLTime -> Bool #

Num TLTime Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Show TLTime Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Buildable TLTime Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Methods

build :: TLTime -> Builder #

HasCLReader TLTime Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

openChest :: Chest -> ChestKey -> TLTime -> OpeningResult Source #

Try to (quickly) open a chest with the given key, verifying the proof.

mkTLTime :: (Integral i, Bits i) => i -> Either Text TLTime Source #

Safely creates TLTime checking for overflow and underflow. Accepts a number of any type.

unsafeMkTLTime :: HasCallStack => Word64 -> TLTime Source #

Partial function for TLTime creation, it's pre-condition is that the argument must not exceed bounds of the TLTime.

Utilities

encodeBase58Check :: ByteString -> Text Source #

Encode a bytestring in Base58Check format.

decodeBase58Check :: Text -> Maybe ByteString Source #

Decode a bytestring from Base58Check format.

decodeBase58CheckWithPrefix :: ByteString -> Text -> Either B58CheckWithPrefixError ByteString Source #

Parse a base58check encoded value expecting some prefix. If the actual prefix matches the expected one, it's stripped of and the resulting payload is returned.