Signer: very simple test for deterministic nonce

This commit is contained in:
Eugen Zalinescu 2018-12-14 10:34:08 +01:00 committed by Grégoire Henry
parent fd9694d8b0
commit d76c24670a
No known key found for this signature in database
GPG Key ID: 50D984F20BD445D2
3 changed files with 95 additions and 3 deletions

View File

@ -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) ;
]

View File

@ -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)

View File

@ -0,0 +1,49 @@
(*****************************************************************************)
(* *)
(* Open Source License *)
(* Copyright (c) 2018 Nomadic Labs <contact@nomadic-labs.com> *)
(* *)
(* 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
]