Library MonadVM.Crypto
File generated by rocq-of-ocaml
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import RocqOfOCaml.Settings.
Require MonadVM.Byte_string.
Require MonadVM.Numeric.
Require MonadVM.Option.
Require MonadVM.Result.
Require MonadVM.Traits.
Require MonadVM.Uint64.
Require Import MonadVM.Support.Variants.
Require MonadVM.Support.CryptoBlake2.
Require Import RocqOfOCaml.Opaque.
Local Ltac _rocq_witness_type typ :=
let typ := eval hnf in typ in
lazymatch typ with
| list ?a ⇒ let a := _rocq_witness_type a in constr:(list a)
| option ?a ⇒ let a := _rocq_witness_type a in constr:(option a)
| prod ?a ?b ⇒ let a := _rocq_witness_type a in
let b := _rocq_witness_type b in constr:(prod a b)
| sum ?a ?b ⇒ let a := _rocq_witness_type a in
let b := _rocq_witness_type b in constr:(sum a b)
| ?a → ?b ⇒ let a := _rocq_witness_type a in
let b := _rocq_witness_type b in constr:(a → b)
| _ ⇒ typ
end.
Require Import RocqOfOCaml.Settings.
Require MonadVM.Byte_string.
Require MonadVM.Numeric.
Require MonadVM.Option.
Require MonadVM.Result.
Require MonadVM.Traits.
Require MonadVM.Uint64.
Require Import MonadVM.Support.Variants.
Require MonadVM.Support.CryptoBlake2.
Require Import RocqOfOCaml.Opaque.
Local Ltac _rocq_witness_type typ :=
let typ := eval hnf in typ in
lazymatch typ with
| list ?a ⇒ let a := _rocq_witness_type a in constr:(list a)
| option ?a ⇒ let a := _rocq_witness_type a in constr:(option a)
| prod ?a ?b ⇒ let a := _rocq_witness_type a in
let b := _rocq_witness_type b in constr:(prod a b)
| sum ?a ?b ⇒ let a := _rocq_witness_type a in
let b := _rocq_witness_type b in constr:(sum a b)
| ?a → ?b ⇒ let a := _rocq_witness_type a in
let b := _rocq_witness_type b in constr:(a → b)
| _ ⇒ typ
end.
keccak_256 bytes computes the Keccak-256 digest of a byte array.
Definition keccak_256 (input : MonadVM.Byte_string.Bytes.t) : string :=
let bytes_value :=
Digestif.KECCAK_256.(Digestif.S.to_raw_string)
(Digestif.KECCAK_256.(Digestif.S.digest_string) None None input) in
MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.of_bytes_exn)
bytes_value.
let bytes_value :=
Digestif.KECCAK_256.(Digestif.S.to_raw_string)
(Digestif.KECCAK_256.(Digestif.S.digest_string) None None input) in
MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.of_bytes_exn)
bytes_value.
The Keccak-256 encoding of the empty byte array.
Definition keccak_256_empty : string :=
keccak_256 MonadVM.Byte_string.Bytes.empty.
Definition secp256k1b : MonadVM.Numeric.U256.t :=
MonadVM.Numeric.U256.op_tildedollar 7.
Definition secp256k1p : MonadVM.Numeric.U256.t :=
MonadVM.Numeric.U256.op_tildeat
"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F".
Definition secp256k1n : MonadVM.Numeric.U256.t :=
MonadVM.Numeric.U256.op_tildeat
"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141".
Definition context : Libsecp256k1.External.Context.t :=
Libsecp256k1.External.Context.create None None tt.
Module signature.
Record record : Set := Build {
r : MonadVM.Numeric.U256.t;
s : MonadVM.Numeric.U256.t;
y_parity : MonadVM.Numeric.U8.t;
}.
Definition with_r r (record_value : record) :=
Build r record_value.(s) record_value.(y_parity).
Definition with_s s (record_value : record) :=
Build record_value.(r) s record_value.(y_parity).
Definition with_y_parity y_parity (record_value : record) :=
Build record_value.(r) record_value.(s) y_parity.
End signature.
Definition signature := signature.record.
keccak_256 MonadVM.Byte_string.Bytes.empty.
Definition secp256k1b : MonadVM.Numeric.U256.t :=
MonadVM.Numeric.U256.op_tildedollar 7.
Definition secp256k1p : MonadVM.Numeric.U256.t :=
MonadVM.Numeric.U256.op_tildeat
"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F".
Definition secp256k1n : MonadVM.Numeric.U256.t :=
MonadVM.Numeric.U256.op_tildeat
"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141".
Definition context : Libsecp256k1.External.Context.t :=
Libsecp256k1.External.Context.create None None tt.
Module signature.
Record record : Set := Build {
r : MonadVM.Numeric.U256.t;
s : MonadVM.Numeric.U256.t;
y_parity : MonadVM.Numeric.U8.t;
}.
Definition with_r r (record_value : record) :=
Build r record_value.(s) record_value.(y_parity).
Definition with_s s (record_value : record) :=
Build record_value.(r) s record_value.(y_parity).
Definition with_y_parity y_parity (record_value : record) :=
Build record_value.(r) record_value.(s) y_parity.
End signature.
Definition signature := signature.record.
ecrecover sign hash recovers the address that signed a message from the signature sign and the signed
hash hash. This is equivalent to ECDSARECOVER (YP (311)) followed by sender address extraction
(YP (323)).
Definition _rocq_fallback_ecrecover_0 :
ltac:(let typ := constr:(ltac:(first [unshelve refine ((Bigstring.t)%type
)]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.
Definition _rocq_fallback_ecrecover_1 :
ltac:(let typ := constr:(ltac:(first [unshelve refine ((string)%type
)]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
_rocq_fallback_ecrecover_0 | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.
Definition _rocq_fallback_ecrecover_2 :
ltac:(let typ := constr:(ltac:(first [unshelve refine ((ascii)%type
)]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
_rocq_fallback_ecrecover_0 | exact
_rocq_fallback_ecrecover_1 | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.
Definition ecrecover (function_parameter : signature)
: string → option MonadVM.Byte_string.B20.t :=
let '{|
MonadVM.Crypto.signature.r := r_value;
MonadVM.Crypto.signature.s := s_value;
MonadVM.Crypto.signature.y_parity := y_parity
|} := function_parameter in
fun (msg_hash : string) ⇒
MonadVM.Option.op_letdollar
(MonadVM.Option.ensure
(orb (MonadVM.Numeric.U8.op_eq y_parity MonadVM.Numeric.U8.zero)
(MonadVM.Numeric.U8.op_eq y_parity MonadVM.Numeric.U8.one)))
(fun function_parameter ⇒
let '_ := function_parameter in
let is_square :=
MonadVM.Numeric.U256.op_eq MonadVM.Numeric.U256.one
(MonadVM.Numeric.U256.exp_mod secp256k1p
(MonadVM.Numeric.U256.op_plus
(MonadVM.Numeric.U256.exp_mod secp256k1p r_value
(MonadVM.Numeric.U256.op_tildedollar 3)) secp256k1b)
(MonadVM.Numeric.U256.op_div
(MonadVM.Numeric.U256.op_minus secp256k1p
(MonadVM.Numeric.U256.op_tildedollar 1))
(MonadVM.Numeric.U256.op_tildedollar 2))) in
MonadVM.Option.op_letdollar (MonadVM.Option.ensure is_square)
(fun function_parameter ⇒
let '_ := function_parameter in
let r_value := MonadVM.Numeric.U256.to_repr r_value in
let s_value := MonadVM.Numeric.U256.to_repr s_value in
let y_parity := MonadVM.Numeric.U8.to_repr y_parity in
let signature_i (function_parameter : int) : ascii :=
match
(function_parameter,
(let 'i_value := function_parameter in
Z.ltb i_value 32),
(let 'i_value := function_parameter in
Z.ltb i_value 64)) with
| (i_value, true, _) ⇒
MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.op_pointdollarlparenrparen)
r_value i_value
| (i_value, _, true) ⇒
MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.op_pointdollarlparenrparen)
s_value (RocqOfOCaml.OCamlInt.sub i_value 32)
| (_, _, _) ⇒
MonadVM.Numeric.U8.Repr.op_pointdollarlparenrparen y_parity 0
end in
MonadVM.Option.op_letdollar
(MonadVM.Result.to_option
(Libsecp256k1.External.Sign.read_recoverable context
((RocqOfOCaml.Bigstring.init
(H := RocqOfOCaml.Opaque.legacy_witness _rocq_fallback_ecrecover_0))
Libsecp256k1.External.Sign.recoverable_bytes signature_i)))
(fun signature ⇒
MonadVM.Option.op_letdollar
(MonadVM.Option.map
(Libsecp256k1.External.Key.to_bytes (Some false) context)
(MonadVM.Result.to_option
(Libsecp256k1.External.Sign.recover context signature
(Bigstring.of_string msg_hash))))
(fun result_bigstring ⇒
let public_key_i (i_value : int) : ascii :=
(RocqOfOCaml.OCamlBigarray.Array1.get
(H := RocqOfOCaml.Opaque.legacy_witness _rocq_fallback_ecrecover_2))
result_bigstring (RocqOfOCaml.OCamlInt.add i_value 1) in
let public_key :=
(MonadVM.Byte_string.Bytes.init
(_rocq_fallback_ecrecover_1)) 64 public_key_i in
MonadVM.Option._return
(MonadVM.Byte_string.B20.of_bytes32_truncating
(keccak_256 public_key)))))).
Definition sha_256 (bs : MonadVM.Byte_string.Bytes.t) : string :=
MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.of_bytes_exn)
(Digestif.SHA256.(Digestif.S.to_raw_string)
(Digestif.SHA256.(Digestif.S.digest_string) None None bs)).
Definition ripemd_160 (bs : MonadVM.Byte_string.Bytes.t)
: MonadVM.Byte_string.B20.t :=
MonadVM.Byte_string.B20.of_bytes_exn
(Digestif.RMD160.(Digestif.S.to_raw_string)
(Digestif.RMD160.(Digestif.S.digest_string) None None bs)).
Definition blake2b_iv : array int64 :=
[
7640891576956012808;
(-4942790177534073029);
4354685564936845355;
(-6534734903238641935);
5840696475078001361;
(-7276294671716946913);
2270897969802886507;
6620516959819538809
].
Definition blake2b_sigma : array (array int) :=
[
[ 0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15 ];
[ 14; 10; 4; 8; 9; 15; 13; 6; 1; 12; 0; 2; 11; 7; 5; 3 ];
[ 11; 8; 12; 0; 5; 2; 15; 13; 10; 14; 3; 6; 7; 1; 9; 4 ];
[ 7; 9; 3; 1; 13; 12; 11; 14; 2; 6; 5; 10; 4; 0; 15; 8 ];
[ 9; 0; 5; 7; 2; 4; 10; 15; 14; 1; 11; 12; 6; 8; 3; 13 ];
[ 2; 12; 6; 10; 0; 11; 8; 3; 4; 13; 7; 5; 15; 14; 1; 9 ];
[ 12; 5; 1; 15; 14; 13; 4; 10; 0; 7; 6; 3; 9; 2; 8; 11 ];
[ 13; 11; 7; 14; 12; 1; 3; 9; 5; 0; 15; 4; 8; 6; 2; 10 ];
[ 6; 15; 14; 9; 11; 3; 0; 8; 12; 2; 13; 7; 1; 4; 10; 5 ];
[ 10; 2; 8; 4; 7; 6; 1; 5; 15; 11; 9; 14; 3; 12; 13; 0 ]
].
Definition rotr64 (x_value : int64) (n_value : int) : int64 :=
MonadVM.Uint64.logor (MonadVM.Uint64.shift_right x_value n_value)
(MonadVM.Uint64.shift_left x_value (RocqOfOCaml.OCamlInt.sub 64 n_value)).
ltac:(let typ := constr:(ltac:(first [unshelve refine ((Bigstring.t)%type
)]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.
Definition _rocq_fallback_ecrecover_1 :
ltac:(let typ := constr:(ltac:(first [unshelve refine ((string)%type
)]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
_rocq_fallback_ecrecover_0 | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.
Definition _rocq_fallback_ecrecover_2 :
ltac:(let typ := constr:(ltac:(first [unshelve refine ((ascii)%type
)]; first [match goal with |- ?g ⇒ let sort := type of g in lazymatch sort with Set ⇒ fail | _ ⇒ shelve end end | exact
_rocq_fallback_ecrecover_0 | exact
_rocq_fallback_ecrecover_1 | eassumption | typeclasses eauto | RocqOfOCaml.Opaque.opaque_inhabit 64%nat])) in let typ := _rocq_witness_type typ in exact typ).
Proof. unshelve (RocqOfOCaml.Opaque.opaque_inhabit 64%nat). all: RocqOfOCaml.Opaque.opaque_inhabit 64%nat. Qed.
Definition ecrecover (function_parameter : signature)
: string → option MonadVM.Byte_string.B20.t :=
let '{|
MonadVM.Crypto.signature.r := r_value;
MonadVM.Crypto.signature.s := s_value;
MonadVM.Crypto.signature.y_parity := y_parity
|} := function_parameter in
fun (msg_hash : string) ⇒
MonadVM.Option.op_letdollar
(MonadVM.Option.ensure
(orb (MonadVM.Numeric.U8.op_eq y_parity MonadVM.Numeric.U8.zero)
(MonadVM.Numeric.U8.op_eq y_parity MonadVM.Numeric.U8.one)))
(fun function_parameter ⇒
let '_ := function_parameter in
let is_square :=
MonadVM.Numeric.U256.op_eq MonadVM.Numeric.U256.one
(MonadVM.Numeric.U256.exp_mod secp256k1p
(MonadVM.Numeric.U256.op_plus
(MonadVM.Numeric.U256.exp_mod secp256k1p r_value
(MonadVM.Numeric.U256.op_tildedollar 3)) secp256k1b)
(MonadVM.Numeric.U256.op_div
(MonadVM.Numeric.U256.op_minus secp256k1p
(MonadVM.Numeric.U256.op_tildedollar 1))
(MonadVM.Numeric.U256.op_tildedollar 2))) in
MonadVM.Option.op_letdollar (MonadVM.Option.ensure is_square)
(fun function_parameter ⇒
let '_ := function_parameter in
let r_value := MonadVM.Numeric.U256.to_repr r_value in
let s_value := MonadVM.Numeric.U256.to_repr s_value in
let y_parity := MonadVM.Numeric.U8.to_repr y_parity in
let signature_i (function_parameter : int) : ascii :=
match
(function_parameter,
(let 'i_value := function_parameter in
Z.ltb i_value 32),
(let 'i_value := function_parameter in
Z.ltb i_value 64)) with
| (i_value, true, _) ⇒
MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.op_pointdollarlparenrparen)
r_value i_value
| (i_value, _, true) ⇒
MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.op_pointdollarlparenrparen)
s_value (RocqOfOCaml.OCamlInt.sub i_value 32)
| (_, _, _) ⇒
MonadVM.Numeric.U8.Repr.op_pointdollarlparenrparen y_parity 0
end in
MonadVM.Option.op_letdollar
(MonadVM.Result.to_option
(Libsecp256k1.External.Sign.read_recoverable context
((RocqOfOCaml.Bigstring.init
(H := RocqOfOCaml.Opaque.legacy_witness _rocq_fallback_ecrecover_0))
Libsecp256k1.External.Sign.recoverable_bytes signature_i)))
(fun signature ⇒
MonadVM.Option.op_letdollar
(MonadVM.Option.map
(Libsecp256k1.External.Key.to_bytes (Some false) context)
(MonadVM.Result.to_option
(Libsecp256k1.External.Sign.recover context signature
(Bigstring.of_string msg_hash))))
(fun result_bigstring ⇒
let public_key_i (i_value : int) : ascii :=
(RocqOfOCaml.OCamlBigarray.Array1.get
(H := RocqOfOCaml.Opaque.legacy_witness _rocq_fallback_ecrecover_2))
result_bigstring (RocqOfOCaml.OCamlInt.add i_value 1) in
let public_key :=
(MonadVM.Byte_string.Bytes.init
(_rocq_fallback_ecrecover_1)) 64 public_key_i in
MonadVM.Option._return
(MonadVM.Byte_string.B20.of_bytes32_truncating
(keccak_256 public_key)))))).
Definition sha_256 (bs : MonadVM.Byte_string.Bytes.t) : string :=
MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.of_bytes_exn)
(Digestif.SHA256.(Digestif.S.to_raw_string)
(Digestif.SHA256.(Digestif.S.digest_string) None None bs)).
Definition ripemd_160 (bs : MonadVM.Byte_string.Bytes.t)
: MonadVM.Byte_string.B20.t :=
MonadVM.Byte_string.B20.of_bytes_exn
(Digestif.RMD160.(Digestif.S.to_raw_string)
(Digestif.RMD160.(Digestif.S.digest_string) None None bs)).
Definition blake2b_iv : array int64 :=
[
7640891576956012808;
(-4942790177534073029);
4354685564936845355;
(-6534734903238641935);
5840696475078001361;
(-7276294671716946913);
2270897969802886507;
6620516959819538809
].
Definition blake2b_sigma : array (array int) :=
[
[ 0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15 ];
[ 14; 10; 4; 8; 9; 15; 13; 6; 1; 12; 0; 2; 11; 7; 5; 3 ];
[ 11; 8; 12; 0; 5; 2; 15; 13; 10; 14; 3; 6; 7; 1; 9; 4 ];
[ 7; 9; 3; 1; 13; 12; 11; 14; 2; 6; 5; 10; 4; 0; 15; 8 ];
[ 9; 0; 5; 7; 2; 4; 10; 15; 14; 1; 11; 12; 6; 8; 3; 13 ];
[ 2; 12; 6; 10; 0; 11; 8; 3; 4; 13; 7; 5; 15; 14; 1; 9 ];
[ 12; 5; 1; 15; 14; 13; 4; 10; 0; 7; 6; 3; 9; 2; 8; 11 ];
[ 13; 11; 7; 14; 12; 1; 3; 9; 5; 0; 15; 4; 8; 6; 2; 10 ];
[ 6; 15; 14; 9; 11; 3; 0; 8; 12; 2; 13; 7; 1; 4; 10; 5 ];
[ 10; 2; 8; 4; 7; 6; 1; 5; 15; 11; 9; 14; 3; 12; 13; 0 ]
].
Definition rotr64 (x_value : int64) (n_value : int) : int64 :=
MonadVM.Uint64.logor (MonadVM.Uint64.shift_right x_value n_value)
(MonadVM.Uint64.shift_left x_value (RocqOfOCaml.OCamlInt.sub 64 n_value)).
blake2f ¬rounds ¬h ¬m ¬t0 ¬t1 ¬final_block computes rounds rounds of the
BLAKE2b compression function F (RFC 7693 §3.2) and returns the updated hash state.
Unfortunately, this is not exposed by any off-the-shelf library.