val ecdsa_verify_finv: p:point -> r:felem -> Stack bool
  (requires fun h ->
    live h p /\ live h r /\ disjoint p r /\
    point_inv h p /\ 0 < as_nat h r /\ as_nat h r < S.order)
    //not (S.is_point_at_inf (from_mont_point (as_point_nat h p))))
  (ensures  fun h0 b h1 -> modifies0 h0 h1 /\
    (let (_X, _Y, _Z) = from_mont_point (as_point_nat h0 p) in
     b <==> (S.fmul _X (S.finv _Z) % S.order = as_nat h0 r)))
let ecdsa_verify_finv p r_q =
  push_frame ();
  let x = create_felem () in
  to_aff_point_x x p;
  qmod_short x x;
  let res = bn_is_eq_vartime4 x r_q in
  pop_frame ();
  res
val ecdsa_verify_finv: p:point -> r:felem -> Stack bool
  (requires fun h ->
    live h p /\ live h r /\ disjoint p r /\
    point_inv h p /\ 0 < as_nat h r /\ as_nat h r < S.order)
    //not (S.is_point_at_inf (from_mont_point (as_point_nat h p))))
  (ensures  fun h0 b h1 -> modifies0 h0 h1 /\
    (let (_X, _Y, _Z) = from_mont_point (as_point_nat h0 p) in
     b <==> (S.fmul _X (S.finv _Z) % S.order = as_nat h0 r)))
let ecdsa_verify_finv p r_q =
push_frame ();
let x = create_felem () in
to_aff_point_x x p;
qmod_short x x;
let res = bn_is_eq_vartime4 x r_q in
pop_frame ();
res
Hacl.Impl.P256.Verify.fst.checked
Spec.P256.Lemmas.fsti.checked Spec.P256.fst.checked prims.fst.checked Lib.IntTypes.fsti.checked Lib.ByteSequence.fsti.checked Lib.Buffer.fsti.checked Hacl.Spec.P256.Montgomery.fsti.checked Hacl.Impl.P256.Scalar.fsti.checked Hacl.Impl.P256.Qinv.fsti.checked Hacl.Impl.P256.PointMul.fsti.checked Hacl.Impl.P256.Point.fsti.checked Hacl.Impl.P256.Bignum.fsti.checked Hacl.Bignum.Base.fst.checked FStar.UInt32.fsti.checked FStar.Pervasives.Native.fst.checked FStar.Pervasives.fsti.checked FStar.Mul.fst.checked FStar.HyperStack.ST.fsti.checked FStar.HyperStack.All.fst.checked FStar.HyperStack.fst.checked
Hacl.Impl.P256.Verify.fst
Hacl.Impl.P256.Point.point Hacl.Impl.P256.Bignum.felem Prims.bool Prims.unit FStar.HyperStack.ST.pop_frame Hacl.Impl.P256.Bignum.bn_is_eq_vartime4 Hacl.Impl.P256.Scalar.qmod_short Hacl.Impl.P256.Point.to_aff_point_x Hacl.Impl.P256.Bignum.create_felem FStar.HyperStack.ST.push_frame
module Hacl.Impl.P256.Verify

open FStar.Mul
open FStar.HyperStack.All
open FStar.HyperStack
module ST = FStar.HyperStack.ST

open Lib.IntTypes
open Lib.Buffer

open Hacl.Impl.P256.Bignum
open Hacl.Impl.P256.Point
open Hacl.Impl.P256.Scalar
open Hacl.Impl.P256.PointMul

module BSeq = Lib.ByteSequence

module S = Spec.P256
module SL = Spec.P256.Lemmas
module SM = Hacl.Spec.P256.Montgomery
module QI = Hacl.Impl.P256.Qinv

#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"

inline_for_extraction noextract
let lbytes len = lbuffer uint8 len

val qmul_mont: sinv:felem -> b:felem -> res:felem -> Stack unit
  (requires fun h ->
    live h sinv /\ live h b /\ live h res /\
    disjoint sinv res /\ disjoint b res /\
    as_nat h sinv < S.order /\ as_nat h b < S.order)
  (ensures fun h0 _ h1 -> modifies (loc res) h0 h1 /\
    as_nat h1 res < S.order /\
    as_nat h1 res = (as_nat h0 sinv * SM.from_qmont (as_nat h0 b) * SM.qmont_R_inv) % S.order)

[@CInline]
let qmul_mont sinv b res =
  let h0 = ST.get () in
  push_frame ();
  let tmp = create_felem () in
  from_qmont tmp b;
  let h1 = ST.get () in
  assert (as_nat h1 tmp == SM.from_qmont (as_nat h0 b));
  qmul res sinv tmp;
  let h2 = ST.get () in
  assert (as_nat h2 res = (as_nat h1 sinv * as_nat h1 tmp * SM.qmont_R_inv) % S.order);
  pop_frame ()


inline_for_extraction noextract
val ecdsa_verification_get_u12: u1:felem -> u2:felem -> r:felem -> s:felem -> z:felem -> Stack unit
  (requires fun h ->
    live h r /\ live h s /\ live h z /\ live h u1 /\ live h u2 /\
    disjoint u1 u2 /\ disjoint u1 z /\ disjoint u1 r /\ disjoint u1 s /\
    disjoint u2 z /\ disjoint u2 r /\ disjoint u2 s /\
    as_nat h s < S.order /\ as_nat h z < S.order /\ as_nat h r < S.order)
  (ensures fun h0 _ h1 -> modifies (loc u1 |+| loc u2) h0 h1 /\
    (let sinv = S.qinv (as_nat h0 s) in
    as_nat h1 u1 == sinv * as_nat h0 z % S.order /\
    as_nat h1 u2 == sinv * as_nat h0 r % S.order))

let ecdsa_verification_get_u12 u1 u2 r s z =
  push_frame ();
  let h0 = ST.get () in
  let sinv = create_felem () in
  QI.qinv sinv s;
  let h1 = ST.get () in
  assert (qmont_as_nat h1 sinv == S.qinv (qmont_as_nat h0 s));
  //assert (as_nat h2 sinv * SM.qmont_R_inv % S.order ==
    //S.qinv (as_nat h1 sinv * SM.qmont_R_inv % S.order));

  SM.qmont_inv_mul_lemma (as_nat h0 s) (as_nat h1 sinv) (as_nat h0 z);
  SM.qmont_inv_mul_lemma (as_nat h0 s) (as_nat h1 sinv) (as_nat h0 r);
  qmul_mont sinv z u1;
  qmul_mont sinv r u2;
  pop_frame ()


inline_for_extraction noextract
val ecdsa_verify_finv: p:point -> r:felem -> Stack bool
  (requires fun h ->
    live h p /\ live h r /\ disjoint p r /\
    point_inv h p /\ 0 < as_nat h r /\ as_nat h r < S.order)
    //not (S.is_point_at_inf (from_mont_point (as_point_nat h p))))
  (ensures  fun h0 b h1 -> modifies0 h0 h1 /\
    (let (_X, _Y, _Z) = from_mont_point (as_point_nat h0 p) in
     b <==> (S.fmul _X (S.finv _Z) % S.order = as_nat h0 r)))
val ecdsa_verify_finv: p:point -> r:felem -> Stack bool
  (requires fun h ->
    live h p /\ live h r /\ disjoint p r /\
    point_inv h p /\ 0 < as_nat h r /\ as_nat h r < S.order)
    //not (S.is_point_at_inf (from_mont_point (as_point_nat h p))))
  (ensures  fun h0 b h1 -> modifies0 h0 h1 /\
    (let (_X, _Y, _Z) = from_mont_point (as_point_nat h0 p) in
     b <==> (S.fmul _X (S.finv _Z) % S.order = as_nat h0 r)))
p: Hacl.Impl.P256.Point.point -> r: Hacl.Impl.P256.Bignum.felem
  -> FStar.HyperStack.ST.Stack Prims.bool