morley-1.20.0: Developer tools for the Michelson Language
Safe HaskellSafe-Inferred
LanguageHaskell2010

Morley.Tezos.Crypto.Timelock

Description

Timelock puzzle algorithms implementation.

WARNING: the timelock mechanism described and implemented here is vulnerable. At the time of writing, no details were released, but creation of smart contracts using this functionality is disabled since Lima.

This module follows the reference implementation for the most part, which you can find in the tezos repository.

For a more high-level overview of the concepts, refer to the timelock documentation page.

The general idea is built upon Rivest, Shamir, Wagner "Time-lock puzzles and timed-release Crypto", there are however some differences from the paper:

  • The paper suggests using RC5 cipher, which Tezos implementation eschews in favor of NaCl's "secret box".
  • The paper suggest generating the symmetric secret key \(K\) directly, then encrypting it with a randomly chosen value \(a\) as \(C_K = K + a^{2^t} \pmod n\). Tezos implementation instead randomly generates only \(a\), and then produces the secret key using BLAKE2b KDF with a fixed key from \(a^{2^t} \pmod n\).
  • Since the secret key is determined only by the "unlocked" value, the time-locked value representation also differs. In the paper it's represented as ((n,a,t,C_K,C_M)), i.e. the tuple of modulus, "locked" value, time, encrypted key and encrypted message. In Tezos implementation it's instead ((a,n,C_M)), and \(t\) is treated as a separate argument.
  • Likely to guard the protocol from guessing attacks, additional "proof" verification is added, described in Boneh, Bünz, Fisch "A Survey of Two Verifiable Delay Functions"
Synopsis

Documentation

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 Word62 to represent it.

The constructor is marked Unsafe since GHC does not warn on overflowing literals (exceeding custom Word62 type bounds), thus the resultant TLTime value may get truncated silently.

>>> UnsafeTLTime 4611686018427387906
UnsafeTLTime {unTLTime = 2}

Constructors

UnsafeTLTime 

Fields

Bundled Patterns

pattern TLTime :: Word62 -> TLTime 

Instances

Instances details
Bounded TLTime Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Show 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 #

HasCLReader TLTime Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Buildable TLTime Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Methods

build :: TLTime -> Doc

buildList :: [TLTime] -> Doc

data Chest Source #

A locked chest

Constructors

Chest 

Fields

Instances

Instances details
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 #

Show Chest Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Methods

showsPrec :: Int -> Chest -> ShowS #

show :: Chest -> String #

showList :: [Chest] -> ShowS #

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 -> () #

Eq Chest Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Methods

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

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

HasRPCRepr Chest Source # 
Instance details

Defined in Morley.AsRPC

Associated Types

type AsRPC Chest Source #

TypeHasDoc Chest Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

IsoValue Chest Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

Associated Types

type ToT Chest :: T Source #

type Rep Chest Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

type Rep Chest
type AsRPC Chest Source # 
Instance details

Defined in Morley.AsRPC

type TypeDocFieldDescriptions Chest Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

type ToT Chest Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

type ToT Chest = 'TChest

data ChestKey Source #

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

Constructors

ChestKey 

Fields

Instances

Instances details
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 #

Show ChestKey Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

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 -> () #

Eq ChestKey Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

HasRPCRepr ChestKey Source # 
Instance details

Defined in Morley.AsRPC

Associated Types

type AsRPC ChestKey Source #

TypeHasDoc ChestKey Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

IsoValue ChestKey Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

Associated Types

type ToT ChestKey :: T Source #

type Rep ChestKey Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

type AsRPC ChestKey Source # 
Instance details

Defined in Morley.AsRPC

type TypeDocFieldDescriptions ChestKey Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Doc

type ToT ChestKey Source # 
Instance details

Defined in Morley.Michelson.Typed.Haskell.Value

data Ciphertext Source #

Ciphertext with nonce.

Constructors

Ciphertext 

Fields

Instances

Instances details
Generic Ciphertext Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Associated Types

type Rep Ciphertext :: Type -> Type #

Show Ciphertext Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Binary Ciphertext Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

NFData Ciphertext Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

Methods

rnf :: Ciphertext -> () #

Eq Ciphertext Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

type Rep Ciphertext Source # 
Instance details

Defined in Morley.Tezos.Crypto.Timelock

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.

createChestAndChestKey Source #

Arguments

:: ByteString

Chest content

-> TLTime

Time (in elementary actions) to open without key.

-> IO (Chest, ChestKey) 

Create a timelock puzzle and a key.

createChestKey :: Chest -> TLTime -> ChestKey Source #

Forge a chest key the hard way.

chestBytes :: Chest -> ByteString Source #

Convert a Chest to binary representation, used by Tezos

chestKeyBytes :: ChestKey -> ByteString Source #

Convert a ChestKey to binary representation, used by Tezos

chestFromBytes :: ByteString -> Either Text Chest Source #

Read a Chest from binary representation, used by Tezos

chestKeyFromBytes :: ByteString -> Either Text ChestKey Source #

Read a ChestKey from binary representation, used by Tezos

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

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

mkTLTime :: Integral i => i -> Either Text TLTime Source #

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

toTLTime :: (Integral a, CheckIntSubType a Word62) => a -> TLTime Source #

Safely creates TLTime.

This is the recommended way to create TLTime values.

When constructing literals, you'll need to specify the type of the literal. Bear in mind that GHC will check for literal overflow on builtin types like Word16 and Word32, but not on Word62, so be aware that toTLTime from Word62 will overflow silently. Prefer using builtin types when possible.

>>> unTLTime $ toTLTime (4611686018427387903 :: Word62)
4611686018427387903
>>> unTLTime $ toTLTime (4611686018427387904 :: Word62)
0

Internal, not safe for cryptography

createChestAndChestKeyFromSeed Source #

Arguments

:: Int

Pseudo-random seed

-> ByteString

Chest content

-> TLTime

TLTime (in elementary actions) to open without key.

-> (Chest, ChestKey) 

Construct a chest purely based on a seed for pseudorandom generator. This is not suitable for cryptography, used in tests.