2020-01-31 15:34:36 +04:00
---
id: crypto-reference
2020-03-17 19:38:41 +04:00
title: Crypto
description: Cryptographic operations
hide_table_of_contents: true
2020-01-31 15:34:36 +04:00
---
2020-03-04 17:19:00 +04:00
import Syntax from '@theme/Syntax';
2020-03-17 19:38:41 +04:00
import SyntaxTitle from '@theme/SyntaxTitle';
< SyntaxTitle syntax = "pascaligo" >
type key
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
type key
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
type key
< / SyntaxTitle >
A public cryptographic key.
< SyntaxTitle syntax = "pascaligo" >
type key_hash
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
type key_hash
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
type key_hash
< / SyntaxTitle >
The hash of a public cryptographic key.
< SyntaxTitle syntax = "pascaligo" >
type signature
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
type signature
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
type signature
< / SyntaxTitle >
A cryptographic signature.
< SyntaxTitle syntax = "pascaligo" >
function blake2b : bytes -> bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
val blake2b : bytes -> bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
let blake2b: bytes => bytes
< / SyntaxTitle >
2020-01-31 15:34:36 +04:00
Runs the [blake2b hash algorithm ](https://en.wikipedia.org/wiki/BLAKE_(hash_function )#BLAKE2 )
over the given `bytes` data and returns a `bytes` representing the hash.
2020-03-04 17:19:00 +04:00
< Syntax syntax = "pascaligo" >
2020-01-31 15:34:36 +04:00
```pascaligo
2020-03-17 19:38:41 +04:00
function hasherman_blake (const s: bytes) : bytes is Crypto.blake2b(s)
2020-01-31 15:34:36 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `blake2b` is *deprecated*. Please use `Crypto.blake2b`.
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "cameligo" >
2020-01-31 15:34:36 +04:00
```cameligo
let hasherman_blake (s: bytes) : bytes = Crypto.blake2b s
```
2020-03-17 19:38:41 +04:00
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "reasonligo" >
2020-01-31 15:34:36 +04:00
```reasonligo
let hasherman_blake = (s: bytes) => Crypto.blake2b(s);
```
2020-03-04 17:19:00 +04:00
< / Syntax >
2020-03-17 19:38:41 +04:00
< SyntaxTitle syntax = "pascaligo" >
function sha256 : bytes -> bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
val sha256 : bytes -> bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
let sha256: bytes => bytes
< / SyntaxTitle >
2020-01-31 15:34:36 +04:00
Runs the [sha256 hash algorithm ](https://en.wikipedia.org/wiki/SHA-2 ) over the given
`bytes` data and returns a `bytes` representing the hash.
2020-03-04 17:19:00 +04:00
< Syntax syntax = "pascaligo" >
2020-01-31 15:34:36 +04:00
```pascaligo
2020-03-17 19:38:41 +04:00
function hasherman (const s : bytes) : bytes is Crypto.sha256(s)
2020-01-31 15:34:36 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `sha_256` is *deprecated*. Please use `Crypto.sha256`.
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "cameligo" >
2020-01-31 15:34:36 +04:00
```cameligo
let hasherman (s : bytes) : bytes =
Crypto.sha256 s
```
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "reasonligo" >
2020-01-31 15:34:36 +04:00
```reasonligo
let hasherman = (s: bytes): bytes => Crypto.sha256(s);
```
2020-03-04 17:19:00 +04:00
< / Syntax >
2020-03-17 19:38:41 +04:00
< SyntaxTitle syntax = "pascaligo" >
function sha512 : bytes -> bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
val sha512 : bytes -> bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
let sha512: bytes => bytes
< / SyntaxTitle >
2020-01-31 15:34:36 +04:00
Runs the [sha512 hash algorithm ](https://en.wikipedia.org/wiki/SHA-2 ) over the given
`bytes` data and returns a `bytes` representing the hash.
2020-03-04 17:19:00 +04:00
< Syntax syntax = "pascaligo" >
2020-01-31 15:34:36 +04:00
```pascaligo
2020-03-17 19:38:41 +04:00
function hasherman512 (const s: bytes) : bytes is Crypto.sha512(s)
2020-01-31 15:34:36 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `sha_512` is *deprecated*. Please use `Crypto.sha512`.
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "cameligo" >
2020-01-31 15:34:36 +04:00
```cameligo
let hasherman512 (s: bytes) : bytes = Crypto.sha512 s
```
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "reasonligo" >
2020-01-31 15:34:36 +04:00
```reasonligo
let hasherman512 = (s: bytes) => Crypto.sha512(s);
```
2020-03-04 17:19:00 +04:00
< / Syntax >
2020-03-17 19:38:41 +04:00
< SyntaxTitle syntax = "pascaligo" >
function hash_key : key -> key_hash
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
val hash_key : key -> key_hash
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
let hash_key: key => key_hash
< / SyntaxTitle >
2020-01-31 15:34:36 +04:00
Hashes a key for easy comparison and storage.
2020-03-04 17:19:00 +04:00
< Syntax syntax = "pascaligo" >
2020-01-31 15:34:36 +04:00
```pascaligo
function check_hash_key (const kh1 : key_hash; const k2 : key) : bool * key_hash is block {
var ret : bool := False ;
2020-03-17 19:38:41 +04:00
var kh2 : key_hash := Crypto.hash_key(k2) ;
2020-01-31 15:34:36 +04:00
if kh1 = kh2 then ret := True else skip;
} with (ret, kh2)
```
2020-03-17 19:38:41 +04:00
> Note that `hash_key` is *deprecated*. Please use `Crypto.hash_key`.
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "cameligo" >
2020-01-31 15:34:36 +04:00
```cameligo
let check_hash_key (kh1, k2: key_hash * key) : bool * key_hash =
let kh2 : key_hash = Crypto.hash_key k2 in
if kh1 = kh2
then (true, kh2)
else (false, kh2)
```
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "reasonligo" >
2020-01-31 15:34:36 +04:00
```reasonligo
let check_hash_key = ((kh1, k2): (key_hash, key)) : (bool, key_hash) => {
let kh2 : key_hash = Crypto.hash_key(k2);
if (kh1 == kh2) {
(true, kh2);
}
else {
(false, kh2);
}
};
```
2020-03-04 17:19:00 +04:00
< / Syntax >
2020-03-17 19:38:41 +04:00
< SyntaxTitle syntax = "pascaligo" >
function check : key -> signature -> bytes -> bool
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
val check : key -> signature -> bytes -> bool
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
let check: (key, signature, bytes) => bool
< / SyntaxTitle >
2020-01-31 15:34:36 +04:00
Check that a message has been signed by a particular key.
> ⚠️ There is no way to *generate* a signed message in LIGO. This is because that would require storing a private key on chain, at which point it isn't very private anymore.
2020-03-04 17:19:00 +04:00
< Syntax syntax = "pascaligo" >
2020-01-31 15:34:36 +04:00
```pascaligo
function check_signature
(const pk: key;
const signed: signature;
const msg: bytes) : bool
2020-03-17 19:38:41 +04:00
is Crypto.check(pk, signed, msg)
2020-01-31 15:34:36 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `crypto_check` is *deprecated*. Please use `Crypto.check`.
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "cameligo" >
2020-01-31 15:34:36 +04:00
```cameligo
let check_signature (pk, signed, msg: key * signature * bytes) : bool =
Crypto.check pk signed msg
```
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "reasonligo" >
2020-01-31 15:34:36 +04:00
```reasonligo
let check_signature = ((pk, signed, msg): (key, signature, bytes)) : bool => {
Crypto.check(pk, signed, msg);
};
```
2020-03-04 17:19:00 +04:00
< / Syntax >