Michelson: BLAKE2B now takes bytes

This commit is contained in:
Benjamin Canou 2018-06-14 23:35:01 +02:00
parent 197b29b040
commit b17a44d7eb
13 changed files with 19 additions and 23 deletions

View File

@ -24,7 +24,7 @@ code { DUP; CAR;
{ DUP; DUP; DUP; DUP; { DUP; DUP; DUP; DUP;
# Check signature on data # Check signature on data
CAR %from; CAR %from;
DIIP{ CDAR %withdraw_amount; BLAKE2B @signed_amount }; DIIP{ CDAR %withdraw_amount; PACK ; BLAKE2B @signed_amount };
DIP{ CDDR %sig }; CHECK_SIGNATURE; DIP{ CDDR %sig }; CHECK_SIGNATURE;
IF {} { PUSH string "Bad signature"; FAILWITH }; IF {} { PUSH string "Bad signature"; FAILWITH };
# Get user account information # Get user account information

View File

@ -2,7 +2,7 @@ parameter key;
storage (pair signature string); storage (pair signature string);
code { DUP; DUP; code { DUP; DUP;
DIP{ CDR; DUP; CAR; DIP{ CDR; DUP; CAR;
DIP{CDR; BLAKE2B}; PAIR}; DIP{CDR; PACK ; BLAKE2B}; PAIR};
CAR; DIP {UNPAIR}; CHECK_SIGNATURE; CAR; DIP {UNPAIR}; CHECK_SIGNATURE;
IF {} {FAIL} ; IF {} {FAIL} ;
CDR; NIL operation ; PAIR}; CDR; NIL operation ; PAIR};

View File

@ -1,7 +1,7 @@
parameter (pair signature (pair string nat)); parameter (pair signature (pair string nat));
storage (pair (pair key nat) string); storage (pair (pair key nat) string);
code { DUP; CAR; DIP{CDR; DUP}; 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; CHECK_SIGNATURE;
IF { CDR; DUP; DIP{CAR; DIP{CAAR}}; CDR; PUSH nat 1; ADD; IF { CDR; DUP; DIP{CAR; DIP{CAAR}}; CDR; PUSH nat 1; ADD;
DIP{SWAP}; SWAP; PAIR; PAIR; NIL operation; PAIR} DIP{SWAP}; SWAP; PAIR; PAIR; NIL operation; PAIR}

View File

@ -1,3 +1,3 @@
parameter (pair mutez (pair timestamp int)) ; parameter (pair mutez (pair timestamp int)) ;
storage bytes ; storage bytes ;
code { CAR ; BLAKE2B ; NIL operation ; PAIR } code { CAR ; PACK ; BLAKE2B ; NIL operation ; PAIR }

View File

@ -1,3 +1,3 @@
parameter string; parameter string;
storage bytes; storage bytes;
code {CAR; BLAKE2B; NIL operation; PAIR}; code {CAR; PACK ; BLAKE2B; NIL operation; PAIR};

View File

@ -7,7 +7,7 @@ code {DUP; DUP; CAR;
# Provide the data # Provide the data
CDR; DIP {CDDR}} CDR; DIP {CDDR}}
{DUP; DIP{SWAP}; SWAP; CDAR; # Move key to the top {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 CHECK_SIGNATURE; # Check to ensure the data is authentic
# Update data # Update data
IF {CDR; SWAP; DIP{DUP}; CDAR; PAIR} IF {CDR; SWAP; DIP{DUP}; CDAR; PAIR}

View File

@ -5,7 +5,7 @@ code { DUP; CAR; DIP{CDDR}; # Stack rangling
IF_NONE { PUSH bool False} # If not referenced, reject IF_NONE { PUSH bool False} # If not referenced, reject
{ DUP; CAR; DIP{CDR}; AND}; { DUP; CAR; DIP{CDR}; AND};
PAIR} 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}; DIP{ DIP{DUP; CDR; DIP{CAR}; DUP};
SWAP; CAR; DIP{DUP; UNPAIR}; CHECK_SIGNATURE }; # Check the first signature SWAP; CAR; DIP{DUP; UNPAIR}; CHECK_SIGNATURE }; # Check the first signature
SWAP; SWAP;

View File

@ -4,7 +4,7 @@ storage (pair (pair (contract %under_key unit)
(contract %over_key unit)) (contract %over_key unit))
(pair (nat :rain %rain_level) (key %weather_service_key))); (pair (nat :rain %rain_level) (key %weather_service_key)));
code { DUP; DUP; code { DUP; DUP;
CAR; MAP_CDR{BLAKE2B}; CAR; MAP_CDR{PACK ; BLAKE2B};
SWAP; CDDDR %weather_service_key; SWAP; CDDDR %weather_service_key;
DIP {UNPAIR} ; CHECK_SIGNATURE @sigok; # Check if the data has been correctly signed DIP {UNPAIR} ; CHECK_SIGNATURE @sigok; # Check if the data has been correctly signed
ASSERT; # If signature is not correct, end the execution ASSERT; # If signature is not correct, end the execution

View File

@ -185,7 +185,7 @@ module Cost_of = struct
let check_signature = step_cost 3 let check_signature = step_cost 3
let hash_key = step_cost 3 let hash_key = step_cost 3
(* TODO: This needs to be a function of the data being hashed *) (* 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 steps_to_quota = step_cost 1
let source = step_cost 3 let source = step_cost 3
let self = step_cost 3 let self = step_cost 3

View File

@ -78,7 +78,7 @@ module Cost_of : sig
val now : Gas.cost val now : Gas.cost
val check_signature : Gas.cost val check_signature : Gas.cost
val hash_key : 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 steps_to_quota : Gas.cost
val source : Gas.cost val source : Gas.cost
val self : Gas.cost val self : Gas.cost

View File

@ -600,10 +600,7 @@ let rec interp
logged_return (Item (cmpres, rest), ctxt) logged_return (Item (cmpres, rest), ctxt)
(* packing *) (* packing *)
| Pack t, Item (value, rest) -> | Pack t, Item (value, rest) ->
unparse_data ctxt Optimized t value >>=? fun (expr, ctxt) -> Script_ir_translator.pack_data ctxt t value >>=? fun (bytes, 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 ->
logged_return (Item (bytes, rest), ctxt) logged_return (Item (bytes, rest), ctxt)
| Unpack t, Item (bytes, rest) -> | Unpack t, Item (bytes, rest) ->
Lwt.return (Gas.consume ctxt (Interp_costs.pack bytes)) >>=? fun ctxt -> Lwt.return (Gas.consume ctxt (Interp_costs.pack bytes)) >>=? fun ctxt ->
@ -699,10 +696,9 @@ let rec interp
| Hash_key, Item (key, rest) -> | Hash_key, Item (key, rest) ->
Lwt.return (Gas.consume ctxt Interp_costs.hash_key) >>=? fun ctxt -> Lwt.return (Gas.consume ctxt Interp_costs.hash_key) >>=? fun ctxt ->
logged_return (Item (Signature.Public_key.hash key, rest), ctxt) logged_return (Item (Signature.Public_key.hash key, rest), ctxt)
| Blake2b ty, Item (v, rest) -> | Blake2b, Item (bytes, rest) ->
Lwt.return (Gas.consume ctxt (Interp_costs.hash v)) >>=? fun ctxt -> Lwt.return (Gas.consume ctxt (Interp_costs.hash bytes)) >>=? fun ctxt ->
hash_data ctxt ty v >>=? fun (hash, ctxt) -> let hash = Raw_hashes.blake2b bytes in
let hash = Script_expr_hash.to_bytes hash in
logged_return (Item (hash, rest), ctxt) logged_return (Item (hash, rest), ctxt)
| Steps_to_quota, rest -> | Steps_to_quota, rest ->
Lwt.return (Gas.consume ctxt Interp_costs.steps_to_quota) >>=? fun ctxt -> Lwt.return (Gas.consume ctxt Interp_costs.steps_to_quota) >>=? fun ctxt ->

View File

@ -202,7 +202,7 @@ let number_of_generated_growing_types : type b a. (b, a) instr -> int = function
| Balance -> 0 | Balance -> 0
| Check_signature -> 0 | Check_signature -> 0
| Hash_key -> 0 | Hash_key -> 0
| Blake2b _ -> 0 | Blake2b -> 0
| Steps_to_quota -> 0 | Steps_to_quota -> 0
| Source -> 0 | Source -> 0
| Sender -> 0 | Sender -> 0
@ -2396,9 +2396,9 @@ and parse_instr
typed ctxt loc Check_signature typed ctxt loc Check_signature
(Item_t (Bool_t None, rest, annot)) (Item_t (Bool_t None, rest, annot))
| Prim (loc, I_BLAKE2B, [], annot), | Prim (loc, I_BLAKE2B, [], annot),
Item_t (t, rest, _) -> Item_t (Bytes_t _, rest, _) ->
parse_var_annot loc annot >>=? fun annot -> parse_var_annot loc annot >>=? fun annot ->
typed ctxt loc (Blake2b t) typed ctxt loc Blake2b
(Item_t (Bytes_t None, rest, annot)) (Item_t (Bytes_t None, rest, annot))
| Prim (loc, I_STEPS_TO_QUOTA, [], annot), | Prim (loc, I_STEPS_TO_QUOTA, [], annot),
stack -> stack ->

View File

@ -343,8 +343,8 @@ and ('bef, 'aft) instr =
('a * 'rest, MBytes.t * 'rest) instr ('a * 'rest, MBytes.t * 'rest) instr
| Unpack : 'a ty -> | Unpack : 'a ty ->
(MBytes.t * 'rest, 'a option * 'rest) instr (MBytes.t * 'rest, 'a option * 'rest) instr
| Blake2b : 'a ty -> | Blake2b :
('a * 'rest, MBytes.t * 'rest) instr (MBytes.t * 'rest, MBytes.t * 'rest) instr
| Steps_to_quota : (* TODO: check that it always returns a nat *) | Steps_to_quota : (* TODO: check that it always returns a nat *)
('rest, n num * 'rest) instr ('rest, n num * 'rest) instr
| Source : | Source :