powm.mlw 71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 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 482 483 484 485 486 487 488 489 490 491 492 493 494 495 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 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 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 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 1014 1015 1016 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 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 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 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 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 1298 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 1349 1350 1351 1352 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 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
module Powm

  use array.Array
  use map.Map
  use mach.c.C
  use ref.Ref
  use mach.int.Int32
  use mach.int.UInt32GMP
  use import mach.int.UInt64GMP as Limb
  use int.Int
  use int.Power
  use types.Types
  use lemmas.Lemmas
  use compare.Compare
  use valuation.Valuation
  use util.Util
  use add.Add
  use sub.Sub
  use mul.Mul
  use logical.Logical
  use div.Div
  use toom.Toom
  use int.EuclideanDivision

  predicate redc (ur u n m : int) =
    mod ur m = mod (power radix n * u) m

  let lemma mod_mul (a b c d m:int)
    requires { mod a m = mod b m }
    requires { 0 < m }
    requires { mod c m = mod d m }
    ensures  { mod (c * a) m = mod (d * b) m }
  =
    let da = div a m in
    let db = div b m in
    let ma = mod a m in
    assert { a = m * da + ma };
    assert { b = m * db + ma };
    assert { c * a = m * (c * da) + (c * ma) /\
             d * b = m * (d * db) + (d * ma) };
    assert { mod (c * a) m = mod (m * (c * da) + (c * ma)) m = mod (c * ma) m };
    assert { mod (d * b) m = mod (m * (d * db) + (d * ma)) m = mod (d * ma) m };
    let dc = div c m in
    let dd = div d m in
    let mc = mod c m in
    assert { c * ma = m * (dc * ma) + ma * mc /\
             d * ma = m * (dd * ma) + ma * mc };
    assert { mod (c * ma) m = mod (m * (dc * ma) + (ma * mc)) m
                            = mod (ma * mc) m };
    assert { mod (d * ma) m = mod (m * (dd * ma) + (ma * mc)) m
                            = mod (ma * mc) m }

  let lemma mod_sum (a b m:int)
    requires { 0 < m }
    ensures  { mod (a + b) m = mod (mod a m + mod b m) m }
  = let da = div a m in
    let db = div b m in
    let ma = mod a m in
    let mb = mod b m in
    assert { mod (a+b) m = mod (m * (da + db) + (ma + mb)) m = mod (ma + mb) m }

  let lemma mod_id (a m:int)
    requires { 0 <= a < m }
    ensures { mod a m = a }
  = ()

  let lemma mod_prod (a b c:int)
    requires { 0 < b }
    requires { 0 < c }
    ensures { c * mod a b = mod (a * c) (b * c) }
  = let d = div a b in
    let m = mod a b in
    assert { a * c = (d * b + m) * c = (b * c) * d + m * c };
    assert { mod (a * c) (b * c) = mod ((b * c) * d + m * c) (b * c)
             = mod (m * c) (b * c) = m * c }
  meta remove_prop axiom mod_prod

  let lemma unredc_inv (u1 ur u2 n m invpn : int)
    requires { redc ur u1 n m }
    requires { redc ur u2 n m }
    requires { 0 < m }
    requires { odd m }
    requires { mod (invpn * power radix n) m = 1 }
    ensures  { mod u1 m = mod u2 m }
  = assert { mod (power radix n * u1) m = mod (power radix n * u2) m };
    assert { mod (invpn * (power radix n * u1)) m
             = mod (invpn * (power radix n * u2)) m };
    assert { mod ((invpn * power radix n) * u1) m = mod (1 * u1) m };
    assert { mod ((invpn * power radix n) * u2) m = mod (1 * u2) m }

  let lemma unredc (u1 ur u2 n m : int)
    requires { redc ur u1 n m }
    requires { redc ur u2 n m }
    requires { 0 < m }
    requires { 0 < n }
    requires { odd m }
    ensures  { mod u1 m = mod u2 m }
  = let rec lemma mod_pow (a b k m:int)
      requires { mod a m = mod b m }
      requires { 0 < k }
      requires { 0 < m }
      ensures  { mod (power a k) m = mod (power b k) m }
      variant  { k }
    = if k = 1 then ()
      else begin
        mod_pow a b (k-1) m;
        mod_mul (power a (k-1)) (power b (k-1)) a b m;
      end
    in
    let rec lemma pow_1 (k:int)
      requires { 0 <= k }
      variant  { k }
      ensures  { power 1 k = 1 }
    = if k > 0 then pow_1 (k-1) in
    let hm = div (m+1) 2 in
    assert { mod (2 * hm) m = 1
             by 2 * hm = m + 1
             so mod (2 * hm) m = mod (m * 1 + 1) m = 1 };
    let hm64 = power hm 64 in
    mod_pow (2*hm) 1 64 m;
    pow_1 64;
    assert { mod (radix * hm64) m = 1
             by radix * hm64 = (power 2 64) * (power hm 64) = power (2 * hm) 64
             so mod (power 1 64) m = mod 1 m = 1 };
    let hm64n = power hm64 n in
    mod_pow (radix * hm64) 1 n m;
    pow_1 n;
    assert { mod ((power radix n) * hm64n) m = 1
             by power radix n * hm64n = power (radix * hm64) n
             so mod (power 1 n) m = mod 1 m = 1 };
    unredc_inv u1 ur u2 n m hm64n

  meta remove_prop axiom unredc_inv
  meta remove_prop axiom unredc

  let wmpn_redc_1 (rp up mp : t) (n: int32) (invm : limb)
    requires { n > 0 }
    requires { valid mp n /\ valid up (2 * n) /\ valid rp n }
    requires { odd (value mp n) }
    requires { mod ((value mp n) * invm) radix = radix - 1 }
    ensures  { redc (value (old up) (2*n)) (value rp n) n (value mp n) }
    ensures  { forall j. j < offset rp \/ j >= offset rp + n
                         -> (pelts rp)[j] = (pelts (old rp))[j] }
    ensures  { value (old up) (2*n) < power radix n * value mp n
               -> value rp n < 2 * value mp n }
  =
    label Start in
    let ref cy = 0 in
    let ref u = C.incr up 0 in
    let ghost vm = value mp (int32'int n) in
    value_sub_head (pelts mp) (offset mp) (offset mp + int32'int n);
    assert { mod ((pelts mp)[offset mp] * invm) radix = radix - 1
             by radix - 1 = mod (vm * invm) radix
                = mod (radix *
                   (value_sub (pelts mp) (offset mp + 1) (offset mp + n) * invm)
                       + (pelts mp)[offset mp] * invm) radix
                = mod ((pelts mp)[offset mp] * invm) radix };
    let ghost ref added : int = 0 in
    for j = 0 to n-1 do
      invariant { offset u = offset up + j }
      invariant { pelts u = pelts up }
      invariant { u.min = up.min }
      invariant { u.max = up.max }
      invariant { plength u = plength up }
      (*invariant { mod (value u n + value up j) (power radix j) = 0 }*)
      invariant { power radix j * value u (n+n-j) + power radix n * value up j
                  = value (old up) (n+n) + vm * added }
      invariant { 0 <= added < power radix j }
      invariant { mod (power radix j * value u (n + n - j)
                      + power radix n * value up j) vm =
                  mod (value (old up) (n+n)) vm }
      let m = mul_mod (C.get u) invm in
      let ghost nnj = n + n - j in
      let ghost m0 = uint64'int (pelts mp)[offset mp] in
      let ghost u0 = uint64'int (pelts u)[offset u] in
      mod_mul (uint64'int m) (u0 * uint64'int invm) m0 m0 radix;
      assert { mod (m * m0) radix
               = mod ((u0 * invm) * m0) radix };
      assert { mod ((u0 * invm) * m0) radix
               = mod (- u0) radix
               by let d = div (invm * m0) radix in
                  invm * m0 = d * radix + radix - 1
               so mod ((u0 * invm) * m0) radix
                  = mod (u0 * (d * radix + radix - 1)) radix
                  = mod (radix * (u0 * d + u0) - u0) radix
                  = mod (- u0) radix };
      assert { mod (u0 + m * m0) radix = 0
               by mod (m * m0) radix
                  = mod (- u0) radix
               so let dm = div (m * m0) radix in
                  let du = div (-u0) radix in
                  m * m0 = - u0 + radix * (dm - du)
               so mod (u0 + m * m0) radix = mod (radix * (dm - du) + 0) radix
                  = 0 };
      label Addmul in
      let ghost oup = pure { up } in
      value_sub_head (pelts u) (offset u) (offset u + int32'int nnj);
      assert { u0 = mod (value u nnj) radix
               by value u nnj = radix *
                   (value_sub (pelts u) (offset u + 1) (offset u + nnj)) + u0 };
      value_concat u n nnj;
      cy <- wmpn_addmul_1 u mp n m;
      value_concat u n nnj;
      value_sub_frame (pelts up) (pelts oup) (offset u + int32'int n)
                                           (offset u + int32'int nnj);
      assert { value u nnj + power radix n * cy = value u nnj at Addmul + vm * m
               by value_sub (pelts u) (offset u + n) (offset u + nnj)
                  = value_sub (pelts u) (offset u + n) (offset u + nnj)
                    at Addmul
               so value u nnj = value u n + power radix n * value_sub (pelts u)
                                                (offset u + n) (offset u + nnj)
               so value u nnj at Addmul
                  = value u n at Addmul
                    + power radix n * value_sub (pelts u)
                                        (offset u + n) (offset u + nnj) };
      value_sub_head (pelts u) (offset u) (offset u + int32'int nnj);
      value_sub_head (pelts mp) (offset mp) (offset mp + int32'int n);
      assert { vm = m0 + radix * value_sub (pelts mp) (offset mp + 1)
                                                      (offset mp + n) };
      assert { (pelts u)[offset u] = 0
                by value u nnj = radix * (value_sub (pelts u) (offset u + 1)
                                                  (offset u + nnj))
                                 + (pelts u)[offset u]
                so (pelts u)[offset u] = mod (value u nnj) radix
                so value u nnj = value u nnj at Addmul + m * vm
                               - power radix n * cy
                             = radix * (power radix (n-1) *(- cy))
                               + (value u nnj at Addmul + m * vm)
                so mod (m * vm) radix
                   = mod (radix * m * value_sub (pelts mp) (offset mp + 1)
                                                           (offset mp + n)
                          + m * m0) radix
                   = mod (m * m0) radix
                so mod (value u nnj) radix
                   = mod (radix * (power radix (n-1) *(- cy))
                          + (value u nnj at Addmul + m * vm)) radix
                   = mod (value u nnj at Addmul + m * vm) radix
                   = mod (mod (value u nnj at Addmul) radix
                          + mod (m * vm) radix)radix
                   = mod (u0 + mod (m * m0) radix) radix
                   = mod (mod u0 radix + mod (m * m0) radix) radix
                   = mod (mod (u0 + m * m0) radix) radix
                   = 0 };
      value_sub_head (pelts u) (offset u) (offset u + int32'int nnj);
      assert { value u nnj = radix * value_sub (pelts u) (offset u + 1)
                                                       (offset u + nnj) };
      value_sub_frame (pelts up) (pelts oup) (offset up)
                               (offset up + int32'int j);
      assert { value up j = value up j at Addmul };
      label Carry in
      value_sub_update_no_change (pelts up) (up.offset + int32'int j) up.offset
                                            (up.offset + int32'int j) cy;
      value_sub_update_no_change (pelts up) (up.offset + int32'int j)
                                        (up.offset + int32'int j + 1)
                                      (up.offset + int32'int nnj) cy;
      C.set u cy;
      added <- added + uint64'int m * power radix (int32'int j);
      assert { added < power radix (j+1)
               by m <= radix - 1
               so added at Carry < power radix j
               so added <= added at Carry + (radix - 1) * power radix j
                  < power radix j + (radix - 1) * power radix j
                  = radix * power radix j = power radix (j+1) };
      value_tail up j;
      assert { value up (j+1) = value up j at Addmul + power radix j * cy };
      label Incr in
      u <- C.incr u 1;
      assert { radix * value u (n + n - (j + 1))
               = radix * (value_sub (pelts u) (offset u + 1)
                                   (offset u + nnj) at Incr)
               = radix * (value_sub (pelts u) (offset u + 1)
                                   (offset u + nnj) at Carry)
               = value (u at Carry) nnj };
      assert {  power radix (j+1) * value u (n + n - (j + 1))
                 + power radix n * value up (j+1)
               = vm * (power radix j * m)
                   + ((power radix j * value u nnj + power radix n * value up j)
                    at Addmul)
               by
               power radix (j+1) * value u (n + n - (j + 1))
                 + power radix n * value up (j+1)
               = power radix j * (radix * value u (n + n - (j + 1)))
                 + power radix n * value up (j+1)
               = power radix j * (value u nnj at Addmul + vm * m
                                 - power radix n * cy)
                 + power radix n * (value up j at Addmul + power radix j * cy)
               = (power radix j * value u nnj + power radix n * value up j)
                    at Addmul
                 + vm * (power radix j * m) };
      assert { 0 <= added
               by 0 <= added at Carry so 0 <= m
               so 0 <= m * power radix j };
      assert { mod (power radix (j+1) * value u (n + n - (j + 1))
                    + power radix n * value up (j+1)) vm
               = mod (vm * (power radix j * m)
                      + ((power radix j * value u nnj
                        + power radix n * value up j) at Addmul)) vm
               = mod (power radix j * value u nnj
                      + power radix n * value up j) vm at Addmul };
    done;
    let u' = C.incr u (-n) in
    assert { pelts u' = pelts up /\ offset u' = offset up };
    cy <- wmpn_add_n rp u (C.incr u (-n)) n;
    assert { mod (power radix n * (value rp n + power radix n * cy)) vm
             = mod (value (old up) (n+n)) vm
             by power radix n * (value rp n + power radix n * cy)
                = power radix n * value u n + power radix n * value up n
             so mod (power radix n * value u n + power radix n * value up n) vm
                = mod (value (old up) (n+n)) vm };
    assert { redc (value (old up) (2 * n))
                  (value rp n + power radix n * cy)
                  n vm };
    assert { power radix n * (value rp n + power radix n * cy)
             = value (old up) (2*n) + vm * added };
    assert { value rp n + power radix n * cy < vm + power radix n
             by value (old up) (2 * n) < power radix n * power radix n
             so added < power radix n
             so vm * added < vm * power radix n
             so power radix n * (value rp n + power radix n * cy)
                = value (old up) (2 * n) + vm * added
                < power radix n * power radix n + vm * power radix n
                = power radix n * (vm + power radix n) };
    assert { value (old up) (2 * n) < power radix n * vm
             -> (value rp n + power radix n * cy < 2 * vm)
             by value (old up) (2 * n) < power radix n * vm
             so added < power radix n
             so vm * added < vm * power radix n
             so power radix n * (value rp n + power radix n * cy)
                = value (old up) (2 * n) + vm * added
                < vm * power radix n + vm * power radix n
                = power radix n * (2 * vm) };
    label Sub in
    begin
      ensures { mod (value rp n) vm
                = old mod (value rp n + power radix n * cy) vm }
      ensures { forall j. j < offset rp \/ j >= offset rp + n
                           -> (pelts rp)[j] = (pelts (old rp))[j] }
      ensures { value (up at Start) (2 * n) < power radix n * vm
                -> (value rp n < 2 * vm) }
    if cy <> 0
    then begin
      assert { cy = 1 };
      let ghost b = wmpn_sub_in_place rp n mp n in
      assert { value rp n + power radix n * (cy - b)
               = (value rp n at Sub + power radix n * cy) - vm };
      assert { b = 1
               by value rp n + power radix n * (cy - b)
               = (value rp n at Sub + power radix n * cy) - vm
               < power radix n
               so cy - b < 1 };
      assert { mod (value rp n) vm
               = mod (value rp n at Sub + power radix n * cy) vm
               by value rp n = (value rp n at Sub + power radix n * cy) - vm
               so mod (value rp n) vm
                  = mod (vm * -1 + (value rp n at Sub + power radix n * cy)) vm
                  = mod (value rp n at Sub + power radix n * cy) vm }
      end
    end;
    assert { mod (power radix n * value rp n) vm
             = mod (power radix n * (value rp n at Sub + power radix n * cy))
                   vm };
    assert { redc (value (old up) (2 * n))
                  (value rp n) n vm }

  (* TODO table like sqrt1 *)
  val binvert_limb_table (n:limb) : limb
    requires { 0 <= n < 128 }
    ensures { 0 <= result < 256 }
    ensures { mod (result * (2 * n + 1)) (power 2 8) = 1 }

  let binvert_limb (n:limb) : limb
    requires { odd n }
    ensures { mod (result * n) radix = 1 }
  =
    let lemma double_prec (n inv:int) (prec:int)
      requires { 0 <= n /\ 0 <= inv }
      requires { mod (inv * n) (power 2 prec) = 1 }
      requires { 0 <= 2 * prec <= Limb.length }
      ensures  { let inv' = mod (2 * inv - inv * (inv * n)) radix in
                 mod (inv' * n) (power 2 (2 * prec)) = 1 }
    =
      let d = div (inv * n) (power 2 prec) in
      assert { inv * n = d * power 2 prec + 1 };
      let inv' = 2 * inv - inv * (inv * n) in
      assert { inv' = 2 * inv - inv * (d * power 2 prec + 1)
                    = inv * (1 - d * power 2 prec) };
      assert { inv' * n = inv * n * (1 - d * power 2 prec)
                 = (d * power 2 prec + 1) * (1 - d * power 2 prec)
                 = 1 - d * d * (power 2 prec * power 2 prec)
                 = 1 - d * d * (power 2 (2 * prec))
                 = power 2 (2 * prec) * (-1 * d * d) + 1 };
      assert { mod (inv' * n) (power 2 (2 * prec)) = 1
               by mod (inv' * n) (power 2 (2 * prec))
                  = mod (power 2 (2 * prec) * (-1 * d * d) + 1)
                    (power 2 (2 * prec))
                  = 1};
      let inv'' = mod inv' radix in
      let d' = div inv' radix in
      assert { inv' = d' * radix + inv''
                    = d' * power 2 Limb.length + inv''
                    = d' * (power 2 (Limb.length - 2 * prec)
                        * power 2 (2 * prec))
                      + inv'' };
      assert { mod (inv' * n) (power 2 (2 * prec))
               = mod (inv'' * n) (power 2 (2 * prec))
               by mod (inv' * n) (power 2 (2 * prec))
                  = mod (power 2 (2 * prec)
                         * ((d' * power 2 (Limb.length - 2 * prec)) * n)
                         + inv'' * n) (power 2 (2 * prec))
                  = mod (inv'' * n) (power 2 (2 * prec)) }
    in
    let h = (n/2) % 128 in
    assert { power 2 8 = 256
             by power 2 2 = 4
             so power 2 4 = 16
             so power 2 8 = power 2 4 * power 2 4 };
    assert { 2 * h + 1 = mod n 256 };
    let ref inv = binvert_limb_table h in
    assert { mod (inv * n) (power 2 8) = 1 };
    label P8 in
    double_prec (uint64'int n) (uint64'int inv) 8;
    inv <- sub_mod (mul_mod 2 inv) (mul_mod inv (mul_mod inv n));
    assert { inv =
             mod (2 * (inv at P8) - (inv at P8) * ((inv at P8) * n)) radix
             by mod (2 * (inv at P8) - (inv at P8) * ((inv at P8) * n)) radix
                = mod (mod (2 * (inv at P8)) radix
                      - mod ((inv at P8) * ((inv at P8) * n)) radix) radix
                = mod (mod (2 * (inv at P8)) radix
                      - mod ((inv at P8) * mod ((inv at P8) * n) radix) radix)
                      radix
                = inv };
    assert { mod (inv * n) (power 2 16) = 1 };
    label P16 in
    double_prec (uint64'int n) (uint64'int inv) 16;
    inv <- sub_mod (mul_mod 2 inv) (mul_mod inv (mul_mod inv n));
    assert { inv =
             mod (2 * (inv at P16) - (inv at P16) * ((inv at P16) * n)) radix
             by mod (2 * (inv at P16) - (inv at P16) * ((inv at P16) * n)) radix
                = mod (mod (2 * (inv at P16)) radix
                      - mod ((inv at P16) * ((inv at P16) * n)) radix) radix
                = mod (mod (2 * (inv at P16)) radix
                      - mod ((inv at P16) * mod ((inv at P16) * n) radix) radix)
                      radix
                = inv };
    assert { mod (inv * n) (power 2 32) = 1 };
    label P32 in
    double_prec (uint64'int n) (uint64'int inv) 32;
    inv <- sub_mod (mul_mod 2 inv) (mul_mod inv (mul_mod inv n));
    assert { inv =
             mod (2 * (inv at P32) - (inv at P32) * ((inv at P32) * n)) radix
             by mod (2 * (inv at P32) - (inv at P32) * ((inv at P32) * n)) radix
                = mod (mod (2 * (inv at P32)) radix
                      - mod ((inv at P32) * ((inv at P32) * n)) radix) radix
                = mod (mod (2 * (inv at P32)) radix
                      - mod ((inv at P32) * mod ((inv at P32) * n) radix) radix)
                      radix
                = inv };
    assert { mod (inv * n) (power 2 64) = 1 };
    inv

  (* TODO rewrite this with array literal once they exist *)
  let win_size [@extraction:inline] (eb:int32) : int32
    ensures { 0 <= result <= 10 }
    ensures { eb > 0 -> result > 0 }
  = if eb = 0 then 0
    else if eb <= 7 then 1
    else if eb <= 25 then 2
    else if eb <= 81 then 3
    else if eb <= 214 then 4
    else if eb <= 673 then 5
    else if eb <= 1793 then 6
    else if eb <= 4609 then 7
    else if eb <= 11521 then 8
    else if eb <= 28161 then 9
    else 10

  let redcify [@extraction:inline] (rp up: t) (un: int32) (mp: t) (n: int32)
    requires { valid rp n /\ valid up un /\ valid mp n }
    requires { 1 <= n /\ 1 <= un }
    requires { un + n < max_int32 }
    requires { (pelts mp)[offset mp + n - 1] > 0 }
    ensures { value rp n = mod (power radix n * value up un) (value mp n) }
    ensures { redc (value rp n) (value up un) n (value mp n) }
  =
    let tp = salloc (UInt32.of_int32 (un + n)) in
    let qp = salloc (UInt32.of_int32 (un + 1)) in
    wmpn_zero tp n;
    label Copy in
    wmpn_copyi (C.incr tp n) up un;
    value_concat tp n (un+n);
    value_sub_frame (pelts tp) (pure { pelts tp at Copy })
                    (offset tp) (offset tp + int32'int n);
    assert { value tp (un + n) = power radix n * value up un };
    wmpn_tdiv_qr qp rp tp (un+n) mp n;
    assert { mod (power radix n * value up un) (value mp n)
             = mod (value tp (un + n)) (value mp n)
             = mod (value rp n) (value mp n)
             by value tp (un + n) = value mp n * value qp (un + 1)
                                    + value rp n };
    assert { 0 <= value rp n < value mp n };
    assert { mod (value rp n) (value mp n) = value rp n };

  function valueb (p:t) (nbits:int) : int
  =
    if nbits < 0 then 0 else
    let i = div nbits 64 in
    value p i
      + power radix i * mod ((pelts p)[offset p + i]) (power 2 (nbits - 64*i))

  let lemma valueb_lower_bound (p:t) (nbits:int)
    ensures { 0 <= valueb p nbits }
  = if nbits < 0 then ()
    else
      let i = div nbits 64 in
      value_sub_lower_bound (pelts p) (offset p) (offset p + i)

  let lemma valueb_upper_bound (p:t) (nbits:int)
    requires { 0 <= nbits }
    ensures { valueb p nbits < power 2 nbits }
  = if nbits < 0 then ()
    else
      let i = div nbits 64 in
      value_sub_upper_bound (pelts p) (offset p) (offset p + i);
      assert { valueb p nbits < power 2 nbits
               by valueb p nbits
                  = value p i
                    + power radix i
                      * mod ((pelts p)[offset p + i]) (power 2 (nbits - 64*i))
               so power radix i = power 2 (64 * i)
               so mod ((pelts p)[offset p + i]) (power 2 (nbits - 64*i))
                  <= power 2 (nbits - 64 * i) - 1
               so value p i < power 2 (64 * i)
               so valueb p nbits < power 2 (64 * i)
                                   * (1 + power 2 (nbits - 64 * i) - 1)
                                 = power 2 (64 * i + (nbits - 64 * i))
                                 = power 2 nbits }

  let getbit [@extraction:inline] (p:t) (ghost pn:int32) (bi:int32) : limb
    requires { valid p pn }
    requires { 1 <= bi }
    requires { pn >= (div (bi + 63) 64) }
    ensures  { 0 <= result <= 1 }
    ensures  { result = mod (div (value p pn) (power 2 (bi-1))) 2 }
    ensures  { valueb p bi = valueb p (bi-1) + power 2 (bi-1) * result }
  =
    let i = Int32.(/) (bi - 1) 64 in
    let mi = Limb.of_int32 (bi - 1) % 64 in
    assert { bi - 1 = 64 * i + mi };
    let lp = C.get_ofs p i in
    value_concat p i (i+1);
    value_concat p (i+1) pn;
    let ghost p' = C.incr p (i+1) in
    assert { value p pn = value p i + power radix i * lp
             + power radix (i+1) * value p' (pn - (i+1)) };
    let lps = lsr_mod lp mi in
    let ghost lpm = mod (uint64'int lp) (power 2 (uint64'int mi)) in
    assert { lp = lpm + power 2 mi * lps };
    assert { power radix i * power 2 mi = power 2 (bi-1)
             by power radix i = power (power 2 64) i = power 2 (64 * i)
             so power radix i * power 2 mi = power 2 (64 * i + mi) };
    assert { value p pn = value p i + power radix i * lpm + power 2 (bi-1) * lps
                          + power radix (i + 1) * value p' (pn - (i+1)) };
    assert { valueb p (bi-1) = value p i + power radix i * lpm
             by valueb p (bi-1)
                = value p i + power radix i
                  * mod ((pelts p)[offset p + i]) (power 2 (bi - 1 - 64 * i))
                = value p i + power radix i * lpm };
    let ghost res = lps % 2 in
    assert { value p pn = valueb p (bi-1) + power 2 (bi-1) * lps
                          + power radix (i+1) * value p' (pn - (i+1)) };
    let ghost dps = div (uint64'int lps) 2 in
    assert { lps = res + 2 * dps };
    assert { valueb p bi = valueb p (bi-1) + power 2 (bi-1) * res
             by bi = 64 * i + (mi+1)
             so (valueb p bi
                = value p i + power radix i * mod lp (power 2 (mi+1))
                by
                if mi < 63
                then
                  div bi 64 = i && mod bi 64 = mi + 1
                  so valueb p bi
                  = value p i + power radix i
                    * mod ((pelts p)[offset p + i]) (power 2 (bi - 64 * i))
                  = value p i + power radix i * mod lp (power 2 (bi - 64 * i))
                  = value p i + power radix i * mod lp (power 2 (mi+1))
                else
                  mi = 63
                  so div bi 64 = (i+1) && mod bi 64 = 0
                  so valueb p bi
                     = value p (i+1)
                       + power radix (i+1) *
                         mod ((pelts p)[offset p + i + 1]) (power 2 0)
                     = value p (i+1)
                  so mod lp (power 2 (mi+1))
                     = mod lp (power 2 64) = mod lp radix = lp
                  so value p (i+1) = value p i
                     + power radix i * mod lp (power 2 (mi + 1)))
             so lp = lpm + power 2 mi * (res + 2 * dps)
                   = power 2 (mi+1) * dps + lpm + power 2 mi * res
             so (0 <= lpm + power 2 mi * res < power 2 (mi+1)
                 by 0 <= lpm < power 2 mi
                 so 0 <= res <= 1
                 so 0 <= power 2 mi * res <= power 2 mi
                 so 0 <= lpm + power 2 mi * res < power 2 mi + power 2 mi
                      = power 2 (mi+1))
             so mod lp (power 2 (mi + 1))
                = mod (power 2 (mi + 1) * dps + (lpm + power 2 mi * res))
                      (power 2 (mi + 1))
                = mod (lpm + power 2 mi * res) (power 2 (mi + 1))
                = lpm + power 2 mi * res };
    assert { power radix (i+1) = power 2 (bi-1) * 2 * power 2 (64 - mi - 1)
             by power 2 (bi - 1) = power radix i * power 2 mi
             so power 2 (bi - 1) * 2 * power 2 (64 -  mi - 1)
                = power radix i * power 2 mi * power 2 1 * power 2 (64 - mi - 1)
                = power radix i * (power 2 (mi + 1) * power 2 (64 - mi - 1))
                = power radix i * power 2 (mi + 1 + (64 - mi - 1))
                = power radix i * power 2 64
                = power radix i * radix = power radix (i+1) };
    let ghost d = dps + power 2 (64 - uint64'int mi - 1)
                  * value p' (int32'int pn - (int32'int i+1)) in
    assert { value p pn = power 2 (bi-1) * (res + 2 * d) + valueb p (bi-1)
             by power 2 (bi - 1) * 2 = power 2 bi
             so power 2 (bi - 1) * lps
                = power 2 (bi - 1) * (res + 2 * dps)
             so value p pn
             = valueb p (bi-1) + power 2 (bi-1) * (res + 2 * dps)
                          + power radix (i+1) * value p' (pn - (i+1))
             = valueb p (bi-1) + power 2 (bi-1) * (res + 2 * dps)
               + power 2 (bi-1) * (2 * power 2 (64 - mi - 1)
                                * value p' (pn - (i+1)))
             = power 2 (bi-1) * (res + 2 * d) + valueb p (bi-1) };
    assert { mod (value p pn) (power 2 (bi-1))
             = mod (valueb p (bi-1)) (power 2 (bi-1))
             = valueb p (bi-1) };
    assert { div (value p pn) (power 2 (bi-1)) = res + 2 * d
             by valueb p (bi - 1) = mod (value p pn) (power 2 (bi-1))
             so power 2 (bi - 1) * div (value p pn) (power 2 (bi - 1))
                  + mod (value p pn) (power 2 (bi - 1))
                = value p pn
                = power 2 (bi - 1) * (res + 2 * d) + valueb p (bi - 1)
             so power 2 (bi - 1) * (res + 2 * d)
                = power 2 (bi - 1) * (div (value p pn) (power 2 (bi-1)))};
    assert { mod (div (value p pn) (power 2 (bi-1))) 2
             = mod (2 * d + res) 2 = mod res 2 = res };
    lps % 2

  let getbits [@extraction:inline] (p: t) (ghost pn: int32) (bi:int32)
                                                            (nbits:int32) : limb
    requires { 1 <= nbits < 64 }
    requires { 0 <= bi }
    requires { 1 <= pn }
    requires { valid p pn }
    requires { pn >= (div (bi + 63) 64) }
    ensures  { nbits <= bi ->
                 valueb p bi = valueb p (bi - nbits)
                                    + power 2 (bi - nbits) * result }
    ensures  { bi < nbits -> valueb p bi = result }
    ensures  { 0 <= result < power 2 nbits }
    ensures  { result = mod (div (value p pn) (power 2 (bi - nbits)))
                                                      (power 2 nbits)  }
  =
    if bi < nbits
    then (C.get p) % (lsl 1 (Limb.of_int32 bi))
    else
      let ghost obi = bi in
      label Start in
      let ref bi = bi - nbits in
      let ghost bni = bi in
      assert { bni = obi - nbits };
      let ref i = bi / 64 in
      bi <- bi % 64;
      assert { i < pn };
      let pr = get_ofs p i in
      let ghost p' = C.incr p (i+1) in
      value_concat p i (i+1);
      value_concat p (i+1) pn;
      assert { value p pn = value p i + power radix i * pr
               + power radix (i+1) * value p' (pn - (i+1)) };
      let ref r = lsr_mod pr (Limb.of_int32 bi) in
      let ghost prm = mod (uint64'int pr) (power 2 (int32'int bi)) in
      assert { pr = prm + power 2 bi * r };
      let nbits_in_r = 64 - bi in
      assert { radix = power 2 nbits_in_r * power 2 bi
               by radix = power 2 64 = power 2 (nbits_in_r + bi)};
      assert { r < power 2 nbits_in_r
               by r = div pr (power 2 bi)
               so r * power 2 bi <= pr
                  < power 2 64
                  = power 2 (nbits_in_r + bi)
                  = power 2 nbits_in_r * power 2 bi
                so r * power 2 bi < power 2 nbits_in_r * power 2 bi};
      assert { power radix i * power 2 bi = power 2 bni
               by power radix i = power (power 2 64) i = power 2 (64 * i)
               so power radix i * power 2 bi = power 2 (64 * i + bi)
                  = power 2 bni };
      assert { value p pn = value p i + power radix i * prm + power 2 bni * r
                            + power radix (i+1) * value p' (pn - (i+1)) };
      assert { valueb p bni = value p i + power radix i * prm
               by valueb p bni
                  = value p i + power radix i
                      * mod ((pelts p)[offset p + i]) (power 2 (bni - 64 * i))
                  = value p i + power radix i * prm };
      if nbits_in_r < nbits
      then begin
        assert { i + 1 < pn
                 by obi
                    = 64 * i + bi + nbits
                    > 64 * i + bi + 64 - bi
                    = 64 * (i+1)
                 so (obi + 63) >= 64 * (i+1) + 64 = 64 * (i+2)
                 so pn >= div (obi + 63) 64 >= i+2 };
        let pr' = get_ofs p (i+1) in
        let prs = lsl_mod_ext pr' (Limb.of_int32 nbits_in_r) in
        let ghost p'' = C.incr p (i+2) in
        value_concat p (i+1) (i+2);
        value_concat p (i+2) pn;
        assert { value p pn
                 = value p i + power radix i * prm + power 2 bni * r
                   + power radix (i+1) * pr'
                   + power radix (i+2) * value p'' (pn - (i+2))
                 by value p pn
                    = value p i + power radix i * prm + power 2 bni * r
                      + power radix (i+1) * value p' (pn - (i+1))
                 so value p' (pn - (i+1))
                    = pr' + radix * value p'' (pn - (i+2)) };
        let ghost prd = div ((power 2 (int32'int nbits_in_r)) * uint64'int pr')
                            radix in
        assert { power 2 nbits_in_r * pr' = radix * prd + prs };
        assert { radix * pr' =  radix * (power 2 bi * prd) + power 2 bi * prs
                 by radix * pr'
                 = power 2 bi * power 2 nbits_in_r * pr'
                 = radix * (power 2 bi * prd) + power 2 bi * prs };
        assert { power radix (i+1) * pr'
                 = power radix (i+1) * (power 2 bi * prd)
                   + power 2 bni * prs
                 by power radix (i+1) = radix * power radix i
                 so power radix (i+1) * pr'
                 = radix * power radix i * pr'
                 = power radix i * (radix * pr')
                 = power radix i * radix * power 2 bi * prd
                   + power radix i * power 2 bi * prs
                 = power radix (i+1) * (power 2 bi * prd)
                   + power radix i * power 2 bi * prs
                 = power radix (i+1) * (power 2 bi * prd)
                   + power 2 bni * prs };
        assert { value p pn
                 = value p i + power radix i * prm
                   + power 2 bni * (r + prs)
                   + power radix (i+1) * (power 2 bi * prd)
                   + power radix (i+2) * value p'' (pn - (i+2)) };
        let ghost or = pure { r } in
        r <- r + prs;
        let ghost res = pure { mod r (power 2 (int32'int nbits)) } in
        let ghost drs = pure { div r (power 2 (int32'int nbits)) } in
        assert { r = res + power 2 nbits * drs };
        assert { power radix (i+1) = power 2 bni * power 2 nbits_in_r
                 by power radix (i+1) = power radix i * radix
                    = power radix i * power 2 bi * power 2 nbits_in_r
                    = power 2 bni * power 2 nbits_in_r };
        assert { valueb p obi
                 = valueb p bni + power 2 bni * res
                 by bni = 64 * i + bi
                 so obi = bni + nbits > bni + 64 - bi = 64 * (i+1)
                 so div obi 64 = i+1
                 so obi - 64 * (i+1)
                    = 64 *i + bi + nbits - 64 * (i+1)
                    = bi + nbits - 64
                    = nbits - nbits_in_r
                 so valueb p obi
                     = value p (i+1)
                       + power radix (i+1)
                         * mod ((pelts p)[offset p + (i+1)])
                               (power 2 (obi - (64 * (i+1))))
                     = value p (i+1)
                       + power radix (i+1)
                         * mod pr' (power 2 (nbits - nbits_in_r))
                 so value p (i+1) = value p i + power radix i * pr
                    = value p i + power radix i * (prm + power 2 bi * or)
                    = valueb p bni + power 2 bni * or
                 so valueb p obi
                    = valueb p bni
                      + power 2 bni * or
                      + power 2 bni * power 2 nbits_in_r
                        * mod pr' (power 2 (nbits - nbits_in_r))
                 so power 2 nbits_in_r * power 2 (nbits - nbits_in_r)
                    = power 2 nbits
                 so power 2 nbits_in_r * mod pr' (power 2 (nbits - nbits_in_r))
                    = mod (pr' * power 2 nbits_in_r)
                          (power 2 (nbits - nbits_in_r) * power 2 nbits_in_r)
                    = mod (power 2 nbits_in_r * pr') (power 2 nbits)
                    = mod (radix * prd + prs) (power 2 nbits)
                 so radix * prd = power 2 64 * prd
                    = power 2 nbits * power 2 (64 - nbits) * prd
                 so mod (radix * prd + prs) (power 2 nbits)
                    = mod (power 2 nbits * (power 2 (64 - nbits) * prd) + prs)
                          (power 2 nbits)
                    = mod prs (power 2 nbits)
                 so valueb p obi
                    = valueb p bni
                      + power 2 bni * (or + mod prs (power 2 nbits))
                 so prs <= radix - power 2 nbits_in_r
                (* so radix = power 2 nbits_in_r * power 2 bi *)
                 so prs = power 2 nbits_in_r * pr' - radix * prd
                         = power 2 nbits_in_r * (pr' - power 2 bi * prd) + 0
                 so mod prs (power 2 nbits_in_r) = mod 0 (power 2 nbits_in_r)
                    = 0
                 so (mod prs (power 2 nbits)
                     <= power 2 nbits - power 2 (nbits_in_r)
                    by let nbr = nbits - nbits_in_r in
                       let k = div prs (power 2 nbits_in_r) in
                       power 2 nbits = power 2 (nbr + nbits_in_r)
                       = power 2 nbr * power 2 nbits_in_r
                    so prs = k * power 2 nbits_in_r
                    so mod prs (power 2 nbits)
                       = mod (k * power 2 nbits_in_r)
                             (power 2 nbr * power 2 nbits_in_r)
                       = power 2 nbits_in_r * (mod k (power 2 nbr))
                    so mod k (power 2 nbr) <= power 2 nbr - 1
                    so mod prs (power 2 nbits)
                       <= (power 2 nbits_in_r) * (power 2 nbr - 1)
                       = power 2 nbits - power 2 nbits_in_r)
                 so or < power 2 nbits_in_r < power 2 nbits
                 so mod or (power 2 nbits) = or
                 so mod or (power 2 nbits) + mod prs (power 2 nbits)
                    < power 2 nbits_in_r + (power 2 nbits - power 2 nbits_in_r)
                    <= power 2 nbits
                 so mod or (power 2 nbits) + mod prs (power 2 nbits)
                    = mod (mod or (power 2 nbits) + mod prs (power 2 nbits))
                          (power 2 nbits)
                    = mod (or + prs) (power 2 nbits)
                    = mod r (power 2 nbits)
                    = res };
        assert { power radix (i+1) * power 2 bi * prd
                 = power 2 bni * radix * prd };
        assert { power radix (i+2) = power 2 bni * radix * power 2 nbits_in_r };
        assert { value p pn
                 = value p i + power radix i * prm
                   + power 2 bni * (res + power 2 nbits * drs)
                   + power 2 bni * radix * prd
                   + power 2 bni * radix * power 2 nbits_in_r
                     * value p'' (pn - (i+2)) };
        let ghost d = pure { prd + power 2 nbits_in_r
                                   * value p'' (pn - (i+2)) } in
        assert { value p pn
                 = power 2 bni * (res + power 2 nbits * drs + radix * d)
                   + valueb p bni };
        assert { mod (value p pn) (power 2 bni)
                 = mod (valueb p bni) (power 2 bni)
                 = valueb p bni };
        assert { div (value p pn) (power 2 bni)
                 = res + power 2 nbits * drs + radix * d
                 by valueb p bni = mod (value p pn) (power 2 bni)
                 so power 2 bni * div (value p pn) (power 2 bni)
                      + mod (value p pn) (power 2 bni)
                    = value p pn
                    = power 2 bni * (res + power 2 nbits * drs + radix * d)
                      + valueb p bni
                 so power 2 bni * (res + power 2 nbits * drs + radix * d)
                    = power 2 bni * div (value p pn) (power 2 bni) };
        assert { radix = power 2 64 = power 2 nbits * power 2 (64 - nbits) };
        let ghost d' = pure { drs + power 2 (64 - nbits) * d } in
        assert { div (value p pn) (power 2 bni)
                 = power 2 nbits * drs
                   + power 2 nbits * power 2 (64 - nbits) * d + res
                 = power 2 nbits * d' + res };
        assert { mod (div (value p pn) (power 2 bni)) (power 2 nbits)
                 = mod (power 2 nbits * d' + res) (power 2 nbits)
                 = mod res (power 2 nbits)
                 = res };
      end
      else begin
        let ghost res = pure { mod r (power 2 nbits) } in
        let ghost drs = pure { div r (power 2 nbits) } in
        assert { r = power 2 nbits * drs + res };
        assert { valueb p obi = valueb p bni + power 2 bni * res
                 by obi = 64 * i + bi + nbits
                        <= 64 * i + bi + (64 - bi)
                        = 64 * (i+1)
                 so if obi < 64 * (i+1)
                    then
                      div obi 64 = i
                      so valueb p obi
                         = value p i
                           + power radix i * mod ((pelts p)[offset p + i])
                                               (power 2 (obi - 64 * i))
                         = value p i
                           + power radix i * mod pr (power 2 (bi + nbits))
                      so pr = prm + power 2 bi * r
                            = prm + power 2 bi * (res + power 2 nbits * drs)
                            = prm + power 2 bi * res
                              + power 2 bi * power 2 nbits * drs
                            = power 2 (bi + nbits) * drs
                              + prm + power 2 bi * res
                      so (0 <= prm + power 2 bi * res < power 2 (bi + nbits)
                          by 0 <= prm < power 2 bi
                          so 0 <= res <= power 2 nbits - 1
                          so power 2 bi * res
                             <= power 2 bi * (power 2 nbits - 1)
                             = power 2 (bi + nbits) - power 2 bi)
                      so mod pr (power 2 (bi + nbits))
                         = mod (power 2 (bi + nbits) * drs
                                 + (prm + power 2 bi * res))
                               (power 2 (bi + nbits))
                         = mod (prm + power 2 bi * res) (power 2 (bi + nbits))
                         = prm + power 2 bi * res
                      so valueb p obi
                         = value p i + power radix i * (prm + power 2 bi * res)
                         = value p i + power radix i * prm + power 2 bni * res
                         = valueb p bni + power 2 bni * res
                    else
                      obi = 64 * (i+1)
                      so div obi 64 = (i+1)
                      so bi + nbits = 64
                      so valueb p obi
                         = value p (i+1)
                           + power radix (i+1)
                             * (mod (pelts p)[offset p + (i + 1)]) (power 2 0)
                         = value p (i+1)
                         = value p i + power radix i * pr
                      so nbits_in_r = nbits
                      so r < power 2 nbits
                      so res = r
                      so pr = prm + power 2 bi * res
                      so valueb p obi
                         = value p i + power radix i * prm
                           + power radix i * power 2 bi * res
                         = valueb p bni + power 2 bni * res };
        assert { mod (div (value p pn) (power 2 bni)) (power 2 nbits) = res
                 by power radix (i+1)
                    = power radix i * radix
                    = power radix i * power 2 bi * power 2 nbits_in_r
                    = power 2 bni * power 2 nbits_in_r
                 so value p pn
                    = valueb p bni + power 2 bni * r
                      + power radix (i+1) * value p' (pn - (i+1))
                    = valueb p bni + power 2 bni * r
                      + power 2 bni
                        * (power 2 nbits_in_r * value p' (pn - (i+1)))
                 so value p pn
                    = power 2 bni
                        * (r + power 2 nbits_in_r * value p' (pn - (i+1)))
                      + valueb p bni
                 so mod (value p pn) (power 2 bni)
                    = mod (valueb p bni) (power 2 bni) = valueb p bni
                 so let d = power 2 (nbits_in_r - nbits)
                            * value p' (pn - (i+1)) in
                    power 2 nbits_in_r
                    = power 2 (nbits_in_r - nbits) * power 2 nbits
                 so value p pn
                    = valueb p bni
                      + power 2 bni * (r + power 2 nbits * d)
                 so value p pn
                    = mod (value p pn) (power 2 bni)
                      + power 2 bni * (div (value p pn) (power 2 bni))
                 so power 2 bni * (r + power 2 nbits * d)
                    = power 2 bni * (div (value p pn) (power 2 bni))
                 so r + power 2 nbits * d
                    = div (value p pn) (power 2 bni)
                 so r + power 2 nbits * d
                    = power 2 nbits * (drs + d) + res
                 so mod (r + power 2 nbits * d) (power 2 nbits)
                    = mod res (power 2 nbits)
                    = res };
      end;
      r % (lsl 1 (Limb.of_int32 nbits))

  let lemma valueb_mod (p:t) (nbits:int) (n:int32)
    requires { nbits >= 0 }
    requires { 64 * n >= nbits }
    ensures  { valueb p nbits = mod (value p n) (power 2 nbits) }
  = let i = div nbits 64 in
    assert { i <= n };
    if i = int32'int n then return else ();
    assert { mod (power 2 (64 * i) * (pelts p)[offset p + i]) (power 2 nbits)
             = power 2 (64 * i)
               * mod (pelts p)[offset p + i] (power 2 (nbits - 64 * i))
             by power 2 nbits = power 2 (64 * i) * power 2 (nbits - 64 * i)
             so power 2 (64 * i)
                * mod (pelts p)[offset p + i] (power 2 (nbits - 64 * i))
                = mod (power 2 (64 * i) * (pelts p)[offset p + i])
                      (power 2 (64 * i) * power 2 (nbits - 64 * i)) };
    assert { valueb p nbits
             = value p i
               + power radix i * mod (pelts p)[offset p + i]
                                     (power 2 (nbits - 64 * i))
             = value p i
               + power 2 (64 * i) * mod (pelts p)[offset p + i]
                                        (power 2 (nbits - 64 * i))
             = value p i + mod (power 2 (64 * i) * (pelts p)[offset p + i])
                               (power 2 nbits) };
    mod_id (value p i) (power 2 nbits);
    assert { mod (power 2 (64 * i) * (pelts p)[offset p + i]) (power 2 nbits)
                 <= power 2 nbits - power 2 (64 * i)
             by mod (power 2 (64 * i) * (pelts p)[offset p + i]) (power 2 nbits)
                = power 2 (64 * i)
                  * mod (pelts p)[offset p + i] (power 2 (nbits - 64 * i))
             so mod (pelts p)[offset p + i] (power 2 (nbits - 64 * i))
                <= power 2 (nbits - 64 * i) - 1
            so power 2 (64 * i)
               * mod (pelts p)[offset p + i] (power 2 (nbits - 64 * i))
               <= power 2 (64 * i) * (power 2 (nbits - 64 * i) - 1)
               = power 2 (64 * i) * power 2 (nbits - 64 * i) - power 2 (64 * i)
               = power 2 nbits - power 2 (64 * i) };
    mod_id (mod (value p i) (power 2 nbits)
           + mod (power 2 (64 * i) * uint64'int (pelts p)[offset p + i])
                 (power 2 nbits))
           (power 2 nbits);
    mod_sum (value p i) (power 2 (64 * i) * uint64'int (pelts p)[offset p + i])
            (power 2 nbits);
    value_tail p (Int32.of_int i);
    assert { valueb p nbits
             = mod (value p i + power 2 (64 * i) * (pelts p)[offset p + i])
                   (power 2 nbits)
             = mod (value p i + power radix i * (pelts p)[offset p + i])
                   (power 2 nbits)
             = mod (value p (i+1)) (power 2 nbits) };
    value_concat p (Int32.of_int i+1) n;
    let x = value_sub (pelts p) (offset p + (i+1)) (offset p + int32'int n) in
    let d = 64 * (i+1) - nbits in
    assert { d >= 0 };
    assert { power radix (i+1) = power 2 (64 * (i+1))
             = power 2 nbits * power 2 d };
    assert { value p n = value p (i+1) + power radix (i+1) * x
             = power 2 nbits * (power 2 d * x) + value p (i+1) };
    assert { mod (value p n) (power 2 nbits)
             = mod (power 2 nbits * (power 2 d * x) + value p (i+1))
                   (power 2 nbits)
             = mod (value p (i+1)) (power 2 nbits)
             = valueb p nbits }

  let lemma valueb_mon (p:t) (pn:int32) (bi1 bi2:int32)
    requires { 0 <= bi1 <= bi2 }
    requires { valid p pn }
    requires { pn >= div (bi2 + 63) 64 }
    ensures  { valueb p bi1 <= valueb p bi2 }
  =
    if bi2 - bi1 >= 64
    then begin
      let i1 = bi1 / 64 in
      let i2 = bi2 / 64 in
      assert { i1 < i2 };
      value_concat p (i1+1) i2;
      value_tail p i1;
      assert { valueb p bi1
               = value p i1 + power radix i1
                    * mod ((pelts p)[offset p + i1]) (power 2 (bi1 - 64 * i1))
               <= value p i1 + power radix i1 * (pelts p)[offset p + i1]
               = value p (i1 + 1)
               by mod ((pelts p)[offset p + i1]) (power 2 (bi1 - 64 * i1))
                  <= (pelts p)[offset p + i1]
             };
      assert { valueb p bi2
               = value p i2 + power radix i2
                    * mod ((pelts p)[offset p + i2]) (power 2 (bi2 - 64 * i2))
               >= value p i2
               by div bi2 64 = i2
               so mod ((pelts p)[offset p + i2]) (power 2 (bi2 - 64 * i2)) >= 0
               so power radix i2 >= 0 };
      assert { value p i2
               = value p (i1+1) + power radix (i1 + 1)
                       * value_sub (pelts p) (offset p + i1 + 1) (offset p + i2)
               >= value p (i1 + 1) }
    end
    else if bi1 = bi2 then ()
    else
      begin
      let x = getbits p pn bi2 (bi2-bi1) in
      assert { valueb p bi2 = valueb p bi1 + power 2 bi1 * x };
      assert { power 2 bi1 * x >= 0 by x >= 0 }
    end

  let lemma redc_mul (p q a b c n m : int)
    requires { redc p a n m }
    requires { redc q b n m }
    requires { redc (p*q) c n m }
    requires { odd m }
    requires { 0 < m /\ 0 < n }
    ensures  { redc c (a * b) n m }
  = assert { mod p m = mod (power radix n * a) m
             /\ mod q m = mod (power radix n * b) m };
    mod_mul p (power radix n * a) q (power radix n * b) m;
    assert { mod (p * q) m = mod (power radix n * c) m
             = mod (power radix n * (power radix n * a * b)) m };
    unredc c (p * q) (power radix n * (a * b)) n m

  let wmpn_powm (rp bp : t) (bn : int32) (ep : t) (en : int32) (mp : t)
                (n : int32) (tp : t) : unit
    requires { valid rp n /\ valid bp bn /\ valid ep en /\ valid mp n }
    requires { valid tp (2*n) }
    requires { odd (value mp n) }
    requires { value ep en > 1 }
    requires { en >= 1 }
    requires { (pelts ep)[offset ep + en - 1] > 0 }
    requires { (pelts mp)[offset mp + n - 1] > 0 }
    requires { n >= 1 }
    requires { bn >= 1 }
    requires { bn + n < max_int32 }
    requires { n * 512 <= max_int32 }
    requires { 64 * en < max_int32 - 64 }
    ensures  { value rp n
               = mod (power (value bp bn) (value ep en)) (value mp n) }
  =
    let ghost vb = value bp (int32'int bn) in
    let ghost vm = value mp (int32'int n) in
    let ghost ve = value ep (int32'int en) in
    (* SIZEINBASE_2EXP start, move to function? *)
    let le = C.get_ofs ep (en - 1) in
    let ref cnt = count_leading_zeros le in
    let ref ebi = 64 * en - cnt in
    (* ep has ebi bits *)
    value_tail ep (en-1);
    assert { ve = value ep (en - 1) + power radix (en - 1) * le };
    assert { power 2 ebi = power radix (en - 1) * power 2 (64 - cnt)
             by power radix (en - 1) = power (power 2 64) (en - 1)
                = power 2 (64 * (en - 1))
             so power radix (en - 1) * power 2 (64 - cnt)
                = power 2 (64 * (en - 1)) * power 2 (64 - cnt)
                = power 2 (64 * (en - 1) + (64 - cnt))
                = power 2 ebi };
    assert { le < power 2 (64 - cnt)
             by radix = power 2 cnt * power 2 (64 - cnt) > power 2 cnt * le };
    assert { le >= power 2 (64 - (cnt + 1))
                 by radix = power 2 (cnt + 1) * power 2 (64 - (cnt + 1))
                 so power 2 (cnt + 1) * power 2 (64 - (cnt + 1))
                    <= power 2 (cnt + 1) * le};
    assert { power 2 (ebi-1) <= ve < power 2 ebi
             by power radix (en - 1) = power (power 2 64) (en - 1)
                = power 2 (64 * (en - 1))
             so 0 <= value ep (en - 1) < power radix (en - 1)
             so power radix (en - 1) * le
                <= ve < power radix (en - 1) * (1 + le) };
    assert { ve = valueb ep ebi
             by ebi > 0
             so 0 <= cnt < 64
             so 64 * (en - 1) < ebi <= 64 * en
             so if cnt = 0
             then
               ebi = 64 * en
               so div ebi 64 = en
               so valueb ep ebi
                  = value ep en
                    + power radix en * mod (pelts ep)[offset ep + en] 1
                  = value ep en
                  = ve
             else
               ebi < 64 * en
               so div ebi 64 = en - 1
               so ebi - 64 * (en - 1) = 64 - cnt
               so mod le (power 2 (64 - cnt)) = le
               so valueb ep ebi
                  = value ep (en - 1)
                    + power radix (en - 1)
                      * mod le (power 2 (ebi - 64 * (en - 1)))
                  = value ep (en - 1)
                    + power radix (en - 1) * le = ve };
    let windowsize = win_size ebi in
    assert { n <= n * power 2 (windowsize - 1) <= max_int32
             by windowsize - 1 <= 9
             so power 2 (windowsize - 1) <= power 2 9
             so power 2 2 = 4
             so power 2 4 = power 2 2 * power 2 2
             so power 2 8 = power 2 4 * power 2 4 = 256
             so power 2 9 = 2 * power 2 8 = 512 };
    let m0 = C.get mp in
    value_concat mp 1 n;
    assert { vm = m0 + radix *
                (value_sub (pelts mp) (offset mp + 1) (offset mp + n)) };
    let im = binvert_limb m0 in
    let mip = minus_mod im in
    assert { mod (vm * mip) radix = radix - 1
             by mod (im * m0) radix = 1
             so (im <> 0
                 by mod (0 * m0) radix = 0)
             so mod (m0 * mip) radix
                = mod (m0 * (mod (- im) radix)) radix
             so mod (- im) radix
                = mod (radix - im) radix
                = radix - im
             so mod (m0 * mip) radix
                = mod (m0 * (radix - im)) radix
                = mod (radix * m0 - im * m0) radix
                = mod (- im * m0) radix
                = mod ((-1)  * (im * m0)) radix
                = mod ((mod (-1) radix)
                       * (mod (im * m0) radix)) radix
                = mod ((radix - 1) * 1) radix
                = radix - 1
             so mod (vm * mip) radix
                = mod ((radix *
                         (value_sub (pelts mp) (offset mp + 1) (offset mp + n))
                       + m0) * mip) radix
                = mod (radix *
                         (value_sub (pelts mp) (offset mp + 1) (offset mp + n)
                         * mip)
                       + m0 * mip) radix
                = mod (m0 * mip) radix };
    let pp = salloc
               (UInt32GMP.lsl (UInt32.of_int32 n)
                              (UInt32.of_int32 windowsize-1)) in
    let ref this_pp = C.incr pp 0 in
    redcify this_pp bp bn mp n;
    let ghost vp = value pp (int32'int n) in
    assert { vp = value this_pp n = mod (power radix n * vb) vm };
    assert { value this_pp n < vm };
    assert { redc (value pp n) vb n vm };
    wmpn_mul_n tp this_pp this_pp n 64;
    assert { value tp (2 * n) = vp * vp };
    wmpn_redc_1 rp tp mp n mip;
    redc_mul vp vp vb vb (value rp (int32'int n)) (int32'int n) vm;
    assert { redc (value rp n) (power vb 2) n vm };
    let ghost ref j = (0:int) in
    label Window in
    for i = UInt32GMP.lsl 1 (UInt32.of_int32 windowsize-1) - 1 downto 1 do
      invariant { j = power 2 (windowsize-1) - 1 - i }
      invariant { min pp = 0 <= j * n }
      invariant { windowsize > 1 -> i > 1 ->
                  j*n + n + n <= power 2 (windowsize-1) * n = max pp }
      invariant { offset this_pp = j * n }
      invariant { pelts this_pp = pelts pp }
      invariant { min this_pp = min pp }
      invariant { max this_pp = max pp }
      invariant { plength this_pp = plength pp }
      invariant { plength tp = plength (tp at Window) }
      invariant { min tp = min (tp at Window) }
      invariant { max tp = max (tp at Window) }
      invariant { forall k. 0 <= k <= j ->
                  redc (value_sub (pelts pp) (k*n) (k*n + n))
                        (power vb (2*k+1)) n vm }
      assert { redc (value this_pp n) (power vb (2*j + 1)) n vm };
      label Mul in
      let ghost ovp = value this_pp (int32'int n) in
      wmpn_mul_n tp this_pp rp n 64;
      this_pp <- C.incr this_pp n;
      assert { offset this_pp = j*n + n };
      assert { forall j. 0 <= j < max pp ->
               (pelts pp)[j] = (pelts pp at Mul)[j] };
      label Redc in
      wmpn_redc_1 this_pp tp mp n mip;
      assert { forall k. 0 <= k < j*n + n ->
               (pelts pp)[k] = (pelts pp at Redc)[k] = (pelts pp at Mul)[k]
               by pelts pp = pelts this_pp
               so pelts pp at Redc = pelts this_pp at Redc
               so k < j*n + n = offset this_pp
               so (pelts this_pp)[k] = (pelts this_pp)[k] at Redc };
      assert { forall k. 0 <= k <= j ->
               redc (value_sub (pelts pp) (k*n) (k*n + n))
                    (power vb (2*k+1)) n vm
               by (forall l. k*n <= l < k*n + n ->
                   (pelts pp)[l] = (pelts pp)[l] at Mul
                   by k*n <= j*n
                   so l < j*n+n)
               so value_sub (pelts pp) (k*n) (k*n + n)
                  = value_sub (pelts pp) (k*n) (k*n + n) at Mul };
      j <- j+1;
      redc_mul ovp (value rp (int32'int n))
               (power vb (2 * j - 1)) (power vb 2)
               (value this_pp (int32'int n)) (int32'int n) vm;
      assert { redc (value this_pp n) (power vb (2*j + 1)) n vm
               by power vb (2*j + 1) = power vb (2*j - 1) * power vb 2 };
    done;
    assert RedcW { forall k. 0 <= k < power 2 (windowsize - 1) ->
                    redc (value_sub (pelts pp) (k*n) (k*n + n))
                         (power vb (2*k+1)) n vm };
    let ref expbits = getbits ep en ebi windowsize in
    begin ensures { ve = valueb ep ebi + power 2 ebi * expbits }
          ensures { 0 <= ebi <= old ebi }
          ensures { expbits < power 2 windowsize }
      if ebi < windowsize
      then begin
        ebi <- 0;
        assert { expbits = valueb ep ebi + power 2 ebi * expbits };
        assert { expbits = ve };
      end
      else ebi <- ebi - windowsize
    end;
    label Shift in
    cnt <- count_trailing_zeros expbits;
    ebi <- ebi + cnt;
    expbits <- lsr expbits (Limb.of_int32 cnt);
    assert { odd expbits
             by expbits at Shift = power 2 cnt * expbits
             so mod (expbits at Shift) (power 2 (cnt+1)) <> 0
             so let d = div expbits 2 in
                let m = mod expbits 2 in
                expbits = 2*d+m
             so expbits at Shift = power 2 (cnt+1) * d + power 2 cnt * m
             so mod (expbits at Shift) (power 2 (cnt + 1))
                = mod (power 2 cnt * m) (power 2 (cnt + 1))
             so m <> 0 };
    assert { ebi <= 64 * en
             by expbits >= 1
             so ve = valueb ep (ebi - cnt)
                   + (power 2 (ebi-cnt) * power 2 cnt * expbits)
                   = valueb ep (ebi - cnt) + power 2 ebi * expbits
                >= power 2 ebi * expbits
                >= power 2 ebi
             so power 2 ebi <= ve < power radix en = power 2 (64 * en) };
    valueb_mod ep (int32'int ebi) en;
    assert { ve = valueb ep ebi + power 2 ebi * expbits
             by expbits at Shift = power 2 cnt * expbits
             so ebi at Shift = ebi - cnt
             so (power 2 ebi * expbits) at Shift
                 = power 2 (ebi - cnt) * power 2 cnt * expbits
                 = power 2 ebi * expbits
             so valueb ep (ebi at Shift) = valueb ep (ebi - cnt)
                < power 2 (ebi - cnt) <= power 2 ebi
             so valueb ep ebi = mod ve (power 2 ebi)
                = mod (power 2 ebi * expbits + valueb ep (ebi at Shift))
                      (power 2 ebi)
                = mod (valueb ep (ebi at Shift))
                      (power 2 ebi)
                = valueb ep (ebi at Shift) };
    assert { div expbits 2 <= max_int32
             by power 2 10 = power 2 4 * power 2 4 * power 2 2 = 16 * 16 * 4
                = 1024
             so expbits <= power 2 windowsize <= power 2 10 < max_int32 };
    let ebh = Limb.to_int32 (lsr_mod expbits 1) in
    assert { 2 * ebh + 1 = expbits
             by expbits = 2 * (div expbits 2) + mod expbits 2
             so div expbits 2 = ebh so mod expbits 2 = 1 };
    assert { n * ebh + n <= n * power 2 (windowsize - 1)
             by 2 * ebh < expbits <= expbits at Shift < power 2 windowsize
                = 2 * power 2 (windowsize-1)
             so ebh < power 2 (windowsize-1)
             so ebh <= power 2 (windowsize-1) - 1
             so n * ebh <= n * (power 2 (windowsize-1) - 1)
                = n * power 2 (windowsize-1) - n };
    let ppn = C.incr pp (n * ebh) in
    wmpn_copyi rp ppn n;
    assert { redc (value rp n) (power vb expbits) n vm
             by value rp n = value ppn n
                = value_sub (pelts pp) (ebh * n) (ebh * n + n)
             so redc (value_sub (pelts pp) (ebh * n) (ebh * n + n))
                     (power vb (2 * ebh + 1)) n vm };
    let ghost ref expdone = uint64'int expbits in
    let ref this_windowsize = windowsize in
    label Loop in
    while (ebi <> 0) do
      variant { ebi }
      invariant { 0 <= ebi <= ebi at Loop }
      invariant { 0 <= expdone }
      invariant { en >= div (ebi + 63) 64 }
      invariant { redc (value rp n) (power vb expdone) n vm }
      invariant { ve = valueb ep ebi + power 2 ebi * expdone }
      invariant { plength tp = plength (tp at Window) }
      invariant { min tp = min (tp at Window) }
      invariant { max tp = max (tp at Window) }
      label ILoop in
      while getbit ep en ebi = 0 do
        variant { ebi }
        invariant { 1 <= ebi <= ebi at ILoop }
        invariant { 0 <= expdone }
        invariant { en >= div (ebi + 63) 64 }
        invariant { redc (value rp n) (power vb expdone) n vm }
        invariant { ve = valueb ep ebi + power 2 ebi * expdone }
        invariant { plength tp = plength (tp at Window) }
        invariant { min tp = min (tp at Window) }
        invariant { max tp = max (tp at Window) }
        let ghost orp = value rp (int32'int n) in
        wmpn_mul_n tp rp rp n 64;
        wmpn_redc_1 rp tp mp n mip;
        redc_mul orp orp (power vb expdone) (power vb expdone)
                 (value rp (int32'int n)) (int32'int n) vm;
        assert { redc (value rp n) (power vb (expdone + expdone)) n vm };
        assert { valueb ep ebi = valueb ep (ebi - 1) };
        assert { ve = valueb ep (ebi-1) + power 2 (ebi-1) * (2 * expdone)
                 by power 2 (ebi - 1) * 2 = power 2 ebi };
        expdone <- 2 * expdone;
        ebi <- ebi - 1;
        if ebi = 0 then begin
          assert { redc (value rp n) (power vb expdone) n vm };
          assert { ve = valueb ep ebi + power 2 ebi * expdone
                      = expdone };
          break (* TODO GMP uses a goto to break twice *)
        end
      done;
      if ebi = 0 then begin
        assert { redc (value rp n) (power vb ve) n vm };
        assert { ve = expdone };
        break
      end;
      assert { valueb ep ebi = valueb ep (ebi - 1) + power 2 (ebi - 1) * 1 };
      label WindowL in
      let ghost oebi = ebi in
      expbits <- getbits ep en ebi windowsize;
      this_windowsize <- windowsize;
      begin ensures { 0 <= ebi < old ebi }
            ensures { ebi + this_windowsize = oebi }
            ensures { valueb ep (old ebi)
                      = valueb ep ebi + power 2 ebi * expbits }
            ensures { 0 < expbits < power 2 this_windowsize }
            ensures { 0 < this_windowsize <= windowsize }
      if (ebi < windowsize)
      then begin
        this_windowsize <- this_windowsize - (windowsize - ebi);
        ebi <- 0;
        assert { expbits = valueb ep (oebi)
                 < power 2 (oebi) = power 2 this_windowsize };
      end
      else ebi <- ebi - windowsize
      end;
      valueb_mon ep en ebi (oebi - 1);
      assert { 0 < expbits
               by valueb ep (oebi)
                   = valueb ep (oebi - 1) + power 2 (oebi - 1) * 1
               so valueb ep (oebi) > valueb ep (oebi-1)
               so valueb ep (oebi) = valueb ep ebi + power 2 ebi * expbits
               so valueb ep ebi <= valueb ep (oebi - 1) };
      label ShiftL in
      cnt <- count_trailing_zeros expbits;
      assert { this_windowsize >= cnt
               by expbits < power 2 this_windowsize
               so mod expbits (power 2 cnt) = 0
               so expbits >= power 2 cnt };
      this_windowsize <- this_windowsize - cnt;
      ebi <- ebi + cnt;
      expbits <- lsr expbits (Limb.of_int32 cnt);
      assert { odd expbits
               by expbits at ShiftL = power 2 cnt * expbits
               so mod (expbits at ShiftL) (power 2 (cnt+1)) <> 0
               so let d = div expbits 2 in
                  let m = mod expbits 2 in
                  expbits = 2*d+m
               so expbits at ShiftL = power 2 (cnt+1) * d + power 2 cnt * m
               so mod (expbits at ShiftL) (power 2 (cnt + 1))
                  = mod (power 2 cnt * m) (power 2 (cnt + 1))
               so m <> 0 };
      assert { ebi <= 64 * en
               by ebi <= ebi at Loop <= 64 * en };
      valueb_mod ep (int32'int ebi) en;
      assert { valueb ep oebi
               = valueb ep ebi + power 2 ebi * expbits
               /\ ve = valueb ep ebi + power 2 ebi * expbits
                                     + power 2 oebi * expdone
               by expbits at ShiftL = power 2 cnt * expbits
               so (ebi at ShiftL) = ebi - cnt
               so (power 2 ebi * expbits) at ShiftL
                  = power 2 (ebi-cnt) * power 2 cnt * expbits
                  = power 2 ebi * expbits
               so valueb ep (ebi at ShiftL) = valueb ep (ebi - cnt)
                  < power 2 (ebi - cnt) <= power 2 ebi
               so oebi = ebi + this_windowsize
               so power 2 oebi = power 2 ebi * power 2 this_windowsize
               so ve = valueb ep (ebi at ShiftL) + power 2 ebi * expbits
                       + power 2 oebi * expdone
                     = valueb ep (ebi at ShiftL) + power 2 ebi * expbits
                       + power 2 ebi * (power 2 this_windowsize * expdone)
                     = valueb ep (ebi at ShiftL)
                       + power 2 ebi
                         * (expbits + power 2 this_windowsize * expdone)
               so valueb ep ebi = mod ve (power 2 ebi)
                  = mod (power 2 ebi
                         * (expbits + power 2 this_windowsize * expdone)
                         + valueb ep (ebi at ShiftL))
                        (power 2 ebi)
                  = mod (valueb ep (ebi at ShiftL)) (power 2 ebi)
                  = valueb ep (ebi at ShiftL) };
      assert { ebi + this_windowsize = oebi };
      assert { ve =
                 valueb ep ebi + power 2 ebi *
                         (expbits + power 2 this_windowsize * expdone)
               by power 2 ebi * power 2 this_windowsize = power 2 oebi };
      label WLoop in
      while this_windowsize <> 0 do
        variant { this_windowsize }
        invariant { 0 <= this_windowsize <= this_windowsize at WLoop }
        invariant { 0 <= expdone }
        invariant { redc (value rp n) (power vb expdone) n vm }
        invariant { ve = valueb ep ebi + power 2 ebi *
                         (expbits + power 2 this_windowsize * expdone) }
        invariant { plength tp = plength (tp at Window) }
        invariant { min tp = min (tp at Window) }
        invariant { max tp = max (tp at Window) }
        let ghost orp = value rp (int32'int n) in
        wmpn_mul_n tp rp rp n 64;
        wmpn_redc_1 rp tp mp n mip;
        redc_mul orp orp (power vb expdone) (power vb expdone)
                 (value rp (int32'int n)) (int32'int n) vm;
        assert { redc (value rp n) (power vb (expdone + expdone)) n vm };
        assert { power 2 (this_windowsize - 1) * (2 * expdone)
                 = power 2 this_windowsize * expdone };
        this_windowsize <- this_windowsize - 1;
        expdone <- 2 * expdone;
      done;
      assert { div expbits 2 <= max_int32
               by power 2 10 = power 2 4 * power 2 4 * power 2 2 = 16 * 16 * 4
                  = 1024
               so expbits <= power 2 windowsize <= power 2 10 < max_int32 };
      let ebh = Limb.to_int32 (lsr_mod expbits 1) in
      assert { 2 * ebh + 1 = expbits
               by expbits = 2 * (div expbits 2) + mod expbits 2
               so div expbits 2 = ebh so mod expbits 2 = 1 };
      assert { ebh < power 2 (windowsize - 1)
               by 2 * ebh < expbits <= expbits at ShiftL < power 2 windowsize
                  = 2 * power 2 (windowsize-1)
               so ebh < power 2 (windowsize-1) };
      assert { n * ebh + n <= n * power 2 (windowsize - 1)
               by ebh <= power 2 (windowsize-1) - 1
               so n * ebh <= n * (power 2 (windowsize-1) - 1)
                  = n * power 2 (windowsize-1) - n };
      let ppn = C.incr pp (n * ebh) in
      assert { redc (value ppn n) (power vb expbits) n vm
               by value ppn n = value_sub (pelts pp) (ebh * n) (ebh * n + n)
               so redc (value_sub (pelts pp) (ebh * n) (ebh * n + n))
                       (power vb (2 * ebh + 1)) n vm };
      let ghost orp = value rp (int32'int n) in
      wmpn_mul_n tp rp ppn n 64;
      wmpn_redc_1 rp tp mp n mip;
      redc_mul orp (value ppn (int32'int n))
               (power vb expdone) (power vb (uint64'int expbits))
               (value rp (int32'int n)) (int32'int n) vm;
      assert { redc (value rp n) (power vb (expdone + expbits)) n vm };
      expdone <- expdone + uint64'int expbits
    done;
    assert { redc (value rp n) (power vb ve) n vm };
    wmpn_copyi tp rp n;
    let ghost otp = pure { tp } in
    wmpn_zero (C.incr tp n) n;
    value_sub_frame (pelts tp) (pelts otp)
                    (offset tp) (offset tp + int32'int n);
    assert { value tp n = value rp n };
    assert { value_sub (pelts tp) (offset tp + n) (offset tp + (n+n)) = 0 };
    value_concat tp n (2 * n);
    assert { value tp (2*n)
                = value rp n + power radix n * 0
                = value rp n };
    assert { redc (value tp (2 * n)) (power vb ve) n vm };
    label Reduce in
    wmpn_redc_1 rp tp mp n mip;
    unredc (value rp (int32'int n))
           (value (pure { rp at Reduce }) (int32'int n))
           (power vb ve) (int32'int n) vm;
    assert { mod (value rp n) vm = mod (power vb ve) vm };
    assert { value rp n < 2 * vm
             by value tp (2 * n) at Reduce
                = value rp n at Reduce
                < power radix n = power radix n * 1 <= power radix n * vm };
   begin ensures { mod (value rp n) vm = mod (power vb ve) vm }
         ensures { value rp n < vm }
     if (wmpn_cmp rp mp n >= 0)
      then begin
        label Sub in
        let ghost _b = wmpn_sub_in_place rp n mp n in
        assert { _b = 0
                 by value rp n - power radix n * _b
                    = value rp n at Sub - vm
                    >= 0
                 so value rp n < power radix n
                 so 0 <= value rp n - power radix n * _b
                    < power radix n * (1 - _b)
                 so _b < 1 };
        assert { value rp n = value rp n at Sub - vm };
        assert { mod (value rp n) vm
                 = mod (vm * (-1) + value rp n at Sub) vm
                 = mod (value rp n at Sub) vm
                 = mod (power vb ve) vm };
        ()
      end
    end;
    assert { value rp n = mod (value rp n) vm }

end