diff --git a/devtools/yes_wallet/get_delegates_023_PtSeouLo.ml b/devtools/yes_wallet/get_delegates_023_PtSeouLo.ml index 905c1655c26236712c38a02c09f8ceefcfdc5343..ac4015b5e8c5579a68f1ef2093b770098b65f3e5 100644 --- a/devtools/yes_wallet/get_delegates_023_PtSeouLo.ml +++ b/devtools/yes_wallet/get_delegates_023_PtSeouLo.ml @@ -41,17 +41,7 @@ module Get_delegates = struct module Signature = struct include Tezos_crypto.Signature.V2 - - module To_latest = struct - let public_key_hash = Fun.id - - let public_key = Fun.id - - let secret_key = Fun.id - - let signature = Fun.id - end - + module To_latest = Signature.V_latest.Of_V2 module Of_latest = Tezos_crypto.Signature.V2.Of_V_latest end diff --git a/devtools/yes_wallet/get_delegates_024_PtTALLiN.ml b/devtools/yes_wallet/get_delegates_024_PtTALLiN.ml index edf09d90f66ba00cc81f892b4cd922c746125693..8af7452877a19f31ea235ae9d978aa1bf7540b7a 100644 --- a/devtools/yes_wallet/get_delegates_024_PtTALLiN.ml +++ b/devtools/yes_wallet/get_delegates_024_PtTALLiN.ml @@ -41,17 +41,7 @@ module Get_delegates = struct module Signature = struct include Tezos_crypto.Signature.V2 - - module To_latest = struct - let public_key_hash = Fun.id - - let public_key = Fun.id - - let secret_key = Fun.id - - let signature = Fun.id - end - + module To_latest = Signature.V_latest.Of_V2 module Of_latest = Tezos_crypto.Signature.V2.Of_V_latest end diff --git a/devtools/yes_wallet/get_delegates_alpha.ml b/devtools/yes_wallet/get_delegates_alpha.ml index 28008403e93e62d9f732c8074d809eeedf555a7f..ff9020deb4103bde8854ab7dd9c677373bb29a5e 100644 --- a/devtools/yes_wallet/get_delegates_alpha.ml +++ b/devtools/yes_wallet/get_delegates_alpha.ml @@ -41,17 +41,7 @@ module Get_delegates = struct module Signature = struct include Tezos_crypto.Signature.V2 - - module To_latest = struct - let public_key_hash = Fun.id - - let public_key = Fun.id - - let secret_key = Fun.id - - let signature = Fun.id - end - + module To_latest = Signature.V_latest.Of_V2 module Of_latest = Tezos_crypto.Signature.V2.Of_V_latest end diff --git a/etherlink/bin_node/lib_dev/kernel_config.ml b/etherlink/bin_node/lib_dev/kernel_config.ml index 09e07ae9114f70683d34100a775618407f9c7744..224df441f23ec101b832618aaafb50e5763cd2cc 100644 --- a/etherlink/bin_node/lib_dev/kernel_config.ml +++ b/etherlink/bin_node/lib_dev/kernel_config.ml @@ -114,7 +114,7 @@ let make_l2 ~eth_bootstrap_balance ~tez_bootstrap_balance (fun manager -> let make_account_field key value converter = let path_prefix = - manager |> Signature.Public_key.hash + manager |> Signature.V2.Public_key.hash |> Tezos_types.Contract.of_implicit |> Tezlink_durable_storage.Path.account |> String.split_on_char '/' |> clean_path @@ -256,7 +256,8 @@ let make_l2 ~eth_bootstrap_balance ~tez_bootstrap_balance in Installer_config.to_file (config_instrs @ world_state_instrs) ~output -let make_tezos_bootstrap_instr tez_bootstrap_balance tez_bootstrap_accounts = +let make_tezos_bootstrap_instr tez_bootstrap_balance + (tez_bootstrap_accounts : Signature.V2.public_key list) = List.map (fun manager -> let tezos_account_info = @@ -267,7 +268,7 @@ let make_tezos_bootstrap_instr tez_bootstrap_balance tez_bootstrap_accounts = public_key = Some manager; } in - let address = Signature.Public_key.hash manager in + let address = Signature.V2.Public_key.hash manager in let (Address (Hex alias)) = Tezosx.Tezos_runtime.ethereum_alias address in let payload = Tezosx.Tezos_runtime.encode_account_info tezos_account_info @@ -282,7 +283,7 @@ let make_tezos_bootstrap_instr tez_bootstrap_balance tez_bootstrap_accounts = "world_state"; "eth_accounts"; "tezos"; - Signature.Public_key_hash.to_b58check address; + Signature.V2.Public_key_hash.to_b58check address; ] (Some ("info", Bytes.to_string payload)) @ make_instr diff --git a/etherlink/bin_node/lib_dev/kernel_config.mli b/etherlink/bin_node/lib_dev/kernel_config.mli index 9ba43fd481d3d65b9bdc9cf1255fb105b86b0e1d..b247fd1d47a0f3ff8488884aabd156f35f8ccc8d 100644 --- a/etherlink/bin_node/lib_dev/kernel_config.mli +++ b/etherlink/bin_node/lib_dev/kernel_config.mli @@ -45,7 +45,7 @@ val make : ?max_delayed_inbox_blueprint_length:string * string -> ?evm_version:evm_version -> ?with_runtimes:Tezosx.runtime list -> - ?tez_bootstrap_accounts:Signature.public_key list -> + ?tez_bootstrap_accounts:Signature.V2.public_key list -> tez_bootstrap_balance:Tezos_types.Tez.t -> output:string -> unit -> @@ -58,7 +58,7 @@ val make_l2 : eth_bootstrap_balance:Z.t -> tez_bootstrap_balance:Tezos_types.Tez.t -> ?eth_bootstrap_accounts:Ethereum_types.address list -> - ?tez_bootstrap_accounts:Signature.Public_key.t list -> + ?tez_bootstrap_accounts:Signature.V2.Public_key.t list -> ?tez_bootstrap_contracts:(Tezos_types.Contract.t * string * string) list -> ?minimum_base_fee_per_gas:string * string -> ?da_fee_per_byte:string * string -> diff --git a/etherlink/bin_node/lib_dev/sequencer.ml b/etherlink/bin_node/lib_dev/sequencer.ml index a42ee46a7d26676b79364c0048270947250780c6..0ad2b2ca6d723307167679d9a5de7871600fc60b 100644 --- a/etherlink/bin_node/lib_dev/sequencer.ml +++ b/etherlink/bin_node/lib_dev/sequencer.ml @@ -8,7 +8,7 @@ type tezlink_sandbox = { chain_id : int; - funded_addresses : Signature.public_key list; + funded_addresses : Signature.V2.public_key list; } type sandbox_config = { @@ -352,7 +352,7 @@ let main ~cctxt ?(genesis_timestamp = Misc.now ()) (fun pk -> let contract = Tezos_types.Contract.of_implicit - (Signature.Public_key.hash pk) + (Signature.V2.Public_key.hash pk) in (* Patch the balance of bootstrap accounts *) let* () = diff --git a/etherlink/bin_node/lib_dev/services.ml b/etherlink/bin_node/lib_dev/services.ml index be51de2590ccc139831227b7444e8d733fa28912..6a3e93a652e9250ecd4228e84266dc3e8620ce56 100644 --- a/etherlink/bin_node/lib_dev/services.ml +++ b/etherlink/bin_node/lib_dev/services.ml @@ -981,7 +981,9 @@ let dispatch_request (type f) ~websocket build_with_input ~f module_ parameters | Tezosx.Get_tezos_ethereum_address.Method -> let f tezos_address = - tezos_address |> Tezosx_mod.Tezos_runtime.ethereum_alias |> rpc_ok + Tezos_crypto.Signature.V2.Of_V_latest.get_public_key_hash_exn + tezos_address + |> Tezosx_mod.Tezos_runtime.ethereum_alias |> rpc_ok in build_with_input ~f module_ parameters | Get_storage_at.Method -> diff --git a/etherlink/bin_node/lib_dev/tezlink/tezlink_backend_sig.ml b/etherlink/bin_node/lib_dev/tezlink/tezlink_backend_sig.ml index 065d05fec71e23f08ad11a09ac7e95c2382a7a39..5cb18c48834732bc3f4613dafa086aed764f5dfd 100644 --- a/etherlink/bin_node/lib_dev/tezlink/tezlink_backend_sig.ml +++ b/etherlink/bin_node/lib_dev/tezlink/tezlink_backend_sig.ml @@ -44,7 +44,7 @@ module type S = sig [`Main] -> block_param -> Tezos_types.Contract.t -> - Signature.Public_key.t option tzresult Lwt.t + Signature.V2.Public_key.t option tzresult Lwt.t val counter : [`Main] -> block_param -> Tezos_types.Contract.t -> Z.t tzresult Lwt.t diff --git a/etherlink/bin_node/lib_dev/tezlink/tezlink_mock.ml b/etherlink/bin_node/lib_dev/tezlink/tezlink_mock.ml index 4cf84fbe0916d025d4e55d6932be738220aab98e..25d4e570868009a547bd1a876dca484f1a8cbca3 100644 --- a/etherlink/bin_node/lib_dev/tezlink/tezlink_mock.ml +++ b/etherlink/bin_node/lib_dev/tezlink/tezlink_mock.ml @@ -42,7 +42,7 @@ let baker_initial_balance = 200000000000L let baker_initial_deposit = Alpha_context.Tez.of_mutez_exn baker_initial_balance -let faucet_public_key_hash = Signature.Public_key_hash.zero +let faucet_public_key_hash = Signature.V2.Public_key_hash.zero let baker_account = let public_key_internal = diff --git a/etherlink/bin_node/lib_dev/tezlink/tezos_types.ml b/etherlink/bin_node/lib_dev/tezlink/tezos_types.ml index 80ba0a4eb27e964d35ad760bf41ec11ad2cd636d..568cded5d719a9b7f8b11c7887a568947a57d23d 100644 --- a/etherlink/bin_node/lib_dev/tezlink/tezos_types.ml +++ b/etherlink/bin_node/lib_dev/tezlink/tezos_types.ml @@ -82,7 +82,7 @@ module Operation = struct module ImportedOperation = Tezlink_imports.Alpha_context.Operation type t = { - source : Signature.public_key_hash; + source : Signature.V2.public_key_hash; first_counter : Z.t; length : int; op : Tezlink_imports.Alpha_context.packed_operation; @@ -108,7 +108,7 @@ module Operation = struct (fun (source, first_counter, length, op, raw, fee, gas_limit) -> {source; first_counter; length; op; raw; fee; gas_limit}) (tup7 - Signature.Public_key_hash.encoding + Signature.V2.Public_key_hash.encoding z int31 (dynamic_size Tezlink_imports.Alpha_context.Operation.encoding) diff --git a/etherlink/bin_node/lib_dev/tezlink/tezos_types.mli b/etherlink/bin_node/lib_dev/tezlink/tezos_types.mli index b34360f5d6560b17ff0af17bc495466494e61b21..d6ba960596d66bd9fec8bd7d7411d14fdd4596e6 100644 --- a/etherlink/bin_node/lib_dev/tezlink/tezos_types.mli +++ b/etherlink/bin_node/lib_dev/tezlink/tezos_types.mli @@ -39,13 +39,13 @@ module Contract : sig val encoding : t Data_encoding.t - val implicit_encoding : Signature.public_key_hash Data_encoding.t + val implicit_encoding : Signature.V2.public_key_hash Data_encoding.t val of_b58check : string -> t tzresult - val of_implicit : Signature.public_key_hash -> t + val of_implicit : Signature.V2.public_key_hash -> t - val of_hex : string -> Signature.public_key_hash option + val of_hex : string -> Signature.V2.public_key_hash option end module Tez : sig @@ -58,7 +58,7 @@ end module Operation : sig type t = { - source : Signature.public_key_hash; + source : Signature.V2.public_key_hash; first_counter : Z.t; length : int; op : Tezlink_imports.Alpha_context.packed_operation; diff --git a/etherlink/bin_node/lib_dev/tezlink_prevalidation.ml b/etherlink/bin_node/lib_dev/tezlink_prevalidation.ml index 532a30a0691cf8c73029409f87084447a9c8af43..19214533aa76ed4ac93f01b0b461a2f4ad189377 100644 --- a/etherlink/bin_node/lib_dev/tezlink_prevalidation.ml +++ b/etherlink/bin_node/lib_dev/tezlink_prevalidation.ml @@ -28,7 +28,7 @@ type error += | Not_a_manager_operation of clue | Unsupported_manager_operation of clue | Oversized_operation of clue * int * int - | Bls_is_not_allowed of clue * public_key_hash + | Bls_is_not_allowed of clue * Signature.V2.public_key_hash let () = register_error_kind @@ -44,12 +44,12 @@ let () = "Failed to validate operation %a: tz4 keys are forbidden %a" pp_clue clue - Signature.Public_key_hash.pp + Signature.V2.Public_key_hash.pp key) Data_encoding.( obj2 (req "operation" clue_encoding) - (req "key" Signature.Public_key_hash.encoding)) + (req "key" Signature.V2.Public_key_hash.encoding)) (function Bls_is_not_allowed (clue, err) -> Some (clue, err) | _ -> None) (fun (clue, err) -> Bls_is_not_allowed (clue, err)) ; register_error_kind @@ -145,7 +145,7 @@ let validate_manager_info ~read ~error_clue (Contents op : packed_contents) = | Some (Hash _), Reveal {public_key; _} -> (* The operation might be the reveal of the public key we're searching for. *) - let open Signature in + let open Signature.V2 in let pkh_revealed = Public_key.hash public_key in if Public_key_hash.equal source pkh_revealed then return @@ Ok (public_key, source, counter) @@ -216,7 +216,7 @@ let validate_reveal ctxt (Reveal {public_key; _}) = | Bls _ -> tzfail @@ Bls_is_not_allowed - (ctxt.error_clue, Signature.Public_key.hash public_key) + (ctxt.error_clue, Signature.V2.Public_key.hash public_key) | _ -> return (Ok ()) let validate_transaction ctxt (Transaction {destination; _}) = @@ -238,7 +238,7 @@ let validate_supported_operation (type kind) ~ctxt let validate_source ~ctxt second_source = let open Lwt_result_syntax in - if Signature.Public_key_hash.equal ctxt.source second_source then + if Signature.V2.Public_key_hash.equal ctxt.source second_source then return (Ok ()) else tzfail_p @@ -469,7 +469,7 @@ let validate_signature ~check_signature shell contents pk signature = if (* That watermark ({!Generic_operation}) is used for all operations except endorsement, which we don't support. *) - Signature.check + Signature.V2.check ~watermark:Generic_operation pk signature @@ -566,12 +566,12 @@ let gas_limit_could_fit state (operation : Tezos_types.Operation.t) = Z.(state.gas + operation.gas_limit <= maximum_gas_per_block) let add_ source cache = - String.Map.add (Signature.Public_key_hash.to_b58check source) cache + String.Map.add (Signature.V2.Public_key_hash.to_b58check source) cache let get_ read cache source = let open Lwt_result_syntax in match - String.Map.find (Signature.Public_key_hash.to_b58check source) cache + String.Map.find (Signature.V2.Public_key_hash.to_b58check source) cache with | Some v -> return v | None -> read (Tezlink_imports.Alpha_context.Contract.Implicit source) diff --git a/etherlink/bin_node/lib_dev/tezosx.ml b/etherlink/bin_node/lib_dev/tezosx.ml index d8cf0b31e87e5c6562415dc8ba062482d7a20f36..afe81c0dca62b168a4bbd2f78d2d25544fa83aa7 100644 --- a/etherlink/bin_node/lib_dev/tezosx.ml +++ b/etherlink/bin_node/lib_dev/tezosx.ml @@ -36,18 +36,18 @@ module Ethereum_runtime = struct end module Tezos_runtime = struct - type address = Signature.public_key_hash + type address = Signature.V2.public_key_hash let address_of_string address = let open Result_syntax in - match Tezos_crypto.Signature.Public_key_hash.of_b58check address with + match Tezos_crypto.Signature.V2.Public_key_hash.of_b58check address with | Ok tezos_kh -> return tezos_kh | Error _ -> tzfail (error_of_fmt "%s is not a valid Tezos address" address) type account_info = { balance : Tezos_types.Tez.t; nonce : int64; - public_key : Signature.public_key option; + public_key : Signature.V2.public_key option; } let decode_account_info bytes = @@ -73,7 +73,7 @@ module Tezos_runtime = struct | Value b when b = Bytes.empty -> return_none | Value value -> return_some - (Signature.Public_key.of_b58check_exn + (Signature.V2.Public_key.of_b58check_exn (Bytes.unsafe_to_string value)) | _ -> fail "public key" () in @@ -99,14 +99,14 @@ module Tezos_runtime = struct match public_key with | Some public_key -> Rlp.Value - (Bytes.of_string (Signature.Public_key.to_b58check public_key)) + (Bytes.of_string (Signature.V2.Public_key.to_b58check public_key)) | None -> Value Bytes.empty in Rlp.encode Rlp.(List [Value balance; Value nonce; public_key]) let ethereum_alias pkh = let (`Hex alias) = - let str = Signature.Public_key_hash.to_b58check pkh in + let str = Signature.V2.Public_key_hash.to_b58check pkh in let alias = Tezos_crypto.Hacl.Hash.Keccak_256.digest (Bytes.unsafe_of_string str) in @@ -135,7 +135,8 @@ module Foreign_address = struct [ Value (Bytes.unsafe_of_string "\x01"); Value - (Bytes.of_string (Signature.Public_key_hash.to_b58check pkh)); + (Bytes.of_string + (Signature.V2.Public_key_hash.to_b58check pkh)); ]) end @@ -144,7 +145,7 @@ module Durable_storage_path = struct module Tezos = struct let info pkh = "/evm/world_state/eth_accounts/tezos/" - ^ Signature.Public_key_hash.to_b58check pkh + ^ Signature.V2.Public_key_hash.to_b58check pkh ^ "/info" let ethereum_alias pkh = diff --git a/etherlink/bin_node/lib_dev/tezosx.mli b/etherlink/bin_node/lib_dev/tezosx.mli index bc85de6b1e285aa2de924b93e93970d8fd6d8fdc..939adf8d9d97f8cdf8b45a8b35935ec5bccd29a8 100644 --- a/etherlink/bin_node/lib_dev/tezosx.mli +++ b/etherlink/bin_node/lib_dev/tezosx.mli @@ -30,19 +30,19 @@ module Ethereum_runtime : sig end module Tezos_runtime : sig - type address = Signature.public_key_hash + type address = Signature.V2.public_key_hash type account_info = { balance : Tezos_types.Tez.t; nonce : int64; - public_key : Signature.public_key option; + public_key : Signature.V2.public_key option; } val decode_account_info : bytes -> account_info tzresult val encode_account_info : account_info -> bytes - val ethereum_alias : Signature.public_key_hash -> Ethereum_types.address + val ethereum_alias : Signature.V2.public_key_hash -> Ethereum_types.address end module Foreign_address : sig diff --git a/etherlink/bin_node/lib_dev/tx_queue_types.ml b/etherlink/bin_node/lib_dev/tx_queue_types.ml index 03809ef1243e342a3292e3afa714b52569a7d59c..1096e9c623bdc11cc8a7bb5bd5b6dfa19191b22e 100644 --- a/etherlink/bin_node/lib_dev/tx_queue_types.ml +++ b/etherlink/bin_node/lib_dev/tx_queue_types.ml @@ -93,15 +93,15 @@ module Tezlink_operation : and type nonce = tezlink_batch_nonces = struct type t = Tezos_types.Operation.t - type address = Signature.public_key_hash + type address = Signature.V2.public_key_hash type nonce = tezlink_batch_nonces - let address_encoding = Signature.Public_key_hash.encoding + let address_encoding = Signature.V2.Public_key_hash.encoding let hash_of_tx_object = Tezos_types.Operation.hash_operation - let address_to_string = Signature.Public_key_hash.to_string + let address_to_string = Signature.V2.Public_key_hash.to_string let from_address_of_tx_object (op : Tezos_types.Operation.t) = op.source @@ -122,7 +122,7 @@ module Tezlink_operation : let to_transaction_object_t t = Michelson t - module AddressMap = Map.Make (Signature.Public_key_hash) + module AddressMap = Map.Make (Signature.V2.Public_key_hash) let make_txpool ~pending:_ ~queued:_ : Transaction_object.txqueue_content = { diff --git a/etherlink/bin_node/main.ml b/etherlink/bin_node/main.ml index 11325705997a32bdc0e8aa3a8d6948d2ff8aded3..c32ac5e2ca1ba33a1c55a2464b200eb96ee23e19 100644 --- a/etherlink/bin_node/main.ml +++ b/etherlink/bin_node/main.ml @@ -229,7 +229,7 @@ module Params = struct let tez_account = Tezos_clic.parameter (fun _ public_key -> - Lwt.return (Signature.Public_key.of_b58check public_key)) + Lwt.return (Signature.V2.Public_key.of_b58check public_key)) let tezosx_account = Tezos_clic.parameter @@ fun _ address -> diff --git a/etherlink/tezt/tests/expected/evm_rollup.ml/EVM node- list events regression.out b/etherlink/tezt/tests/expected/evm_rollup.ml/EVM node- list events regression.out index c692ebf665cd72d89837c734530f33faa0a9c83b..186d4b66f6c629770382dad8e7aa71737305d80b 100644 --- a/etherlink/tezt/tests/expected/evm_rollup.ml/EVM node- list events regression.out +++ b/etherlink/tezt/tests/expected/evm_rollup.ml/EVM node- list events regression.out @@ -10,7 +10,7 @@ applied_sequencer_upgrade: { "sequencer": $Signature.Public_key, "level": $positive_bignum } } $Signature.Public_key: - /* A Ed25519, Secp256k1, or P256 public key (Base58Check-encoded) */ + /* A Ed25519, Secp256k1, P256 or a BLS public key (Base58Check-encoded) */ $unistring $positive_bignum: /* Positive big number @@ -320,7 +320,7 @@ blueprints_publisher_request_failed: "number": $unistring, "nb_chunks": integer ∈ [-2^30, 2^30], "chunk_index": integer ∈ [-2^30, 2^30], - "signature": $Signature.V2 } ... ], + "signature": $Signature.V3 } ... ], "inbox_payload": [ $unistring ... ] } || [ $unistring ... ] /* Inbox */ } @@ -328,7 +328,7 @@ blueprints_publisher_request_failed: "request": "new_l2_head", "rollup_head": integer ∈ [-2^31-1, 2^31] }, "errors": any } } - $Signature.V2: + $Signature.V3: /* A Ed25519, Secp256k1, P256 or BLS signature (Base58Check-encoded) */ $unistring $positive_bignum: @@ -749,7 +749,7 @@ evm_context_request_failed: "number": $unistring }, "errors": any } } $Signature.Public_key: - /* A Ed25519, Secp256k1, or P256 public key (Base58Check-encoded) */ + /* A Ed25519, Secp256k1, P256 or a BLS public key (Base58Check-encoded) */ $unistring $bignum: /* Big number @@ -990,7 +990,7 @@ evm_events_new_event: $timestamp.protocol, $positive_bignum ] } } $Signature.Public_key: - /* A Ed25519, Secp256k1, or P256 public key (Base58Check-encoded) */ + /* A Ed25519, Secp256k1, P256 or a BLS public key (Base58Check-encoded) */ $unistring $bignum: /* Big number @@ -1059,7 +1059,7 @@ failed_sequencer_upgrade: "current_sequencer": $Signature.Public_key /* Some */ || null /* None */, "level": $positive_bignum } } $Signature.Public_key: - /* A Ed25519, Secp256k1, or P256 public key (Base58Check-encoded) */ + /* A Ed25519, Secp256k1, P256 or a BLS public key (Base58Check-encoded) */ $unistring $positive_bignum: /* Positive big number @@ -1132,7 +1132,7 @@ gcp_kms_is_ready: { /* gcp_kms_is_ready version 0 */ "gcp_kms_is_ready.v0": $Signature.Public_key } $Signature.Public_key: - /* A Ed25519, Secp256k1, or P256 public key (Base58Check-encoded) */ + /* A Ed25519, Secp256k1, P256 or a BLS public key (Base58Check-encoded) */ $unistring $unistring: /* Universal string representation @@ -1481,7 +1481,7 @@ patched_sequencer_key: { "pk": $Signature.Public_key, "pkh": $Signature.Public_key_hash } } $Signature.Public_key: - /* A Ed25519, Secp256k1, or P256 public key (Base58Check-encoded) */ + /* A Ed25519, Secp256k1, P256 or a BLS public key (Base58Check-encoded) */ $unistring $Signature.Public_key_hash: /* A Ed25519, Secp256k1, P256, or BLS public key hash (Base58Check-encoded) */ @@ -1517,7 +1517,7 @@ pending_sequencer_upgrade: "pool_address": $unistring, "timestamp": $timestamp.protocol } } $Signature.Public_key: - /* A Ed25519, Secp256k1, or P256 public key (Base58Check-encoded) */ + /* A Ed25519, Secp256k1, P256 or a BLS public key (Base58Check-encoded) */ $unistring $timestamp.protocol: /* A timestamp as seen by the protocol: second-level precision, epoch based. */ @@ -2521,7 +2521,7 @@ blueprints_publisher_request_failed: "number": $unistring, "nb_chunks": integer ∈ [-2^30, 2^30], "chunk_index": integer ∈ [-2^30, 2^30], - "signature": $Signature.V2 } ... ], + "signature": $Signature.V3 } ... ], "inbox_payload": [ $unistring ... ] } || [ $unistring ... ] /* Inbox */ } @@ -2529,7 +2529,7 @@ blueprints_publisher_request_failed: "request": "new_l2_head", "rollup_head": integer ∈ [-2^31-1, 2^31] }, "errors": any } } - $Signature.V2: + $Signature.V3: /* A Ed25519, Secp256k1, P256 or BLS signature (Base58Check-encoded) */ $unistring $positive_bignum: @@ -2699,7 +2699,7 @@ evm_context_request_failed: "number": $unistring }, "errors": any } } $Signature.Public_key: - /* A Ed25519, Secp256k1, or P256 public key (Base58Check-encoded) */ + /* A Ed25519, Secp256k1, P256 or a BLS public key (Base58Check-encoded) */ $unistring $bignum: /* Big number diff --git a/src/bin_signer/handler.ml b/src/bin_signer/handler.ml index 027f72a388a6ca20e15d3aa934534d6b09f38f5f..66f3027c4384056e75b9d76fafe6f6b9344e5b28 100644 --- a/src/bin_signer/handler.ml +++ b/src/bin_signer/handler.ml @@ -301,7 +301,10 @@ let sign ?signing_version ?magic_bytes ~check_high_watermark ~require_auth | Some Version_1 -> let* s = Client_keys.V1.sign cctxt sk_uri bytes in return (Signature.V_latest.Of_V1.signature s) - | Some Version_2 -> Client_keys.V2.sign cctxt sk_uri bytes + | Some Version_2 -> + let* s = Client_keys.V2.sign cctxt sk_uri bytes in + return (Signature.V_latest.Of_V2.signature s) + | Some Version_3 -> Client_keys.V3.sign cctxt sk_uri bytes | None -> Client_keys.V_latest.sign cctxt sk_uri bytes in if check_high_watermark then diff --git a/src/lib_client_base/client_keys.ml b/src/lib_client_base/client_keys.ml index 59e243fd648e7fef272e19eddae718424697b496..1aab8f84e8a60cf6a4ad7ddda83bfcc0827afe9a 100644 --- a/src/lib_client_base/client_keys.ml +++ b/src/lib_client_base/client_keys.ml @@ -1040,6 +1040,41 @@ module V2 = Make (struct let generate_key = generate_key ?algo:None + module Adapter = struct + let public_key_hash : + Tezos_crypto.Signature.Public_key_hash.t -> Public_key_hash.t tzresult = + let open Result_syntax in + function + | Bls k -> return (Bls k : Public_key_hash.t) + | Ed25519 k -> return (Ed25519 k : Public_key_hash.t) + | Secp256k1 k -> return (Secp256k1 k : Public_key_hash.t) + | P256 k -> return (P256 k : Public_key_hash.t) + + let public_key : + Tezos_crypto.Signature.Public_key.t -> Public_key.t tzresult = + let open Result_syntax in + function + | Bls k -> return (Bls k : Public_key.t) + | Ed25519 k -> return (Ed25519 k : Public_key.t) + | Secp256k1 k -> return (Secp256k1 k : Public_key.t) + | P256 k -> return (P256 k : Public_key.t) + + let signature : Tezos_crypto.Signature.t -> t tzresult = + let open Result_syntax in + function + | Bls k -> return (Bls k : t) + | Ed25519 k -> return (Ed25519 k : t) + | Secp256k1 k -> return (Secp256k1 k : t) + | P256 k -> return (P256 k : t) + | Unknown k -> return (Unknown k : t) + end +end) + +module V3 = Make (struct + include Tezos_crypto.Signature.V3 + + let generate_key = generate_key ?algo:None + module Adapter = struct let identity x = Ok x diff --git a/src/lib_client_base/client_keys.mli b/src/lib_client_base/client_keys.mli index 4b360684bff5a14e8f677304eee8ab249bac0651..5b778d07345854106426494debee4a83a3758811 100644 --- a/src/lib_client_base/client_keys.mli +++ b/src/lib_client_base/client_keys.mli @@ -322,6 +322,14 @@ module V2 : and type watermark := Tezos_crypto.Signature.V2.watermark and type signature := Tezos_crypto.Signature.V2.t +module V3 : + S + with type public_key_hash := Tezos_crypto.Signature.V3.Public_key_hash.t + and type public_key := Tezos_crypto.Signature.V3.Public_key.t + and type secret_key := Tezos_crypto.Signature.V3.Secret_key.t + and type watermark := Tezos_crypto.Signature.V3.watermark + and type signature := Tezos_crypto.Signature.V3.t + module V_latest : S with type public_key_hash := diff --git a/src/lib_client_base_unix/client_main_run.ml b/src/lib_client_base_unix/client_main_run.ml index 159be45ac395d6746a1cdd5ed56a2eae64af4be5..9397c9e199647bf11e21a5ccb7070a92143d00ce 100644 --- a/src/lib_client_base_unix/client_main_run.ml +++ b/src/lib_client_base_unix/client_main_run.ml @@ -116,7 +116,10 @@ let register_default_signer ?signing_version ?other_registrations ?logger | Some Signature.Version_1 -> let* sign = Client_keys.V1.sign cctxt sk_uri payload in return @@ Signature.V_latest.Of_V1.signature sign - | Some Signature.Version_2 -> Client_keys.V2.sign cctxt sk_uri payload + | Some Signature.Version_2 -> + let* sign = Client_keys.V2.sign cctxt sk_uri payload in + return @@ Signature.V_latest.Of_V2.signature sign + | Some Signature.Version_3 -> Client_keys.V3.sign cctxt sk_uri payload | None -> Client_keys.V_latest.sign cctxt sk_uri payload) | [] -> failwith diff --git a/src/lib_crypto/signature.ml b/src/lib_crypto/signature.ml index f87d15f63bdb482efb41bc35f9e2b6edca6224c8..039fb6634e3bfbfeb70e63c5beb25f290b32e01e 100644 --- a/src/lib_crypto/signature.ml +++ b/src/lib_crypto/signature.ml @@ -25,16 +25,18 @@ (* *) (*****************************************************************************) -type version = Version_0 | Version_1 | Version_2 +type version = Version_0 | Version_1 | Version_2 | Version_3 let version_encoding = let open Data_encoding in conv_with_guard - (function Version_0 -> 0 | Version_1 -> 1 | Version_2 -> 2) + (function + | Version_0 -> 0 | Version_1 -> 1 | Version_2 -> 2 | Version_3 -> 3) (function | 0 -> Ok Version_0 | 1 -> Ok Version_1 | 2 -> Ok Version_2 + | 3 -> Ok Version_3 | _ -> Error "Invalid signature version") int8 @@ -47,9 +49,13 @@ let version_arg = | "0" -> Ok Version_0 | "1" -> Ok Version_1 | "2" -> Ok Version_2 + | "3" -> Ok Version_3 | _ -> Error "Invalid signature version") ~construct:(function - | Version_0 -> "0" | Version_1 -> "1" | Version_2 -> "2") + | Version_0 -> "0" + | Version_1 -> "1" + | Version_2 -> "2" + | Version_3 -> "3") () module type CONV = sig @@ -91,15 +97,20 @@ module type CONV_OPT = sig val get_public_key_hash_exn : V_from.Public_key_hash.t -> V_to.Public_key_hash.t + val get_secret_key : + V_from.Secret_key.t -> V_to.Secret_key.t Error_monad.tzresult + + val get_secret_key_exn : V_from.Secret_key.t -> V_to.Secret_key.t + val get_signature : V_from.t -> V_to.t Error_monad.tzresult val get_signature_exn : V_from.t -> V_to.t end module V_latest = struct - let version = Version_2 + let version = Version_3 - include Signature_v2 + include Signature_v3 end module V0 = struct @@ -168,6 +179,22 @@ module V0 = struct "Conversion of public key hash from latest signature version to V0 \ impossible." + let get_secret_key s = + match secret_key s with + | Some s -> Ok s + | None -> + Error_monad.error_with + "Conversion of secret key from latest signature version to V0 \ + impossible." + + let get_secret_key_exn s = + match secret_key s with + | Some s -> s + | None -> + Stdlib.failwith + "Conversion of secret key from latest signature version to V0 \ + impossible." + let get_signature s = match signature s with | Some s -> Ok s @@ -252,6 +279,22 @@ module V1 = struct "Conversion of public key hash from latest signature version to V1 \ impossible." + let get_secret_key s = + match secret_key s with + | Some s -> Ok s + | None -> + Error_monad.error_with + "Conversion of secret key from latest signature version to V1 \ + impossible." + + let get_secret_key_exn s = + match secret_key s with + | Some s -> s + | None -> + Stdlib.failwith + "Conversion of secret key from latest signature version to V1 \ + impossible." + let get_signature s = match signature s with | Some s -> Ok s @@ -278,13 +321,31 @@ module V2 = struct module Of_V_latest : CONV_OPT with module V_from := V_latest and module V_to := Signature_v2 = struct - let public_key_hash = Option.some + let public_key_hash : V_latest.Public_key_hash.t -> Public_key_hash.t option + = function + | V_latest.Ed25519 k -> Some (Ed25519 k) + | V_latest.Secp256k1 k -> Some (Secp256k1 k) + | V_latest.P256 k -> Some (P256 k) + | V_latest.Bls k -> Some (Bls k) - let public_key = Option.some + let public_key : V_latest.Public_key.t -> Public_key.t option = function + | V_latest.Ed25519 k -> Some (Ed25519 k) + | V_latest.Secp256k1 k -> Some (Secp256k1 k) + | V_latest.P256 k -> Some (P256 k) + | V_latest.Bls k -> Some (Bls k) - let secret_key = Option.some + let secret_key : V_latest.Secret_key.t -> Secret_key.t option = function + | V_latest.Ed25519 k -> Some (Ed25519 k) + | V_latest.Secp256k1 k -> Some (Secp256k1 k) + | V_latest.P256 k -> Some (P256 k) + | V_latest.Bls k -> Some (Bls k) - let signature = Option.some + let signature : V_latest.t -> t option = function + | V_latest.Ed25519 k -> Some (Ed25519 k) + | V_latest.Secp256k1 k -> Some (Secp256k1 k) + | V_latest.P256 k -> Some (P256 k) + | V_latest.Unknown k -> Some (Unknown k) + | V_latest.Bls k -> Some (Bls k) let get_public_key pk = match public_key pk with @@ -318,6 +379,22 @@ module V2 = struct "Conversion of public key hash from latest signature version to V2 \ impossible." + let get_secret_key s = + match secret_key s with + | Some s -> Ok s + | None -> + Error_monad.error_with + "Conversion of secret key from latest signature version to V2 \ + impossible." + + let get_secret_key_exn s = + match secret_key s with + | Some s -> s + | None -> + Stdlib.failwith + "Conversion of secret key from latest signature version to V2 \ + impossible." + let get_signature s = match signature s with | Some s -> Ok s @@ -336,10 +413,93 @@ module V2 = struct end end +module V3 = struct + include Signature_v3 + + let version = Version_3 + + module Of_V_latest : + CONV_OPT with module V_from := V_latest and module V_to := Signature_v3 = + struct + let public_key_hash = Option.some + + let public_key = Option.some + + let secret_key = Option.some + + let signature = Option.some + + let get_public_key pk = + match public_key pk with + | Some pk -> Ok pk + | None -> + Error_monad.error_with + "Conversion of public key from latest signature version to V3 \ + impossible." + + let get_public_key_exn pk = + match public_key pk with + | Some pk -> pk + | None -> + Stdlib.failwith + "Conversion of public key hash from latest signature version to V3 \ + impossible." + + let get_public_key_hash pkh = + match public_key_hash pkh with + | Some pkh -> Ok pkh + | None -> + Error_monad.error_with + "Conversion of public key hash from latest signature version to V3 \ + impossible." + + let get_public_key_hash_exn pkh = + match public_key_hash pkh with + | Some pkh -> pkh + | None -> + Stdlib.failwith + "Conversion of public key hash from latest signature version to V3 \ + impossible." + + let get_secret_key s = + match secret_key s with + | Some s -> Ok s + | None -> + Error_monad.error_with + "Conversion of secret key from latest signature version to V3 \ + impossible." + + let get_secret_key_exn s = + match secret_key s with + | Some s -> s + | None -> + Stdlib.failwith + "Conversion of secret key from latest signature version to V3 \ + impossible." + + let get_signature s = + match signature s with + | Some s -> Ok s + | None -> + Error_monad.error_with + "Conversion of signature from latest signature version to V3 \ + impossible." + + let get_signature_exn s = + match signature s with + | Some s -> s + | None -> + Stdlib.failwith + "Conversion of signature from latest signature version to V3 \ + impossible." + end +end + include V_latest -module Of_V_latest = V2.Of_V_latest +module Of_V_latest = V3.Of_V_latest -module Of_V2 : CONV with module V_from := V2 and module V_to := V2 = struct +module Of_V3 : CONV with module V_from := V3 and module V_to := V_latest = +struct let public_key_hash = Fun.id let public_key = Fun.id diff --git a/src/lib_crypto/signature.mli b/src/lib_crypto/signature.mli index af89933f7c1464ad36ad41ec6b959c828c0c21a2..f31975929682f6ff347451985999fdbf36d7ab53 100644 --- a/src/lib_crypto/signature.mli +++ b/src/lib_crypto/signature.mli @@ -28,7 +28,7 @@ (** Cryptographic signatures are versioned to expose different versions to different protocols, depending on the support. *) -type version = Version_0 | Version_1 | Version_2 +type version = Version_0 | Version_1 | Version_2 | Version_3 val version_encoding : version Data_encoding.t @@ -75,6 +75,11 @@ module type CONV_OPT = sig val get_public_key_hash_exn : V_from.Public_key_hash.t -> V_to.Public_key_hash.t + val get_secret_key : + V_from.Secret_key.t -> V_to.Secret_key.t Error_monad.tzresult + + val get_secret_key_exn : V_from.Secret_key.t -> V_to.Secret_key.t + val get_signature : V_from.t -> V_to.t Error_monad.tzresult val get_signature_exn : V_from.t -> V_to.t @@ -85,7 +90,7 @@ end module V_latest : sig val version : version - include module type of Signature_v2 + include module type of Signature_v3 end (** [V0] supports Ed25519, Secp256k1, and P256. *) @@ -110,7 +115,7 @@ module V1 : sig CONV_OPT with module V_from := V_latest and module V_to := Signature_v1 end -(** [V2] supports Ed25519, Secp256k1, P256, and BLS (aug). *) +(** [V2] supports Ed25519, Secp256k1, P256, and BLS (pop). *) module V2 : sig val version : version @@ -121,6 +126,17 @@ module V2 : sig CONV_OPT with module V_from := V_latest and module V_to := Signature_v2 end +(** [V3] supports Ed25519, Secp256k1, P256, and BLS (pop). *) +module V3 : sig + val version : version + + include module type of Signature_v3 + + (** Converting from signatures of {!V_latest} to {!V3}. *) + module Of_V_latest : + CONV_OPT with module V_from := V_latest and module V_to := Signature_v3 +end + include module type of V_latest (** Converting from signatures of {!V_latest} to {!V_latest}. This module @@ -138,3 +154,6 @@ module Of_V1 : CONV with module V_from := V1 and module V_to := V_latest (** Converting from signatures of {!V2} to {!V_latest}. *) module Of_V2 : CONV with module V_from := V2 and module V_to := V_latest + +(** Converting from signatures of {!V3} to {!V_latest}. *) +module Of_V3 : CONV with module V_from := V3 and module V_to := V_latest diff --git a/src/lib_crypto/signature_v3.ml b/src/lib_crypto/signature_v3.ml new file mode 100644 index 0000000000000000000000000000000000000000..54c5440d6553fe16518767924dcf6b4aebfe1686 --- /dev/null +++ b/src/lib_crypto/signature_v3.ml @@ -0,0 +1,1025 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* SPDX-FileCopyrightText: 2025 Nomadic Labs *) +(* *) +(*****************************************************************************) + +open Error_monad + +type public_key_hash = + | Ed25519 of Ed25519.Public_key_hash.t + | Secp256k1 of Secp256k1.Public_key_hash.t + | P256 of P256.Public_key_hash.t + | Bls of Bls.Public_key_hash.t + +type public_key = + | Ed25519 of Ed25519.Public_key.t + | Secp256k1 of Secp256k1.Public_key.t + | P256 of P256.Public_key.t + | Bls of Bls.Public_key.t + +type secret_key = + | Ed25519 of Ed25519.Secret_key.t + | Secp256k1 of Secp256k1.Secret_key.t + | P256 of P256.Secret_key.t + | Bls of Bls.Secret_key.t + +type watermark = Signature_v0.watermark = + | Block_header of Chain_id.t + | Endorsement of Chain_id.t + | Generic_operation + | Custom of Bytes.t + +module Public_key_hash = struct + type t = public_key_hash = + | Ed25519 of Ed25519.Public_key_hash.t + | Secp256k1 of Secp256k1.Public_key_hash.t + | P256 of P256.Public_key_hash.t + | Bls of Bls.Public_key_hash.t + + let name = "Signature.Public_key_hash" + + let title = "A Ed25519, Secp256k1, P256, or BLS public key hash" + + let is_bls : t -> bool = function + | Bls _ -> true + | Ed25519 _ | Secp256k1 _ | P256 _ -> false + + type Base58.data += Data of t (* unused *) + + let b58check_encoding = + (* unused *) + Base58.register_encoding + ~prefix:"\255\255" + ~length:2 + ~to_raw:(fun _ -> assert false) + ~of_raw:(fun _ -> assert false) + ~wrap:(fun x -> Data x) + + let raw_encoding = + let open Data_encoding in + def "public_key_hash" ~description:title + @@ union + [ + case + (Tag 0) + Ed25519.Public_key_hash.encoding + ~title:"Ed25519" + (function Ed25519 x -> Some x | _ -> None) + (function x -> Ed25519 x); + case + (Tag 1) + Secp256k1.Public_key_hash.encoding + ~title:"Secp256k1" + (function Secp256k1 x -> Some x | _ -> None) + (function x -> Secp256k1 x); + case + (Tag 2) + ~title:"P256" + P256.Public_key_hash.encoding + (function P256 x -> Some x | _ -> None) + (function x -> P256 x); + case + (Tag 3) + ~title:"Bls" + Bls.Public_key_hash.encoding + (function Bls x -> Some x | _ -> None) + (function x -> Bls x); + ] + + let to_bytes s = Data_encoding.Binary.to_bytes_exn raw_encoding s + + let of_bytes_opt s = Data_encoding.Binary.of_bytes_opt raw_encoding s + + let to_string s = Bytes.to_string (to_bytes s) + + let of_string_opt s = of_bytes_opt (Bytes.of_string s) + + let size = 1 + Ed25519.size + + let zero = Ed25519 Ed25519.Public_key_hash.zero + + include Helpers.MakeRaw (struct + type nonrec t = t + + let name = name + + let of_bytes_opt = of_bytes_opt + + let of_string_opt = of_string_opt + + let to_string = to_string + end) + + let of_b58check_opt s = + match Base58.decode s with + | Some (Ed25519.Public_key_hash.Data pkh) -> Some (Ed25519 pkh) + | Some (Secp256k1.Public_key_hash.Data pkh) -> Some (Secp256k1 pkh) + | Some (P256.Public_key_hash.Data pkh) -> Some (P256 pkh) + | Some (Bls.Public_key_hash.Data pkh) -> Some (Bls pkh) + | _ -> None + + let of_b58check_exn s = + match of_b58check_opt s with + | Some x -> x + | None -> Format.kasprintf Stdlib.failwith "Unexpected data (%s)" name + + let of_b58check s = + match of_b58check_opt s with + | Some x -> Ok x + | None -> + error_with "Failed to read a b58check_encoding data (%s): %S" name s + + let to_b58check = function + | Ed25519 pkh -> Ed25519.Public_key_hash.to_b58check pkh + | Secp256k1 pkh -> Secp256k1.Public_key_hash.to_b58check pkh + | P256 pkh -> P256.Public_key_hash.to_b58check pkh + | Bls pkh -> Bls.Public_key_hash.to_b58check pkh + + let to_short_b58check = function + | Ed25519 pkh -> Ed25519.Public_key_hash.to_short_b58check pkh + | Secp256k1 pkh -> Secp256k1.Public_key_hash.to_short_b58check pkh + | P256 pkh -> P256.Public_key_hash.to_short_b58check pkh + | Bls pkh -> Bls.Public_key_hash.to_short_b58check pkh + + let to_path key l = + match key with + | Ed25519 h -> "ed25519" :: Ed25519.Public_key_hash.to_path h l + | Secp256k1 h -> "secp256k1" :: Secp256k1.Public_key_hash.to_path h l + | P256 h -> "p256" :: P256.Public_key_hash.to_path h l + | Bls h -> "bls" :: Bls.Public_key_hash.to_path h l + + let of_path = function + | "ed25519" :: q -> ( + match Ed25519.Public_key_hash.of_path q with + | Some pkh -> Some (Ed25519 pkh) + | None -> None) + | "secp256k1" :: q -> ( + match Secp256k1.Public_key_hash.of_path q with + | Some pkh -> Some (Secp256k1 pkh) + | None -> None) + | "p256" :: q -> ( + match P256.Public_key_hash.of_path q with + | Some pkh -> Some (P256 pkh) + | None -> None) + | "bls" :: q -> ( + match Bls.Public_key_hash.of_path q with + | Some pkh -> Some (Bls pkh) + | None -> None) + | _ -> assert false + + (* FIXME classification des erreurs *) + + let of_path_exn = function + | "ed25519" :: q -> Ed25519 (Ed25519.Public_key_hash.of_path_exn q) + | "secp256k1" :: q -> Secp256k1 (Secp256k1.Public_key_hash.of_path_exn q) + | "p256" :: q -> P256 (P256.Public_key_hash.of_path_exn q) + | "bls" :: q -> Bls (Bls.Public_key_hash.of_path_exn q) + | _ -> assert false + + (* FIXME classification des erreurs *) + + let path_length = + let l1 = Ed25519.Public_key_hash.path_length + and l2 = Secp256k1.Public_key_hash.path_length + and l3 = P256.Public_key_hash.path_length + and l4 = Bls.Public_key_hash.path_length in + assert (Compare.Int.(l1 = l2)) ; + assert (Compare.Int.(l1 = l3)) ; + assert (Compare.Int.(l1 = l4)) ; + 1 + l1 + + let prefix_path _ = assert false (* unused *) + + let seeded_hash = Stdlib.Hashtbl.seeded_hash + + let hash = Stdlib.Hashtbl.hash + + include Compare.Make (struct + type nonrec t = t + + let compare a b = + match (a, b) with + | Ed25519 x, Ed25519 y -> Ed25519.Public_key_hash.compare x y + | Secp256k1 x, Secp256k1 y -> Secp256k1.Public_key_hash.compare x y + | P256 x, P256 y -> P256.Public_key_hash.compare x y + | Bls x, Bls y -> Bls.Public_key_hash.compare x y + | _ -> Stdlib.compare a b + end) + + include Helpers.MakeEncoder (struct + type nonrec t = t + + let name = name + + let title = title + + let raw_encoding = raw_encoding + + let of_b58check = of_b58check + + let of_b58check_opt = of_b58check_opt + + let of_b58check_exn = of_b58check_exn + + let to_b58check = to_b58check + + let to_short_b58check = to_short_b58check + end) + + include Helpers.MakeIterator (struct + type nonrec t = t + + let hash = hash + + let seeded_hash = seeded_hash + + let compare = compare + + let equal = equal + + let encoding = encoding + end) + + let rpc_arg = + Tezos_rpc.Arg.like + rpc_arg + ~descr:"A Secp256k1 of a Ed25519 public key hash (Base58Check-encoded)" + "pkh" + + module Logging = struct + let tag = Tag.def ~doc:title name pp + end +end + +module Public_key = struct + type t = public_key = + | Ed25519 of Ed25519.Public_key.t + | Secp256k1 of Secp256k1.Public_key.t + | P256 of P256.Public_key.t + | Bls of Bls.Public_key.t + + let name = "Signature.Public_key" + + let title = "A Ed25519, Secp256k1, P256 or a BLS public key" + + let hash pk = + match pk with + | Ed25519 pk -> Public_key_hash.Ed25519 (Ed25519.Public_key.hash pk) + | Secp256k1 pk -> Public_key_hash.Secp256k1 (Secp256k1.Public_key.hash pk) + | P256 pk -> Public_key_hash.P256 (P256.Public_key.hash pk) + | Bls pk -> Public_key_hash.Bls (Bls.Public_key.hash pk) + + include Compare.Make (struct + type nonrec t = t + + let compare a b = + match (a, b) with + | Ed25519 x, Ed25519 y -> Ed25519.Public_key.compare x y + | Secp256k1 x, Secp256k1 y -> Secp256k1.Public_key.compare x y + | P256 x, P256 y -> P256.Public_key.compare x y + | Bls x, Bls y -> Bls.Public_key.compare x y + | Ed25519 _, (Secp256k1 _ | P256 _ | Bls _) -> -1 + | Secp256k1 _, (P256 _ | Bls _) -> -1 + | P256 _, Bls _ -> -1 + | Bls _, (P256 _ | Secp256k1 _ | Ed25519 _) -> 1 + | P256 _, (Secp256k1 _ | Ed25519 _) -> 1 + | Secp256k1 _, Ed25519 _ -> 1 + end) + + type Base58.data += Data of t (* unused *) + + let b58check_encoding = + (* unused *) + Base58.register_encoding + ~prefix:"\255\255" + ~length:2 + ~to_raw:(fun _ -> assert false) + ~of_raw:(fun _ -> assert false) + ~wrap:(fun x -> Data x) + + let of_b58check_opt s = + match Base58.decode s with + | Some (Ed25519.Public_key.Data public_key) -> Some (Ed25519 public_key) + | Some (Secp256k1.Public_key.Data public_key) -> Some (Secp256k1 public_key) + | Some (P256.Public_key.Data public_key) -> Some (P256 public_key) + | Some (Bls.Public_key.Data public_key) -> Some (Bls public_key) + | _ -> None + + let of_b58check_exn s = + match of_b58check_opt s with + | Some x -> x + | None -> Format.kasprintf Stdlib.failwith "Unexpected data (%s)" name + + let of_b58check s = + match of_b58check_opt s with + | Some x -> Ok x + | None -> + error_with "Failed to read a b58check_encoding data (%s): %S" name s + + let to_b58check = function + | Ed25519 pk -> Ed25519.Public_key.to_b58check pk + | Secp256k1 pk -> Secp256k1.Public_key.to_b58check pk + | P256 pk -> P256.Public_key.to_b58check pk + | Bls pk -> Bls.Public_key.to_b58check pk + + let to_short_b58check = function + | Ed25519 pk -> Ed25519.Public_key.to_short_b58check pk + | Secp256k1 pk -> Secp256k1.Public_key.to_short_b58check pk + | P256 pk -> P256.Public_key.to_short_b58check pk + | Bls pk -> Bls.Public_key.to_short_b58check pk + + let of_bytes_without_validation b = + let tag = Bytes.(get_int8 b 0) in + let b = Bytes.(sub b 1 (length b - 1)) in + match tag with + | 0 -> + Option.bind + (Ed25519.Public_key.of_bytes_without_validation b) + (fun pk -> Some (Ed25519 pk)) + | 1 -> + Option.bind + (Secp256k1.Public_key.of_bytes_without_validation b) + (fun pk -> Some (Secp256k1 pk)) + | 2 -> + Option.bind (P256.Public_key.of_bytes_without_validation b) (fun pk -> + Some (P256 pk)) + | 3 -> + Option.bind (Bls.Public_key.of_bytes_without_validation b) (fun pk -> + Some (Bls pk)) + | _ -> None + + include Helpers.MakeEncoder (struct + type nonrec t = t + + let name = name + + let title = title + + let raw_encoding = + let open Data_encoding in + def "public_key" ~description:title + @@ union + [ + case + (Tag 0) + Ed25519.Public_key.encoding + ~title:"Ed25519" + (function Ed25519 x -> Some x | _ -> None) + (function x -> Ed25519 x); + case + (Tag 1) + Secp256k1.Public_key.encoding + ~title:"Secp256k1" + (function Secp256k1 x -> Some x | _ -> None) + (function x -> Secp256k1 x); + case + ~title:"P256" + (Tag 2) + P256.Public_key.encoding + (function P256 x -> Some x | _ -> None) + (function x -> P256 x); + case + ~title:"Bls" + (Tag 3) + Bls.Public_key.encoding + (function Bls x -> Some x | _ -> None) + (function x -> Bls x); + ] + + let of_b58check = of_b58check + + let of_b58check_opt = of_b58check_opt + + let of_b58check_exn = of_b58check_exn + + let to_b58check = to_b58check + + let to_short_b58check = to_short_b58check + end) + + let size pk = Data_encoding.Binary.length encoding pk + + let pp ppf t = Format.fprintf ppf "%s" (to_b58check t) +end + +module Secret_key = struct + type t = secret_key = + | Ed25519 of Ed25519.Secret_key.t + | Secp256k1 of Secp256k1.Secret_key.t + | P256 of P256.Secret_key.t + | Bls of Bls.Secret_key.t + + let name = "Signature.Secret_key" + + let title = "A Ed25519, Secp256k1, P256 or a BLS secret key" + + let to_public_key = function + | Ed25519 sk -> Public_key.Ed25519 (Ed25519.Secret_key.to_public_key sk) + | Secp256k1 sk -> + Public_key.Secp256k1 (Secp256k1.Secret_key.to_public_key sk) + | P256 sk -> Public_key.P256 (P256.Secret_key.to_public_key sk) + | Bls sk -> Public_key.Bls (Bls.Secret_key.to_public_key sk) + + include Compare.Make (struct + type nonrec t = t + + let compare a b = + match (a, b) with + | Ed25519 x, Ed25519 y -> Ed25519.Secret_key.compare x y + | Secp256k1 x, Secp256k1 y -> Secp256k1.Secret_key.compare x y + | P256 x, P256 y -> P256.Secret_key.compare x y + | Bls x, Bls y -> Bls.Secret_key.compare x y + | _ -> Stdlib.compare a b + end) + + type Base58.data += Data of t (* unused *) + + let b58check_encoding = + (* unused *) + Base58.register_encoding + ~prefix:"\255\255" + ~length:2 + ~to_raw:(fun _ -> assert false) + ~of_raw:(fun _ -> assert false) + ~wrap:(fun x -> Data x) + + let of_b58check_opt b = + match Base58.decode b with + | Some (Ed25519.Secret_key.Data sk) -> Some (Ed25519 sk) + | Some (Secp256k1.Secret_key.Data sk) -> Some (Secp256k1 sk) + | Some (P256.Secret_key.Data sk) -> Some (P256 sk) + | Some (Bls.Secret_key.Data sk) -> Some (Bls sk) + | _ -> None + + let of_b58check_exn s = + match of_b58check_opt s with + | Some x -> x + | None -> Format.kasprintf Stdlib.failwith "Unexpected data (%s)" name + + let of_b58check s = + match of_b58check_opt s with + | Some x -> Ok x + | None -> + error_with "Failed to read a b58check_encoding data (%s): %S" name s + + let to_b58check = function + | Ed25519 sk -> Ed25519.Secret_key.to_b58check sk + | Secp256k1 sk -> Secp256k1.Secret_key.to_b58check sk + | P256 sk -> P256.Secret_key.to_b58check sk + | Bls sk -> Bls.Secret_key.to_b58check sk + + let to_short_b58check = function + | Ed25519 sk -> Ed25519.Secret_key.to_short_b58check sk + | Secp256k1 sk -> Secp256k1.Secret_key.to_short_b58check sk + | P256 sk -> P256.Secret_key.to_short_b58check sk + | Bls sk -> Bls.Secret_key.to_short_b58check sk + + include Helpers.MakeEncoder (struct + type nonrec t = t + + let name = name + + let title = title + + let raw_encoding = + let open Data_encoding in + def "secret_key" ~description:title + @@ union + [ + case + (Tag 0) + Ed25519.Secret_key.encoding + ~title:"Ed25519" + (function Ed25519 x -> Some x | _ -> None) + (function x -> Ed25519 x); + case + (Tag 1) + Secp256k1.Secret_key.encoding + ~title:"Secp256k1" + (function Secp256k1 x -> Some x | _ -> None) + (function x -> Secp256k1 x); + case + (Tag 2) + ~title:"P256" + P256.Secret_key.encoding + (function P256 x -> Some x | _ -> None) + (function x -> P256 x); + case + (Tag 3) + ~title:"Bls" + Bls.Secret_key.encoding + (function Bls x -> Some x | _ -> None) + (function x -> Bls x); + ] + + let of_b58check = of_b58check + + let of_b58check_opt = of_b58check_opt + + let of_b58check_exn = of_b58check_exn + + let to_b58check = to_b58check + + let to_short_b58check = to_short_b58check + end) + + let pp ppf t = Format.fprintf ppf "%s" (to_b58check t) +end + +type signature = + | Ed25519 of Ed25519.t + | Secp256k1 of Secp256k1.t + | P256 of P256.t + | Bls of Bls.t + | Unknown of Bytes.t + +type prefix = Bls_prefix of Bytes.t + +type splitted = {prefix : prefix option; suffix : Bytes.t} + +type t = signature + +let name = "Signature.V3" + +let title = "A Ed25519, Secp256k1, P256 or BLS signature" + +let to_bytes = function + | Ed25519 b -> Ed25519.to_bytes b + | Secp256k1 b -> Secp256k1.to_bytes b + | P256 b -> P256.to_bytes b + | Bls b -> Bls.to_bytes b + | Unknown b -> b + +let of_bytes_opt s = + let len = Bytes.length s in + if len = Bls.size then Option.map (fun b -> Bls b) (Bls.of_bytes_opt s) + else if len = Ed25519.size then Some (Unknown s) + else None + +let () = + assert (Ed25519.size = 64) ; + assert (Secp256k1.size = 64) ; + assert (P256.size = 64) ; + assert (Bls.size = 96) + +type Base58.data += Data_unknown of Bytes.t + +let unknown_b58check_encoding = + Base58.register_encoding + ~prefix:Base58.Prefix.generic_signature + ~length:Ed25519.size + ~to_raw:Bytes.to_string + ~of_raw:(fun s -> Some (Bytes.of_string s)) + ~wrap:(fun x -> Data_unknown x) + +let () = Base58.check_encoded_prefix unknown_b58check_encoding "sig" 96 + +type Base58.data += Data of t (* unused *) + +let b58check_encoding = + (* unused *) + Base58.register_encoding + ~prefix:"\255\255" + ~length:2 + ~to_raw:(fun _ -> assert false) + ~of_raw:(fun _ -> assert false) + ~wrap:(fun x -> Data x) + +include Compare.Make (struct + type nonrec t = t + + let compare a b = + let a = to_bytes a and b = to_bytes b in + Bytes.compare a b +end) + +let of_b58check_opt s = + if TzString.has_prefix ~prefix:Ed25519.b58check_encoding.encoded_prefix s then + Option.map (fun x -> Ed25519 x) (Ed25519.of_b58check_opt s) + else if + TzString.has_prefix ~prefix:Secp256k1.b58check_encoding.encoded_prefix s + then Option.map (fun x -> Secp256k1 x) (Secp256k1.of_b58check_opt s) + else if TzString.has_prefix ~prefix:P256.b58check_encoding.encoded_prefix s + then Option.map (fun x -> P256 x) (P256.of_b58check_opt s) + else if TzString.has_prefix ~prefix:Bls.b58check_encoding.encoded_prefix s + then Option.map (fun x -> Bls x) (Bls.of_b58check_opt s) + else + Option.map + (fun x -> Unknown x) + (Base58.simple_decode unknown_b58check_encoding s) + +let of_b58check_exn s = + match of_b58check_opt s with + | Some x -> x + | None -> Format.kasprintf Stdlib.failwith "Unexpected data (%s)" name + +let of_b58check s = + match of_b58check_opt s with + | Some x -> Ok x + | None -> error_with "Failed to read a b58check_encoding data (%s): %S" name s + +let to_b58check = function + | Ed25519 b -> Ed25519.to_b58check b + | Secp256k1 b -> Secp256k1.to_b58check b + | P256 b -> P256.to_b58check b + | Bls b -> Bls.to_b58check b + | Unknown b -> Base58.simple_encode unknown_b58check_encoding b + +let to_short_b58check = function + | Ed25519 b -> Ed25519.to_short_b58check b + | Secp256k1 b -> Secp256k1.to_short_b58check b + | P256 b -> P256.to_short_b58check b + | Bls b -> Bls.to_short_b58check b + | Unknown b -> Base58.simple_encode unknown_b58check_encoding b + +let raw_encoding = + conv + to_bytes + (fun b -> + match of_bytes_opt b with + | None -> + Format.kasprintf + Stdlib.failwith + "Not a valid signature: %a" + Hex.pp + (Hex.of_bytes b) + | Some s -> s) + Variable.bytes + +include Helpers.MakeEncoder (struct + type nonrec t = t + + let name = name + + let title = title + + let raw_encoding = raw_encoding + + let of_b58check = of_b58check + + let of_b58check_opt = of_b58check_opt + + let of_b58check_exn = of_b58check_exn + + let to_b58check = to_b58check + + let to_short_b58check = to_short_b58check +end) + +let to_bytes s = Data_encoding.Binary.to_bytes_exn raw_encoding s + +let of_bytes_opt s = Data_encoding.Binary.of_bytes_opt raw_encoding s + +let to_string s = Bytes.to_string (to_bytes s) + +let of_string_opt s = of_bytes_opt (Bytes.of_string s) + +include Helpers.MakeRaw (struct + type nonrec t = t + + let name = name + + let of_bytes_opt = of_bytes_opt + + let of_string_opt = of_string_opt + + let to_string = to_string +end) + +let size t = Data_encoding.Binary.length encoding t + +let pp ppf t = Format.fprintf ppf "%s" (to_b58check t) + +let of_ed25519 s = Ed25519 s + +let of_secp256k1 s = Secp256k1 s + +let of_p256 s = P256 s + +let of_bls s = Bls s + +let zero = of_ed25519 Ed25519.zero + +(* NOTE: At the moment, only BLS signatures can be encoded with a tag. We impose + this restriction so that there is only one valid binary representation for a + same signature (modulo malleability). + + We reserve the tags 0, 1, 2 and 255 for tags of the other signatures if we + decide to unify signature representation one day.*) +let prefix_encoding = + let open Data_encoding in + def + "bls_signature_prefix" + ~description:"The prefix of a BLS signature, i.e. the first 32 bytes." + @@ union + [ + case + (Tag 3) + ~title:"Bls_prefix" + (Fixed.bytes (Bls.size - Ed25519.size)) + (function Bls_prefix x -> Some x) + (function x -> Bls_prefix x); + ] + +let split_signature = function + | (Ed25519 _ | Secp256k1 _ | P256 _) as s -> + {prefix = None; suffix = to_bytes s} + | Bls s -> + let s = Bls.to_bytes s in + let prefix = Bytes.sub s 0 32 in + let suffix = Bytes.sub s 32 64 in + {prefix = Some (Bls_prefix prefix); suffix} + | Unknown s -> + assert (Compare.Int.(Bytes.length s = 64)) ; + {prefix = None; suffix = s} + +let of_splitted {prefix; suffix} = + let open Option_syntax in + match prefix with + | None -> of_bytes_opt suffix + | Some (Bls_prefix prefix) -> + let+ s = Bls.of_bytes_opt (Bytes.cat prefix suffix) in + Bls s + +let bytes_of_watermark = function + | Block_header chain_id -> + Bytes.cat (Bytes.of_string "\x01") (Chain_id.to_bytes chain_id) + | Endorsement chain_id -> + Bytes.cat (Bytes.of_string "\x02") (Chain_id.to_bytes chain_id) + | Generic_operation -> Bytes.of_string "\x03" + | Custom bytes -> bytes + +let pp_watermark ppf = + let open Format in + function + | Block_header chain_id -> fprintf ppf "Block-header: %a" Chain_id.pp chain_id + | Endorsement chain_id -> fprintf ppf "Endorsement: %a" Chain_id.pp chain_id + | Generic_operation -> pp_print_string ppf "Generic-operation" + | Custom bytes -> + let hexed = Hex.of_bytes bytes |> Hex.show in + fprintf + ppf + "Custom: 0x%s" + (try String.sub hexed 0 10 ^ "..." with Invalid_argument _ -> hexed) + +let sign ?watermark secret_key message = + let watermark = Option.map bytes_of_watermark watermark in + match secret_key with + | Secret_key.Ed25519 sk -> of_ed25519 (Ed25519.sign ?watermark sk message) + | Secp256k1 sk -> of_secp256k1 (Secp256k1.sign ?watermark sk message) + | P256 sk -> of_p256 (P256.sign ?watermark sk message) + | Bls sk -> of_bls (Bls.sign ?watermark sk message) + +let check ?watermark public_key signature message = + let watermark = Option.map bytes_of_watermark watermark in + match (public_key, signature) with + | Public_key.Ed25519 pk, Unknown signature -> ( + match Ed25519.of_bytes_opt signature with + | Some s -> Ed25519.check ?watermark pk s message + | None -> false) + | Public_key.Secp256k1 pk, Unknown signature -> ( + match Secp256k1.of_bytes_opt signature with + | Some s -> Secp256k1.check ?watermark pk s message + | None -> false) + | Public_key.P256 pk, Unknown signature -> ( + match P256.of_bytes_opt signature with + | Some s -> P256.check ?watermark pk s message + | None -> false) + | Public_key.Bls pk, Unknown signature -> ( + match Bls.of_bytes_opt signature with + | Some s -> Bls.check ?watermark pk s message + | None -> false) + | Public_key.Ed25519 pk, Ed25519 signature -> + Ed25519.check ?watermark pk signature message + | Public_key.Secp256k1 pk, Secp256k1 signature -> + Secp256k1.check ?watermark pk signature message + | Public_key.P256 pk, P256 signature -> + P256.check ?watermark pk signature message + | Public_key.Bls pk, Bls signature -> + Bls.check ?watermark pk signature message + | _ -> false + +type algo = Ed25519 | Secp256k1 | P256 | Bls + +let hardcoded_sk = + let ed, secp, p, bls = + ( Secret_key.of_b58check_exn + "edsk3gUfUPyBSfrS9CCgmCiQsTCHGkviBDusMxDJstFtojtc1zcpsh", + Secret_key.of_b58check_exn + "spsk2XJu4wuYsHeuDaCktD3ECnnpn574ceSWHEJVvXTt7JP6ztySCL", + Secret_key.of_b58check_exn + "p2sk2k6YAkNJ8CySZCS3vGA5Ht6Lj6LXG3yb8UrHvMKZy7Ab8JUtWh", + Secret_key.of_b58check_exn + "BLsk1hfuv6V8JJRaLDBJgPTRGLKusTZnTmWGrvSKYzUaMuzvPLmeGG" ) + in + function Ed25519 -> ed | Secp256k1 -> secp | P256 -> p | Bls -> bls + +let hardcoded_pk = + (* precompute signatures *) + let ed, secp, p, bls = + ( Secret_key.to_public_key (hardcoded_sk Ed25519), + Secret_key.to_public_key (hardcoded_sk Secp256k1), + Secret_key.to_public_key (hardcoded_sk P256), + Secret_key.to_public_key (hardcoded_sk Bls) ) + in + function Ed25519 -> ed | Secp256k1 -> secp | P256 -> p | Bls -> bls + +let hardcoded_msg = Bytes.of_string "Cheers" + +let hardcoded_sig = + (* precompute signatures *) + let ed, secp, p, bls = + ( sign (hardcoded_sk Ed25519) hardcoded_msg, + sign (hardcoded_sk Secp256k1) hardcoded_msg, + sign (hardcoded_sk P256) hardcoded_msg, + sign (hardcoded_sk Bls) hardcoded_msg ) + in + function Ed25519 -> ed | Secp256k1 -> secp | P256 -> p | Bls -> bls + +let fast_fake_sign ?watermark:_ (sk : Secret_key.t) _msg = + let algo = + match sk with + | Ed25519 _ -> Ed25519 + | Secp256k1 _ -> Secp256k1 + | P256 _ -> P256 + | Bls _ -> Bls + in + hardcoded_sig algo + +(* Fast checking does not simulate computation and directly returns true*) +let fast_fake_check ?watermark:_ _pk _signature _msg = true + +let fake_check ?watermark:_ (pk : Public_key.t) _signature _msg = + (* call a successfull [check] to simulate crypto costs *) + let algo = + match pk with + | Ed25519 _ -> Ed25519 + | Secp256k1 _ -> Secp256k1 + | P256 _ -> P256 + | Bls _ -> Bls + in + ignore (check (hardcoded_pk algo) (hardcoded_sig algo) hardcoded_msg) ; + true + +(* yes_crypto semantics: + - Yes: use the normal [sign] and [check] functions to simulate crypto costs, + but [check] always returns true. + - Fast: return hardcoded signatures and skip any crypto costs. + In both modes, verification always succeeds. *) +let sign = + match Helpers.yes_crypto_kind with + | Fast -> fast_fake_sign + | Yes -> sign + | No -> sign + +let check = + match Helpers.yes_crypto_kind with + | Fast -> fast_fake_check + | Yes -> fake_check + | No -> check + +let append ?watermark sk msg = Bytes.cat msg (to_bytes (sign ?watermark sk msg)) + +let concat msg signature = Bytes.cat msg (to_bytes signature) + +let algos = [Ed25519; Secp256k1; P256; Bls] + +let fake_generate_key (pkh, pk, _) = + let sk_of_pk (pk : public_key) : secret_key = + let pk_b = Data_encoding.Binary.to_bytes_exn Public_key.encoding pk in + let sk_b = Bytes.sub pk_b 0 33 in + let sk = Data_encoding.Binary.of_bytes_exn Secret_key.encoding sk_b in + sk + in + let fake_sk = sk_of_pk pk in + (pkh, pk, fake_sk) + +let generate_key ?(algo = Ed25519) ?seed () = + match algo with + | Ed25519 -> + let pkh, pk, sk = Ed25519.generate_key ?seed () in + (Public_key_hash.Ed25519 pkh, Public_key.Ed25519 pk, Secret_key.Ed25519 sk) + | Secp256k1 -> + let pkh, pk, sk = Secp256k1.generate_key ?seed () in + ( Public_key_hash.Secp256k1 pkh, + Public_key.Secp256k1 pk, + Secret_key.Secp256k1 sk ) + | P256 -> + let pkh, pk, sk = P256.generate_key ?seed () in + (Public_key_hash.P256 pkh, Public_key.P256 pk, Secret_key.P256 sk) + | Bls -> + let pkh, pk, sk = Bls.generate_key ?seed () in + (Public_key_hash.Bls pkh, Public_key.Bls pk, Secret_key.Bls sk) + +let fake_generate_key ?(algo = Ed25519) ?seed () = + let true_keys = generate_key ~algo ?seed () in + fake_generate_key true_keys + +let generate_key = + match Helpers.yes_crypto_kind with + | Fast | Yes -> + (* We keep the original keys generation to stay as close as possible of the + initial performance. *) + fake_generate_key + | No -> generate_key + +let deterministic_nonce sk msg = + match sk with + | Secret_key.Ed25519 sk -> Ed25519.deterministic_nonce sk msg + | Secret_key.Secp256k1 sk -> Secp256k1.deterministic_nonce sk msg + | Secret_key.P256 sk -> P256.deterministic_nonce sk msg + | Secret_key.Bls sk -> Bls.deterministic_nonce sk msg + +let deterministic_nonce_hash sk msg = + match sk with + | Secret_key.Ed25519 sk -> Ed25519.deterministic_nonce_hash sk msg + | Secret_key.Secp256k1 sk -> Secp256k1.deterministic_nonce_hash sk msg + | Secret_key.P256 sk -> P256.deterministic_nonce_hash sk msg + | Secret_key.Bls sk -> Bls.deterministic_nonce_hash sk msg + +let pop_verify pubkey ?msg proof = + match pubkey with + | Public_key.Bls pk -> Bls12_381_signature.MinPk.Pop.pop_verify pk ?msg proof + | _ -> false + +module Of_V0 = struct + let public_key_hash : Signature_v0.Public_key_hash.t -> Public_key_hash.t = + function + | Signature_v0.Ed25519 k -> Ed25519 k + | Signature_v0.Secp256k1 k -> Secp256k1 k + | Signature_v0.P256 k -> P256 k + + let public_key : Signature_v0.Public_key.t -> Public_key.t = function + | Signature_v0.Ed25519 k -> Ed25519 k + | Signature_v0.Secp256k1 k -> Secp256k1 k + | Signature_v0.P256 k -> P256 k + + let secret_key : Signature_v0.Secret_key.t -> Secret_key.t = function + | Signature_v0.Ed25519 k -> Ed25519 k + | Signature_v0.Secp256k1 k -> Secp256k1 k + | Signature_v0.P256 k -> P256 k + + let signature : Signature_v0.t -> t = function + | Signature_v0.Ed25519 k -> Ed25519 k + | Signature_v0.Secp256k1 k -> Secp256k1 k + | Signature_v0.P256 k -> P256 k + | Signature_v0.Unknown k -> Unknown k +end + +module Of_V1 = struct + let public_key_hash : Signature_v1.Public_key_hash.t -> Public_key_hash.t = + function + | Signature_v1.Ed25519 k -> Ed25519 k + | Signature_v1.Secp256k1 k -> Secp256k1 k + | Signature_v1.P256 k -> P256 k + | Signature_v1.Bls k -> Bls k + + let public_key : Signature_v1.Public_key.t -> Public_key.t = function + | Signature_v1.Ed25519 k -> Ed25519 k + | Signature_v1.Secp256k1 k -> Secp256k1 k + | Signature_v1.P256 k -> P256 k + | Signature_v1.Bls k -> Bls k + + let secret_key : Signature_v1.Secret_key.t -> Secret_key.t = function + | Signature_v1.Ed25519 k -> Ed25519 k + | Signature_v1.Secp256k1 k -> Secp256k1 k + | Signature_v1.P256 k -> P256 k + | Signature_v1.Bls k -> Bls k + + let signature : Signature_v1.t -> t = function + | Signature_v1.Ed25519 k -> Ed25519 k + | Signature_v1.Secp256k1 k -> Secp256k1 k + | Signature_v1.P256 k -> P256 k + | Signature_v1.Unknown k -> Unknown k + | Signature_v1.Bls k -> Bls k +end + +module Of_V2 = struct + let public_key_hash : Signature_v2.Public_key_hash.t -> Public_key_hash.t = + function + | Signature_v2.Ed25519 k -> Ed25519 k + | Signature_v2.Secp256k1 k -> Secp256k1 k + | Signature_v2.P256 k -> P256 k + | Signature_v2.Bls k -> Bls k + + let public_key : Signature_v2.Public_key.t -> Public_key.t = function + | Signature_v2.Ed25519 k -> Ed25519 k + | Signature_v2.Secp256k1 k -> Secp256k1 k + | Signature_v2.P256 k -> P256 k + | Signature_v2.Bls k -> Bls k + + let secret_key : Signature_v2.Secret_key.t -> Secret_key.t = function + | Signature_v2.Ed25519 k -> Ed25519 k + | Signature_v2.Secp256k1 k -> Secp256k1 k + | Signature_v2.P256 k -> P256 k + | Signature_v2.Bls k -> Bls k + + let signature : Signature_v2.t -> t = function + | Signature_v2.Ed25519 k -> Ed25519 k + | Signature_v2.Secp256k1 k -> Secp256k1 k + | Signature_v2.P256 k -> P256 k + | Signature_v2.Unknown k -> Unknown k + | Signature_v2.Bls k -> Bls k +end diff --git a/src/lib_crypto/signature_v3.mli b/src/lib_crypto/signature_v3.mli new file mode 100644 index 0000000000000000000000000000000000000000..56a7d7df0a264b62ec1b6e665bd429d6dee7c9c3 --- /dev/null +++ b/src/lib_crypto/signature_v3.mli @@ -0,0 +1,157 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* SPDX-FileCopyrightText: 2025 Nomadic Labs *) +(* *) +(*****************************************************************************) + +type public_key_hash = + | Ed25519 of Ed25519.Public_key_hash.t + | Secp256k1 of Secp256k1.Public_key_hash.t + | P256 of P256.Public_key_hash.t + | Bls of Bls.Public_key_hash.t + +type public_key = + | Ed25519 of Ed25519.Public_key.t + | Secp256k1 of Secp256k1.Public_key.t + | P256 of P256.Public_key.t + | Bls of Bls.Public_key.t + +type secret_key = + | Ed25519 of Ed25519.Secret_key.t + | Secp256k1 of Secp256k1.Secret_key.t + | P256 of P256.Secret_key.t + | Bls of Bls.Secret_key.t + +type watermark = Signature_v0.watermark = + | Block_header of Chain_id.t + | Endorsement of Chain_id.t + | Generic_operation + | Custom of Bytes.t + +val bytes_of_watermark : watermark -> Bytes.t + +val pp_watermark : Format.formatter -> watermark -> unit + +type signature = + | Ed25519 of Ed25519.t + | Secp256k1 of Secp256k1.t + | P256 of P256.t + | Bls of Bls.t + | Unknown of Bytes.t + +(** A signature prefix holds data only for signature that are more than 64 bytes + long. *) +type prefix = Bls_prefix of Bytes.t + +include + S.SPLIT_SIGNATURE + with type Public_key_hash.t = public_key_hash + and type Public_key.t = public_key + and type Secret_key.t = secret_key + and type watermark := watermark + and type prefix := prefix + and type t = signature + +module Public_key_hash : sig + include module type of Public_key_hash + + val is_bls : t -> bool +end + +(** [append sk buf] is the concatenation of [buf] and the + serialization of the signature of [buf] signed by [sk]. *) +val append : ?watermark:watermark -> secret_key -> Bytes.t -> Bytes.t + +(** [concat buf t] is the concatenation of [buf] and the serialization + of [t]. *) +val concat : Bytes.t -> t -> Bytes.t + +include S.RAW_DATA with type t := t + +(** The size of the signature in bytes. Can be [64] for Ed25519, Secp256k1 and + P256 signatures or [96] for BLS signatures. *) +val size : t -> int + +(** [of_secp256k1 s] returns a wrapped version of the Secp256k1 signature [s] in + {!t}. *) +val of_secp256k1 : Secp256k1.t -> t + +(** [of_ed25519 s] returns a wrapped version of the Ed25519 signature [s] in + {!t}. *) +val of_ed25519 : Ed25519.t -> t + +(** [of_p256 s] returns a wrapped version of the P256 signature [s] in {!t}. *) +val of_p256 : P256.t -> t + +(** [of_bls s] returns a wrapped version of the BLS signature [s] in {!t}. *) +val of_bls : Bls.t -> t + +(** The type of signing algorithms. *) +type algo = Ed25519 | Secp256k1 | P256 | Bls + +(** The list of signing algorithm supported, i.e. all constructors of type + {!algo}. *) +val algos : algo list + +(** [generate_key ~algo ~seed ()] generates a key pair for the signing algorithm + [algo] from the random seed [seed]. *) +val generate_key : + ?algo:algo -> + ?seed:Bytes.t -> + unit -> + public_key_hash * public_key * secret_key + +(** This module provides conversion functions for values (of keys and + signatures) of the module {!Signature_V0}. Note that these functions are + total because [Signature_v1] supports more signature kinds than + {!Signature_v0}. *) +module Of_V0 : sig + (** Convert a public key hash from V0 to V2. *) + val public_key_hash : Signature_v0.Public_key_hash.t -> Public_key_hash.t + + (** Convert a public key from V0 to V2. *) + val public_key : Signature_v0.Public_key.t -> Public_key.t + + (** Convert a secret key from V0 to V2. *) + val secret_key : Signature_v0.Secret_key.t -> Secret_key.t + + (** Convert a signature from V0 to V2. *) + val signature : Signature_v0.t -> t +end + +(** This module provides conversion functions for values (of keys and + signatures) of the module {!Signature_V1}. Note that these functions are + total because [Signature_v1] supports more signature kinds than + {!Signature_v0}. *) +module Of_V1 : sig + (** Convert a public key hash from V1 to V2. *) + val public_key_hash : Signature_v1.Public_key_hash.t -> Public_key_hash.t + + (** Convert a public key from V1 to V2. *) + val public_key : Signature_v1.Public_key.t -> Public_key.t + + (** Convert a secret key from V1 to V2. *) + val secret_key : Signature_v1.Secret_key.t -> Secret_key.t + + (** Convert a signature from V1 to V2. *) + val signature : Signature_v1.t -> t +end + +(** This module provides conversion functions for values (of keys and + signatures) of the module {!Signature_V2}. Note that these functions are + total because [Signature_v1] supports more signature kinds than + {!Signature_v0}. *) +module Of_V2 : sig + (** Convert a public key hash from V2 to V3. *) + val public_key_hash : Signature_v2.Public_key_hash.t -> Public_key_hash.t + + (** Convert a public key from V2 to V3. *) + val public_key : Signature_v2.Public_key.t -> Public_key.t + + (** Convert a secret key from V2 to V3. *) + val secret_key : Signature_v2.Secret_key.t -> Secret_key.t + + (** Convert a signature from V2 to V3. *) + val signature : Signature_v2.t -> t +end diff --git a/src/lib_protocol_environment/environment_V15.ml b/src/lib_protocol_environment/environment_V15.ml index cac5ea07215eca0e42c88a0ea60df016640d91f0..b1c7331579455ebe42050bd794241fcccbd93cdb 100644 --- a/src/lib_protocol_environment/environment_V15.ml +++ b/src/lib_protocol_environment/environment_V15.ml @@ -1616,6 +1616,9 @@ struct | Ok () -> Ok true let share_is_trap delegate share ~traps_fraction = + let delegate = + Tezos_crypto.Signature.V_latest.Of_V2.public_key_hash delegate + in match Tezos_crypto_dal.Trap.share_is_trap delegate share ~traps_fraction with diff --git a/src/lib_protocol_environment/environment_V16.ml b/src/lib_protocol_environment/environment_V16.ml index 1353dc8dfa078554f5d4e5a1b4a67c9713caab0b..1a2a9589f547325d9d8e33f66ccba155d156dff4 100644 --- a/src/lib_protocol_environment/environment_V16.ml +++ b/src/lib_protocol_environment/environment_V16.ml @@ -1616,6 +1616,9 @@ struct | Ok () -> Ok true let share_is_trap delegate share ~traps_fraction = + let delegate = + Tezos_crypto.Signature.V_latest.Of_V2.public_key_hash delegate + in match Tezos_crypto_dal.Trap.share_is_trap delegate share ~traps_fraction with diff --git a/src/lib_signer_backends/encrypted.ml b/src/lib_signer_backends/encrypted.ml index 01142af3ee1bb84321b8a74dd5998d56145472c4..e3a93171813610a56b3de96728922ce0266bbbaa 100644 --- a/src/lib_signer_backends/encrypted.ml +++ b/src/lib_signer_backends/encrypted.ml @@ -530,10 +530,18 @@ struct match Tezos_crypto.Signature.V2.Of_V_latest.secret_key sk with | Some sk -> let s = Tezos_crypto.Signature.V2.sign ?watermark sk buf in - return s + return (Signature.V_latest.Of_V2.signature s) | None -> Error_monad.failwith "Failed to handle secret key in Signature version 2") + | Some Version_3 -> ( + match Tezos_crypto.Signature.V3.Of_V_latest.secret_key sk with + | Some sk -> + let s = Tezos_crypto.Signature.V3.sign ?watermark sk buf in + return s + | None -> + Error_monad.failwith + "Failed to handle secret key in Signature version 3") | None -> return (Tezos_crypto.Signature.V_latest.sign ?watermark sk buf) let deterministic_nonce sk_uri buf = diff --git a/src/lib_signer_backends/unencrypted.ml b/src/lib_signer_backends/unencrypted.ml index b988a6674ca711c9dabf82a90190001c9d3e8d87..c8d87a4ea7e42e6db93305a7c9a7e6c97cbb4a36 100644 --- a/src/lib_signer_backends/unencrypted.ml +++ b/src/lib_signer_backends/unencrypted.ml @@ -103,10 +103,18 @@ let sign ?version ?watermark sk_uri buf = match Tezos_crypto.Signature.V2.Of_V_latest.secret_key sk with | Some sk -> let s = Tezos_crypto.Signature.V2.sign ?watermark sk buf in - return s + return (Signature.V_latest.Of_V2.signature s) | None -> Error_monad.failwith "Failed to handle secret key in Signature version 2") + | Some Version_3 -> ( + match Tezos_crypto.Signature.V3.Of_V_latest.secret_key sk with + | Some sk -> + let s = Tezos_crypto.Signature.V3.sign ?watermark sk buf in + return s + | None -> + Error_monad.failwith + "Failed to handle secret key in Signature version 3") | None -> return (Tezos_crypto.Signature.V_latest.sign ?watermark sk buf) let deterministic_nonce sk_uri buf = diff --git a/src/proto_023_PtSeouLo/lib_agnostic_baker/baker_commands_helpers.ml b/src/proto_023_PtSeouLo/lib_agnostic_baker/baker_commands_helpers.ml index bff9aa16e8bd0eb1aeb00fe44de2750284a3ceb7..a8679ab150e5fd79c3e7659ce3b0825049b2ec76 100644 --- a/src/proto_023_PtSeouLo/lib_agnostic_baker/baker_commands_helpers.ml +++ b/src/proto_023_PtSeouLo/lib_agnostic_baker/baker_commands_helpers.ml @@ -44,6 +44,9 @@ let run_baker (cctxt : Tezos_client_base.Client_context.full) ?dal_node_rpc_ctxt {vote_file; liquidity_baking_vote = to_protocol liquidity_baking_vote}) votes in + let*? sources = + List.map_e Signature.Of_V_latest.get_public_key_hash sources + in let* delegates = Baking_commands.get_delegates cctxt sources in Client_daemon.Baker.run cctxt diff --git a/src/proto_023_PtSeouLo/lib_dal/dal_plugin_registration.ml b/src/proto_023_PtSeouLo/lib_dal/dal_plugin_registration.ml index d99859236b66a2caf4cbd03fd0d74234a5c16098..74b851e7fab4c58cd46e9c0fe79506ac55af9475 100644 --- a/src/proto_023_PtSeouLo/lib_dal/dal_plugin_registration.ml +++ b/src/proto_023_PtSeouLo/lib_dal/dal_plugin_registration.ml @@ -155,6 +155,7 @@ module Plugin = struct let* block_hash = Protocol_client_context.Alpha_block_services.hash cctxt ~chain ~block () in + let*? source = Signature.Of_V_latest.get_public_key_hash source in let* counter = let* pcounter = Plugin.Alpha_services.Contract.counter cpctxt (chain, `Head 0) source @@ -189,6 +190,7 @@ module Plugin = struct Operation.unsigned_encoding ({branch = block_hash}, Contents_list (Single operation)) in + let*? src_sk = Signature.Of_V_latest.get_secret_key src_sk in let bytes = Signature.append ~watermark:Signature.Generic_operation src_sk bytes in diff --git a/src/proto_023_PtSeouLo/lib_plugin/mempool.ml b/src/proto_023_PtSeouLo/lib_plugin/mempool.ml index 3b7a898aa71b18424d5ec7405bed2bf1508d3c55..e871b5a98145f2e412df04df4cf560a63372fc84 100644 --- a/src/proto_023_PtSeouLo/lib_plugin/mempool.ml +++ b/src/proto_023_PtSeouLo/lib_plugin/mempool.ml @@ -853,31 +853,44 @@ let sources_from_operation ctxt ({shell = _; protocol_data = Operation_data {contents; _}} : Main.operation) = let open Lwt_syntax in - match contents with - | Single (Failing_noop _) -> return_nil - | Single (Preattestation consensus_content) - | Single (Attestation {consensus_content; dal_content = _}) -> - let level = Level.from_raw ctxt consensus_content.level in - sources_from_level_and_slot ctxt level consensus_content.slot - | Single (Preattestations_aggregate {consensus_content; committee}) -> - sources_from_aggregate ctxt consensus_content committee - | Single (Attestations_aggregate {consensus_content; committee}) -> - sources_from_aggregate - ctxt - consensus_content - (Operation.committee_slots committee) - | Single (Seed_nonce_revelation _) - | Single (Double_consensus_operation_evidence _) - | Single (Double_baking_evidence _) - | Single (Dal_entrapment_evidence _) - | Single (Activate_account _) - | Single (Vdf_revelation _) -> - return_nil - | Single (Proposals {source; _}) | Single (Ballot {source; _}) -> - return [source] - | Single (Drain_delegate {delegate; _}) -> return [delegate] - | Single (Manager_operation {source; _}) -> return [source] - | Cons (Manager_operation {source; _}, _) -> return [source] + let* sources = + match contents with + | Single (Failing_noop _) -> return_nil + | Single (Preattestation consensus_content) + | Single (Attestation {consensus_content; dal_content = _}) -> + let level = Level.from_raw ctxt consensus_content.level in + let* sources = + sources_from_level_and_slot ctxt level consensus_content.slot + in + return sources + | Single (Preattestations_aggregate {consensus_content; committee}) -> + let* sources = + sources_from_aggregate ctxt consensus_content committee + in + return sources + | Single (Attestations_aggregate {consensus_content; committee}) -> + let* sources = + sources_from_aggregate + ctxt + consensus_content + (Operation.committee_slots committee) + in + return sources + | Single (Seed_nonce_revelation _) + | Single (Double_consensus_operation_evidence _) + | Single (Double_baking_evidence _) + | Single (Dal_entrapment_evidence _) + | Single (Activate_account _) + | Single (Vdf_revelation _) -> + return_nil + | Single (Proposals {source; _}) | Single (Ballot {source; _}) -> + return [source] + | Single (Drain_delegate {delegate; _}) -> return [delegate] + | Single (Manager_operation {source; _}) -> return [source] + | Cons (Manager_operation {source; _}, _) -> return [source] + in + let map_pkh_env = List.map Tezos_crypto.Signature.Of_V2.public_key_hash in + return @@ map_pkh_env sources module Internal_for_tests = struct let default_config_with_clock_drift clock_drift = diff --git a/src/proto_023_PtSeouLo/lib_plugin/plugin_registerer.ml b/src/proto_023_PtSeouLo/lib_plugin/plugin_registerer.ml index a28208d9a4932a39cded4ee0b9c0b8d9d3063f43..262854e56d9159488de5a81611f9801b789a9912 100644 --- a/src/proto_023_PtSeouLo/lib_plugin/plugin_registerer.ml +++ b/src/proto_023_PtSeouLo/lib_plugin/plugin_registerer.ml @@ -50,6 +50,9 @@ module Delegators_contribution_plugin = struct let delegated_breakdown_at_sampling context ~cycle ~delegate_pkh = let open Lwt_result_syntax in + let*? delegate_pkh = + Signature.Of_V_latest.get_public_key_hash delegate_pkh + in let* output = Delegators_contribution.delegated_breakdown_at_sampling context @@ -80,6 +83,9 @@ module Delegators_contribution_plugin = struct let min_delegated_breakdown context ~delegate_pkh = let open Lwt_result_syntax in + let*? delegate_pkh = + Signature.Of_V_latest.get_public_key_hash delegate_pkh + in let* { total_delegated; own_delegated; diff --git a/src/proto_024_PtTALLiN/lib_agnostic_baker/baker_commands_helpers.ml b/src/proto_024_PtTALLiN/lib_agnostic_baker/baker_commands_helpers.ml index bff9aa16e8bd0eb1aeb00fe44de2750284a3ceb7..a8679ab150e5fd79c3e7659ce3b0825049b2ec76 100644 --- a/src/proto_024_PtTALLiN/lib_agnostic_baker/baker_commands_helpers.ml +++ b/src/proto_024_PtTALLiN/lib_agnostic_baker/baker_commands_helpers.ml @@ -44,6 +44,9 @@ let run_baker (cctxt : Tezos_client_base.Client_context.full) ?dal_node_rpc_ctxt {vote_file; liquidity_baking_vote = to_protocol liquidity_baking_vote}) votes in + let*? sources = + List.map_e Signature.Of_V_latest.get_public_key_hash sources + in let* delegates = Baking_commands.get_delegates cctxt sources in Client_daemon.Baker.run cctxt diff --git a/src/proto_024_PtTALLiN/lib_dal/dal_plugin_registration.ml b/src/proto_024_PtTALLiN/lib_dal/dal_plugin_registration.ml index e152929b0606d97ec625008b1e048be5771eef32..f2e1f34abe0dba227132fec3a125fad3611d86e9 100644 --- a/src/proto_024_PtTALLiN/lib_dal/dal_plugin_registration.ml +++ b/src/proto_024_PtTALLiN/lib_dal/dal_plugin_registration.ml @@ -172,6 +172,7 @@ module Plugin = struct let* block_hash = Protocol_client_context.Alpha_block_services.hash cctxt ~chain ~block () in + let*? source = Signature.Of_V_latest.get_public_key_hash source in let* counter = let* pcounter = Plugin.Alpha_services.Contract.counter cpctxt (chain, `Head 0) source @@ -206,6 +207,7 @@ module Plugin = struct Operation.unsigned_encoding ({branch = block_hash}, Contents_list (Single operation)) in + let*? src_sk = Signature.Of_V_latest.get_secret_key src_sk in let bytes = Signature.append ~watermark:Signature.Generic_operation src_sk bytes in @@ -343,12 +345,9 @@ module Plugin = struct in List.fold_left_es (fun acc ({delegate; indexes} : Plugin.RPC.Dal.S.shards_assignment) -> - let delegate_pkh = - Tezos_crypto.Signature.Of_V2.public_key_hash delegate - in let* tb_slot = match - Signature.Public_key_hash.Map.find delegate_pkh pkh_to_tb_slot_map + Signature.Public_key_hash.Map.find delegate pkh_to_tb_slot_map with | Some slot -> return (Slot.to_int slot) | None -> @@ -359,9 +358,10 @@ module Plugin = struct (Resto.Path.to_string Tezos_rpc.Path.(Plugin.RPC.Dal.path / "shards"))) in + let delegate = Tezos_crypto.Signature.Of_V2.public_key_hash delegate in return @@ Tezos_crypto.Signature.Public_key_hash.Map.add - delegate_pkh + delegate (indexes, tb_slot) acc) Tezos_crypto.Signature.Public_key_hash.Map.empty diff --git a/src/proto_024_PtTALLiN/lib_plugin/mempool.ml b/src/proto_024_PtTALLiN/lib_plugin/mempool.ml index 58fa927f260220afc1d9b0f95d11d2f141d65a07..bf9c54ad60224f4e5adecb9d4866a03185d962fa 100644 --- a/src/proto_024_PtTALLiN/lib_plugin/mempool.ml +++ b/src/proto_024_PtTALLiN/lib_plugin/mempool.ml @@ -855,31 +855,47 @@ let sources_from_operation ctxt ({shell = _; protocol_data = Operation_data {contents; _}} : Main.operation) = let open Lwt_syntax in - match contents with - | Single (Failing_noop _) -> return_nil - | Single (Preattestation consensus_content) - | Single (Attestation {consensus_content; dal_content = _}) -> - let attested_level = Level.from_raw ctxt consensus_content.level in - sources_from_level_and_slot ctxt ~attested_level consensus_content.slot - | Single (Preattestations_aggregate {consensus_content; committee}) -> - sources_from_aggregate ctxt consensus_content committee - | Single (Attestations_aggregate {consensus_content; committee}) -> - sources_from_aggregate - ctxt - consensus_content - (Operation.committee_slots committee) - | Single (Seed_nonce_revelation _) - | Single (Double_consensus_operation_evidence _) - | Single (Double_baking_evidence _) - | Single (Dal_entrapment_evidence _) - | Single (Activate_account _) - | Single (Vdf_revelation _) -> - return_nil - | Single (Proposals {source; _}) | Single (Ballot {source; _}) -> - return [source] - | Single (Drain_delegate {delegate; _}) -> return [delegate] - | Single (Manager_operation {source; _}) -> return [source] - | Cons (Manager_operation {source; _}, _) -> return [source] + let* sources = + match contents with + | Single (Failing_noop _) -> return_nil + | Single (Preattestation consensus_content) + | Single (Attestation {consensus_content; dal_content = _}) -> + let attested_level = Level.from_raw ctxt consensus_content.level in + let* sources = + sources_from_level_and_slot + ctxt + ~attested_level + consensus_content.slot + in + return sources + | Single (Preattestations_aggregate {consensus_content; committee}) -> + let* sources = + sources_from_aggregate ctxt consensus_content committee + in + return sources + | Single (Attestations_aggregate {consensus_content; committee}) -> + let* sources = + sources_from_aggregate + ctxt + consensus_content + (Operation.committee_slots committee) + in + return sources + | Single (Seed_nonce_revelation _) + | Single (Double_consensus_operation_evidence _) + | Single (Double_baking_evidence _) + | Single (Dal_entrapment_evidence _) + | Single (Activate_account _) + | Single (Vdf_revelation _) -> + return_nil + | Single (Proposals {source; _}) | Single (Ballot {source; _}) -> + return [source] + | Single (Drain_delegate {delegate; _}) -> return [delegate] + | Single (Manager_operation {source; _}) -> return [source] + | Cons (Manager_operation {source; _}, _) -> return [source] + in + let map_pkh_env = List.map Tezos_crypto.Signature.Of_V2.public_key_hash in + return @@ map_pkh_env sources module Internal_for_tests = struct let default_config_with_clock_drift clock_drift = diff --git a/src/proto_024_PtTALLiN/lib_plugin/plugin_registerer.ml b/src/proto_024_PtTALLiN/lib_plugin/plugin_registerer.ml index a28208d9a4932a39cded4ee0b9c0b8d9d3063f43..262854e56d9159488de5a81611f9801b789a9912 100644 --- a/src/proto_024_PtTALLiN/lib_plugin/plugin_registerer.ml +++ b/src/proto_024_PtTALLiN/lib_plugin/plugin_registerer.ml @@ -50,6 +50,9 @@ module Delegators_contribution_plugin = struct let delegated_breakdown_at_sampling context ~cycle ~delegate_pkh = let open Lwt_result_syntax in + let*? delegate_pkh = + Signature.Of_V_latest.get_public_key_hash delegate_pkh + in let* output = Delegators_contribution.delegated_breakdown_at_sampling context @@ -80,6 +83,9 @@ module Delegators_contribution_plugin = struct let min_delegated_breakdown context ~delegate_pkh = let open Lwt_result_syntax in + let*? delegate_pkh = + Signature.Of_V_latest.get_public_key_hash delegate_pkh + in let* { total_delegated; own_delegated; diff --git a/src/proto_alpha/lib_agnostic_baker/baker_commands_helpers.ml b/src/proto_alpha/lib_agnostic_baker/baker_commands_helpers.ml index bff9aa16e8bd0eb1aeb00fe44de2750284a3ceb7..a8679ab150e5fd79c3e7659ce3b0825049b2ec76 100644 --- a/src/proto_alpha/lib_agnostic_baker/baker_commands_helpers.ml +++ b/src/proto_alpha/lib_agnostic_baker/baker_commands_helpers.ml @@ -44,6 +44,9 @@ let run_baker (cctxt : Tezos_client_base.Client_context.full) ?dal_node_rpc_ctxt {vote_file; liquidity_baking_vote = to_protocol liquidity_baking_vote}) votes in + let*? sources = + List.map_e Signature.Of_V_latest.get_public_key_hash sources + in let* delegates = Baking_commands.get_delegates cctxt sources in Client_daemon.Baker.run cctxt diff --git a/src/proto_alpha/lib_dal/dal_plugin_registration.ml b/src/proto_alpha/lib_dal/dal_plugin_registration.ml index 04e5765f81e5d1da9a486538bc00f4d26dd91fad..7871f5563ceb90ce63caa7939afb07b78877a73d 100644 --- a/src/proto_alpha/lib_dal/dal_plugin_registration.ml +++ b/src/proto_alpha/lib_dal/dal_plugin_registration.ml @@ -210,6 +210,7 @@ module Plugin = struct let* block_hash = Protocol_client_context.Alpha_block_services.hash cctxt ~chain ~block () in + let*? source = Signature.Of_V_latest.get_public_key_hash source in let* counter = let* pcounter = Plugin.Alpha_services.Contract.counter cpctxt (chain, `Head 0) source @@ -244,6 +245,7 @@ module Plugin = struct Operation.unsigned_encoding ({branch = block_hash}, Contents_list (Single operation)) in + let*? src_sk = Signature.Of_V_latest.get_secret_key src_sk in let bytes = Signature.append ~watermark:Signature.Generic_operation src_sk bytes in @@ -381,12 +383,9 @@ module Plugin = struct in List.fold_left_es (fun acc ({delegate; indexes} : Plugin.RPC.Dal.S.shards_assignment) -> - let delegate_pkh = - Tezos_crypto.Signature.Of_V2.public_key_hash delegate - in let* tb_slot = match - Signature.Public_key_hash.Map.find delegate_pkh pkh_to_tb_slot_map + Signature.Public_key_hash.Map.find delegate pkh_to_tb_slot_map with | Some slot -> return (Slot.to_int slot) | None -> @@ -396,9 +395,10 @@ module Plugin = struct (Resto.Path.to_string Tezos_rpc.Path.(Plugin.RPC.Dal.path / "shards"))) in + let delegate = Tezos_crypto.Signature.Of_V2.public_key_hash delegate in return @@ Tezos_crypto.Signature.Public_key_hash.Map.add - delegate_pkh + delegate (indexes, tb_slot) acc) Tezos_crypto.Signature.Public_key_hash.Map.empty diff --git a/src/proto_alpha/lib_plugin/mempool.ml b/src/proto_alpha/lib_plugin/mempool.ml index 58fa927f260220afc1d9b0f95d11d2f141d65a07..bf9c54ad60224f4e5adecb9d4866a03185d962fa 100644 --- a/src/proto_alpha/lib_plugin/mempool.ml +++ b/src/proto_alpha/lib_plugin/mempool.ml @@ -855,31 +855,47 @@ let sources_from_operation ctxt ({shell = _; protocol_data = Operation_data {contents; _}} : Main.operation) = let open Lwt_syntax in - match contents with - | Single (Failing_noop _) -> return_nil - | Single (Preattestation consensus_content) - | Single (Attestation {consensus_content; dal_content = _}) -> - let attested_level = Level.from_raw ctxt consensus_content.level in - sources_from_level_and_slot ctxt ~attested_level consensus_content.slot - | Single (Preattestations_aggregate {consensus_content; committee}) -> - sources_from_aggregate ctxt consensus_content committee - | Single (Attestations_aggregate {consensus_content; committee}) -> - sources_from_aggregate - ctxt - consensus_content - (Operation.committee_slots committee) - | Single (Seed_nonce_revelation _) - | Single (Double_consensus_operation_evidence _) - | Single (Double_baking_evidence _) - | Single (Dal_entrapment_evidence _) - | Single (Activate_account _) - | Single (Vdf_revelation _) -> - return_nil - | Single (Proposals {source; _}) | Single (Ballot {source; _}) -> - return [source] - | Single (Drain_delegate {delegate; _}) -> return [delegate] - | Single (Manager_operation {source; _}) -> return [source] - | Cons (Manager_operation {source; _}, _) -> return [source] + let* sources = + match contents with + | Single (Failing_noop _) -> return_nil + | Single (Preattestation consensus_content) + | Single (Attestation {consensus_content; dal_content = _}) -> + let attested_level = Level.from_raw ctxt consensus_content.level in + let* sources = + sources_from_level_and_slot + ctxt + ~attested_level + consensus_content.slot + in + return sources + | Single (Preattestations_aggregate {consensus_content; committee}) -> + let* sources = + sources_from_aggregate ctxt consensus_content committee + in + return sources + | Single (Attestations_aggregate {consensus_content; committee}) -> + let* sources = + sources_from_aggregate + ctxt + consensus_content + (Operation.committee_slots committee) + in + return sources + | Single (Seed_nonce_revelation _) + | Single (Double_consensus_operation_evidence _) + | Single (Double_baking_evidence _) + | Single (Dal_entrapment_evidence _) + | Single (Activate_account _) + | Single (Vdf_revelation _) -> + return_nil + | Single (Proposals {source; _}) | Single (Ballot {source; _}) -> + return [source] + | Single (Drain_delegate {delegate; _}) -> return [delegate] + | Single (Manager_operation {source; _}) -> return [source] + | Cons (Manager_operation {source; _}, _) -> return [source] + in + let map_pkh_env = List.map Tezos_crypto.Signature.Of_V2.public_key_hash in + return @@ map_pkh_env sources module Internal_for_tests = struct let default_config_with_clock_drift clock_drift = diff --git a/src/proto_alpha/lib_plugin/plugin_registerer.ml b/src/proto_alpha/lib_plugin/plugin_registerer.ml index a28208d9a4932a39cded4ee0b9c0b8d9d3063f43..e99f04ce335465dd97eda5979b9bd25c8db5aed9 100644 --- a/src/proto_alpha/lib_plugin/plugin_registerer.ml +++ b/src/proto_alpha/lib_plugin/plugin_registerer.ml @@ -50,6 +50,10 @@ module Delegators_contribution_plugin = struct let delegated_breakdown_at_sampling context ~cycle ~delegate_pkh = let open Lwt_result_syntax in + let*? delegate_pkh = + Signature.Of_V_latest.get_public_key_hash delegate_pkh + in + let* output = Delegators_contribution.delegated_breakdown_at_sampling context @@ -80,6 +84,9 @@ module Delegators_contribution_plugin = struct let min_delegated_breakdown context ~delegate_pkh = let open Lwt_result_syntax in + let*? delegate_pkh = + Signature.Of_V_latest.get_public_key_hash delegate_pkh + in let* { total_delegated; own_delegated; diff --git a/teztale/bin_teztale_archiver/PtSeouLo_machine.real.ml b/teztale/bin_teztale_archiver/PtSeouLo_machine.real.ml index 180d52a80a0e6cbea77adf69eef013907f79dd33..c7fdc03698901fbf77c9456cd617e583c986fa7c 100644 --- a/teztale/bin_teztale_archiver/PtSeouLo_machine.real.ml +++ b/teztale/bin_teztale_archiver/PtSeouLo_machine.real.ml @@ -200,6 +200,9 @@ module Services : Protocol_machinery.PROTOCOL_SERVICES = struct return @@ List.map (fun Plugin.RPC.Dal.S.{delegate; indexes} -> + let delegate = + Tezos_crypto.Signature.Of_V2.public_key_hash delegate + in Data.Dal.{delegate; assigned_shard_indices = indexes}) shard_assignments diff --git a/teztale/bin_teztale_archiver/PtTALLiN_machine.real.ml b/teztale/bin_teztale_archiver/PtTALLiN_machine.real.ml index ed842de7ba4d38e98d37902c1166fd37e0ce06fe..7d20f291ead5887ceb8814d13df615b726e3a9c3 100644 --- a/teztale/bin_teztale_archiver/PtTALLiN_machine.real.ml +++ b/teztale/bin_teztale_archiver/PtTALLiN_machine.real.ml @@ -200,6 +200,9 @@ module Services : Protocol_machinery.PROTOCOL_SERVICES = struct return @@ List.map (fun Plugin.RPC.Dal.S.{delegate; indexes} -> + let delegate = + Tezos_crypto.Signature.Of_V2.public_key_hash delegate + in Data.Dal.{delegate; assigned_shard_indices = indexes}) shard_assignments diff --git a/teztale/bin_teztale_archiver/alpha_machine.real.ml b/teztale/bin_teztale_archiver/alpha_machine.real.ml index cd31a5f9c488e8631c3c3a5965913bb5a1b3b2e9..d227ab272162c54a2a825a30d6b58f35a37346d0 100644 --- a/teztale/bin_teztale_archiver/alpha_machine.real.ml +++ b/teztale/bin_teztale_archiver/alpha_machine.real.ml @@ -200,6 +200,9 @@ module Services : Protocol_machinery.PROTOCOL_SERVICES = struct return @@ List.map (fun Plugin.RPC.Dal.S.{delegate; indexes} -> + let delegate = + Tezos_crypto.Signature.Of_V2.public_key_hash delegate + in Data.Dal.{delegate; assigned_shard_indices = indexes}) shard_assignments