From b17a44d7eb9db06cbbf2c727f143db8a9ea9a8ec Mon Sep 17 00:00:00 2001 From: Benjamin Canou Date: Thu, 14 Jun 2018 23:35:01 +0200 Subject: [PATCH] Michelson: BLAKE2B now takes bytes --- src/bin_client/test/contracts/accounts.tz | 2 +- src/bin_client/test/contracts/check_signature.tz | 2 +- src/bin_client/test/contracts/data_publisher.tz | 2 +- .../test/contracts/hash_consistency_checker.tz | 2 +- src/bin_client/test/contracts/hash_string.tz | 2 +- src/bin_client/test/contracts/int_publisher.tz | 2 +- .../test/contracts/parameterized_multisig.tz | 2 +- src/bin_client/test/contracts/weather_insurance.tz | 2 +- src/proto_alpha/lib_protocol/src/michelson_v1_gas.ml | 2 +- .../lib_protocol/src/michelson_v1_gas.mli | 2 +- .../lib_protocol/src/script_interpreter.ml | 12 ++++-------- .../lib_protocol/src/script_ir_translator.ml | 6 +++--- src/proto_alpha/lib_protocol/src/script_typed_ir.ml | 4 ++-- 13 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/bin_client/test/contracts/accounts.tz b/src/bin_client/test/contracts/accounts.tz index 90a90f4b7..c327692ca 100644 --- a/src/bin_client/test/contracts/accounts.tz +++ b/src/bin_client/test/contracts/accounts.tz @@ -24,7 +24,7 @@ code { DUP; CAR; { DUP; DUP; DUP; DUP; # Check signature on data CAR %from; - DIIP{ CDAR %withdraw_amount; BLAKE2B @signed_amount }; + DIIP{ CDAR %withdraw_amount; PACK ; BLAKE2B @signed_amount }; DIP{ CDDR %sig }; CHECK_SIGNATURE; IF {} { PUSH string "Bad signature"; FAILWITH }; # Get user account information diff --git a/src/bin_client/test/contracts/check_signature.tz b/src/bin_client/test/contracts/check_signature.tz index 768b4b81f..1d0569cb8 100644 --- a/src/bin_client/test/contracts/check_signature.tz +++ b/src/bin_client/test/contracts/check_signature.tz @@ -2,7 +2,7 @@ parameter key; storage (pair signature string); code { DUP; DUP; DIP{ CDR; DUP; CAR; - DIP{CDR; BLAKE2B}; PAIR}; + DIP{CDR; PACK ; BLAKE2B}; PAIR}; CAR; DIP {UNPAIR}; CHECK_SIGNATURE; IF {} {FAIL} ; CDR; NIL operation ; PAIR}; diff --git a/src/bin_client/test/contracts/data_publisher.tz b/src/bin_client/test/contracts/data_publisher.tz index 0e08b98f9..9240d6302 100644 --- a/src/bin_client/test/contracts/data_publisher.tz +++ b/src/bin_client/test/contracts/data_publisher.tz @@ -1,7 +1,7 @@ parameter (pair signature (pair string nat)); storage (pair (pair key nat) string); code { DUP; CAR; DIP{CDR; DUP}; - SWAP; DIP{DUP}; CAAR; DIP{DUP; CAR; DIP{CDR; BLAKE2B}}; + SWAP; DIP{DUP}; CAAR; DIP{DUP; CAR; DIP{CDR; PACK ; BLAKE2B}}; CHECK_SIGNATURE; IF { CDR; DUP; DIP{CAR; DIP{CAAR}}; CDR; PUSH nat 1; ADD; DIP{SWAP}; SWAP; PAIR; PAIR; NIL operation; PAIR} diff --git a/src/bin_client/test/contracts/hash_consistency_checker.tz b/src/bin_client/test/contracts/hash_consistency_checker.tz index 456c66507..fb98a39da 100644 --- a/src/bin_client/test/contracts/hash_consistency_checker.tz +++ b/src/bin_client/test/contracts/hash_consistency_checker.tz @@ -1,3 +1,3 @@ parameter (pair mutez (pair timestamp int)) ; storage bytes ; -code { CAR ; BLAKE2B ; NIL operation ; PAIR } \ No newline at end of file +code { CAR ; PACK ; BLAKE2B ; NIL operation ; PAIR } \ No newline at end of file diff --git a/src/bin_client/test/contracts/hash_string.tz b/src/bin_client/test/contracts/hash_string.tz index 531432992..b0b8ddea6 100644 --- a/src/bin_client/test/contracts/hash_string.tz +++ b/src/bin_client/test/contracts/hash_string.tz @@ -1,3 +1,3 @@ parameter string; storage bytes; -code {CAR; BLAKE2B; NIL operation; PAIR}; +code {CAR; PACK ; BLAKE2B; NIL operation; PAIR}; diff --git a/src/bin_client/test/contracts/int_publisher.tz b/src/bin_client/test/contracts/int_publisher.tz index 7436fbc2e..6ee49b979 100644 --- a/src/bin_client/test/contracts/int_publisher.tz +++ b/src/bin_client/test/contracts/int_publisher.tz @@ -7,7 +7,7 @@ code {DUP; DUP; CAR; # Provide the data CDR; DIP {CDDR}} {DUP; DIP{SWAP}; SWAP; CDAR; # Move key to the top - DIP {DUP; CAR; DIP {CDR; BLAKE2B}}; # Arrange the new piece of data + DIP {DUP; CAR; DIP {CDR; PACK ; BLAKE2B}}; # Arrange the new piece of data CHECK_SIGNATURE; # Check to ensure the data is authentic # Update data IF {CDR; SWAP; DIP{DUP}; CDAR; PAIR} diff --git a/src/bin_client/test/contracts/parameterized_multisig.tz b/src/bin_client/test/contracts/parameterized_multisig.tz index 725770527..17d301b78 100644 --- a/src/bin_client/test/contracts/parameterized_multisig.tz +++ b/src/bin_client/test/contracts/parameterized_multisig.tz @@ -5,7 +5,7 @@ code { DUP; CAR; DIP{CDDR}; # Stack rangling IF_NONE { PUSH bool False} # If not referenced, reject { DUP; CAR; DIP{CDR}; AND}; PAIR} - { DUP; CAR; DIP{CDR; DUP; BLAKE2B}; PAIR; SWAP; # Create the signature pair + { DUP; CAR; DIP{CDR; DUP; PACK ; BLAKE2B}; PAIR; SWAP; # Create the signature pair DIP{ DIP{DUP; CDR; DIP{CAR}; DUP}; SWAP; CAR; DIP{DUP; UNPAIR}; CHECK_SIGNATURE }; # Check the first signature SWAP; diff --git a/src/bin_client/test/contracts/weather_insurance.tz b/src/bin_client/test/contracts/weather_insurance.tz index db03fa94e..858fe918f 100644 --- a/src/bin_client/test/contracts/weather_insurance.tz +++ b/src/bin_client/test/contracts/weather_insurance.tz @@ -4,7 +4,7 @@ storage (pair (pair (contract %under_key unit) (contract %over_key unit)) (pair (nat :rain %rain_level) (key %weather_service_key))); code { DUP; DUP; - CAR; MAP_CDR{BLAKE2B}; + CAR; MAP_CDR{PACK ; BLAKE2B}; SWAP; CDDDR %weather_service_key; DIP {UNPAIR} ; CHECK_SIGNATURE @sigok; # Check if the data has been correctly signed ASSERT; # If signature is not correct, end the execution diff --git a/src/proto_alpha/lib_protocol/src/michelson_v1_gas.ml b/src/proto_alpha/lib_protocol/src/michelson_v1_gas.ml index 5649a0870..8258db7ae 100644 --- a/src/proto_alpha/lib_protocol/src/michelson_v1_gas.ml +++ b/src/proto_alpha/lib_protocol/src/michelson_v1_gas.ml @@ -185,7 +185,7 @@ module Cost_of = struct let check_signature = step_cost 3 let hash_key = step_cost 3 (* TODO: This needs to be a function of the data being hashed *) - let hash _data = step_cost 3 + let hash data = step_cost (MBytes.length data) +@ alloc_bytes_cost 32 let steps_to_quota = step_cost 1 let source = step_cost 3 let self = step_cost 3 diff --git a/src/proto_alpha/lib_protocol/src/michelson_v1_gas.mli b/src/proto_alpha/lib_protocol/src/michelson_v1_gas.mli index d7e3da74b..d056601fd 100644 --- a/src/proto_alpha/lib_protocol/src/michelson_v1_gas.mli +++ b/src/proto_alpha/lib_protocol/src/michelson_v1_gas.mli @@ -78,7 +78,7 @@ module Cost_of : sig val now : Gas.cost val check_signature : Gas.cost val hash_key : Gas.cost - val hash : 'a -> Gas.cost + val hash : MBytes.t -> Gas.cost val steps_to_quota : Gas.cost val source : Gas.cost val self : Gas.cost diff --git a/src/proto_alpha/lib_protocol/src/script_interpreter.ml b/src/proto_alpha/lib_protocol/src/script_interpreter.ml index 812c8dff1..2b23e5d9f 100644 --- a/src/proto_alpha/lib_protocol/src/script_interpreter.ml +++ b/src/proto_alpha/lib_protocol/src/script_interpreter.ml @@ -600,10 +600,7 @@ let rec interp logged_return (Item (cmpres, rest), ctxt) (* packing *) | Pack t, Item (value, rest) -> - unparse_data ctxt Optimized t value >>=? fun (expr, ctxt) -> - let expr = (Micheline.strip_locations expr) in - let bytes = Data_encoding.Binary.to_bytes_exn Script.expr_encoding expr in - Lwt.return (Gas.consume ctxt (Interp_costs.unpack bytes)) >>=? fun ctxt -> + Script_ir_translator.pack_data ctxt t value >>=? fun (bytes, ctxt) -> logged_return (Item (bytes, rest), ctxt) | Unpack t, Item (bytes, rest) -> Lwt.return (Gas.consume ctxt (Interp_costs.pack bytes)) >>=? fun ctxt -> @@ -699,10 +696,9 @@ let rec interp | Hash_key, Item (key, rest) -> Lwt.return (Gas.consume ctxt Interp_costs.hash_key) >>=? fun ctxt -> logged_return (Item (Signature.Public_key.hash key, rest), ctxt) - | Blake2b ty, Item (v, rest) -> - Lwt.return (Gas.consume ctxt (Interp_costs.hash v)) >>=? fun ctxt -> - hash_data ctxt ty v >>=? fun (hash, ctxt) -> - let hash = Script_expr_hash.to_bytes hash in + | Blake2b, Item (bytes, rest) -> + Lwt.return (Gas.consume ctxt (Interp_costs.hash bytes)) >>=? fun ctxt -> + let hash = Raw_hashes.blake2b bytes in logged_return (Item (hash, rest), ctxt) | Steps_to_quota, rest -> Lwt.return (Gas.consume ctxt Interp_costs.steps_to_quota) >>=? fun ctxt -> diff --git a/src/proto_alpha/lib_protocol/src/script_ir_translator.ml b/src/proto_alpha/lib_protocol/src/script_ir_translator.ml index 0ef5b98b1..faa07a154 100644 --- a/src/proto_alpha/lib_protocol/src/script_ir_translator.ml +++ b/src/proto_alpha/lib_protocol/src/script_ir_translator.ml @@ -202,7 +202,7 @@ let number_of_generated_growing_types : type b a. (b, a) instr -> int = function | Balance -> 0 | Check_signature -> 0 | Hash_key -> 0 - | Blake2b _ -> 0 + | Blake2b -> 0 | Steps_to_quota -> 0 | Source -> 0 | Sender -> 0 @@ -2396,9 +2396,9 @@ and parse_instr typed ctxt loc Check_signature (Item_t (Bool_t None, rest, annot)) | Prim (loc, I_BLAKE2B, [], annot), - Item_t (t, rest, _) -> + Item_t (Bytes_t _, rest, _) -> parse_var_annot loc annot >>=? fun annot -> - typed ctxt loc (Blake2b t) + typed ctxt loc Blake2b (Item_t (Bytes_t None, rest, annot)) | Prim (loc, I_STEPS_TO_QUOTA, [], annot), stack -> diff --git a/src/proto_alpha/lib_protocol/src/script_typed_ir.ml b/src/proto_alpha/lib_protocol/src/script_typed_ir.ml index 2535035e0..8362d5741 100644 --- a/src/proto_alpha/lib_protocol/src/script_typed_ir.ml +++ b/src/proto_alpha/lib_protocol/src/script_typed_ir.ml @@ -343,8 +343,8 @@ and ('bef, 'aft) instr = ('a * 'rest, MBytes.t * 'rest) instr | Unpack : 'a ty -> (MBytes.t * 'rest, 'a option * 'rest) instr - | Blake2b : 'a ty -> - ('a * 'rest, MBytes.t * 'rest) instr + | Blake2b : + (MBytes.t * 'rest, MBytes.t * 'rest) instr | Steps_to_quota : (* TODO: check that it always returns a nat *) ('rest, n num * 'rest) instr | Source :