val sub_borrow_u32: sub_borrow_st U32
let sub_borrow_u32 cin x y r =
  let res = to_u64 x -. to_u64 y -. to_u64 cin in
  assert (v res == ((v x - v y) % pow2 64 - v cin) % pow2 64);
  Math.Lemmas.lemma_mod_add_distr (- v cin) (v x - v y) (pow2 64);
  assert (v res == (v x - v y - v cin) % pow2 64);

  assert (v res % pow2 32 = (v x - v y - v cin) % pow2 64 % pow2 32);
  Math.Lemmas.pow2_modulo_modulo_lemma_1 (v x - v y - v cin) 32 64;
  assert (v res % pow2 32 = (v x - v y - v cin) % pow2 32);

  let c = to_u32 (res >>. 32ul) &. u32 1 in
  assert_norm (pow2 1 = 2);
  mod_mask_lemma (to_u32 (res >>. 32ul)) 1ul;
  assert (v ((mk_int #U32 #SEC 1 <<. 1ul) -! mk_int 1) == 1);
  assert (v c = v res / pow2 32 % pow2 1);

  r.(0ul) <- to_u32 res;
  assert (v c = (if 0 <= v x - v y - v cin then 0 else 1));
  c
val sub_borrow_u32: sub_borrow_st U32
let sub_borrow_u32 cin x y r =
let res = to_u64 x -. to_u64 y -. to_u64 cin in
assert (v res == ((v x - v y) % pow2 64 - v cin) % pow2 64);
Math.Lemmas.lemma_mod_add_distr (- v cin) (v x - v y) (pow2 64);
assert (v res == (v x - v y - v cin) % pow2 64);
assert (v res % pow2 32 = (v x - v y - v cin) % pow2 64 % pow2 32);
Math.Lemmas.pow2_modulo_modulo_lemma_1 (v x - v y - v cin) 32 64;
assert (v res % pow2 32 = (v x - v y - v cin) % pow2 32);
let c = to_u32 (res >>. 32ul) &. u32 1 in
assert_norm (pow2 1 = 2);
mod_mask_lemma (to_u32 (res >>. 32ul)) 1ul;
assert (v ((mk_int #U32 #SEC 1 <<. 1ul) -! mk_int 1) == 1);
assert (v c = v res / pow2 32 % pow2 1);
r.(0ul) <- to_u32 res;
assert (v c = (if 0 <= v x - v y - v cin then 0 else 1));
c
Hacl.IntTypes.Intrinsics.fst.checked
prims.fst.checked Lib.IntTypes.fsti.checked Lib.Buffer.fsti.checked FStar.UInt32.fsti.checked FStar.Seq.fst.checked FStar.Pervasives.fsti.checked FStar.Mul.fst.checked FStar.Math.Lemmas.fst.checked FStar.HyperStack.ST.fsti.checked FStar.HyperStack.All.fst.checked FStar.HyperStack.fst.checked
Hacl.IntTypes.Intrinsics.fst
Lib.IntTypes.uint_t Lib.IntTypes.U32 Lib.IntTypes.SEC Lib.Buffer.lbuffer Lib.IntTypes.size Prims.unit Prims._assert Prims.b2t Prims.op_Equality Prims.int Lib.IntTypes.v Prims.op_LessThanOrEqual Prims.op_Subtraction Prims.bool Lib.Buffer.op_Array_Assignment FStar.UInt32.__uint_to_t Lib.IntTypes.to_u32 Lib.IntTypes.U64 Prims.op_Modulus Prims.op_Division Prims.pow2 Prims.eq2 Lib.IntTypes.op_Subtraction_Bang Lib.IntTypes.op_Less_Less_Dot Lib.IntTypes.mk_int Lib.IntTypes.mod_mask_lemma Lib.IntTypes.op_Greater_Greater_Dot FStar.Pervasives.assert_norm Lib.IntTypes.int_t Lib.IntTypes.op_Amp_Dot Lib.IntTypes.u32 FStar.Math.Lemmas.pow2_modulo_modulo_lemma_1 FStar.Math.Lemmas.lemma_mod_add_distr Prims.op_Minus Lib.IntTypes.op_Subtraction_Dot Lib.IntTypes.to_u64
module Hacl.IntTypes.Intrinsics

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

open Lib.IntTypes
open Lib.Buffer

open FStar.Mul

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

inline_for_extraction noextract
let add_carry_st (t:inttype{t = U32 \/ t = U64}) =
    cin:uint_t t SEC
  -> x:uint_t t SEC
  -> y:uint_t t SEC
  -> r:lbuffer (uint_t t SEC) (size 1) ->
  Stack (uint_t t SEC)
  (requires fun h -> live h r /\ v cin <= 1)
  (ensures  fun h0 c h1 ->
    modifies1 r h0 h1 /\ v c <= 1 /\
    (let r = Seq.index (as_seq h1 r) 0 in
    v r + v c * pow2 (bits t) == v x + v y + v cin))

val add_carry_u32: add_carry_st U32
let add_carry_u32 cin x y r =
  let res = to_u64 x +. to_u64 cin +. to_u64 y in
  let c = to_u32 (res >>. 32ul) in
  r.(0ul) <- to_u32 res;
  c

inline_for_extraction noextract
let sub_borrow_st (t:inttype{t = U32 \/ t = U64}) =
    cin:uint_t t SEC
  -> x:uint_t t SEC
  -> y:uint_t t SEC
  -> r:lbuffer (uint_t t SEC) (size 1) ->
  Stack (uint_t t SEC)
  (requires fun h -> live h r /\ v cin <= 1)
  (ensures  fun h0 c h1 ->
    modifies1 r h0 h1 /\ v c <= 1 /\
    (let r = Seq.index (as_seq h1 r) 0 in
    v r - v c * pow2 (bits t) == v x - v y - v cin))
val sub_borrow_u32: sub_borrow_st U32
Hacl.IntTypes.Intrinsics.sub_borrow_st Lib.IntTypes.U32