Michelson: add SHA256 and SHA512

This commit is contained in:
Benjamin Canou 2018-06-14 23:46:03 +02:00
parent b17a44d7eb
commit 710998caf4
8 changed files with 46 additions and 6 deletions

View File

@ -180,6 +180,8 @@ module Script : sig
| I_PACK
| I_UNPACK
| I_BLAKE2B
| I_SHA256
| I_SHA512
| I_ABS
| I_ADD
| I_AMOUNT

View File

@ -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 (MBytes.length data) +@ alloc_bytes_cost 32
let hash data len = step_cost (MBytes.length data) +@ alloc_bytes_cost len
let steps_to_quota = step_cost 1
let source = step_cost 3
let self = step_cost 3

View File

@ -78,7 +78,7 @@ module Cost_of : sig
val now : Gas.cost
val check_signature : Gas.cost
val hash_key : Gas.cost
val hash : MBytes.t -> Gas.cost
val hash : MBytes.t -> int -> Gas.cost
val steps_to_quota : Gas.cost
val source : Gas.cost
val self : Gas.cost

View File

@ -29,6 +29,8 @@ type prim =
| I_PACK
| I_UNPACK
| I_BLAKE2B
| I_SHA256
| I_SHA512
| I_ABS
| I_ADD
| I_AMOUNT
@ -159,6 +161,8 @@ let string_of_prim = function
| I_PACK -> "PACK"
| I_UNPACK -> "UNPACK"
| I_BLAKE2B -> "BLAKE2B"
| I_SHA256 -> "SHA256"
| I_SHA512 -> "SHA512"
| I_ABS -> "ABS"
| I_ADD -> "ADD"
| I_AMOUNT -> "AMOUNT"
@ -270,6 +274,8 @@ let prim_of_string = function
| "PACK" -> ok I_PACK
| "UNPACK" -> ok I_UNPACK
| "BLAKE2B" -> ok I_BLAKE2B
| "SHA256" -> ok I_SHA256
| "SHA512" -> ok I_SHA512
| "ABS" -> ok I_ABS
| "ADD" -> ok I_ADD
| "AMOUNT" -> ok I_AMOUNT
@ -426,6 +432,8 @@ let prim_encoding =
("PACK", I_PACK) ;
("UNPACK", I_UNPACK) ;
("BLAKE2B", I_BLAKE2B) ;
("SHA256", I_SHA256) ;
("SHA512", I_SHA512) ;
("ABS", I_ABS) ;
("ADD", I_ADD) ;
("AMOUNT", I_AMOUNT) ;

View File

@ -27,6 +27,8 @@ type prim =
| I_PACK
| I_UNPACK
| I_BLAKE2B
| I_SHA256
| I_SHA512
| I_ABS
| I_ADD
| I_AMOUNT

View File

@ -697,9 +697,17 @@ let rec interp
Lwt.return (Gas.consume ctxt Interp_costs.hash_key) >>=? fun ctxt ->
logged_return (Item (Signature.Public_key.hash key, rest), ctxt)
| Blake2b, Item (bytes, rest) ->
Lwt.return (Gas.consume ctxt (Interp_costs.hash bytes)) >>=? fun ctxt ->
Lwt.return (Gas.consume ctxt (Interp_costs.hash bytes 32)) >>=? fun ctxt ->
let hash = Raw_hashes.blake2b bytes in
logged_return (Item (hash, rest), ctxt)
| Sha256, Item (bytes, rest) ->
Lwt.return (Gas.consume ctxt (Interp_costs.hash bytes 32)) >>=? fun ctxt ->
let hash = Raw_hashes.sha256 bytes in
logged_return (Item (hash, rest), ctxt)
| Sha512, Item (bytes, rest) ->
Lwt.return (Gas.consume ctxt (Interp_costs.hash bytes 64)) >>=? fun ctxt ->
let hash = Raw_hashes.sha512 bytes in
logged_return (Item (hash, rest), ctxt)
| Steps_to_quota, rest ->
Lwt.return (Gas.consume ctxt Interp_costs.steps_to_quota) >>=? fun ctxt ->
let steps = match Gas.level ctxt with

View File

@ -203,6 +203,8 @@ let number_of_generated_growing_types : type b a. (b, a) instr -> int = function
| Check_signature -> 0
| Hash_key -> 0
| Blake2b -> 0
| Sha256 -> 0
| Sha512 -> 0
| Steps_to_quota -> 0
| Source -> 0
| Sender -> 0
@ -244,6 +246,8 @@ let namespace = function
| I_PACK
| I_UNPACK
| I_BLAKE2B
| I_SHA256
| I_SHA512
| I_ABS
| I_ADD
| I_AMOUNT
@ -2400,6 +2404,16 @@ and parse_instr
parse_var_annot loc annot >>=? fun annot ->
typed ctxt loc Blake2b
(Item_t (Bytes_t None, rest, annot))
| Prim (loc, I_SHA256, [], annot),
Item_t (Bytes_t _, rest, _) ->
parse_var_annot loc annot >>=? fun annot ->
typed ctxt loc Sha256
(Item_t (Bytes_t None, rest, annot))
| Prim (loc, I_SHA512, [], annot),
Item_t (Bytes_t _, rest, _) ->
parse_var_annot loc annot >>=? fun annot ->
typed ctxt loc Sha512
(Item_t (Bytes_t None, rest, annot))
| Prim (loc, I_STEPS_TO_QUOTA, [], annot),
stack ->
parse_var_annot loc annot ~default:default_steps_annot >>=? fun annot ->
@ -2440,7 +2454,7 @@ and parse_instr
| I_CREATE_CONTRACT | I_SET_DELEGATE | I_NOW
| I_IMPLICIT_ACCOUNT | I_AMOUNT | I_BALANCE
| I_CHECK_SIGNATURE | I_HASH_KEY | I_SOURCE | I_SENDER
| I_BLAKE2B | I_STEPS_TO_QUOTA | I_ADDRESS
| I_BLAKE2B | I_SHA256 | I_SHA512 | I_STEPS_TO_QUOTA | I_ADDRESS
as name), (_ :: _ as l), _), _ ->
fail (Invalid_arity (loc, name, 0, List.length l))
| Prim (loc, (I_NONE | I_LEFT | I_RIGHT | I_NIL | I_MAP | I_ITER
@ -2479,7 +2493,8 @@ and parse_instr
| Prim (loc, I_TRANSFER_TOKENS, [], _),
stack ->
fail (Bad_stack (loc, I_TRANSFER_TOKENS, 4, stack))
| Prim (loc, (I_DROP | I_DUP | I_CAR | I_CDR | I_SOME | I_BLAKE2B | I_DIP
| Prim (loc, (I_DROP | I_DUP | I_CAR | I_CDR | I_SOME
| I_BLAKE2B | I_SHA256 | I_SHA512 | I_DIP
| I_IF_NONE | I_LEFT | I_RIGHT | I_IF_LEFT | I_IF
| I_LOOP | I_IF_CONS | I_IMPLICIT_ACCOUNT
| I_NEG | I_ABS | I_INT | I_NOT
@ -2508,7 +2523,8 @@ and parse_instr
I_LT ; I_GT ; I_LE ; I_GE ;
I_TRANSFER_TOKENS ; I_CREATE_ACCOUNT ;
I_CREATE_CONTRACT ; I_NOW ; I_AMOUNT ; I_BALANCE ;
I_IMPLICIT_ACCOUNT ; I_CHECK_SIGNATURE ; I_BLAKE2B ; I_HASH_KEY ;
I_IMPLICIT_ACCOUNT ; I_CHECK_SIGNATURE ;
I_BLAKE2B ; I_SHA256 ; I_SHA512 ; I_HASH_KEY ;
I_STEPS_TO_QUOTA ;
I_PUSH ; I_NONE ; I_LEFT ; I_RIGHT ; I_NIL ;
I_EMPTY_SET ; I_DIP ; I_LOOP ;

View File

@ -345,6 +345,10 @@ and ('bef, 'aft) instr =
(MBytes.t * 'rest, 'a option * 'rest) instr
| Blake2b :
(MBytes.t * 'rest, MBytes.t * 'rest) instr
| Sha256 :
(MBytes.t * 'rest, MBytes.t * 'rest) instr
| Sha512 :
(MBytes.t * 'rest, MBytes.t * 'rest) instr
| Steps_to_quota : (* TODO: check that it always returns a nat *)
('rest, n num * 'rest) instr
| Source :