From c8e0654ffe46fe0a62deb44b719393b629a0b79d Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Fri, 31 Jan 2020 03:34:36 -0800 Subject: [PATCH 1/2] Add crypto reference page to docs Change 'black2b' to 'blake2b' in CameLIGO/ReasonLIGO frontend --- gitlab-pages/docs/reference/crypto.md | 155 ++++++++++++++++++++++++++ src/passes/operators/operators.ml | 2 +- src/test/contracts/crypto.ligo | 3 + src/test/contracts/crypto.mligo | 2 + src/test/contracts/crypto.religo | 2 + src/test/integration_tests.ml | 48 +++++++- 6 files changed, 209 insertions(+), 3 deletions(-) create mode 100644 gitlab-pages/docs/reference/crypto.md create mode 100644 src/test/contracts/crypto.ligo create mode 100644 src/test/contracts/crypto.mligo create mode 100644 src/test/contracts/crypto.religo diff --git a/gitlab-pages/docs/reference/crypto.md b/gitlab-pages/docs/reference/crypto.md new file mode 100644 index 000000000..571c0265b --- /dev/null +++ b/gitlab-pages/docs/reference/crypto.md @@ -0,0 +1,155 @@ +--- +id: crypto-reference +title: Crypto +--- + +## Crypto.black2b(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. + + + + + +```pascaligo +function hasherman_blake (const s: bytes) : bytes is blake2b(s) +``` + + + +```cameligo +let hasherman_blake (s: bytes) : bytes = Crypto.blake2b s +``` + + + +```reasonligo +let hasherman_blake = (s: bytes) => Crypto.blake2b(s); +``` + + + +## 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. + + + + +```pascaligo +function hasherman (const s : bytes) : bytes is + begin skip end with sha_256(s) +``` + + +```cameligo +let hasherman (s : bytes) : bytes = + Crypto.sha256 s +``` + + +```reasonligo +let hasherman = (s: bytes): bytes => Crypto.sha256(s); +``` + + + +## 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. + + + + + +```pascaligo +function hasherman512 (const s: bytes) : bytes is sha_512(s) +``` + + + +```cameligo +let hasherman512 (s: bytes) : bytes = Crypto.sha512 s +``` + + + +```reasonligo +let hasherman512 = (s: bytes) => Crypto.sha512(s); +``` + + + +## Crypto.hash_key(k: key) : key_hash + +Hashes a key for easy comparison and storage. + + + + +```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 +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 +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); + } +}; +``` + + + +## 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. + + + + +```pascaligo +function check_signature + (const pk: key; + const signed: signature; + const msg: bytes) : bool + is crypto_check(pk, signed, msg) +``` + + +```cameligo +let check_signature (pk, signed, msg: key * signature * bytes) : bool = + Crypto.check pk signed msg +``` + + +```reasonligo +let check_signature = ((pk, signed, msg): (key, signature, bytes)) : bool => { + Crypto.check(pk, signed, msg); +}; +``` + + diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index 6c8c92112..e6eb15c3e 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -162,7 +162,7 @@ module Simplify = struct | "failwith" -> ok C_FAILWITH | "Crypto.hash" -> ok C_HASH - | "Crypto.black2b" -> ok C_BLAKE2b + | "Crypto.blake2b" -> ok C_BLAKE2b | "Crypto.sha256" -> ok C_SHA256 | "Crypto.sha512" -> ok C_SHA512 | "Crypto.hash_key" -> ok C_HASH_KEY diff --git a/src/test/contracts/crypto.ligo b/src/test/contracts/crypto.ligo new file mode 100644 index 000000000..4e08f7b16 --- /dev/null +++ b/src/test/contracts/crypto.ligo @@ -0,0 +1,3 @@ +function hasherman512 (const s: bytes) : bytes is sha_512(s) + +function hasherman_blake (const s: bytes) : bytes is blake2b(s) diff --git a/src/test/contracts/crypto.mligo b/src/test/contracts/crypto.mligo new file mode 100644 index 000000000..9fa7e99e8 --- /dev/null +++ b/src/test/contracts/crypto.mligo @@ -0,0 +1,2 @@ +let hasherman512 (s: bytes) : bytes = Crypto.sha512 s +let hasherman_blake (s: bytes) : bytes = Crypto.blake2b s diff --git a/src/test/contracts/crypto.religo b/src/test/contracts/crypto.religo new file mode 100644 index 000000000..c5b17ce8e --- /dev/null +++ b/src/test/contracts/crypto.religo @@ -0,0 +1,2 @@ +let hasherman512 = (s: bytes) => Crypto.sha512(s); +let hasherman_blake = (s: bytes) => Crypto.blake2b(s); diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 2449e085e..58b492fc2 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -428,6 +428,48 @@ let bytes_arithmetic () : unit result = let%bind () = Assert.assert_fail @@ Ast_simplified.Misc.assert_value_eq (b3 , b1) in 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%bind program = mtype_file "./contracts/bytes_arithmetic.mligo" 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 "arithmetic" arithmetic ; test "arithmetic (mligo)" arithmetic_mligo ; - test "arithmetic (religo)" arithmetic_religo ; - test "bitwise_arithmetic" bitwise_arithmetic ; + test "arithmetic (religo)" arithmetic_religo ; test "bitwise_arithmetic" bitwise_arithmetic ; test "bitwise_arithmetic (mligo)" bitwise_arithmetic_mligo; test "bitwise_arithmetic (religo)" bitwise_arithmetic_religo; 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 (mligo)" bytes_arithmetic_mligo ; 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 (mligo)" set_arithmetic_mligo ; test "set_arithmetic (religo)" set_arithmetic_religo ; From ef83e2c39a79f1f2a33ac1f2d9438201735e0aa9 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Fri, 31 Jan 2020 03:40:59 -0800 Subject: [PATCH 2/2] Add correction of 'black2b' to changelog --- CHANGELOG.md | 6 +++++- gitlab-pages/docs/reference/crypto.md | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc0b56a81..1977c3295 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,14 @@ ## [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 ### Added - running failing code in `ligo interpret`, `ligo dry-run`, `ligo run-function` will no longer be an error (return value : 0) - + ## [1899dfe8d7285580b3aa30fab933ed589f8f1bc5] - 2020-01-08 ### Added - Partial application and OCaml-like currying behavior to CameLIGO & ReasonLIGO diff --git a/gitlab-pages/docs/reference/crypto.md b/gitlab-pages/docs/reference/crypto.md index 571c0265b..7234d4880 100644 --- a/gitlab-pages/docs/reference/crypto.md +++ b/gitlab-pages/docs/reference/crypto.md @@ -3,7 +3,7 @@ id: crypto-reference title: Crypto --- -## Crypto.black2b(data: bytes): bytes +## 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.