···40404141 val generate : rng:(int -> Cstruct.t) -> priv * pub
42424343- val sign : ?mask:[ `No | `Yes | `Yes_with of Mirage_crypto_rng.g ] ->
4444- key:priv -> ?k:Cstruct.t -> Cstruct.t -> Cstruct.t * Cstruct.t
4343+ val sign : key:priv -> ?k:Cstruct.t -> Cstruct.t -> Cstruct.t * Cstruct.t
45444645 val verify : key:pub -> Cstruct.t * Cstruct.t -> Cstruct.t -> bool
4746···467466 let q = S.scalar_mult d P.params_g in
468467 (d, q)
469468470470- let blind mask =
471471- let inv a =
472472- let b = create () in
473473- F.inv b a;
474474- F.to_montgomery b b;
475475- b
476476- in
477477- let rec rng g =
478478- let r = Mirage_crypto_rng.generate ?g Param.byte_length in
479479- if not_zero r && smaller_n r then begin
480480- let ba = from_be_cstruct r in
481481- F.to_montgomery ba ba;
482482- Some (ba, inv ba)
483483- end else
484484- rng g
485485- in
486486- match mask with
487487- | `No -> None
488488- | `Yes -> rng None
489489- | `Yes_with g -> rng (Some g)
490490-491491- let sign ?(mask = `Yes) ~key ?k msg =
469469+ let sign ~key ?k msg =
492470 (* blinding: literature: s = k^-1 * (m + r * priv_key) mod n
493471 we blind, similar to OpenSSL (https://github.com/openssl/openssl/commit/a3e9d5aa980f238805970f420adf5e903d35bf09):
494472 s = k^-1 * blind^-1 (blind * m + blind * r * priv_key) mod n
495473 *)
496496- let b = blind mask in
497474 let msg = padded msg in
498475 let e = from_be_cstruct msg in
499476 let g = K_gen_default.g ~key msg in
···524501 let rd = create () in
525502 let dmon = from_be_cstruct (S.to_cstruct key) in
526503 F.to_montgomery dmon dmon;
527527- (match b with None -> () | Some (b, _) -> F.mul dmon b dmon);
528504 F.mul rd r_mon dmon;
529505 let cmon = create () in
530506 let zmon = create () in
531507 F.to_montgomery zmon e;
532532- (match b with None -> () | Some (b, _) -> F.mul zmon b zmon);
533508 F.add cmon zmon rd;
534509 let smon = create () in
535510 F.mul smon kmon cmon;
536536- (match b with None -> () | Some (_, b') -> F.mul smon b' smon);
537511 let s = create () in
538512 F.from_montgomery s smon;
539513 let s = to_be_cstruct s in
+5-8
ec/mirage_crypto_ec.mli
···100100101101 (** {2 Cryptographic operations} *)
102102103103- val sign : ?mask:[ `No | `Yes | `Yes_with of Mirage_crypto_rng.g ] ->
104104- key:priv -> ?k:Cstruct.t -> Cstruct.t -> Cstruct.t * Cstruct.t
105105- (** [sign ~mask ~key ~k digest] signs the message [digest] using the private
106106- [key]. Only the leftmost bits within the curve order are considered. If
107107- [mask] is provided, the computation is blinded to protect the private
108108- key operation. If [k] is not provided, it is computed using the
109109- deterministic construction from RFC 6979. The result is a pair of
110110- [r] and [s]. *)
103103+ val sign : key:priv -> ?k:Cstruct.t -> Cstruct.t -> Cstruct.t * Cstruct.t
104104+ (** [sign ~key ~k digest] signs the message [digest] using the private
105105+ [key]. Only the leftmost bits within the curve order are considered.
106106+ If [k] is not provided, it is computed using the deterministic
107107+ construction from RFC 6979. The result is a pair of [r] and [s]. *)
111108112109 val verify : key:pub -> Cstruct.t * Cstruct.t -> Cstruct.t -> bool
113110 (** [verify ~key (r, s) digest] verifies the signature [r, s] on the message