This commit is contained in:
Sander Spies 2020-01-31 16:06:47 +01:00
commit ff16af9331
7 changed files with 214 additions and 4 deletions

View File

@ -2,6 +2,10 @@
## [Unreleased] ## [Unreleased]
## [Add crypto reference page to docs](https://gitlab.com/ligolang/ligo/-/merge_requests/370)
### Changed
- Corrected typo in CameLIGO/ReasonLIGO front end where Crypto.blake2b was 'Crypto.black2b'
## [Failwith do not fail](https://gitlab.com/ligolang/ligo/merge_requests/337) - 2020-01-17 ## [Failwith do not fail](https://gitlab.com/ligolang/ligo/merge_requests/337) - 2020-01-17
### Added ### Added
- running failing code in `ligo interpret`, `ligo dry-run`, `ligo run-function` will no longer be an error (return value : 0) - running failing code in `ligo interpret`, `ligo dry-run`, `ligo run-function` will no longer be an error (return value : 0)

View File

@ -0,0 +1,155 @@
---
id: crypto-reference
title: Crypto
---
## Crypto.blake2b(data: bytes): bytes
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.
<!--DOCUSAURUS_CODE_TABS-->
<!--PascaLIGO-->
```pascaligo
function hasherman_blake (const s: bytes) : bytes is blake2b(s)
```
<!--CameLIGO-->
```cameligo
let hasherman_blake (s: bytes) : bytes = Crypto.blake2b s
```
<!--ReasonLIGO-->
```reasonligo
let hasherman_blake = (s: bytes) => Crypto.blake2b(s);
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Crypto.sha256(data: bytes) : bytes
Runs the [sha256 hash algorithm](https://en.wikipedia.org/wiki/SHA-2) over the given
`bytes` data and returns a `bytes` representing the hash.
<!--DOCUSAURUS_CODE_TABS-->
<!--PascaLIGO-->
```pascaligo
function hasherman (const s : bytes) : bytes is
begin skip end with sha_256(s)
```
<!--CameLIGO-->
```cameligo
let hasherman (s : bytes) : bytes =
Crypto.sha256 s
```
<!--ReasonLIGO-->
```reasonligo
let hasherman = (s: bytes): bytes => Crypto.sha256(s);
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Crypto.sha512(data: bytes) : bytes
Runs the [sha512 hash algorithm](https://en.wikipedia.org/wiki/SHA-2) over the given
`bytes` data and returns a `bytes` representing the hash.
<!--DOCUSAURUS_CODE_TABS-->
<!--PascaLIGO-->
```pascaligo
function hasherman512 (const s: bytes) : bytes is sha_512(s)
```
<!--CameLIGO-->
```cameligo
let hasherman512 (s: bytes) : bytes = Crypto.sha512 s
```
<!--ReasonLIGO-->
```reasonligo
let hasherman512 = (s: bytes) => Crypto.sha512(s);
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Crypto.hash_key(k: key) : key_hash
Hashes a key for easy comparison and storage.
<!--DOCUSAURUS_CODE_TABS-->
<!--PascaLIGO-->
```pascaligo
function check_hash_key (const kh1 : key_hash; const k2 : key) : bool * key_hash is block {
var ret : bool := False ;
var kh2 : key_hash := crypto_hash_key(k2) ;
if kh1 = kh2 then ret := True else skip;
} with (ret, kh2)
```
<!--CameLIGO-->
```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)
```
<!--ReasonLIGO-->
```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);
}
};
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Crypto.check(pk: key, signed: signature, data: bytes) : bool
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.
<!--DOCUSAURUS_CODE_TABS-->
<!--PascaLIGO-->
```pascaligo
function check_signature
(const pk: key;
const signed: signature;
const msg: bytes) : bool
is crypto_check(pk, signed, msg)
```
<!--CameLIGO-->
```cameligo
let check_signature (pk, signed, msg: key * signature * bytes) : bool =
Crypto.check pk signed msg
```
<!--ReasonLIGO-->
```reasonligo
let check_signature = ((pk, signed, msg): (key, signature, bytes)) : bool => {
Crypto.check(pk, signed, msg);
};
```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -162,7 +162,7 @@ module Simplify = struct
| "failwith" -> ok C_FAILWITH | "failwith" -> ok C_FAILWITH
| "Crypto.hash" -> ok C_HASH | "Crypto.hash" -> ok C_HASH
| "Crypto.black2b" -> ok C_BLAKE2b | "Crypto.blake2b" -> ok C_BLAKE2b
| "Crypto.sha256" -> ok C_SHA256 | "Crypto.sha256" -> ok C_SHA256
| "Crypto.sha512" -> ok C_SHA512 | "Crypto.sha512" -> ok C_SHA512
| "Crypto.hash_key" -> ok C_HASH_KEY | "Crypto.hash_key" -> ok C_HASH_KEY

View File

@ -0,0 +1,3 @@
function hasherman512 (const s: bytes) : bytes is sha_512(s)
function hasherman_blake (const s: bytes) : bytes is blake2b(s)

View File

@ -0,0 +1,2 @@
let hasherman512 (s: bytes) : bytes = Crypto.sha512 s
let hasherman_blake (s: bytes) : bytes = Crypto.blake2b s

View File

@ -0,0 +1,2 @@
let hasherman512 = (s: bytes) => Crypto.sha512(s);
let hasherman_blake = (s: bytes) => Crypto.blake2b(s);

View File

@ -428,6 +428,48 @@ let bytes_arithmetic () : unit result =
let%bind () = Assert.assert_fail @@ Ast_simplified.Misc.assert_value_eq (b3 , b1) in let%bind () = Assert.assert_fail @@ Ast_simplified.Misc.assert_value_eq (b3 , b1) in
ok () ok ()
let crypto () : unit result =
let%bind program = type_file "./contracts/crypto.ligo" in
let%bind foo = e_bytes_hex "0f00" in
let%bind foototo = e_bytes_hex "0f007070" in
let%bind b1 = Test_helpers.run_typed_program_with_simplified_input program "hasherman512" foo in
let%bind () = expect_eq program "hasherman512" foo b1 in
let%bind b2 = Test_helpers.run_typed_program_with_simplified_input program "hasherman512" foototo in
let%bind () = Assert.assert_fail @@ Ast_simplified.Misc.assert_value_eq (b2 , b1) in
let%bind b4 = Test_helpers.run_typed_program_with_simplified_input program "hasherman_blake" foo in
let%bind () = expect_eq program "hasherman_blake" foo b4 in
let%bind b5 = Test_helpers.run_typed_program_with_simplified_input program "hasherman_blake" foototo in
let%bind () = Assert.assert_fail @@ Ast_simplified.Misc.assert_value_eq (b5 , b4) in
ok ()
let crypto_mligo () : unit result =
let%bind program = mtype_file "./contracts/crypto.mligo" in
let%bind foo = e_bytes_hex "0f00" in
let%bind foototo = e_bytes_hex "0f007070" in
let%bind b1 = Test_helpers.run_typed_program_with_simplified_input program "hasherman512" foo in
let%bind () = expect_eq program "hasherman512" foo b1 in
let%bind b2 = Test_helpers.run_typed_program_with_simplified_input program "hasherman512" foototo in
let%bind () = Assert.assert_fail @@ Ast_simplified.Misc.assert_value_eq (b2 , b1) in
let%bind b4 = Test_helpers.run_typed_program_with_simplified_input program "hasherman_blake" foo in
let%bind () = expect_eq program "hasherman_blake" foo b4 in
let%bind b5 = Test_helpers.run_typed_program_with_simplified_input program "hasherman_blake" foototo in
let%bind () = Assert.assert_fail @@ Ast_simplified.Misc.assert_value_eq (b5 , b4) in
ok ()
let crypto_religo () : unit result =
let%bind program = retype_file "./contracts/crypto.religo" in
let%bind foo = e_bytes_hex "0f00" in
let%bind foototo = e_bytes_hex "0f007070" in
let%bind b1 = Test_helpers.run_typed_program_with_simplified_input program "hasherman512" foo in
let%bind () = expect_eq program "hasherman512" foo b1 in
let%bind b2 = Test_helpers.run_typed_program_with_simplified_input program "hasherman512" foototo in
let%bind () = Assert.assert_fail @@ Ast_simplified.Misc.assert_value_eq (b2 , b1) in
let%bind b4 = Test_helpers.run_typed_program_with_simplified_input program "hasherman_blake" foo in
let%bind () = expect_eq program "hasherman_blake" foo b4 in
let%bind b5 = Test_helpers.run_typed_program_with_simplified_input program "hasherman_blake" foototo in
let%bind () = Assert.assert_fail @@ Ast_simplified.Misc.assert_value_eq (b5 , b4) in
ok ()
let bytes_arithmetic_mligo () : unit result = let bytes_arithmetic_mligo () : unit result =
let%bind program = mtype_file "./contracts/bytes_arithmetic.mligo" in let%bind program = mtype_file "./contracts/bytes_arithmetic.mligo" in
let%bind foo = e_bytes_hex "0f00" in let%bind foo = e_bytes_hex "0f00" in
@ -2189,8 +2231,7 @@ let main = test_suite "Integration (End to End)" [
test "bool (religo)" bool_expression_religo ; test "bool (religo)" bool_expression_religo ;
test "arithmetic" arithmetic ; test "arithmetic" arithmetic ;
test "arithmetic (mligo)" arithmetic_mligo ; test "arithmetic (mligo)" arithmetic_mligo ;
test "arithmetic (religo)" arithmetic_religo ; test "arithmetic (religo)" arithmetic_religo ; test "bitwise_arithmetic" bitwise_arithmetic ;
test "bitwise_arithmetic" bitwise_arithmetic ;
test "bitwise_arithmetic (mligo)" bitwise_arithmetic_mligo; test "bitwise_arithmetic (mligo)" bitwise_arithmetic_mligo;
test "bitwise_arithmetic (religo)" bitwise_arithmetic_religo; test "bitwise_arithmetic (religo)" bitwise_arithmetic_religo;
test "string_arithmetic" string_arithmetic ; test "string_arithmetic" string_arithmetic ;
@ -2199,6 +2240,9 @@ let main = test_suite "Integration (End to End)" [
test "bytes_arithmetic" bytes_arithmetic ; test "bytes_arithmetic" bytes_arithmetic ;
test "bytes_arithmetic (mligo)" bytes_arithmetic_mligo ; test "bytes_arithmetic (mligo)" bytes_arithmetic_mligo ;
test "bytes_arithmetic (religo)" bytes_arithmetic_religo ; test "bytes_arithmetic (religo)" bytes_arithmetic_religo ;
test "crypto" crypto ;
test "crypto (mligo)" crypto_mligo ;
test "crypto (religo)" crypto_religo ;
test "set_arithmetic" set_arithmetic ; test "set_arithmetic" set_arithmetic ;
test "set_arithmetic (mligo)" set_arithmetic_mligo ; test "set_arithmetic (mligo)" set_arithmetic_mligo ;
test "set_arithmetic (religo)" set_arithmetic_religo ; test "set_arithmetic (religo)" set_arithmetic_religo ;