Revision 9aebedd3b0745f5e261919cb56e8b5b317828c63 authored by Joel Bjornson on 23 November 2022, 12:04:53 UTC, committed by Joel Bjornson on 23 November 2022, 13:15:53 UTC
1 parent ebf2abc
yes-node.patch
diff --git a/src/lib_crypto/signature.ml b/src/lib_crypto/signature.ml
index 4dc73c00f8..89c3e29ce8 100644
--- a/src/lib_crypto/signature.ml
+++ b/src/lib_crypto/signature.ml
@@ -634,7 +634,7 @@ let pp_watermark ppf =
"Custom: 0x%s"
(try String.sub hexed 0 10 ^ "..." with Invalid_argument _ -> hexed)
-let sign ?watermark secret_key message =
+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)
@@ -664,6 +664,81 @@ let check ?watermark public_key signature message =
P256.check ?watermark pk signature message
| _ -> false
+let fake_sign pk_bytes msg =
+ let msg = Blake2B.to_bytes @@ Blake2B.hash_bytes [msg] in
+ let half = size / 2 in
+ let tmp = Bytes.init size (fun _ -> '0') in
+ let all_or_half buf = Stdlib.min (Bytes.length buf) half in
+ Bytes.blit pk_bytes 0 tmp 0 (all_or_half pk_bytes) ;
+ Bytes.blit msg 0 tmp half (all_or_half msg) ;
+ of_bytes_exn tmp
+
+type algo = Ed25519 | Secp256k1 | P256
+
+let fake_sign_from_pk pk msg =
+ let pk_bytes = Data_encoding.Binary.to_bytes_exn Public_key.encoding pk in
+ fake_sign pk_bytes msg
+
+let hardcoded_sk algo : secret_key =
+ match
+ match algo with
+ | Ed25519 ->
+ Secret_key.of_b58check
+ "edsk3gUfUPyBSfrS9CCgmCiQsTCHGkviBDusMxDJstFtojtc1zcpsh"
+ | Secp256k1 ->
+ Secret_key.of_b58check
+ "spsk2XJu4wuYsHeuDaCktD3ECnnpn574ceSWHEJVvXTt7JP6ztySCL"
+ | P256 ->
+ Secret_key.of_b58check
+ "p2sk2k6YAkNJ8CySZCS3vGA5Ht6Lj6LXG3yb8UrHvMKZy7Ab8JUtWh"
+ with
+ | Ok key -> key
+ | _ -> assert false
+
+let hardcoded_pk =
+ (* precompute signatures *)
+ let (ed, secp, p) =
+ ( Secret_key.to_public_key (hardcoded_sk Ed25519),
+ Secret_key.to_public_key (hardcoded_sk Secp256k1),
+ Secret_key.to_public_key (hardcoded_sk P256) )
+ in
+ function Ed25519 -> ed | Secp256k1 -> secp | P256 -> p
+
+let hardcoded_msg = Bytes.of_string "Cheers"
+
+let hardcoded_sig =
+ (* precompute signatures *)
+ let (ed, secp, p) =
+ ( _sign (hardcoded_sk Ed25519) hardcoded_msg,
+ _sign (hardcoded_sk Secp256k1) hardcoded_msg,
+ _sign (hardcoded_sk P256) hardcoded_msg )
+ in
+ function Ed25519 -> ed | Secp256k1 -> secp | P256 -> p
+
+let algo_of_pk (pk : Public_key.t) =
+ match pk with
+ | Ed25519 _ -> Ed25519
+ | Secp256k1 _ -> Secp256k1
+ | P256 _ -> P256
+
+let check_harcoded_signature pk =
+ let algo = algo_of_pk pk in
+ check (hardcoded_pk algo) (hardcoded_sig algo) hardcoded_msg
+
+let fake_check ?watermark pk signature msg =
+ (* computing the fake signature do hash the message,
+ this operation is linear in the size of the message *)
+ let _ = check_harcoded_signature pk in
+ (* checking a valid, harcoded signature, to do at least once the crypto maths *)
+ if equal signature (fake_sign_from_pk pk msg) then true
+ else check ?watermark pk signature msg
+
+let sign ?watermark:_ sk msg =
+ let pk_bytes = Data_encoding.Binary.to_bytes_exn Secret_key.encoding sk in
+ fake_sign pk_bytes msg
+
+let check = fake_check
+
(* The following cache is a hack to work around a quadratic algorithm
in Tezos Mainnet protocols up to Edo. *)
@@ -735,9 +810,17 @@ let append ?watermark sk msg = Bytes.cat msg (to_bytes (sign ?watermark sk msg))
let concat msg signature = Bytes.cat msg (to_bytes signature)
-type algo = Ed25519 | Secp256k1 | P256
-
-let generate_key ?(algo = Ed25519) ?seed () =
+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 original_generate_key ?(algo = Ed25519) ?seed () =
match algo with
| Ed25519 ->
let (pkh, pk, sk) = Ed25519.generate_key ?seed () in
@@ -751,6 +834,12 @@ let generate_key ?(algo = Ed25519) ?seed () =
let (pkh, pk, sk) = P256.generate_key ?seed () in
(Public_key_hash.P256 pkh, Public_key.P256 pk, Secret_key.P256 sk)
+let generate_key ?(algo = Ed25519) ?seed () =
+ (* We keep the original keys generation to stay as close as possible of the
+ initial performences. *)
+ let true_keys = original_generate_key ~algo ?seed () in
+ fake_generate_key true_keys
+
let deterministic_nonce sk msg =
match sk with
| Secret_key.Ed25519 sk -> Ed25519.deterministic_nonce sk msg
![swh spinner](/static/img/swh-spinner.gif)
Computing file changes ...