From d76c24670ad0bcb9127ea4991c68eb8e8c1db3cb Mon Sep 17 00:00:00 2001 From: Eugen Zalinescu Date: Fri, 14 Dec 2018 10:34:08 +0100 Subject: [PATCH] Signer: very simple test for deterministic nonce --- .../client_keys_commands.ml | 36 ++++++++++++++ src/lib_crypto/test/dune | 13 +++-- .../test/test_deterministic_nonce.ml | 49 +++++++++++++++++++ 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 src/lib_crypto/test/test_deterministic_nonce.ml diff --git a/src/lib_client_commands/client_keys_commands.ml b/src/lib_client_commands/client_keys_commands.ml index fe8a43451..d37e3433e 100644 --- a/src/lib_client_commands/client_keys_commands.ml +++ b/src/lib_client_commands/client_keys_commands.ml @@ -443,4 +443,40 @@ let commands version : Client_context.io_wallet Clic.command list = Secret_key.set cctxt [] >>=? fun () -> Public_key_hash.set cctxt []) ; + command ~group ~desc: "Compute deterministic nonce." + no_options + (prefixes [ "generate" ; "nonce"; "for" ] + @@ Public_key_hash.alias_param + @@ prefixes [ "from" ] + @@ string + ~name: "data" + ~desc: "string from which to deterministically generate the nonce" + @@ stop) + (fun () (name, _pkh) data (cctxt : Client_context.io_wallet) -> + let data = MBytes.of_string data in + Secret_key.mem cctxt name >>=? fun sk_present -> + fail_unless sk_present + (failure "secret key not present for %s" name) >>=? fun () -> + Secret_key.find cctxt name >>=? fun sk_uri -> + Client_keys.deterministic_nonce sk_uri data >>=? fun nonce -> + cctxt#message "%a" MBytes.pp_hex nonce >>= fun () -> return_unit) ; + + command ~group ~desc: "Compute deterministic nonce hash." + no_options + (prefixes [ "generate" ; "nonce"; "hash"; "for" ] + @@ Public_key_hash.alias_param + @@ prefixes [ "from" ] + @@ string + ~name: "data" + ~desc: "string from which to deterministically generate the nonce hash" + @@ stop) + (fun () (name, _pkh) data (cctxt : Client_context.io_wallet) -> + let data = MBytes.of_string data in + Secret_key.mem cctxt name >>=? fun sk_present -> + fail_unless sk_present + (failure "secret key not present for %s" name) >>=? fun () -> + Secret_key.find cctxt name >>=? fun sk_uri -> + Client_keys.deterministic_nonce_hash sk_uri data >>=? fun nonce_hash -> + cctxt#message "%a" MBytes.pp_hex nonce_hash >>= fun () -> return_unit) ; + ] diff --git a/src/lib_crypto/test/dune b/src/lib_crypto/test/dune index de622e41e..48ce21e76 100644 --- a/src/lib_crypto/test/dune +++ b/src/lib_crypto/test/dune @@ -3,7 +3,8 @@ test_base58 test_ed25519 test_blake2b - test_pvss) + test_pvss + test_deterministic_nonce) (libraries tezos-stdlib tezos-crypto tezos-data-encoding @@ -20,7 +21,8 @@ test_base58.exe test_ed25519.exe test_blake2b.exe - test_pvss.exe)) + test_pvss.exe + test_deterministic_nonce.exe)) (alias (name runtest_merkle) @@ -42,13 +44,18 @@ (name runtest_pvss) (action (run %{exe:test_pvss.exe}))) +(alias + (name runtest_deterministic_nonce) + (action (run %{exe:test_deterministic_nonce.exe}))) + (alias (name runtest) (deps (alias runtest_merkle) (alias runtest_base58) (alias runtest_ed25519) (alias runtest_blake2b) - (alias runtest_pvss))) + (alias runtest_pvss) + (alias runtest_deterministic_nonce))) (alias (name runtest_indent) diff --git a/src/lib_crypto/test/test_deterministic_nonce.ml b/src/lib_crypto/test/test_deterministic_nonce.ml new file mode 100644 index 000000000..c430eb0b5 --- /dev/null +++ b/src/lib_crypto/test/test_deterministic_nonce.ml @@ -0,0 +1,49 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2018 Nomadic Labs *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +let test_hash_matches (module X : S.SIGNATURE) () = + let _, _, sk = X.generate_key () in + let data = MBytes.of_string "ce input sa pun eu aici oare?" in + let nonce = X.deterministic_nonce sk data in + let nonce_hash = X.deterministic_nonce_hash sk data in + let hashed_nonce = Blake2B.hash_bytes [nonce] in + if nonce_hash <> Blake2B.to_bytes hashed_nonce then + Alcotest.failf "the hash of deterministic_nonce is NOT deterministic_nonce_hash" + + +let ed25519 = (module Ed25519 : S.SIGNATURE) +let p256 = (module P256 : S.SIGNATURE) +let secp256k1 = (module Secp256k1 : S.SIGNATURE) + +let tests = [ + "hash_matches_ed25519", `Quick, (test_hash_matches ed25519); + "hash_matches_p256", `Quick, (test_hash_matches p256); + "hash_matches_secp256k1", `Quick, (test_hash_matches secp256k1); +] + +let () = + Alcotest.run "tezos-crypto" [ + "deterministic_nonce", tests + ]