mp2.mlw 96.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
(*
module Limb

  use import int.Int
  clone export mach.int.UInt32

  type limb = uint32

  val add_carry (x y:limb) : limb
    ensures { 0 <= to_int result <= 1 }

  val add_res (x y:limb) : limb
    ensures { result + add_carry x y = add_with_carry x y zero_unsigned }

end *)

module N

19 20
 (**)
  use import array.Array
21
  use import mach.int.Int32
22
  use import mach.c.C
23
  use import mach.int.UInt32 as Limb
24 25
  use import int.Int
  use import int.Power
26 27
  use import map.Map
  use import ref.Ref
28 29 30 31 32 33 34 35 36
  type limb = uint32

  lemma limb_max_bound: 1 <= max_uint32

  constant radix : int = max_uint32 + 1

  function l2i (x:limb) : int = UInt32.to_int x

  function p2i (i:int32) : int = Int32.to_int i
37

38
  type t = ptr limb
39

40 41 42 43 44 45 46 47 48 49 50 51
  (** {2 Integer value of a natural number} *)

  (** [value_sub x n m] denotes the integer represented by
     the digits x[n..m-1] with lsb at index n *)
  let rec ghost function value_sub (x:map int limb) (n:int) (m:int) : int
     variant {m - n}
   =
     if n < m then
       l2i x[n] + radix * value_sub x (n+1) m
       else 0

  function value (x:t) : int =
52
     value_sub (pelts x) 0 (plength x)
53 54

  function value_sub_shift (x:t) (sz:int) : int =
55
     value_sub (pelts x) x.offset (x.offset + sz)
56

57 58 59 60 61 62
  let from_limb (l:limb) : t
    ensures { is_null result \/ plength result = 1 }
    ensures { is_null result \/ value_sub_shift result 1 = l2i l }
  =
    let p = malloc (UInt32.of_int 1) in
    if not (is_null p)
63
    then C.set p l;
64 65
    p

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  use map.MapEq
  use map.Const

  predicate map_eq_sub_shift (x y:map int 'a) (xi yi sz:int) =
    forall i. 0 <= i < sz -> x[xi+i] = y[yi+i]

  let lemma map_eq_shift (x y:map int 'a) (xi yi sz k:int)
    requires { map_eq_sub_shift x y xi yi sz }
    requires { 0 <= k < sz }
    ensures { x[xi+k] = y[yi+k] }
  = ()

  let rec lemma map_eq_shift_zero (x y: map int 'a) (n m: int)
    requires { map_eq_sub_shift x y n n (m-n) }
    variant { m - n }
    ensures { MapEq.map_eq_sub x y n m }
  =
    if n < m then
    begin
      assert { forall i. 0 <= i < m-n -> x[n+i] = y[n+i] };
      assert { forall i. n <= i < m ->
87 88
                 let j = i - n in 0 <= j < m-n ->
                     x[n+j] = y[n+j] -> x[i] = y[i]};
89 90
      map_eq_shift_zero x y (n+1) m;
    end
91
    else assert { 1+2=3 }
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

  let rec lemma value_sub_frame (x y:map int limb) (n m:int)
    requires { MapEq.map_eq_sub x y n m }
    variant  { m - n }
    ensures  { value_sub x n m = value_sub y n m }
  =
    if n < m then value_sub_frame x y (n+1) m else ()

  let rec lemma value_sub_frame_shift (x y:map int limb) (xi yi sz:int)
    requires { map_eq_sub_shift x y xi yi sz }
    variant { sz }
    ensures { value_sub x xi (xi+sz) = value_sub y yi (yi+sz) }
 =
    if sz>0
    then begin
      map_eq_shift x y xi yi sz 0;
      assert { forall i. 0 <= i < sz-1 ->
109 110 111
                 0 <= 1+i < sz ->
                 let j = 1+i in x[xi+j] = y[yi+j] ->
                   x[xi+1+i] = y[yi+1+i]  };
112 113 114 115 116 117 118 119 120
      value_sub_frame_shift x y (xi+1) (yi+1) (sz-1)
      end
    else assert { 1+2 = 3 }

  let rec lemma value_sub_tail (x:map int limb) (n m:int)
    requires { n <= m }
    variant  { m - n }
    ensures  {
      value_sub x n (m+1) =
121
        value_sub x n m + l2i (Map.get x m) * power radix (m-n) }
122 123 124 125 126 127 128
  = "vc:sp" if n < m then value_sub_tail x (n+1) m else ()(*assert { 1+2=3 }*)

  let rec lemma value_sub_concat (x:map int limb) (n m l:int)
    requires { n <= m <= l}
    variant  { m - n }
    ensures  {
      value_sub x n l =
129
        value_sub x n m + value_sub x m l * power radix (m-n) }
130 131 132 133 134 135 136 137
  =
  if n < m then
     begin
     assert {n<m};
     value_sub_concat x (n+1) m l
     end
  else ()

138 139 140 141 142
  let lemma value_sub_head (x:map int limb) (n m:int)
    requires { n < m }
    ensures { value_sub x n m = l2i x[n] + radix * value_sub x (n+1) m }
  = value_sub_concat x n (n+1) m

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  let lemma value_sub_update (x:map int limb) (i n m:int) (v:limb)
    requires { n <= i < m }
    ensures {
      value_sub (Map.set x i v) n m =
      value_sub x n m + power radix (i - n) * (l2i v - l2i (Map.get x i))
    }
  = assert { MapEq.map_eq_sub x (Map.set x i v) n i };
    assert { MapEq.map_eq_sub x (Map.set x i v) (i+1) m };
    value_sub_concat x n i m;
    value_sub_concat (Map.set x i v) n i m

  let rec lemma value_zero (x:map int limb) (n m:int)
    requires { MapEq.map_eq_sub x (Const.const UInt32.zero_unsigned) n m }
    variant  { m - n }
    ensures  { value_sub x n m = 0 }
  = if n < m then value_zero x (n+1) m else ()

160 161 162 163 164 165 166 167 168 169
  let lemma value_sub_update_no_change (x: map int limb) (i n m: int) (v:limb)
     requires { n <= m }
     requires { i < n \/ m <= i }
     ensures { value_sub x n m = value_sub (Map.set x i v) n m }
  = value_sub_frame x (Map.set x i v) n m

  let lemma value_sub_shift_no_change (x:map int limb) (ofs i sz:int) (v:limb)
     requires { i < 0 \/ sz <= i }
     requires { 0 <= sz }
     ensures { value_sub x ofs (ofs + sz) =
170
               value_sub (Map.set x (ofs+i) v) ofs (ofs+sz) }
171 172
  = value_sub_frame_shift x (Map.set x (ofs+i) v) ofs ofs sz

173 174 175 176 177 178 179
  (** {2 Comparisons} *)

    let rec lemma value_sub_lower_bound (x:map int limb) (x1 x2:int)
    variant  { x2 - x1 }
    ensures  { 0 <= value_sub x x1 x2 }
  = if x2 <= x1 then () else
      begin
180 181
        assert { value_sub x x1 x2 = l2i (Map.get x x1) + radix * value_sub x (x1+1) x2};
        value_sub_lower_bound x (x1+1) x2
182 183 184 185 186 187 188 189 190 191 192 193 194 195
      end

  let rec lemma value_sub_upper_bound (x:map int limb) (x1 x2:int)
    requires { x1 <= x2 }
    variant  { x2 - x1 }
    ensures  { value_sub x x1 x2 < power radix (x2 - x1) }
  = if x1 = x2 then () else
      begin
      assert { value_sub x x1 x2 <= value_sub x x1 (x2-1) + power radix (x2-x1-1) * (radix - 1) };
      value_sub_upper_bound x x1 (x2-1)
      end

  let lemma value_sub_lower_bound_tight (x:map int limb) (x1 x2:int)
    requires { x1 < x2 }
196
    ensures  { power radix (x2-x1-1) *  l2i (Map.get x (x2-1)) <= value_sub x x1 x2 }
197
  = assert   { value_sub x x1 x2 = value_sub x x1 (x2-1)
198
               + power radix (x2-x1-1) * l2i (Map.get x (x2-1)) }
199 200 201 202 203 204

  let lemma value_sub_upper_bound_tight (x:map int limb) (x1 x2:int)
    requires { x1 < x2 }
    ensures  { value_sub x x1 x2 < power radix (x2-x1-1) *  (l2i (Map.get x (x2-1)) + 1) }
  = value_sub_upper_bound x x1 (x2-1)

205 206 207 208 209 210
  let lemma prod_compat_strict_r (a b c:int)
    requires { 0 <= a < b }
    requires { 0 < c }
    ensures { c * a < c * b }
  = ()

211 212 213 214 215 216 217 218 219
  exception Break32 int32

  use import ref.Refint

  function compare_int (x y:int) : int =
    if x < y then -1 else if x=y then 0 else 1

  (** [compare_same_size] compares [x[0..sz-1]] and [y[0..sz-1]] as unsigned integers. It corresponds to [GMPN_CMP]. *)
  let compare_same_size (x y:t) (sz:int32) : int32
220 221
    requires { valid_ptr_itv x (p2i sz) }
    requires { valid_ptr_itv y (p2i sz) }
222
    ensures { p2i result = compare_int (value_sub_shift x (p2i sz))
223 224
              (value_sub_shift y (p2i sz))
              }
225 226 227 228 229 230 231 232 233 234 235 236
  =
   let i = ref sz in
   let zero = Int32.of_int 0 in
   let uzero = UInt32.of_int 0 in
   let lx = ref uzero in
   let ly = ref uzero in
   let res = ref zero in
   try
     while Int32.(>=) !i (Int32.of_int 1) do
       variant { p2i !i }
       invariant { 0 <= p2i !i <= p2i sz }
       invariant { forall j. p2i !i <= j < p2i sz ->
237
                   (pelts x)[x.offset+j] = (pelts y)[y.offset+j] }
238
       assert { forall j. 0 <= j < p2i sz - (p2i !i) ->
239 240 241 242 243
                let k = p2i !i+j in
                p2i !i <= k < p2i sz ->
                (pelts x)[x.offset+k] = (pelts y)[y.offset+k] /\
                (pelts x)[p2i !i+x.offset+j] = (pelts y)[p2i !i+y.offset+j] };
       value_sub_frame_shift (pelts x) (pelts y) (p2i !i+x.offset) (p2i !i+y.offset) ((p2i sz) - (p2i !i));
244 245 246 247 248 249 250 251
       let ghost k = p2i !i in
       i := Int32.(-) !i (Int32.of_int 1);

       assert { 0 <= p2i !i < p2i sz };
       lx := get_ofs x !i;
       ly := get_ofs y !i;
       if (UInt32.ne !lx !ly)
       then begin
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
            value_sub_concat (pelts x) x.offset (x.offset+k) (x.offset+ p2i sz);
            value_sub_concat (pelts y) y.offset (y.offset+k) (y.offset+ p2i sz);
            assert { compare_int (value_sub_shift x (p2i sz))
                       (value_sub_shift y (p2i sz))
                   = compare_int (value_sub_shift x k) (value_sub_shift y k) };
            value_sub_tail (pelts x) x.offset (x.offset+k-1);
            value_sub_tail (pelts y) y.offset (y.offset+k-1);
            if UInt32.(>) !lx !ly
            then begin
             value_sub_upper_bound (pelts y) y.offset (y.offset+k-1);
             value_sub_lower_bound (pelts x) x.offset (x.offset+k-1);
             assert { value_sub_shift x k - value_sub_shift y k =
                      (l2i !lx - l2i !ly) * (power radix (k-1))
                    - ((value_sub_shift y (k-1)) - (value_sub_shift x (k-1)))
                       };
             assert { (l2i !lx - l2i !ly) * (power radix (k-1))
                      >= power radix (k-1)
                      > ((value_sub_shift y (k-1)) - (value_sub_shift x (k-1)))
                       };
             res := Int32.of_int 1
            end
            else begin
             assert { l2i !ly > l2i !lx };
             value_sub_upper_bound (pelts x) x.offset (x.offset+k-1);
             value_sub_lower_bound (pelts y) y.offset (y.offset+k-1);
             assert { value_sub_shift y k - value_sub_shift x k =
                    (l2i !ly - l2i !lx) * (power radix (k-1))
                    - ((value_sub_shift x (k-1)) - (value_sub_shift y (k-1)))
                     };
             assert { (l2i !ly - l2i !lx) * (power radix (k-1))
                      >= power radix (k-1)
                      > ((value_sub_shift x (k-1)) - (value_sub_shift y (k-1)))
                     };
            res := Int32.of_int (-1)
            end;
         raise Break32 !res
         end
289 290
       else ()
     done;
291
     value_sub_frame_shift (pelts x) (pelts y) x.offset y.offset (p2i sz);
292 293 294 295 296 297
     zero
   with Break32 r -> r
   end

   (* [is_zero] checks if [x[0..sz-1]] is zero. It corresponds to [mpn_zero_p]. *)
   let is_zero (x:t) (sz:int32) : int32
298
     requires { valid_ptr_itv x (p2i sz) }
299 300 301 302 303 304 305 306
     ensures { 0 <= Int32.to_int result <= 1 }
     ensures { Int32.to_int result = 1 <-> value_sub_shift x (p2i sz) = 0 }
   =
     let i = ref sz in
     let uzero = UInt32.of_int 0 in
     let lx = ref uzero in
     try
       while Int32.(>=) !i (Int32.of_int 1) do
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
         variant { p2i !i }
         invariant { 0 <= p2i !i <= p2i sz }
         invariant { value_sub (pelts x) (x.offset + p2i !i) (x.offset + p2i sz)=0 }
         let ghost k = p2i !i in
         i := Int32.(-) !i (Int32.of_int 1);
         assert { 0 <= p2i !i < p2i sz };
         lx := get_ofs x !i;
         if (UInt32.ne !lx uzero)
         then begin
           value_sub_concat (pelts x) x.offset (x.offset+k) (x.offset + p2i sz);
           value_sub_lower_bound_tight (pelts x) x.offset (x.offset+k);
           value_sub_lower_bound (pelts x) (x.offset+k) (x.offset + p2i sz);
           raise Break32 (Int32.of_int 0)
         end
         else begin
           assert { 1+2=3 };
         end
324 325 326 327 328
       done;
       Int32.of_int 1
     with Break32 r -> r
     end

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  (** [zero r sz] sets [(r,sz)] to zero. Corresponds to [mpn_zero]. *)
  let zero (r:t) (sz:int32) : unit
    requires { valid_ptr_itv r (p2i sz) }
    ensures { value_sub_shift r (p2i sz) = 0 }
  =
    let i = ref (Int32.of_int 0) in
    let lzero = Limb.of_int 0 in
    while Int32.(<) !i sz do
      invariant { 0 <= p2i !i <= p2i sz }
      variant { p2i sz - p2i !i }
      invariant { value_sub_shift r (p2i !i) = 0 }
      set_ofs r !i lzero;
      value_sub_tail (pelts r) r.offset (r.offset + p2i !i);
      i := Int32.(+) !i (Int32.of_int 1);
    done

345 346 347 348
  (** {2 Addition} *)

  exception Break

MARCHE Claude's avatar
MARCHE Claude committed
349
  (** [add_limb r x y sz] adds to [x] the value of the limb [y],
350
      writes the result in [r] and returns the carry. [r] and [x]
351
      have size [sz]. This corresponds to the function [mpn_add_1] *)
MARCHE Claude's avatar
MARCHE Claude committed
352
  (* r and x must be separated. This is enforced by Why3 regions in typing *)
353 354
  let add_limb (r x:t) (y:limb) (sz:int32) : limb
    requires { valid_ptr_itv x (p2i sz) }
355 356 357
    requires { valid_ptr_itv r (p2i sz) }
    requires { p2i sz > 0 } (* ? GMP does the same for 0 and 1*)
    ensures { value_sub_shift r (p2i sz) + (power radix (p2i sz)) * l2i result =
358
              value_sub_shift x (p2i sz) + l2i y }
359
    ensures { 0 <= l2i result <= 1 }
360
    writes { r.data.contents.elts }
361 362 363 364 365 366 367 368 369
  =
    let limb_zero = UInt32.of_int 0 in
    let c = ref y in
    let lx = ref limb_zero in
    let i = ref (Int32.of_int 0) in
    while Int32.(<) !i sz && UInt32.ne !c limb_zero do
      invariant { 0 <= p2i !i <= p2i sz }
      invariant { p2i !i > 0 -> 0 <= l2i !c <= 1 }
      invariant { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
370
                  value_sub_shift x (p2i !i) + l2i y}
371 372 373 374 375
      variant { p2i sz - p2i !i }
      label StartLoop in
      lx := get_ofs x !i;
      let (res, carry) = add_with_carry !lx !c limb_zero in
      set_ofs r !i res;
376
      assert { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
377
                  value_sub_shift x (p2i !i) + l2i y };
378 379 380
      c := carry;
      let ghost k = p2i !i in
      i := Int32.(+) !i (Int32.of_int 1);
381 382
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts x) x.offset (x.offset + k);
383 384
      let ghost m = power radix k in
      assert { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c
385 386 387 388 389 390 391 392 393 394 395 396 397 398
             = value_sub_shift x (p2i !i) + l2i y
             by
             value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c
             = value_sub_shift r k + (power radix k) * l2i res
                               + (power radix (p2i !i)) * l2i !c
             = value_sub_shift r k + (power radix k) * l2i res
                               + (power radix k) * radix * l2i !c
             = value_sub_shift r k + (power radix k) * (l2i res + radix * l2i !c)
             = value_sub_shift r k +
               (power radix k) * (l2i !lx + l2i (!c at StartLoop))
             = value_sub_shift r k + (power radix k) * l2i (!c at StartLoop)
                               + (power radix k) * l2i !lx
             = value_sub_shift x k + l2i y + (power radix k) * l2i !lx
             = value_sub_shift x (p2i !i) + l2i y }
399 400 401 402 403 404 405
    done;
    if Int32.eq !i sz then !c
    else begin
    while Int32.(<) !i sz do
      invariant { l2i !c  = 0 }
      invariant { 0 <= p2i !i <= p2i sz }
      invariant { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
406
                  value_sub_shift x (p2i !i) + l2i y}
407 408 409
      variant { p2i sz - p2i !i }
      lx := get_ofs x !i;
      set_ofs r !i !lx;
410
      assert { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
411
                  value_sub_shift x (p2i !i) + l2i y };
412 413
      let ghost k = p2i !i in
      i := Int32.(+) !i (Int32.of_int 1);
414 415
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts x) x.offset (x.offset + k);
416 417 418 419 420
    done;
    !c
    end


421 422
  (** [add_limbs r x y sz] adds [x[0..sz-1]] and [y[0..sz-1]] and writes the result in [r].
      Returns the carry, either [0] or [1]. Corresponds to the function [mpn_add_n]. *)
423

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
  let add_limbs (r x y:t) (sz:int32) : limb
    requires { valid_ptr_itv x (p2i sz) }
    requires { valid_ptr_itv y (p2i sz) }
    requires { valid_ptr_itv r (p2i sz) }
    ensures { 0 <= l2i result <= 1 }
    ensures { value_sub_shift r (p2i sz) + (power radix (p2i sz)) * l2i result =
            value_sub_shift x (p2i sz) + value_sub_shift y (p2i sz) }
    writes { r.data.contents.elts }
    =
    let limb_zero = UInt32.of_int 0 in
    let lx = ref limb_zero in
    let ly = ref limb_zero in
    let c = ref limb_zero in
    let i = ref (Int32.of_int 0) in
    while Int32.(<) !i sz do
      variant { p2i sz - p2i !i }
      invariant { 0 <= p2i !i <= p2i sz }
      invariant { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
                value_sub_shift x (p2i !i) + value_sub_shift y (p2i !i) }
      invariant { 0 <= l2i !c <= 1 }
      label StartLoop in
      lx := get_ofs x !i;
      ly := get_ofs y !i;
      let res, carry = add_with_carry !lx !ly !c in
      set_ofs r !i res;
      assert { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
                value_sub_shift x (p2i !i) + value_sub_shift y (p2i !i) };
      c := carry;
      let ghost k = p2i !i in
      i := Int32.(+) !i (Int32.of_int 1);
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts x) x.offset (x.offset + k);
      value_sub_tail (pelts y) y.offset (y.offset + k);
      assert { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
                value_sub_shift x (p2i !i) + value_sub_shift y (p2i !i)
              by
              value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c
              = value_sub_shift r k + (power radix k) * l2i res
                   + (power radix (p2i !i)) * l2i !c
              = value_sub_shift r k + (power radix k) * l2i res
                   + (power radix k) * radix * l2i !c
              = value_sub_shift r k + (power radix k) * (l2i res + radix * l2i !c)
              = value_sub_shift r k +
                  (power radix k) * (l2i !lx + l2i !ly + l2i (!c at StartLoop))
              = value_sub_shift r k + (power radix k) * l2i (!c at StartLoop)
                 + (power radix k) * (l2i !lx + l2i !ly)
              = value_sub_shift x k + value_sub_shift y k
                 + (power radix k) * (l2i !lx + l2i !ly)
              = value_sub_shift x k + (power radix k) * l2i !lx
                 + value_sub_shift y k + (power radix k) * l2i !ly
              = value_sub_shift x (p2i !i)
                 + value_sub_shift y k + (power radix k) * l2i !ly
              = value_sub_shift x (p2i !i)
                 + (value_sub_shift y k + (power radix k) * l2i !ly)
              = value_sub_shift x (p2i !i) + value_sub_shift y (p2i !i) }
    done;
    !c

Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
482 483 484
  (** [add r x y sx sy] adds [(x, sx)] to [(y,sy)] and writes the
      result in [(r, sx)].  [sx] must be greater than or equal to
      [sy]. Returns carry, either 0 or 1. Corresponds to [mpn_add]. *)
485 486 487 488 489 490 491 492 493 494
  let add (r x y:t) (sx sy:int32) : limb
    requires { 0 <= p2i sy <= p2i sx }
    requires { valid_ptr_itv x (p2i sx) }
    requires { valid_ptr_itv y (p2i sy) }
    requires { valid_ptr_itv r (p2i sx) }
    ensures { value_sub_shift r (p2i sx) + (power radix (p2i sx)) * l2i result =
              value_sub_shift x (p2i sx) + value_sub_shift y (p2i sy) }
    ensures { 0 <= l2i result <= 1 }
    writes { r.data.contents.elts }
 =
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
495
    let limb_zero = Limb.of_int 0 in
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
    let lx = ref limb_zero in
    let ly = ref limb_zero in
    let c = ref limb_zero in
    let i = ref (Int32.of_int 0) in
    while Int32.(<) !i sy do
      variant { p2i sy - p2i !i }
      invariant { 0 <= p2i !i <= p2i sy }
      invariant { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
                value_sub_shift x (p2i !i) + value_sub_shift y (p2i !i) }
      invariant { 0 <= l2i !c <= 1 }
      label StartLoop in
      lx := get_ofs x !i;
      ly := get_ofs y !i;
      let res, carry = add_with_carry !lx !ly !c in
      set_ofs r !i res;
      assert { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
                value_sub_shift x (p2i !i) + value_sub_shift y (p2i !i) };
      c := carry;
      let ghost k = p2i !i in
      i := Int32.(+) !i (Int32.of_int 1);
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts x) x.offset (x.offset + k);
      value_sub_tail (pelts y) y.offset (y.offset + k);
      assert { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
                value_sub_shift x (p2i !i) + value_sub_shift y (p2i !i)
              by
              value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c
              = value_sub_shift r k + (power radix k) * l2i res
                   + (power radix (p2i !i)) * l2i !c
              = value_sub_shift r k + (power radix k) * l2i res
                   + (power radix k) * radix * l2i !c
              = value_sub_shift r k + (power radix k) * (l2i res + radix * l2i !c)
              = value_sub_shift r k +
                  (power radix k) * (l2i !lx + l2i !ly + l2i (!c at StartLoop))
              = value_sub_shift r k + (power radix k) * l2i (!c at StartLoop)
                 + (power radix k) * (l2i !lx + l2i !ly)
              = value_sub_shift x k + value_sub_shift y k
                 + (power radix k) * (l2i !lx + l2i !ly)
              = value_sub_shift x k + (power radix k) * l2i !lx
                 + value_sub_shift y k + (power radix k) * l2i !ly
              = value_sub_shift x (p2i !i)
                 + value_sub_shift y k + (power radix k) * l2i !ly
              = value_sub_shift x (p2i !i)
                 + (value_sub_shift y k + (power radix k) * l2i !ly)
              = value_sub_shift x (p2i !i) + value_sub_shift y (p2i !i) };
    done;
    while Int32.(<) !i sx do
      variant { p2i sx - p2i !i }
      invariant { p2i sy <= p2i !i <= p2i sx }
      invariant { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
                value_sub_shift x (p2i !i) + value_sub_shift y (p2i sy) }
      invariant { 0 <= l2i !c <= 1 }
      label StartLoop2 in
      lx := get_ofs x !i;
      let res, carry = add_with_carry !lx limb_zero !c in
      set_ofs r !i res;
      assert { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
                value_sub_shift x (p2i !i) + value_sub_shift y (p2i sy) };
      c := carry;
      let ghost k = p2i !i in
      i := Int32.(+) !i (Int32.of_int 1);
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts x) x.offset (x.offset + k);
      assert { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
                value_sub_shift x (p2i !i) + value_sub_shift y (p2i sy)
              by
              value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c
              = value_sub_shift r k + (power radix k) * l2i res
                   + (power radix (p2i !i)) * l2i !c
              = value_sub_shift r k + (power radix k) * l2i res
                   + (power radix k) * radix * l2i !c
              = value_sub_shift r k + (power radix k) * (l2i res + radix * l2i !c)
              = value_sub_shift r k +
                  (power radix k) * (l2i !lx + 0 + l2i (!c at StartLoop2))
              = value_sub_shift r k + (power radix k) * l2i (!c at StartLoop2)
                 + (power radix k) * l2i !lx
              = value_sub_shift x k + value_sub_shift y (p2i sy)
                 + (power radix k) * l2i !lx
              = value_sub_shift x (p2i !i)
                 + value_sub_shift y (p2i sy) }
    done;
    !c

Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
  (** [sub_limb r x y sz] substracts [y] from [(x, sz)] and writes
      the result to [(r, sz)]. Returns borrow, either 0 or
      1. Corresponds to [mpn_sub_1]. *)
  let sub_limb (r x:t) (y:limb) (sz:int32) : limb
    requires { valid_ptr_itv x (p2i sz) }
    requires { valid_ptr_itv r (p2i sz) }
    requires { 0 < p2i sz }
    ensures { value_sub_shift r (p2i sz) - power radix (p2i sz) * l2i result
              = value_sub_shift x (p2i sz) - l2i y }
    ensures { 0 <= l2i result <= 1 }
    writes { r.data.contents.elts }
  =
    let limb_zero = Limb.of_int 0 in
    let b = ref y in
    let lx = ref limb_zero in
    let i = ref (Int32.of_int 0) in
    while Int32.(<) !i sz && UInt32.ne !b limb_zero do
      invariant { 0 <= p2i !i <= p2i sz }
      invariant { p2i !i > 0 -> 0 <= l2i !b <= 1 }
      invariant { value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b
                  = value_sub_shift x (p2i !i) - l2i y }
      variant { p2i sz - p2i !i }
      label StartLoop in
      lx := get_ofs x !i;
      let (res, borrow) = sub_with_borrow !lx !b limb_zero in
      set_ofs r !i res;
      assert { value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b =
                 value_sub_shift x (p2i !i) - l2i y };
      b := borrow;
      let ghost k = p2i !i in
      i := Int32.(+) !i (Int32.of_int 1);
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts x) x.offset (x.offset + k);
      assert { value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b
                  = value_sub_shift x (p2i !i) - l2i y
             by
               value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b
             = value_sub_shift r k + power radix k * l2i res
                 - power radix (p2i !i) * l2i !b
             = value_sub_shift r k + power radix k * l2i res
                 - power radix k * radix * l2i !b
             = value_sub_shift r k + power radix k * (l2i res - radix * l2i !b)
             = value_sub_shift r k +
                 (power radix k) * (l2i !lx - l2i (!b at StartLoop))
             = value_sub_shift r k - power radix k * l2i (!b at StartLoop)
                 + power radix k * l2i !lx
             = value_sub_shift x k - l2i y + power radix k * l2i !lx
             = value_sub_shift x (p2i !i) - l2i y
      };
    done;
    if Int32.eq !i sz then !b
    else begin
    while Int32.(<) !i sz do
      invariant { l2i !b = 0 }
      invariant { 0 <= p2i !i <= p2i sz }
      invariant { value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b
                   = value_sub_shift x (p2i !i) - l2i y }
      variant { p2i sz - p2i !i }
      lx := get_ofs x !i;
      set_ofs r !i !lx;
      assert { value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b
                   = value_sub_shift x (p2i !i) - l2i y };
      let ghost k = p2i !i in
      i := Int32.(+) !i (Int32.of_int 1);
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts x) x.offset (x.offset + k);
    done;
    !b
  end

  (** [sub_limbs r x y sz] substracts [(y, sz)] from [(x, sz)] and
      writes the result to [(r, sz)]. Returns borrow, either 0 or
      1. Corresponds to [mpn_sub_n]. *)
  let sub_limbs (r x y:t) (sz:int32) : limb
    requires { valid_ptr_itv x (p2i sz) }
    requires { valid_ptr_itv y (p2i sz) }
    requires { valid_ptr_itv r (p2i sz) }
    ensures { 0 <= l2i result <= 1 }
    ensures { value_sub_shift r (p2i sz) - power radix (p2i sz) * l2i result
              = value_sub_shift x (p2i sz) - value_sub_shift y (p2i sz) }
    writes { r.data.contents.elts }
  =
    let limb_zero = UInt32.of_int 0 in
    let lx = ref limb_zero in
    let ly = ref limb_zero in
    let b = ref limb_zero in
    let i = ref (Int32.of_int 0) in
    while Int32.(<) !i sz do
      variant { p2i sz - p2i !i }
      invariant { 0 <= p2i !i <= p2i sz }
      invariant { value_sub_shift r (p2i !i) - (power radix (p2i !i)) * l2i !b
                  = value_sub_shift x (p2i !i) - value_sub_shift y (p2i !i) }
      invariant { 0 <= l2i !b <= 1 }
      label StartLoop in
      lx := get_ofs x !i;
      ly := get_ofs y !i;
      let res, borrow = sub_with_borrow !lx !ly !b in
      set_ofs r !i res;
      assert { value_sub_shift r (p2i !i) - (power radix (p2i !i)) * l2i !b =
      value_sub_shift x (p2i !i) - value_sub_shift y (p2i !i) };
      b := borrow;
      let ghost k = p2i !i in
      i := Int32.(+) !i (Int32.of_int 1);
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts x) x.offset (x.offset + k);
      value_sub_tail (pelts y) y.offset (y.offset + k);
      assert { value_sub_shift r (p2i !i) - (power radix (p2i !i)) * l2i !b
                  = value_sub_shift x (p2i !i) - value_sub_shift y (p2i !i)
               by
                 value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b
               = value_sub_shift r k + power radix k * l2i res
                   - power radix (p2i !i) * l2i !b
               = value_sub_shift r k + power radix k * l2i res
                   - power radix k * radix * l2i !b
               = value_sub_shift r k
                 + power radix k * (l2i res - radix * l2i !b)
               = value_sub_shift r k
                 + power radix k * (l2i !lx - l2i !ly - l2i (!b at StartLoop))
               = value_sub_shift r k - power radix k * l2i (!b at StartLoop)
                 + power radix k * (l2i !lx - l2i !ly)
               = value_sub_shift x k - value_sub_shift y k
                 + power radix k * (l2i !lx - l2i !ly)
               = value_sub_shift x k - value_sub_shift y k
                 + power radix k * l2i !lx - power radix k * l2i !ly
               = value_sub_shift x k + power radix k * l2i !lx
                 - (value_sub_shift y k + power radix k * l2i !ly)
               = value_sub_shift x (p2i !i)
                 - (value_sub_shift y k + power radix k * l2i !ly)
               = value_sub_shift x (p2i !i) - value_sub_shift y (p2i !i)
        };
      done;
      !b

  (** [sub r x y sx sy] adds [(x, sx)] to [(y,sy)] and writes the
      result in [(r, sx)]. [sx] must be greater than or equal to
      [sy]. Returns borrow, either 0 or 1. Corresponds to [mpn_sub]. *)
  let sub (r x y:t) (sx sy:int32) : limb
    requires { 0 <= p2i sy <= p2i sx }
    requires { valid_ptr_itv x (p2i sx) }
    requires { valid_ptr_itv y (p2i sy) }
    requires { valid_ptr_itv r (p2i sx) }
    ensures { value_sub_shift r (p2i sx)  - power radix (p2i sx) * l2i result
              = value_sub_shift x (p2i sx) - value_sub_shift y (p2i sy) }
    ensures { 0 <= l2i result <= 1 }
    writes { r.data.contents.elts }
  =
    let limb_zero = Limb.of_int 0 in
    let lx = ref limb_zero in
    let ly = ref limb_zero in
    let b = ref limb_zero in
    let i = ref (Int32.of_int 0) in
    let one = Int32.of_int 1 in
    while Int32.(<) !i sy do
      variant { p2i sy - p2i !i }
      invariant { 0 <= p2i !i <= p2i sy }
      invariant { value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b =
                  value_sub_shift x (p2i !i) - value_sub_shift y (p2i !i) }
      invariant { 0 <= l2i !b <= 1 }
      label StartLoop in
      lx := get_ofs x !i;
      ly := get_ofs y !i;
      let res, borrow = sub_with_borrow !lx !ly !b in
      set_ofs r !i res;
      assert { value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b =
                  value_sub_shift x (p2i !i) - value_sub_shift y (p2i !i) };
      b := borrow;
      let ghost k = p2i !i in
      i := Int32.(+) !i one;
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts x) x.offset (x.offset + k);
      value_sub_tail (pelts y) y.offset (y.offset + k);
      assert { value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b =
              value_sub_shift x (p2i !i) - value_sub_shift y (p2i !i)
              by
              value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b
              = value_sub_shift r k + power radix k * l2i res
                - power radix (p2i !i) * l2i !b
              = value_sub_shift r k + power radix k * l2i res
                - (power radix k) * radix * l2i !b
              = value_sub_shift r k
                + power radix k * (l2i res - radix * l2i !b)
              = value_sub_shift r k
                + power radix k * (l2i !lx - l2i !ly - l2i (!b at StartLoop))
              = value_sub_shift r k - (power radix k) * l2i (!b at StartLoop)
                + power radix k * (l2i !lx - l2i !ly)
              = value_sub_shift x k - value_sub_shift y k
                + power radix k * (l2i !lx - l2i !ly)
              = value_sub_shift x k + power radix k * l2i !lx
                - value_sub_shift y k - power radix k * l2i !ly
              = value_sub_shift x (p2i !i)
                - (value_sub_shift y k + power radix k * l2i !ly)
              = value_sub_shift x (p2i !i) - value_sub_shift y (p2i !i) };
    done;
    while Int32.(<) !i sx do
      variant { p2i sx - p2i !i }
      invariant { p2i sy <= p2i !i <= p2i sx }
      invariant { value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b =
                  value_sub_shift x (p2i !i) - value_sub_shift y (p2i sy) }
      invariant { 0 <= l2i !b <= 1 }
      label StartLoop2 in
      lx := get_ofs x !i;
      let res, borrow = sub_with_borrow !lx limb_zero !b in
      set_ofs r !i res;
      assert { value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b =
      value_sub_shift x (p2i !i) - value_sub_shift y (p2i sy) };
      b := borrow;
      let ghost k = p2i !i in
      i := Int32.(+) !i one;
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts x) x.offset (x.offset + k);
      assert { value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b =
               value_sub_shift x (p2i !i) - value_sub_shift y (p2i sy)
            by
              value_sub_shift r (p2i !i) - power radix (p2i !i) * l2i !b
            = value_sub_shift r k + power radix k * l2i res
              - (power radix (p2i !i)) * l2i !b
            = value_sub_shift r k + power radix k * l2i res
              - (power radix k) * radix * l2i !b
            = value_sub_shift r k + power radix k * (l2i res - radix * l2i !b)
            = value_sub_shift r k
              + power radix k * (l2i !lx - 0 - l2i (!b at StartLoop2))
            = value_sub_shift r k - (power radix k) * l2i (!b at StartLoop2)
              + (power radix k) * l2i !lx
            = value_sub_shift x k - value_sub_shift y (p2i sy)
              + (power radix k) * l2i !lx
            = value_sub_shift x (p2i !i)
              - value_sub_shift y (p2i sy) }
    done;
    !b

809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
    (** {2 Multiplication} *)

    (** [mul_limb r x y sz] multiplies [x[0..sz-1]] by the limb [y] and
    writes the n least significant limbs in [r], and returns the most
    significant limb. It corresponds to [mpn_mul_1]. *)
  let mul_limb (r x:t) (y:limb) (sz:int32) : limb
    requires { valid_ptr_itv x (p2i sz) }
    requires { valid_ptr_itv r (p2i sz) }
    ensures { value_sub_shift r (p2i sz) + (power radix (p2i sz)) * l2i result
                = value_sub_shift x (p2i sz) * l2i y }
    writes { r.data.contents.elts }
  =
    let limb_zero = UInt32.of_int 0 in
    let lx = ref limb_zero in
    let c = ref limb_zero in
    let i = ref (Int32.of_int 0) in
    while Int32.(<) !i sz do
      variant { p2i sz - p2i !i }
      invariant { 0 <= p2i !i <= p2i sz }
      invariant { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
                  value_sub_shift x (p2i !i) * l2i y }
      label StartLoop in
      lx := get_ofs x !i;
      let rl, rh = Limb.mul_double !lx y in
      let res, carry = Limb.add_with_carry rl !c limb_zero in
      label BeforeWrite in
      value_sub_shift_no_change (pelts r) r.offset (p2i !i) (p2i !i) res;
      set_ofs r !i res;
      assert { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
                  value_sub_shift x (p2i !i) * l2i y };
      assert { l2i rh < radix - 1
               by
               (l2i !lx * l2i y <= l2i !lx * (radix-1) <= (radix-1)*(radix-1)
                 by
                0 <= l2i !lx <= radix - 1 /\ 0 <= l2i y <= radix -1)
                 /\
               (radix * l2i rh <= l2i !lx * l2i y
                 by
               l2i rl + radix * l2i rh = l2i !lx * l2i y)
               so
               radix * l2i rh <= (radix -1) * (radix -1)
               };
      c := Limb.(+) rh carry;
      let ghost k = p2i !i in
      i := Int32.(+) !i (Int32.of_int 1);
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts x) x.offset (x.offset + k);
      assert { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c =
               value_sub_shift x (p2i !i) * l2i y
               by
                 value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c
               = value_sub_shift r k + (power radix k) * l2i res
                     + (power radix (p2i !i)) * l2i !c
               = value_sub_shift r k + (power radix k) * l2i res
                     + (power radix k) * radix * l2i !c
               = value_sub_shift r k + (power radix k) * (l2i res + radix * l2i !c)
               = value_sub_shift r k + (power radix k) *
                                   (l2i res + radix * (l2i rh + l2i carry))
               = value_sub_shift r k + (power radix k) *
                                   (l2i res + radix * l2i carry + radix * l2i rh)
               = value_sub_shift r k + (power radix k) *
                                   (l2i (!c at StartLoop) + l2i rl + radix*l2i rh)
               = value_sub_shift r k + (power radix k) *
                                   (l2i (!c at StartLoop) + l2i !lx * l2i y)
               = value_sub_shift r k + (power radix k) * l2i (!c at StartLoop)
                                 + (power radix k) * l2i !lx * l2i y
               = value_sub_shift x k * l2i y + (power radix k) * l2i !lx * l2i y
               = (value_sub_shift x k + (power radix k) * l2i !lx) * l2i y
               = value_sub_shift x (p2i !i) * l2i y
               };
    done;
    !c

  (** [addmul_limb r x y sz] multiplies [(x, sz)] by [i], adds the [sz]
      least significant limbs to [(r, sz)] and writes the result in [(r,sz)].
      Returns the most significant limb of the product plus the carry
      of the addition. Corresponds to [mpn_addmul_1].*)

  let addmul_limb (r x:t) (y:limb) (sz:int32):limb
    requires { valid_ptr_itv x (p2i sz) }
    requires { valid_ptr_itv r (p2i sz) }
    ensures { value_sub_shift r (p2i sz) + (power radix (p2i sz)) * l2i result
            = value_sub_shift (old r) (p2i sz)
              + value_sub_shift x (p2i sz) * l2i y }
    writes { r.data.contents.elts }
    ensures { forall j. j < r.offset \/ r.offset + p2i sz <= j ->
              (pelts r)[j] = (pelts (old r))[j] }
896
  =
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    let limb_zero = Limb.of_int 0 in
    let lx = ref limb_zero in
    let lr = ref limb_zero in
    let c = ref limb_zero in
    let i = ref (Int32.of_int 0) in
    while Int32.(<) !i sz do
      variant { p2i sz - p2i !i }
      invariant { 0 <= p2i !i <= p2i sz }
      invariant { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c
                 = value_sub_shift (old r) (p2i !i)
                   + value_sub_shift x (p2i !i) * l2i y }
      invariant { forall j. p2i !i <= j < p2i sz ->
                 (pelts (old r)) [r.offset+j] = (pelts r)[r.offset + j]  }
      invariant { forall j. j < r.offset \/ r.offset + p2i sz <= j ->
                 (pelts r)[j] = (pelts (old r))[j] }
      label StartLoop in
      let ghost k = p2i !i in
      lx := get_ofs x !i;
      lr := get_ofs r !i;
      assert { l2i !lr = l2i (pelts (old r))[r.offset+ p2i !i] };
      let rl, rh = Limb.mul_double !lx y in
      let res, carry = Limb.add3 !lr rl !c in
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts x) x.offset (x.offset + k);
      value_sub_update (pelts r) (r.offset + p2i !i) r.offset (r.offset + p2i !i +1) res;
      set_ofs r !i res;
      assert { forall j. (p2i !i + 1) <= j < p2i sz ->
               (pelts (old r))[r.offset+j] = (pelts r)[r.offset+j]
               by
               (pelts r)[r.offset+j] = ((pelts r) at StartLoop)[r.offset+j]
                                  = (pelts (old r))[r.offset+j] };
      assert { value_sub_shift r (p2i !i + 1)
              = value_sub_shift (r at StartLoop) (p2i !i + 1)
                + (power radix (p2i !i)) * (l2i res - l2i !lr) };
      assert { l2i rl + radix * l2i rh <= (radix-1)*(radix-1)
               by
               (l2i !lx * l2i y <= l2i !lx * (radix-1) <= (radix-1)*(radix-1)
                 by
                0 <= l2i !lx <= radix - 1 /\ 0 <= l2i y <= radix -1)
                /\
                l2i rl + radix * l2i rh = l2i !lx * l2i y
                };
      assert { l2i rh < radix - 1
               by
               l2i rl + radix * l2i rh  <= (radix -1) * (radix -1)
               so
               radix * l2i rh <= (radix -1) * (radix -1)
               };
      assert { l2i rh = radix - 2 -> l2i rl <= 1
               by
               l2i rl + radix * l2i rh <= (radix-1)*(radix-1) };
      assert { l2i rh = radix - 2 -> l2i carry <= 1
               by l2i rl <= 1 };
      c := Limb.(+) rh carry;
      i := Int32.(+) !i (Int32.of_int 1);
      assert { value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c
                 = value_sub_shift (old r) (p2i !i)
                   + value_sub_shift x (p2i !i) * l2i y
               by
                (value_sub_shift r (p2i !i) + (power radix (p2i !i)) * l2i !c
                = value_sub_shift (r at StartLoop) (p2i !i) +
                   (power radix k) * (l2i res - l2i !lr)
                   + (power radix (p2i !i)) * l2i !c
                = value_sub_shift (r at StartLoop) (p2i !i) +
                   (power radix k) * (l2i res - l2i !lr)
                   + (power radix (p2i !i)) * (l2i rh + l2i carry)
                = value_sub_shift (r at StartLoop) (p2i !i) +
                   (power radix k) * (l2i res - l2i !lr)
                   + (power radix k) * radix * (l2i rh + l2i carry)
                = value_sub_shift (r at StartLoop) (p2i !i) +
                   (power radix k) * (l2i res - l2i !lr
                                   + radix * (l2i rh + l2i carry))
                = value_sub_shift (r at StartLoop) (p2i !i) +
                   (power radix k) * (l2i res + radix * l2i carry
                          - l2i !lr + radix * l2i rh)
                = value_sub_shift (r at StartLoop) (p2i !i) +
                   (power radix k) * (l2i rl + l2i !lr + l2i (!c at StartLoop)
                          - l2i !lr + radix * l2i rh)
                = value_sub_shift (r at StartLoop) (p2i !i) +
                   (power radix k) * (l2i rl + radix * l2i rh + l2i (!c at StartLoop))
                = value_sub_shift (r at StartLoop) (p2i !i) +
                   (power radix k) * (l2i !lx * l2i y + l2i (!c at StartLoop))
                = value_sub_shift (r at StartLoop) k
                    + (power radix k) * l2i !lr
                    + (power radix k) * (l2i !lx * l2i y + l2i (!c at StartLoop))
                = value_sub_shift (r at StartLoop) k
                    + (power radix k) * l2i (!c at StartLoop)
                    + (power radix k) * (l2i !lx * l2i y + l2i !lr)
                = value_sub_shift (old r) k
                    + value_sub_shift x k * l2i y
                    + (power radix k) * (l2i !lx * l2i y + l2i !lr)
                = value_sub_shift (old r) k
                    + (power radix k) * l2i !lr
                    + (value_sub_shift x k + (power radix k)*l2i !lx) * l2i y
                = value_sub_shift (old r) (p2i !i)
                    + (value_sub_shift x k + (power radix k)*l2i !lx) * l2i y
                = value_sub_shift (old r) (p2i !i)
                    + value_sub_shift x (p2i !i) * l2i y
                    by
                  value_sub_shift (old r) (p2i !i) = value_sub_shift (old r) k
                     + (power radix k) * l2i !lr
                     )
                    };
    done;
    !c

  (** [mul_limbs r x y sz] multiplies [(x, sz)] and [(y, sz)] and
  writes the result to [(r, 2*sz)]. [r] must not overlap with either
  [x] or [y]. Corresponds to [mpn_mul_n].  *)
  let mul_limbs (r x y:t) (sz:int32) : unit
    requires { p2i sz > 0 }
    requires { valid_ptr_itv x (p2i sz) }
    requires { valid_ptr_itv y (p2i sz) }
    requires { valid_ptr_itv r (p2i sz +  p2i sz) }
    writes { r.data.contents.elts }
    ensures { value_sub_shift r (p2i sz + p2i sz)
              = value_sub_shift x (p2i sz) * value_sub_shift y (p2i sz) }
1014 1015 1016
  =
    zero r sz;
    let limb_zero = Limb.of_int 0 in
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
    let one = Int32.of_int 1 in
    let rp = ref r in
    let ly = ref limb_zero in
    let c = ref limb_zero in
    let i = ref (Int32.of_int 0) in
    while Int32.(<) !i sz do
      invariant { 0 <= p2i !i <= p2i sz }
      invariant { value_sub_shift r (p2i !i + p2i sz)
                    + (power radix (p2i !i + p2i sz)) * l2i !c
                  = value_sub_shift x (p2i sz)
                      * value_sub_shift y (p2i !i) }
      invariant { (!rp).offset = r.offset + p2i !i }
      invariant { plength !rp = plength r }
      invariant { pelts !rp = pelts r }
      invariant { 0 <= l2i !c <= 1 }
      variant { p2i sz - p2i !i }
      label StartLoop in
      let ghost k = p2i !i in
      value_sub_concat (pelts r) r.offset (r.offset + k)
                       (r.offset + k + p2i sz);
      assert { value_sub_shift r k
             + (power radix k) * value_sub (pelts r) (r.offset + k)
                                                   (r.offset + k + p2i sz)
             = value_sub_shift r (k + p2i sz) };
      ly := get_ofs y !i;
      let c' =  addmul_limb !rp x !ly sz in
      assert { value_sub_shift !rp (p2i sz) + power radix (p2i sz) * l2i c'
              = value_sub_shift (!rp at StartLoop) (p2i sz)
                + value_sub_shift x (p2i sz) * l2i !ly };
      assert { MapEq.map_eq_sub (pelts r) (pelts r at StartLoop)
                r.offset (!rp).offset
                by (!rp).offset = r.offset + p2i !i
                so forall j. r.offset <= j < (!rp).offset
                   ->
                   (j < (!rp).offset
                    so (pelts !rp)[j] = (pelts !rp at StartLoop)[j]
                         = (pelts r at StartLoop)[j]) };
      let (res, carry) = add_with_carry c' limb_zero !c in
      label BeforeCarry in
      value_sub_update_no_change (pelts r) ((!rp).offset + p2i sz)
                        r.offset  (r.offset + p2i !i) res;
      set_ofs !rp sz res;
      c:= carry;
      i := Int32.(+) !i one;
      assert { value_sub_shift r k = value_sub_shift (r at BeforeCarry) k
             = value_sub_shift (r at StartLoop) k};
      value_sub_tail (pelts r) r.offset (r.offset + p2i sz + k);
      value_sub_tail (pelts y) y.offset (y.offset + k);
      value_sub_concat (pelts r) r.offset (r.offset + k) (r.offset + k + p2i sz);
      assert { value_sub (pelts r) (r.offset+k) (r.offset+k+p2i sz)
               = value_sub_shift !rp (p2i sz) };
      assert { value_sub_shift r (p2i !i + p2i sz)
                    + (power radix (p2i !i + p2i sz)) * l2i !c
                  = value_sub_shift x (p2i sz)
                      * value_sub_shift y (p2i !i)
               by
                 power radix (k + p2i sz) = power radix k * power radix (p2i sz)
               so
                 value_sub_shift (r at StartLoop) k
                 + (power radix k) * value_sub (pelts r at StartLoop)
                                   (r.offset + k) (r.offset + k + p2i sz)
                 = value_sub_shift (r at StartLoop) (k + p2i sz)
               so
                 value_sub_shift r (p2i !i + p2i sz)
                    + (power radix (p2i !i + p2i sz)) * l2i !c
               = value_sub_shift r (k + p2i sz)
                    + (power radix (k + p2i sz)) * l2i res
                    + (power radix (p2i !i + p2i sz)) * l2i !c
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sz)
                    + (power radix (k + p2i sz)) * l2i res
                    + (power radix (p2i !i + p2i sz)) * l2i !c
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sz)
                    + (power radix k) * (power radix (p2i sz)) * l2i res
                    + (power radix (p2i !i + p2i sz)) * l2i !c
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sz)
                    + (power radix k) * (power radix (p2i sz)) * l2i res
                    + (power radix k) * (power radix (p2i sz)) * radix * l2i !c
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sz)
                    + (power radix k) * (power radix (p2i sz))
                             * (l2i res + radix * l2i !c)
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sz)
                    + (power radix k) * (power radix (p2i sz))
                             * (l2i c' + l2i (!c at StartLoop))
               = value_sub_shift r k + (power radix k)
                    * (value_sub_shift !rp (p2i sz)
                      + power radix (p2i sz) * l2i c'
                      + (power radix (p2i sz)) * l2i (!c at StartLoop))
               = value_sub_shift r k + (power radix k)
                    * (value_sub_shift (!rp at StartLoop) (p2i sz)
                       + value_sub_shift x (p2i sz) * l2i !ly
                       + (power radix (p2i sz)) * l2i (!c at StartLoop))
               = value_sub_shift r k
                 + power radix k * (value_sub_shift (!rp at StartLoop) (p2i sz))
                 + power radix k * (value_sub_shift x (p2i sz) * l2i !ly
                    + (power radix (p2i sz)) * (l2i (!c at StartLoop)))
               = value_sub_shift (r at StartLoop) k
                 + power radix k * (value_sub_shift (!rp at StartLoop) (p2i sz))
                 + power radix k * (value_sub_shift x (p2i sz) * l2i !ly
                    + (power radix (p2i sz)) * (l2i (!c at StartLoop)))
               = value_sub_shift (r at StartLoop) k
                 + power radix k * (value_sub (pelts r at StartLoop) (r.offset+k)
                                              (r.offset+k+ p2i sz))
                 + power radix k * (value_sub_shift x (p2i sz) * l2i !ly
                    + (power radix (p2i sz)) * (l2i (!c at StartLoop)))
               = value_sub_shift (r at StartLoop) (k + p2i sz)
                 + power radix k * (value_sub_shift x (p2i sz) * l2i !ly
                    + (power radix (p2i sz)) * (l2i (!c at StartLoop)))
               = value_sub_shift (r at StartLoop) (k + p2i sz)
                 + power radix k * value_sub_shift x (p2i sz) * l2i !ly
                 + power radix k * power radix (p2i sz) * (l2i (!c at StartLoop))
               = value_sub_shift (r at StartLoop) (k + p2i sz)
                 + power radix k * power radix (p2i sz) * (l2i (!c at StartLoop))
                 + power radix k * value_sub_shift x (p2i sz) * l2i !ly
               = value_sub_shift (r at StartLoop) (k + p2i sz)
                 + power radix (k + p2i sz) * (l2i (!c at StartLoop))
                 + power radix k * value_sub_shift x (p2i sz) * l2i !ly
               = value_sub_shift x (p2i sz) * value_sub_shift y k
                + power radix k * value_sub_shift x (p2i sz) * l2i !ly
               = value_sub_shift x (p2i sz) *
                 (value_sub_shift y k + power radix k * l2i !ly)
               = value_sub_shift x (p2i sz) * value_sub_shift y (p2i !i)
             };
1144
      rp := C.incr !rp one;
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
    done;
    value_sub_lower_bound (pelts r) r.offset (r.offset + p2i sz + p2i sz);
    value_sub_upper_bound (pelts x) x.offset (x.offset + p2i sz);
    value_sub_upper_bound (pelts y) y.offset (y.offset + p2i sz);
    assert { 0 <= value_sub_shift x (p2i sz) < power radix (p2i sz) };
    assert { 0 <= value_sub_shift y (p2i sz) < power radix (p2i sz) };
    prod_compat_strict_r (value_sub_shift y (p2i sz)) (power radix (p2i sz))
                         (power radix (p2i sz));
    assert { l2i !c = 0 by
             0 < power radix (p2i sz)
             so
             value_sub_shift r (p2i sz + p2i sz)
                    + (power radix (p2i sz + p2i sz)) * l2i !c
                  = value_sub_shift x (p2i sz)
                      * value_sub_shift y (p2i sz)
             so
             (power radix (p2i sz + p2i sz))*l2i !c <=
                    value_sub_shift x (p2i sz)
                    * value_sub_shift y (p2i sz)
                    <= (power radix (p2i sz)) * value_sub_shift y (p2i sz)
                    < (power radix (p2i sz))*(power radix (p2i sz))
             so
             (power radix (p2i sz + p2i sz))*l2i !c <
                    (power radix (p2i sz))*(power radix (p2i sz))  }
1169

1170 1171 1172 1173 1174 1175 1176
 let addmul_limbs (r x y:t) (sz:int32) : limb
    requires { p2i sz > 0 }
    requires { valid_ptr_itv x (p2i sz) }
    requires { valid_ptr_itv y (p2i sz) }
    requires { valid_ptr_itv r (p2i sz +  p2i sz) }
    writes { r.data.contents.elts }
    ensures { value_sub_shift r (p2i sz + p2i sz)
1177
                + power radix (p2i sz + p2i sz) * l2i result
1178
              = value_sub_shift (old r) (p2i sz + p2i sz)
1179 1180
                + value_sub_shift x (p2i sz) * value_sub_shift y (p2i sz) }
  =
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    let limb_zero = Limb.of_int 0 in
    let one = Int32.of_int 1 in
    let rp = ref r in
    let ly = ref limb_zero in
    let lr = ref limb_zero in
    let c = ref limb_zero in
    let i = ref (Int32.of_int 0) in
    let rec lemma old_tail_shift (i:int)
      requires { i >= 0 }
      variant { i }
1191 1192
      ensures { value_sub_shift (old r) (i+1) = value_sub_shift (old r) i
              + power radix i * l2i (pelts (old r))[r.offset+i] }
1193
      =
1194
        if i > 0 then old_tail_shift (i-1) else assert {1+2=3}  in
1195 1196 1197 1198 1199
    while Int32.(<) !i sz do
      invariant { 0 <= p2i !i <= p2i sz }
      invariant { value_sub_shift r (p2i !i + p2i sz)
                    + (power radix (p2i !i + p2i sz)) * l2i !c
                  = value_sub_shift (old r) (p2i !i + p2i sz)
1200
                    + value_sub_shift x (p2i sz) * value_sub_shift y (p2i !i) }
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
      invariant { (!rp).offset = r.offset + p2i !i }
      invariant { r.data = (!rp).data }
      invariant { 0 <= l2i !c <= 1 }
      invariant { forall j. (!rp).offset + p2i sz <= j ->
                 (pelts (old r)) [j] = (pelts r)[j]  }
      variant { p2i sz - p2i !i }
      label StartLoop in
      let ghost k = p2i !i in
      value_sub_concat (pelts r) r.offset (r.offset + k)
                       (r.offset + k + p2i sz);
      assert { value_sub_shift r k
             + (power radix k) * value_sub (pelts r) (r.offset + k)
                                                   (r.offset + k + p2i sz)
             = value_sub_shift r (k + p2i sz) };
      ly := get_ofs y !i;
      let c' =  addmul_limb !rp x !ly sz in
      assert { value_sub_shift !rp (p2i sz) + power radix (p2i sz) * l2i c'
              = value_sub_shift (!rp at StartLoop) (p2i sz)
                + value_sub_shift x (p2i sz) * l2i !ly };
      assert { MapEq.map_eq_sub (pelts r) (pelts r at StartLoop)
                r.offset (!rp).offset
                by (!rp).offset = r.offset + p2i !i
                so forall j. r.offset <= j < (!rp).offset
                   ->
                   (j < (!rp).offset
                    so (pelts !rp)[j] = (pelts !rp at StartLoop)[j]
                         = (pelts r at StartLoop)[j]) };
      lr := get_ofs !rp sz;
      assert { l2i !lr = l2i (pelts (old r))[r.offset+ p2i !i + p2i sz] };
      let (res, carry) = add_with_carry c' !lr !c in
      label BeforeCarry in
      value_sub_update_no_change (pelts r) ((!rp).offset + p2i sz)
                        r.offset  (r.offset + p2i !i) res;
      set_ofs !rp sz res;
      assert { value_sub_shift !rp (p2i sz) = value_sub_shift (!rp at BeforeCarry) (p2i sz) };
      c:= carry;
      i := Int32.(+) !i one;
      assert { value_sub_shift r k = value_sub_shift (r at BeforeCarry) k
             = value_sub_shift (r at StartLoop) k};
      value_sub_tail (pelts r) r.offset (r.offset + p2i sz + k);
      value_sub_tail (pelts y) y.offset (y.offset + k);
      old_tail_shift (k+p2i sz);
      value_sub_concat (pelts r) r.offset (r.offset + k) (r.offset + k + p2i sz);
      assert { value_sub (pelts r) (r.offset+k) (r.offset+k+p2i sz)
               = value_sub_shift !rp (p2i sz) };
      assert { value_sub_shift r (p2i !i + p2i sz)
                    + (power radix (p2i !i + p2i sz)) * l2i !c
                  = value_sub_shift (old r) (p2i !i + p2i sz)
1249
                    + value_sub_shift x (p2i sz)
1250 1251 1252 1253 1254 1255 1256 1257
                      * value_sub_shift y (p2i !i)
               by
                 power radix (k + p2i sz) = power radix k * power radix (p2i sz)
               so
                 value_sub_shift (r at StartLoop) k
                 + (power radix k) * value_sub (pelts r at StartLoop)
                                   (r.offset + k) (r.offset + k + p2i sz)
                 = value_sub_shift (r at StartLoop) (k + p2i sz)
1258 1259 1260 1261 1262 1263 1264 1265 1266
               so (value_sub_shift (old r) (p2i !i+p2i sz)
                  = value_sub_shift (old r) (k+p2i sz)
                  + power radix (k+p2i sz) * l2i !lr
                  by l2i !lr = l2i (pelts (old r))[r.offset + k + p2i sz])
               so
                  value_sub_shift !rp (p2i sz) + (power radix (p2i sz)) * l2i c' =
                  value_sub_shift (!rp at StartLoop) (p2i sz)
                  + value_sub_shift x (p2i sz) * l2i !ly
               so
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
                 value_sub_shift r (p2i !i + p2i sz)
                    + (power radix (p2i !i + p2i sz)) * l2i !c
               = value_sub_shift r (k + p2i sz)
                    + (power radix (k + p2i sz)) * l2i res
                    + (power radix (p2i !i + p2i sz)) * l2i !c
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sz)
                    + (power radix (k + p2i sz)) * l2i res
                    + (power radix (p2i !i + p2i sz)) * l2i !c
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sz)
                    + (power radix k) * (power radix (p2i sz)) * l2i res
                    + (power radix (p2i !i + p2i sz)) * l2i !c
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sz)
                    + (power radix k) * (power radix (p2i sz)) * l2i res
                    + (power radix k) * (power radix (p2i sz)) * radix * l2i !c
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sz)
                    + (power radix k) * (power radix (p2i sz))
                             * (l2i res + radix * l2i !c)
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sz)
                    + (power radix k) * (power radix (p2i sz))
                             * (l2i c' + l2i (!c at StartLoop) + l2i !lr)
               = value_sub_shift r k + (power radix k)
                    * (value_sub_shift !rp (p2i sz)
                      + power radix (p2i sz) * (l2i c'+ l2i (!c at StartLoop) + l2i !lr))
               = value_sub_shift r k + (power radix k)
                    * (value_sub_shift !rp (p2i sz)
                      + power radix (p2i sz) * l2i c'
1298
                      + power radix (p2i sz) * (l2i (!c at StartLoop) + l2i !lr))
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
               = value_sub_shift r k + (power radix k)
                    * (value_sub_shift (!rp at StartLoop) (p2i sz)
                       + value_sub_shift x (p2i sz) * l2i !ly
                       + (power radix (p2i sz)) * (l2i (!c at StartLoop) + l2i !lr))
               = value_sub_shift r k
                 + power radix k * (value_sub_shift (!rp at StartLoop) (p2i sz))
                 + power radix k * (value_sub_shift x (p2i sz) * l2i !ly
                    + (power radix (p2i sz)) * (l2i (!c at StartLoop) + l2i !lr))
               = value_sub_shift (r at StartLoop) k
                 + power radix k * (value_sub_shift (!rp at StartLoop) (p2i sz))
                 + power radix k * (value_sub_shift x (p2i sz) * l2i !ly
                    + (power radix (p2i sz)) * (l2i (!c at StartLoop) + l2i !lr))
               = value_sub_shift (r at StartLoop) k
                 + power radix k * (value_sub (pelts r at StartLoop) (r.offset+k)
                                              (r.offset+k+ p2i sz))
                 + power radix k * (value_sub_shift x (p2i sz) * l2i !ly
                    + (power radix (p2i sz)) * (l2i (!c at StartLoop) + l2i !lr))
               = value_sub_shift (r at StartLoop) (k + p2i sz)
                 + power radix k * (value_sub_shift x (p2i sz) * l2i !ly
                    + (power radix (p2i sz)) * (l2i (!c at StartLoop) + l2i !lr))
               = value_sub_shift (r at StartLoop) (k + p2i sz)
                 + power radix k * value_sub_shift x (p2i sz) * l2i !ly
                 + power radix k * power radix (p2i sz) * (l2i (!c at StartLoop) + l2i !lr)
               = value_sub_shift (r at StartLoop) (k + p2i sz)
                 + power radix k * power radix (p2i sz) * (l2i (!c at StartLoop) + l2i !lr)
                 + power radix k * value_sub_shift x (p2i sz) * l2i !ly
               = value_sub_shift (r at StartLoop) (k + p2i sz)
                 + power radix (k + p2i sz) * (l2i (!c at StartLoop) + l2i !lr)
                 + power radix k * value_sub_shift x (p2i sz) * l2i !ly
               = value_sub_shift (r at StartLoop) (k + p2i sz)
                 + power radix (k + p2i sz) * (l2i (!c at StartLoop))
                 + power radix (k + p2i sz) * l2i !lr
                 + power radix k * value_sub_shift x (p2i sz) * l2i !ly
               = value_sub_shift (old r) (k+p2i sz)
                 + value_sub_shift x (p2i sz) * value_sub_shift y k
                 + power radix (k + p2i sz) * l2i !lr
                 + power radix k * value_sub_shift x (p2i sz) * l2i !ly
               = value_sub_shift (old r) (k+p2i sz)
                 + power radix (k + p2i sz) * l2i !lr
                 + value_sub_shift x (p2i sz) * value_sub_shift y k
                 + power radix k * value_sub_shift x (p2i sz) * l2i !ly
               = value_sub_shift (old r) (k+p2i sz)
                 + power radix (k + p2i sz) * l2i !lr
                 + value_sub_shift x (p2i sz) * (value_sub_shift y k + power radix k * l2i !ly)
               = value_sub_shift (old r) (k+p2i sz)
                 + power radix (k + p2i sz) * l2i !lr
                 + value_sub_shift x (p2i sz) * value_sub_shift y (p2i !i)
               = value_sub_shift (old r) (p2i !i +p2i sz)
                 + value_sub_shift x (p2i sz) * value_sub_shift y (p2i !i)
             };
1349
      rp := C.incr !rp one;
1350 1351 1352
    done;
    !c

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
  (** [mul r x y sx sy] multiplies [(x, sx)] and [(y,sy)] and writes
  the result in [(r, sx+sy)]. [sx] must be greater than or equal to
  [sy]. Corresponds to [mpn_mul]. *)
  let mul (r x y:t) (sx sy:int32) : unit
    requires { 0 < p2i sy <= p2i sx }
    requires { valid_ptr_itv x (p2i sx) }
    requires { valid_ptr_itv y (p2i sy) }
    requires { valid_ptr_itv r (p2i sy + p2i sx) }
    writes { r.data.contents.elts }
    ensures { value_sub_shift r (p2i sy + p2i sx)
              = value_sub_shift x (p2i sx) * value_sub_shift y (p2i sy) }
  =
    zero r sx;
    let limb_zero = Limb.of_int 0 in
    let one = Int32.of_int 1 in
    let rp = ref r in
    let ly = ref limb_zero in
    let c = ref limb_zero in
    let i = ref (Int32.of_int 0) in
    while Int32.(<) !i sy do
      invariant { 0 <= p2i !i <= p2i sy }
      invariant { value_sub_shift r (p2i !i + p2i sx)
                    + (power radix (p2i !i + p2i sx)) * l2i !c
                  = value_sub_shift x (p2i sx)
                      * value_sub_shift y (p2i !i) }
      invariant { (!rp).offset = r.offset + p2i !i }
      invariant { plength !rp = plength r }
      invariant { pelts !rp = pelts r }
      invariant { 0 <= l2i !c <= 1 }
      variant { p2i sy - p2i !i }
      label StartLoop in
      let ghost k = p2i !i in
      value_sub_concat (pelts r) r.offset (r.offset + k)
                       (r.offset + k + p2i sx);
      assert { value_sub_shift r k
             + (power radix k) * value_sub (pelts r) (r.offset + k)
                                                   (r.offset + k + p2i sx)
             = value_sub_shift r (k + p2i sx) };
      ly := get_ofs y !i;
      let c' =  addmul_limb !rp x !ly sx in
      assert { value_sub_shift !rp (p2i sx) + power radix (p2i sx) * l2i c'
              = value_sub_shift (!rp at StartLoop) (p2i sx)
                + value_sub_shift x (p2i sx) * l2i !ly };
      assert { MapEq.map_eq_sub (pelts r) (pelts r at StartLoop)
                r.offset (!rp).offset
                by (!rp).offset = r.offset + p2i !i
                so forall j. r.offset <= j < (!rp).offset
                   ->
                   (j < (!rp).offset
                    so (pelts !rp)[j] = (pelts !rp at StartLoop)[j]
                         = (pelts r at StartLoop)[j]) };
      let (res, carry) = add_with_carry c' limb_zero !c in
      label BeforeCarry in
      value_sub_update_no_change (pelts r) ((!rp).offset + p2i sx)
                        r.offset  (r.offset + p2i !i) res;
      set_ofs !rp sx res;
      c:= carry;
      i := Int32.(+) !i one;
      assert { value_sub_shift r k = value_sub_shift (r at BeforeCarry) k
             = value_sub_shift (r at StartLoop) k};
      value_sub_tail (pelts r) r.offset (r.offset + p2i sx + k);
      value_sub_tail (pelts y) y.offset (y.offset + k);
      value_sub_concat (pelts r) r.offset (r.offset + k) (r.offset + k + p2i sx);
      assert { value_sub (pelts r) (r.offset+k) (r.offset+k+p2i sx)
               = value_sub_shift !rp (p2i sx) };
      assert { value_sub_shift r (p2i !i + p2i sx)
                    + (power radix (p2i !i + p2i sx)) * l2i !c
                  = value_sub_shift x (p2i sx)
                      * value_sub_shift y (p2i !i)
               by
                 power radix (k + p2i sx) = power radix k * power radix (p2i sx)
               so
                 value_sub_shift (r at StartLoop) k
                 + (power radix k) * value_sub (pelts r at StartLoop)
                                   (r.offset + k) (r.offset + k + p2i sx)
                 = value_sub_shift (r at StartLoop) (k + p2i sx)
               so
                 value_sub_shift r (p2i !i + p2i sx)
                    + (power radix (p2i !i + p2i sx)) * l2i !c
               = value_sub_shift r (k + p2i sx)
                    + (power radix (k + p2i sx)) * l2i res
                    + (power radix (p2i !i + p2i sx)) * l2i !c
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sx)
                    + (power radix (k + p2i sx)) * l2i res
                    + (power radix (p2i !i + p2i sx)) * l2i !c
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sx)
                    + (power radix k) * (power radix (p2i sx)) * l2i res
                    + (power radix (p2i !i + p2i sx)) * l2i !c
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sx)
                    + (power radix k) * (power radix (p2i sx)) * l2i res
                    + (power radix k) * (power radix (p2i sx)) * radix * l2i !c
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sx)
                    + (power radix k) * (power radix (p2i sx))
                             * (l2i res + radix * l2i !c)
               = value_sub_shift r k
                    + (power radix k) * value_sub_shift !rp (p2i sx)
                    + (power radix k) * (power radix (p2i sx))
                             * (l2i c' + l2i (!c at StartLoop))
               = value_sub_shift r k + (power radix k)
                    * (value_sub_shift !rp (p2i sx)
                      + power radix (p2i sx) * l2i c'
                      + (power radix (p2i sx)) * l2i (!c at StartLoop))
               = value_sub_shift r k + (power radix k)
                    * (value_sub_shift (!rp at StartLoop) (p2i sx)
                       + value_sub_shift x (p2i sx) * l2i !ly
                       + (power radix (p2i sx)) * l2i (!c at StartLoop))
               = value_sub_shift r k
                 + power radix k * (value_sub_shift (!rp at StartLoop) (p2i sx))
                 + power radix k * (value_sub_shift x (p2i sx) * l2i !ly
                    + (power radix (p2i sx)) * (l2i (!c at StartLoop)))
               = value_sub_shift (r at StartLoop) k
                 + power radix k * (value_sub_shift (!rp at StartLoop) (p2i sx))
                 + power radix k * (value_sub_shift x (p2i sx) * l2i !ly
                    + (power radix (p2i sx)) * (l2i (!c at StartLoop)))
               = value_sub_shift (r at StartLoop) k
                 + power radix k * (value_sub (pelts r at StartLoop) (r.offset+k)
                                              (r.offset+k+ p2i sx))
                 + power radix k * (value_sub_shift x (p2i sx) * l2i !ly
                    + (power radix (p2i sx)) * (l2i (!c at StartLoop)))
               = value_sub_shift (r at StartLoop) (k + p2i sx)
                 + power radix k * (value_sub_shift x (p2i sx) * l2i !ly
                    + (power radix (p2i sx)) * (l2i (!c at StartLoop)))
               = value_sub_shift (r at StartLoop) (k + p2i sx)
                 + power radix k * value_sub_shift x (p2i sx) * l2i !ly
                 + power radix k * power radix (p2i sx) * (l2i (!c at StartLoop))
               = value_sub_shift (r at StartLoop) (k + p2i sx)
                 + power radix k * power radix (p2i sx) * (l2i (!c at StartLoop))
                 + power radix k * value_sub_shift x (p2i sx) * l2i !ly
               = value_sub_shift (r at StartLoop) (k + p2i sx)
                 + power radix (k + p2i sx) * (l2i (!c at StartLoop))
                 + power radix k * value_sub_shift x (p2i sx) * l2i !ly
               = value_sub_shift x (p2i sx) * value_sub_shift y k
                + power radix k * value_sub_shift x (p2i sx) * l2i !ly
               = value_sub_shift x (p2i sx) *
                 (value_sub_shift y k + power radix k * l2i !ly)
               = value_sub_shift x (p2i sx) * value_sub_shift y (p2i !i)
             };
1494
      rp := C.incr !rp one;
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
    done;
    value_sub_lower_bound (pelts r) r.offset (r.offset + p2i sy + p2i sx);
    value_sub_upper_bound (pelts x) x.offset (x.offset + p2i sx);
    value_sub_upper_bound (pelts y) y.offset (y.offset + p2i sy);
    assert { 0 <= value_sub_shift x (p2i sx) < power radix (p2i sx) };
    assert { 0 <= value_sub_shift y (p2i sy) < power radix (p2i sy) };
    prod_compat_strict_r (value_sub_shift y (p2i sy)) (power radix (p2i sy))
                         (power radix (p2i sx));
    assert { l2i !c = 0 by
             0 < power radix (p2i sx)
             so
             value_sub_shift r (p2i sy + p2i sx)
                    + (power radix (p2i sy + p2i sx)) * l2i !c
                  = value_sub_shift x (p2i sx)
                      * value_sub_shift y (p2i sy)
             so
             (power radix (p2i sy + p2i sx))*l2i !c <=
                    value_sub_shift x (p2i sx)
                    * value_sub_shift y (p2i sy)
                    <= (power radix (p2i sx)) * value_sub_shift y (p2i sy)
                    < (power radix (p2i sx))*(power radix (p2i sy))
             so
             (power radix (p2i sx + p2i sy))*l2i !c <
                    (power radix (p2i sx))*(power radix (p2i sy))  }
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1519 1520 1521 1522


  (** {2 Logical operations} *)

1523
  use import int.EuclideanDivision
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1524

1525 1526
  (* is a logical lemma in ComputerDivision*)
  let lemma mod_mult (x y z:int)
1527
    requires { x > 0 }
1528 1529 1530 1531
    ensures { mod (x * y + z) x = mod z x }
  =
    ()

Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1532 1533 1534 1535 1536
  val lsld (x cnt:limb) : (limb,limb)
    requires { 0 < l2i cnt < 32 }
    returns { (r,d) -> l2i r + radix * l2i d =
              (power 2 (l2i cnt)) * l2i x }

1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
  let lemma pow2_32 ()
    ensures { power 2 32 = radix }
  =
    assert { power 2 2 = 4 };
    assert { power 2 4 = (power 2 2)*(power 2 2) };
    assert { power 2 8 = (power 2 4)*(power 2 4) };
    assert { power 2 16 = (power 2 8)*(power 2 8) };
    assert { power 2 32 = (power 2 16)*(power 2 16) = radix };
    ()

  let lsld_ext (x cnt:limb) : (limb,limb)
    requires { 0 < l2i cnt < 32 }
    returns { (r,d) -> l2i r + radix * l2i d =
              (power 2 (l2i cnt)) * l2i x }
    returns { (r,_d) ->  mod (l2i r) (power 2 (l2i cnt)) = 0 }
    returns { (r,_d) ->  l2i r <= radix - (power 2 (l2i cnt)) }
1553
    returns { (_r,d) -> l2i d < power 2 (l2i cnt) }
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
  =
    let r,d = lsld x cnt in
    let p = power 2 (l2i cnt) in
    let q = power 2 (32 - l2i cnt) in
    assert { p > 0 /\ q > 0 };
    assert { radix = p * q by
                radix = power 2 32 = power 2 (l2i cnt + (32 - l2i cnt))
                = p*q };
    assert { mod radix p = 0
             by mod radix p
                = mod (p * q + 0) p
                = mod 0 p
                = 0 };
    assert { l2i r < radix };
    mod_mult p (q*l2i d) (l2i r);
    mod_mult p (l2i x) 0;
    assert { mod (l2i r) p = 0
             by
             mod (l2i r) p = mod (p * (q * l2i d) + l2i r) p
             so p * (q * l2i d) = radix * l2i d
             so mod (l2i r) p = mod (radix * l2i d + l2i r) p
                = mod (p * l2i x) p
                = mod 0 p
                = 0 };
    assert { l2i r <= radix - p
             by
             l2i r = p * (div (l2i r) p) + (mod (l2i r) p)
                   = p * (div (l2i r) p)
             so
             radix = p * q
             so
             l2i r < radix
             so (div (l2i r) p >= q -> (l2i r = p * div (l2i r) p >= p*q = radix)
                                   -> false)
             so div (l2i r) p <= q-1
             so l2i r = p * div (l2i r) p <= p * (q-1) = p*q - p = radix - p };
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
    assert { l2i d < p
             by
             l2i r + radix * l2i d = p * l2i x
             so
             radix * l2i d <= p * l2i x
             so
             l2i x < radix /\ p > 0
             so p * l2i x < p * radix
             so radix * l2i d < p * radix
             so l2i d < p
             };
1601 1602
    (r,d)

Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
  val lsl (x cnt:limb) : limb
    requires { 0 < l2i cnt < 32 }
    ensures { l2i result =
              (l2i x) * (mod (power 2 (l2i cnt)) radix) }

  val lsr (x cnt:limb) : limb
    requires { 0 < l2i cnt < 32 }
    ensures { l2i result = div (l2i x) (power 2 (l2i cnt)) }

  (** [lshift r x sz cnt] shifts [(x,sz)] [cnt] bits to the left and
      writes the result in [(r, sz)]. Returns the [cnt] most significant
1614
      bits of [(x, sz)]. Corresponds to [mpn_lshift]. *)
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1615 1616 1617 1618 1619 1620
  (*TODO overlapping allowed if r >= x*)
  let lshift (r x:t) (sz:int32) (cnt:uint32) : limb
    requires { 0 < l2i cnt < 32 }
    requires { valid_ptr_itv r (p2i sz) }
    requires { valid_ptr_itv x (p2i sz) }
    requires { 0 < p2i sz }
1621
    ensures { value_sub_shift r (p2i sz) + (power radix (p2i sz)) * l2i result =
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1622 1623 1624 1625 1626 1627 1628
              value_sub_shift x (p2i sz) * (power 2 (l2i cnt)) }
  =
    let limb_zero = Limb.of_int 0 in
    let zero = Int32.of_int 0 in
    let one = Int32.of_int 1 in
    let minus_one = Int32.of_int (-1) in
    let msb = Int32.(-) sz one in
1629 1630
    let xp = ref (C.incr x msb) in
    let rp = ref (C.incr r msb) in
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1631
    let high = ref limb_zero in
1632
    let low = ref (C.get !xp) in
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1633
    let i = ref msb in
1634
    let l, retval = lsld_ext !low cnt in
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1635 1636 1637 1638 1639 1640
    high := l;
    while (Int32.(>) !i zero) do
      variant { p2i !i }
      invariant { 0 <= p2i !i < p2i sz }
      invariant { radix * value_sub (pelts r) (r.offset + 1 + p2i !i) (r.offset + p2i sz)
                  + (power radix (p2i sz - p2i !i)) * l2i retval + l2i !high
1641
                = value_sub_shift !xp (p2i sz - p2i !i)
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1642 1643 1644 1645 1646 1647 1648
                  * (power 2 (l2i cnt)) }
      invariant { (!rp).offset = r.offset + p2i !i }
      invariant { (!xp).offset = x.offset + p2i !i }
      invariant { plength !rp = plength r }
      invariant { pelts !rp = pelts r }
      invariant { plength !xp = plength x }
      invariant { pelts !xp = pelts x }
1649 1650
      invariant { l2i !high <= radix - power 2 (l2i cnt) }
      label StartLoop in
1651 1652
      xp := C.incr !xp minus_one;
      low := C.get !xp;
1653
      let l,h = lsld_ext !low cnt in
1654
      assert { l2i !high + l2i h < radix  };
1655 1656 1657
      let ghost v = Limb.(+) !high h in
      value_sub_update_no_change (pelts r) (!rp).offset (r.offset + 1+ p2i !i)
                                 (r.offset + p2i sz) v;
1658 1659
      C.set !rp (Limb.(+) !high h);
      rp := C.incr !rp minus_one;
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1660
      high := l;
1661
      let ghost k = p2i !i in
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1662
      i := Int32.(-) !i one;
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
      value_sub_head (pelts r) (r.offset + k) (r.offset + p2i sz);
      value_sub_head (pelts !xp) (!xp).offset (x.offset + p2i sz);
      assert { radix
               * value_sub (pelts r) (r.offset + k) (r.offset + p2i sz)
               + (power radix (p2i sz - p2i !i)) * l2i retval + l2i !high
              = value_sub_shift !xp (p2i sz - p2i !i)
                * (power 2 (l2i cnt))
             by
               l2i (pelts r)[r.offset + k]
             = l2i (pelts r)[(!rp.offset at StartLoop)]
             = l2i (!high at StartLoop) + l2i h
             so
                power radix (p2i sz - p2i !i)
              = power radix (p2i sz - (k - 1))
              = power radix ((p2i sz - k) +1)
              = radix * power radix (p2i sz - k)
             so
              l2i !low = l2i (pelts x)[(!xp).offset]
             so
               radix * value_sub (pelts r) (r.offset + k) (r.offset + p2i sz)
                + (power radix (p2i sz - p2i !i)) * l2i retval + l2i !high
             = radix * value_sub (pelts r) (r.offset + k) (r.offset + p2i sz)
                + radix * (power radix (p2i sz - k)) * l2i retval + l2i !high
             = radix * ( l2i (pelts r)[r.offset + k]
                          + radix * (value_sub (pelts r)
                                         (r.offset + 1 + k) (r.offset + p2i sz)))
               + radix * (power radix (p2i sz - k)) * l2i retval + l2i !high
             =  radix * ( l2i (!high at StartLoop) + l2i h
                          + radix * (value_sub (pelts r)
                                         (r.offset + 1 + k) (r.offset + p2i sz)))
               + radix * (power radix (p2i sz - k)) * l2i retval + l2i !high
             = radix * ( l2i (!high at StartLoop)
                          + radix * (value_sub (pelts r)
                                         (r.offset + 1 + k) (r.offset + p2i sz)))
               + radix * l2i h
               + radix * (power radix (p2i sz - k)) * l2i retval + l2i !high
             = radix * ( l2i (!high at StartLoop)
                          + radix * (value_sub (pelts r)
                                         (r.offset + 1 + k) (r.offset + p2i sz)))
               + radix * l2i h
               + radix * (power radix (p2i sz - k)) * l2i retval + l2i l
             = radix * ( l2i (!high at StartLoop)
                          + radix * (value_sub (pelts r)
                                         (r.offset + 1 + k) (r.offset + p2i sz)))
               + radix * (power radix (p2i sz - k)) * l2i retval + l2i l
               + radix * l2i h
             = radix * ( l2i (!high at StartLoop)
                          + radix * (value_sub (pelts r)
                                         (r.offset + 1 + k) (r.offset + p2i sz)))
               + radix * (power radix (p2i sz - k)) * l2i retval
               + (power 2 (l2i cnt)) * l2i !low
             = radix * ( l2i (!high at StartLoop)
                          + radix * (value_sub (pelts r)
                                         (r.offset + 1 + k) (r.offset + p2i sz)))
               + radix * (power radix (p2i sz - k)) * l2i retval
               + (power 2 (l2i cnt)) * l2i (pelts x)[(!xp).offset]
             = radix * ( l2i (!high at StartLoop)
                          + radix * (value_sub (pelts r)
                                         (r.offset + 1 + k) (r.offset + p2i sz))
                          + power radix (p2i sz - k) * l2i retval )
               + (power 2 (l2i cnt)) * l2i (pelts x)[(!xp).offset]
             = radix * ( radix * (value_sub (pelts r)
                                         (r.offset + 1 + k) (r.offset + p2i sz))
                         + power radix (p2i sz - k) * l2i retval
                         + l2i (!high at StartLoop) )
               + (power 2 (l2i cnt)) * l2i (pelts x)[(!xp).offset]
             = radix * value_sub_shift (!xp at StartLoop) (p2i sz - k)
                     * (power 2 (l2i cnt))
               + (power 2 (l2i cnt)) * l2i (pelts x)[(!xp).offset]
             = (power 2 (l2i cnt)) *
                      (l2i (pelts x)[(!xp).offset]
                      + radix * value_sub_shift (!xp at StartLoop) (p2i sz - k))
             = (power 2 (l2i cnt)) * value_sub_shift !xp (p2i sz - p2i !i)
   };
Raphaël Rieu-Helft's avatar
Raphaël Rieu-Helft committed
1737
   done;
1738 1739 1740 1741 1742 1743 1744
   assert { l2i !high + radix * value_sub (pelts r) (r.offset + 1) (r.offset + p2i sz)
                  + (power radix (p2i sz)) * l2i retval
                = value_sub_shift !xp (p2i sz)
                  * (power 2 (l2i cnt)) };
   value_sub_head (pelts r) r.offset (r.offset + p2i sz);
   value_sub_update_no_change (pelts r) r.offset (r.offset+1)
                              (r.offset + p2i sz) !high;
1745
   C.set r !high;
1746 1747
   retval

1748 1749 1750 1751 1752 1753 1754 1755 1756
  (** [rshift r x sz cnt] shifts [(x,sz)] [cnt] bits to the right and
      writes the result in [(r, sz)]. Returns the [cnt] least significant
      bits of [(x, sz)]. Corresponds to [mpn_rshift]. *)
  (*TODO overlapping allowed if r <= x*)
  let rshift (r x:t) (sz:int32) (cnt:uint32) : limb
    requires { valid_ptr_itv x (p2i sz) }
    requires { valid_ptr_itv r (p2i sz) }
    requires { 0 < l2i cnt < 32 }
    requires { 0 < p2i sz }
1757
    ensures { l2i result + radix * value_sub_shift r (p2i sz)
1758
              = value_sub_shift x (p2i sz) * (power 2 (32- l2i cnt)) }
1759
  =
1760 1761 1762 1763 1764 1765 1766
    let tnc = UInt32.(-) (UInt32.of_int 32) cnt in
    let limb_zero = Limb.of_int 0 in
    let zero = Int32.of_int 0 in
    let one = Int32.of_int 1 in
    let msb = Int32.(-) sz one in
    let xp = ref x in
    let rp = ref r in
1767
    let high = ref (C.get !xp) in
1768 1769 1770 1771 1772 1773
    let retval, h = lsld_ext !high tnc in
    let low = ref h in
    let i = ref zero in
    while (Int32.(<) !i msb) do
      variant { p2i sz - p2i !i }
      invariant { 0 <= p2i !i <= p2i msb }
1774
      invariant { l2i retval + radix * (value_sub_shift r (p2i !i)
1775
                  + (power radix (p2i !i)) * l2i !low)
1776
                  = value_sub_shift x (1 + p2i !i) * (power 2 (l2i tnc)) }
1777 1778 1779 1780 1781 1782 1783 1784 1785
      invariant { (!rp).offset = r.offset + p2i !i }
      invariant { (!xp).offset = x.offset + p2i !i }
      invariant { plength !rp = plength r }
      invariant { plength !xp = plength x }
      invariant { pelts !rp = pelts r }
      invariant { pelts !xp = pelts x }
      invariant { l2i !low < power 2 (l2i tnc) }
      label StartLoop in
      let ghost k = p2i !i in
1786 1787
      xp := C.incr !xp one;
      high := C.get !xp;
1788 1789 1790 1791
      let l,h = lsld_ext !high tnc in
      assert { l2i !low + l2i l < radix };
      let ghost v = Limb.(+) !low l in
      value_sub_shift_no_change (pelts r) r.offset (p2i !i) (p2i !i) v;
1792
      C.set !rp (Limb.(+) !low l);
1793
      assert { value_sub_shift r k = value_sub_shift (r at StartLoop) k };
1794
      rp := C.incr !rp one;
1795 1796 1797 1798
      low := h;
      i := Int32.(+) !i one;
      value_sub_tail (pelts r) r.offset (r.offset + k);
      value_sub_tail (pelts r) x.offset (x.offset + p2i !i);
1799
      assert { l2i retval + radix * (value_sub_shift r (p2i !i)
1800
                 + (power radix (p2i !i)) * l2i !low)
1801
                 = value_sub_shift x (1 + p2i !i) * (power 2 (l2i tnc))
1802 1803 1804 1805 1806 1807 1808
               by
                 l2i (pelts r)[r.offset + k]
               = l2i (pelts r)[(!rp.offset at StartLoop)]
               = l2i (!low at StartLoop) + l2i l
               so
                 l2i !high = l2i (pelts x)[(!xp).offset]
               so
1809
                 l2i retval + radix * (value_sub_shift r (p2i !i)
1810
                                      + (power radix (p2i !i)) * l2i !low)
1811
               = l2i retval + radix * (value_sub_shift r k
1812 1813 1814 1815 1816 1817
                              + power radix k * l2i (pelts r)[r.offset+k]
                              + power radix (p2i !i) * l2i !low)
               = l2i retval + radix * (value_sub_shift r k
                              + power radix k * (l2i (!low at StartLoop) + l2i l)
                              + power radix (p2i !i) * l2i !low)
               = l2i retval + radix * (value_sub_shift r k
1818
                              + power radix k * l2i (!low at StartLoop)
1819 1820 1821 1822
                              + power radix k * l2i l
                              + power radix (p2i !i) * l2i !low)
               = l2i retval + radix * (value_sub_shift r k
                              + power radix k * l2i (!low at StartLoop))
1823
                 + radix * (power radix k * l2i l
1824 1825
                            + power radix (p2i !i) * l2i !low)
               = value_sub_shift x (k+1) * power 2 (l2i tnc)
1826
                 + radix * (power radix k * l2i l
1827 1828
                            + power radix (p2i !i) * l2i !low)
               = value_sub_shift x (p2i !i) * power 2 (l2i tnc)
1829
                 + radix * (power radix k * l2i l
1830 1831
                            + power radix (p2i !i) * l2i !low)
               = value_sub_shift x (p2i !i) * power 2 (l2i tnc)
1832
                 + radix * (power radix k * l2i l
1833 1834 1835 1836 1837 1838 1839 1840 1841
                            + power radix k * radix * l2i !low)
               = value_sub_shift x (p2i !i) * power 2 (l2i tnc)
                 + radix * (power radix k * (l2i l + radix * l2i !low))
               = value_sub_shift x (p2i !i) * power 2 (l2i tnc)
                 + radix * (power radix k * l2i !high * power 2 (l2i tnc))
               = value_sub_shift x (p2i !i) * power 2 (l2i tnc)
                 + power radix (p2i !i) * l2i !high * power 2 (l2i tnc)
               = (value_sub_shift x (p2i !i) + power radix (p2i !i) * l2i !high)
                 * power 2 (l2i tnc)
1842
               = (value_sub_shift x (p2i !i)
1843 1844 1845 1846 1847 1848
                  + power radix (p2i !i) * l2i (pelts x)[x.offset + p2i !i])
                 * power 2 (l2i tnc)
               = value_sub_shift x (1 + p2i !i) * power 2 (l2i tnc)
      };
    done;
    label EndLoop in
1849
    assert { l2i retval + radix * (value_sub_shift r (p2i msb)
1850 1851 1852 1853 1854
                  + (power radix (p2i msb)) * l2i !low)
             = value_sub_shift x (p2i sz) * (power 2 (l2i tnc)) };
    value_sub_tail (pelts r) r.offset (r.offset + p2i msb);
    assert { (!rp).offset = r.offset + p2i msb };
    value_sub_shift_no_change (pelts r) r.offset (p2i sz) (p2i sz) !low;
1855
    C.set !rp !low;
1856
    assert { value_sub_shift r (p2i sz)
1857
           = value_sub_shift r (p2i msb) + power radix (p2i msb) * l2i !low };
1858 1859 1860
    assert { value_sub_shift r (p2i sz)
           = value_sub_shift (r at EndLoop) (p2i msb)
             + power radix (p2i msb) * l2i !low
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
           by
           value_sub_shift r (p2i msb) = value_sub_shift (r at EndLoop) (p2i msb)
           };
    retval

  (** {2 Division} *)

  (** Based on Niels Möller and Torbjörn Granlund, “Improved
      division by invariant integers” 2010 *)

1871
  use import int.MinMax
1872 1873

  val invert_limb (d:limb) : limb
1874
    requires { l2i d >= div radix 2 }
1875 1876 1877 1878
    ensures { l2i result = (div (radix*radix - 1) (l2i d)) - radix }


  let div2by1_inv (uh ul d v:limb) : (limb,limb)
1879
    requires { l2i d >= div radix 2 }
1880 1881 1882 1883 1884 1885 1886
    requires { l2i uh < l2i d }
    requires { l2i v = (div (radix*radix - 1) (l2i d)) - radix }
    returns { q,r -> l2i q * l2i d + l2i r = l2i ul + radix * l2i uh }
    returns { _q, r -> 0 <= l2i r < l2i d }
  =
    let zero = Limb.of_int 0 in
    let one = Limb.of_int 1 in
1887
    let ghost k = radix * radix - (radix + l2i v) * l2i d in
1888
    let ghost u = l2i ul + radix * l2i uh in
1889
    assert { 1 <= k <= l2i d };
1890
    let l,h = mul_double v uh in
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
    let sl,c = add_with_carry l ul zero in
    let sh,c' = add_with_carry uh h c in  (* <c',sh,sl> = <uh, ul> + <h,l> *)
    assert { l2i sl + radix * l2i sh + radix * radix * l2i c'
    	     = l2i l + radix * l2i h + l2i ul + radix * l2i uh };
    assert { l2i c' = 0
    	     by
	     l2i uh < l2i d
	     so l2i v * l2i uh <= l2i v * l2i d
	     so k = radix * radix - (radix + l2i v) * l2i d
	     	  = radix * radix - radix * l2i d - l2i v * l2i d
             so l2i v * l2i d = radix * radix - radix * l2i d - k
	     	      	      = radix * (radix - l2i d) - k
	     so k > 0
	     so l2i v * l2i d < radix * (radix - l2i d)
	     so l2i v * l2i uh < radix * (radix - l2i d)
	     so l2i l + radix * l2i h = l2i v * l2i uh
	     so l2i l + radix * l2i h < radix * (radix - l2i d)
	     so l2i uh <= l2i d - 1
	     so radix * l2i uh <= radix * (l2i d - 1) = radix * l2i d - radix
	     so l2i l + radix * l2i h + radix * l2i uh
	     	< radix * (radix - l2i d) + radix * l2i uh
	     	<= radix * (radix - l2i d) + radix * l2i d - radix
		<= radix * (radix - l2i d + l2i d) - radix = radix * radix - radix
             so l2i ul < radix
	     so l2i l + radix * l2i h + l2i ul + radix * l2i uh
	     	= l2i l + radix * l2i h + radix * l2i uh + l2i ul
		< radix * radix - radix + l2i ul
		< radix * radix - radix + radix = radix * radix
	     so l2i sl + radix * l2i sh + radix * radix * l2i c'
	     	= l2i l + radix * l2i h + l2i ul + radix * l2i uh
		< radix * radix
             so radix * radix * l2i c' <= l2i sl + radix * l2i sh + radix * radix * l2i c'
	     so radix * radix * l2i c' < radix * radix
     };
    assert { l2i sl + radix * l2i sh = l2i l + radix * l2i h + l2i ul + radix * l2i uh
    	     = l2i v * l2i uh + l2i ul + radix * l2i uh
	     = l2i ul + (radix + l2i v) * l2i uh };
    let qh = ref sh in
1929
    let ql = ref sl in
1930 1931 1932
    let ghost q0 = l2i !ql in
    let ghost cq = l2i sh + 1 in (*candidate quotient*)
    let ghost cr = l2i ul - cq * l2i d + radix * l2i uh in (*candidate remainder*)
1933
    assert { cq * l2i d + cr = u};
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946