aboutsummaryrefslogtreecommitdiff
path: root/src/ModularArithmetic/ModularBaseSystemListProofs.v
blob: 4282394980a2f3946106785d7174a8f2f63e6c5e (plain)
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
Require Import Coq.ZArith.Zpower Coq.ZArith.ZArith.
Require Import Coq.Numbers.Natural.Peano.NPeano.
Require Import Coq.Lists.List.
Require Import Crypto.Tactics.VerdiTactics.
Require Import Crypto.BaseSystem.
Require Import Crypto.BaseSystemProofs.
Require Import Crypto.ModularArithmetic.Conversion.
Require Import Crypto.ModularArithmetic.Pow2Base.
Require Import Crypto.ModularArithmetic.Pow2BaseProofs.
Require Import Crypto.ModularArithmetic.ExtendedBaseVector.
Require Import Crypto.ModularArithmetic.PrimeFieldTheorems.
Require Import Crypto.ModularArithmetic.PseudoMersenneBaseParams.
Require Import Crypto.ModularArithmetic.PseudoMersenneBaseParamProofs.
Require Import Crypto.Util.Tactics.
Require Import Crypto.Util.ListUtil.
Require Import Crypto.Util.ZUtil.
Require Import Crypto.Util.Notations.

Require Import Crypto.ModularArithmetic.ModularBaseSystemListZOperations.
Require Import Crypto.ModularArithmetic.ModularBaseSystemList.
Local Open Scope Z_scope.

Section LengthProofs.
  Context `{prm :PseudoMersenneBaseParams}.
  Local Notation base := (base_from_limb_widths limb_widths).

  Lemma length_encode {x} : length (encode x) = length limb_widths.
  Proof.
    cbv [encode encodeZ]; intros.
    rewrite encode'_spec;
      auto using encode'_length, limb_widths_nonneg, Nat.eq_le_incl, base_from_limb_widths_length.
  Qed.

  Lemma length_reduce : forall us,
      (length limb_widths <= length us <= length (ext_base limb_widths))%nat ->
      (length (reduce us) = length limb_widths)%nat.
  Proof.
    rewrite extended_base_length.
    unfold reduce; intros.
    rewrite add_length_exact.
    pose proof (@base_from_limb_widths_length limb_widths).
    rewrite map_length, firstn_length, skipn_length, Min.min_l, Max.max_l;
      omega.
  Qed.

  Lemma length_mul {u v} :
      length u = length limb_widths
      -> length v = length limb_widths
      -> length (mul u v) = length limb_widths.
  Proof.
    cbv [mul]; intros.
    apply length_reduce.
    destruct u; try congruence.
    + rewrite @nil_length0 in *; omega.
    + rewrite mul_length_exact, extended_base_length, base_from_limb_widths_length; try omega;
        repeat match goal with
               | |- _ => progress intros
               | |- nth_default _ (ext_base _) 0 = 1 => apply b0_1
               | x := nth_default _ (ext_base _) |- _ => apply ext_base_good
               | x := nth_default _ base |- _ => apply base_good
               | x := nth_default _ base |- _ => apply limb_widths_good
               | |- 2 ^ _ <> 0 => apply Z.pow_nonzero
               | |- _ => solve [apply BaseSystem.b0_1]
               | |- _ => solve [auto using limb_widths_nonneg, sum_firstn_limb_widths_nonneg, limb_widths_match_modulus]
               | |- _ => omega
               | |- _ => congruence
               end.
  Qed.

  Section Sub.
  Context {mm : list Z} (mm_length : length mm = length limb_widths).

  Lemma length_sub {u v} :
      length u = length limb_widths
      -> length v = length limb_widths
      -> length (sub mm u v) = length limb_widths.
  Proof.
    cbv [sub]; intros.
    rewrite sub_length, add_length_exact.
    repeat rewrite Max.max_r; omega.
  Qed.
  End Sub.

  Lemma length_carry_and_reduce {us}: forall i, length (carry_and_reduce i us) = length us.
  Proof. intros; unfold carry_and_reduce; autorewrite with distr_length; reflexivity. Qed.
  Hint Rewrite @length_carry_and_reduce : distr_length.

  Lemma length_carry {u i} :
    length u = length limb_widths
    -> length (carry i u) = length limb_widths.
  Proof. intros; unfold carry; break_if; autorewrite with distr_length; omega. Qed.
  Hint Rewrite @length_carry : distr_length.

  Lemma length_carry_sequence {u i} :
    length u = length limb_widths
    -> length (carry_sequence i u) = length limb_widths.
  Proof.
   induction i; intros; unfold carry_sequence;
     simpl; autorewrite with distr_length; auto. Qed.
  Hint Rewrite @length_carry_sequence : distr_length.

  Lemma length_carry_full {u} :
    length u = length limb_widths
    -> length (carry_full u) = length limb_widths.
  Proof. intros; unfold carry_full; autorewrite with distr_length; congruence. Qed.
  Hint Rewrite @length_carry_full : distr_length.

  Lemma length_modulus_digits : length modulus_digits = length limb_widths.
  Proof.
    intros; unfold modulus_digits, encodeZ.
    rewrite encode'_spec, encode'_length;
      auto using encode'_length, limb_widths_nonneg, Nat.eq_le_incl, base_from_limb_widths_length.
  Qed.
  Hint Rewrite @length_modulus_digits : distr_length.

  Lemma length_conditional_subtract_modulus {int_width u cond} :
    length u = length limb_widths
    -> length (conditional_subtract_modulus int_width u cond) = length limb_widths.
  Proof.
    intros; unfold conditional_subtract_modulus.
    rewrite map2_length, map_length, length_modulus_digits.
    apply Min.min_case; omega.
  Qed.
  Hint Rewrite @length_conditional_subtract_modulus : distr_length.

  Lemma length_freeze {int_width u} :
    length u = length limb_widths
    -> length (freeze int_width u) = length limb_widths.
  Proof.
    intros; unfold freeze; repeat autorewrite with distr_length; congruence.
  Qed.

  Lemma length_pack : forall {target_widths}
                             {target_widths_nonneg : forall x, In x target_widths -> 0 <= x}
                             {pf us},
      length (pack target_widths_nonneg pf us) = length target_widths.
  Proof.
    cbv [pack]; intros.
    apply length_convert.
  Qed.

  Lemma length_unpack : forall {target_widths}
                             {target_widths_nonneg : forall x, In x target_widths -> 0 <= x}
                             {pf us},
      length (unpack target_widths_nonneg pf us) = length limb_widths.
  Proof.
    cbv [pack]; intros.
    apply length_convert.
  Qed.

End LengthProofs.

Section ModulusDigitsProofs.
  Context `{prm :PseudoMersenneBaseParams}
          (c_upper_bound : c - 1 < 2 ^ nth_default 0 limb_widths 0).
  Local Notation base := (base_from_limb_widths limb_widths).
  Local Hint Resolve sum_firstn_limb_widths_nonneg.
  Local Hint Resolve limb_widths_nonneg.

  Lemma decode_modulus_digits : decode' base modulus_digits = modulus.
  Proof.
    cbv [modulus_digits].
    pose proof c_pos. pose proof modulus_pos.
    rewrite encodeZ_spec by eauto using limb_widths_nonnil, limb_widths_good.
    apply Z.mod_small.
    cbv [upper_bound]. fold k.
    assert (Z.pos modulus = 2 ^ k - c) by (cbv [c]; ring).
    omega.
  Qed.

  Lemma bounded_modulus_digits : bounded limb_widths modulus_digits.
  Proof.
    apply bounded_encodeZ; auto using limb_widths_nonneg.
    pose proof modulus_pos; omega.
  Qed.

  Lemma modulus_digits_ones : forall i, (0 < i < length limb_widths)%nat ->
    nth_default 0 modulus_digits i = Z.ones (nth_default 0 limb_widths i).
  Proof.
    repeat match goal with
           | |- _ => progress (cbv [BaseSystem.decode]; intros)
           | |- _ => progress autorewrite with Ztestbit
           | |- _ => unique pose proof c_pos
           | |- _ => unique pose proof modulus_pos
           | |- _ => unique assert (Z.pos modulus = 2 ^ k - c) by (cbv [c]; ring)
           | |- _ => break_if
           | |- _ => rewrite decode_modulus_digits
           | |- _ => rewrite Z.testbit_pow2_mod
                       by eauto using nth_default_limb_widths_nonneg
           | |- _ => rewrite Z.ones_spec by eauto using nth_default_limb_widths_nonneg
           | |- _ => erewrite digit_select
                        by (eauto; apply bounded_encodeZ; eauto; omega)
           | |- Z.testbit (2 ^ k - c) _ = _ =>
             rewrite Z.testbit_sub_pow2 by (try omega; cbv [k];
               pose proof (sum_firstn_prefix_le limb_widths (S i) (length limb_widths));
               specialize_by (eauto || omega);
               rewrite sum_firstn_succ_default in *; split; zero_bounds; eauto)
           | |- Z.pow2_mod _ _ = Z.ones _ => apply Z.bits_inj'
           | |- Z.testbit (Z.pos modulus) ?i = true => transitivity (Z.testbit (2 ^ k - c) i)
           | |- _ => congruence
           end.

    replace (c - 1) with ((c - 1) mod 2 ^ nth_default 0 limb_widths 0) by (apply Z.mod_small; omega).
    rewrite Z.mod_pow2_bits_high; auto.
    pose proof (sum_firstn_prefix_le limb_widths 1 i).
    specialize_by (eauto ||  omega).
    rewrite !sum_firstn_succ_default, !sum_firstn_0 in *.
    split; zero_bounds; eauto using nth_default_limb_widths_nonneg.
  Qed.

  Lemma bounded_le_modulus_digits : forall us i, length us = length limb_widths ->
    bounded limb_widths us -> (0 < i < length limb_widths)%nat ->
    nth_default 0 us i <= nth_default 0 modulus_digits i.
  Proof.
    intros until 0; rewrite bounded_iff; intros.
    rewrite modulus_digits_ones by omega.
    specialize (H0 i).
    rewrite Z.ones_equiv.
    omega.
  Qed.

End ModulusDigitsProofs.

Section ModulusComparisonProofs.
  Context `{prm :PseudoMersenneBaseParams}
          (c_upper_bound : c - 1 < 2 ^ nth_default 0 limb_widths 0).
  Local Notation base := (base_from_limb_widths limb_widths).
  Local Hint Resolve sum_firstn_limb_widths_nonneg.
  Local Hint Resolve limb_widths_nonneg.

  Fixpoint compare' us vs i :=
    match i with
    | O => Eq
    | S i' => if Z_eq_dec (nth_default 0 us i') (nth_default 0 vs i')
              then compare' us vs i'
              else Z.compare (nth_default 0 us i') (nth_default 0 vs i')
    end.

  (* Lexicographically compare two vectors of equal length, starting from the END of the list
     (in our context, this is the most significant end). NOT constant time. *)
  Definition compare us vs := compare' us vs (length us).

  Lemma decode_firstn_compare' : forall us vs i,
    (i <= length limb_widths)%nat ->
    length us = length limb_widths -> bounded limb_widths us ->
    length vs = length limb_widths -> bounded limb_widths vs ->
    (Z.compare (decode' base (firstn i us)) (decode' base (firstn i vs))
     = compare' us vs i).
  Proof.
    induction i;
      repeat match goal with
             | |- _ => progress intros
             | |- _ => progress (simpl compare')
             | |- _ => progress specialize_by (assumption || omega)
             | |- _ => rewrite sum_firstn_0
             | |- _ => rewrite set_higher
             | |- _ => rewrite nth_default_base by eauto
             | |- _ => rewrite firstn_length, Min.min_l by omega
             | |- _ => rewrite firstn_O
             | |- _ => rewrite firstn_succ with (d := 0) by omega
             | |- _ => rewrite Z.compare_add_shiftl by
               (eauto || (rewrite decode_firstn_pow2_mod, Z.pow2_mod_pow2_mod, Z.min_id by
                  (eauto || omega); reflexivity))
             | |- appcontext[2 ^ ?x * ?y] => replace (2 ^ x * y) with (y << x) by
               (rewrite (Z.mul_comm (2 ^ x)); apply Z.shiftl_mul_pow2; eauto)
             | |- _ => tauto
             | |- _ => split
             | |- _ => break_if
             end.
  Qed.

  Lemma decode_compare' : forall us vs,
    length us = length limb_widths -> bounded limb_widths us ->
    length vs = length limb_widths -> bounded limb_widths vs ->
    (Z.compare (decode' base us) (decode' base vs)
     = compare' us vs (length limb_widths)).
  Proof.
    intros.
    rewrite <-decode_firstn_compare' by (auto || omega).
    rewrite !firstn_all by auto.
    reflexivity.
  Qed.

  Lemma ge_modulus'_0 : forall {A} f us i,
    ge_modulus' (A := A) f us 0 i = f 0.
  Proof.
    induction i; intros; simpl; cbv [cmovne cmovl]; break_if; auto.
  Qed.

  Lemma ge_modulus'_01 : forall {A} f us i b,
    (b = 0 \/ b = 1) ->
    (ge_modulus' (A := A) f us b i = f 0 \/ ge_modulus' (A := A) f us b i = f 1).
  Proof.
    induction i; intros;
      try intuition (subst; cbv [ge_modulus' LetIn.Let_In cmovl cmovne]; break_if; tauto).
    simpl; cbv [LetIn.Let_In cmovl cmovne].
    break_if; apply IHi; tauto.
  Qed.

  Lemma ge_modulus_01 : forall us,
    (ge_modulus us = 0 \/ ge_modulus us = 1).
  Proof.
    cbv [ge_modulus]; intros; apply ge_modulus'_01; tauto.
  Qed.

  Lemma ge_modulus'_true_digitwise : forall us,
    length us = length limb_widths ->
    forall i, (i < length us)%nat -> ge_modulus' id us 1 i = 1 ->
              forall j, (j <= i)%nat ->
                        nth_default 0 modulus_digits j <= nth_default 0 us j.
  Proof.
    induction i;
      repeat match goal with
             | |- _ => progress intros; simpl in *
             | |- _ => progress cbv [LetIn.Let_In cmovne cmovl] in *
             | |- _ =>erewrite (ge_modulus'_0 (@id Z)) in *
             | H : (?x <= 0)%nat |- _ => progress replace x with 0%nat in * by omega
             | |- _ => break_if
             | |- _ => discriminate
             | |- _ => solve [rewrite ?Z.leb_le, ?Z.eqb_eq in *; omega]
             end.
    destruct (le_dec j i).
    + apply IHi; auto; omega.
    + replace j with (S i) in * by omega; rewrite Z.eqb_eq in *; try omega.
  Qed.

  Lemma ge_modulus'_compare' : forall us, length us = length limb_widths -> bounded limb_widths us ->
    forall i, (i < length limb_widths)%nat ->
    (ge_modulus' id us 1 i = 0 <-> compare' us modulus_digits (S i) = Lt).
  Proof.
    induction i;
      repeat match goal with
             | |- _ => progress (intros; cbv [LetIn.Let_In id cmovne cmovl])
             | |- _ => progress (simpl compare' in * )
             | |- _ => progress specialize_by omega
             | |- _ => (progress rewrite ?Z.compare_eq_iff,
                       ?Z.compare_gt_iff, ?Z.compare_lt_iff in * )
             | |- appcontext[ge_modulus' _  _ _ 0] =>
               cbv [ge_modulus']
             | |- appcontext[ge_modulus' _ _ _ (S _)] =>
               unfold ge_modulus'; fold (ge_modulus' (@id Z))
             | |- _ => break_if
             | |- _ => rewrite Nat.sub_0_r
             | |- _ => rewrite (ge_modulus'_0 (@id Z))
             | |- _ => rewrite Bool.andb_true_r
             | |- _ => rewrite Z.leb_compare; break_match
             | |- _ => rewrite Z.eqb_compare; break_match
             | |- _ => (rewrite Z.leb_le in * )
             | |- _ => (rewrite Z.leb_gt in * )
             | |- _ => (rewrite Z.eqb_eq in * )
             | |- _ => (rewrite Z.eqb_neq in * )
             | |- _ => split; (congruence || omega)
             | |- _ => assumption
             end;
       pose proof (bounded_le_modulus_digits c_upper_bound us (S i));
         specialize_by (auto || omega); split; (congruence || omega).
  Qed.

   Lemma ge_modulus_spec : forall u, length u = length limb_widths ->
     bounded limb_widths u ->
    (ge_modulus u = 0 <-> 0 <= BaseSystem.decode base u < modulus).
  Proof.
    cbv [ge_modulus]; intros.
    assert (0 < length limb_widths)%nat
      by (pose proof limb_widths_nonnil; destruct limb_widths;
          distr_length; omega || congruence).
    rewrite ge_modulus'_compare' by (auto || omega).
    replace (S (length limb_widths - 1)) with (length limb_widths) by omega.
    rewrite <-decode_compare'
      by (try (apply length_modulus_digits || apply bounded_encodeZ); eauto;
          pose proof modulus_pos; omega).
    rewrite Z.compare_lt_iff.
    rewrite decode_modulus_digits.
    repeat (split; intros; eauto using decode_nonneg).
    cbv [BaseSystem.decode] in *. omega.
  Qed.

End ModulusComparisonProofs.

Section ConditionalSubtractModulusProofs.
  Context `{prm :PseudoMersenneBaseParams}
          (* B is machine integer width (e.g. 32, 64) *)
          {B} (B_pos : 0 < B) (B_compat : forall w, In w limb_widths -> w <= B)
          (c_upper_bound : c - 1 < 2 ^ nth_default 0 limb_widths 0)
          (lt_1_length_limb_widths : (1 < length limb_widths)%nat).
  Local Notation base := (base_from_limb_widths limb_widths).
  Local Hint Resolve sum_firstn_limb_widths_nonneg.
  Local Hint Resolve limb_widths_nonneg.
  Local Hint Resolve length_modulus_digits.

  Lemma map2_sub_eq : forall us vs, length us = length vs ->
    map2 (fun x y => x - y) us vs = BaseSystem.sub us vs.
  Proof.
    induction us; destruct vs; boring; try omega.
  Qed.

  (* TODO : ListUtil *)
  Lemma map_id_strong : forall {A} f (xs : list A),
    (forall x, In x xs -> f x = x) -> map f xs = xs.
  Proof.
    induction xs; intros; auto.
    simpl; f_equal; auto using in_eq, in_cons.
  Qed.

  Lemma bounded_digit_fits : forall us,
    length us = length limb_widths -> bounded limb_widths us ->
    forall x, In x us -> 0 <= x < 2 ^ B.
  Proof.
    intros.
    let i := fresh "i" in
    match goal with H : In ?x ?us, Hb : bounded _ _ |- _ =>
                    apply In_nth with (d := 0) in H; destruct H as [i [? ?] ];
                      rewrite bounded_iff in Hb; specialize (Hb i);
                        assert (2 ^ nth i limb_widths 0 <= 2 ^ B) by
                          (apply Z.pow_le_mono_r; try apply B_compat, nth_In; omega) end.
    rewrite !nth_default_eq in *.
    omega.
  Qed.

  Lemma map_land_max_ones : forall us,
    length us = length limb_widths ->
    bounded limb_widths us -> map (Z.land (Z.ones B)) us = us.
  Proof.
    repeat match goal with
           | |- _ => progress intros
           | |- _ => apply map_id_strong
           | |- appcontext[Z.ones ?n &' ?x] => rewrite (Z.land_comm _ x);
                                                 rewrite Z.land_ones by omega
           | |- _ => apply Z.mod_small
           | |- _ => solve [eauto using bounded_digit_fits]
           end.
  Qed.

  Lemma map_land_zero : forall us, map (Z.land 0) us = zeros (length us).
  Proof.
    induction us; boring.
  Qed.

  Hint Rewrite @length_modulus_digits @length_zeros : distr_length.
  Lemma conditional_subtract_modulus_spec : forall u cond
    (cond_01 : cond = 0 \/ cond = 1),
    length u = length limb_widths ->
    BaseSystem.decode base (conditional_subtract_modulus B u cond) =
    BaseSystem.decode base u - cond * modulus.
  Proof.
    repeat match goal with
           | |- _ => progress (cbv [conditional_subtract_modulus neg]; intros)
           | |- _ => destruct cond_01; subst
           | |- _ => break_if
           | |- _ => rewrite map_land_max_ones by auto using bounded_modulus_digits
           | |- _ => rewrite map_land_zero
           | |- _ => rewrite map2_sub_eq by distr_length
           | |- _ => rewrite sub_rep by auto
           | |- _ => rewrite zeros_rep
           | |- _ => rewrite decode_modulus_digits by auto
           | |- _ => f_equal; ring
           | |- _ => discriminate
           end.
  Qed.

  Lemma conditional_subtract_modulus_preserves_bounded : forall u,
      length u = length limb_widths ->
      bounded limb_widths u ->
      bounded limb_widths (conditional_subtract_modulus B u (ge_modulus u)).
  Proof.
    repeat match goal with
           | |- _ => progress (cbv [conditional_subtract_modulus neg]; intros)
           | |- _ => unique pose proof bounded_modulus_digits
           | |- _ => rewrite map_land_max_ones by auto using bounded_modulus_digits
           | |- _ => rewrite map_land_zero
           | |- _ => rewrite length_modulus_digits in *
           | |- _ => rewrite length_zeros in *
           | |- _ => rewrite Min.min_l in * by omega
           | |- _ => rewrite nth_default_zeros
           | |- _ => rewrite nth_default_map2 with (d1 := 0) (d2 := 0)
           | |- _ => break_if
           | |- bounded _ _ => apply bounded_iff
           | |- 0 <= 0 < _ => split; zero_bounds; eauto using nth_default_limb_widths_nonneg
           end;
    repeat match goal with
           | H : bounded _ ?x |- appcontext[nth_default 0 ?x ?i] =>
             rewrite bounded_iff in H; specialize (H i)
           | |- _ => omega
           end.
    cbv [ge_modulus] in Heqb.
    rewrite Z.eqb_eq in *.
    apply ge_modulus'_true_digitwise with (j := i) in Heqb; auto; omega.
  Qed.

  Lemma bounded_mul2_modulus : forall u, length u = length limb_widths ->
    bounded limb_widths u -> ge_modulus u = 1 ->
    modulus <= BaseSystem.decode base u < 2 * modulus.
  Proof.
    intros.
    pose proof (@decode_upper_bound _ limb_widths_nonneg u).
    specialize_by auto.
    cbv [upper_bound] in *.
    fold k in *.
    assert (Z.pos modulus = 2 ^ k - c) by (cbv [c]; ring).
    destruct (Z_le_dec modulus (BaseSystem.decode base u)).
    + split; try omega.
      apply Z.lt_le_trans with (m := 2 ^ k); try omega.
      assert (2 * c <= 2 ^ k); try omega.
      transitivity (2 ^ (nth_default 0 limb_widths 0 + 1));
        try (rewrite Z.pow_add_r, ?Z.pow_1_r;
             eauto using nth_default_limb_widths_nonneg; omega).
      apply Z.pow_le_mono_r; try omega.
      unfold k.
      pose proof (sum_firstn_prefix_le limb_widths 2 (length limb_widths)).
      specialize_by (eauto || omega).
      etransitivity; try eassumption.
      rewrite !sum_firstn_succ_default, sum_firstn_0.
      assert (0 < nth_default 0 limb_widths 1); try omega.
      apply limb_widths_pos.
      rewrite nth_default_eq.
      apply nth_In.
      omega.
    + assert (0 <= BaseSystem.decode base u < modulus) as Hlt_modulus by omega.
      apply ge_modulus_spec in Hlt_modulus; auto.
      congruence.
  Qed.

  Lemma conditional_subtract_lt_modulus : forall u,
    length u = length limb_widths ->
    bounded limb_widths u ->
    ge_modulus (conditional_subtract_modulus B u (ge_modulus u)) = 0.
  Proof.
    intros.
    rewrite ge_modulus_spec by auto using length_conditional_subtract_modulus, conditional_subtract_modulus_preserves_bounded.
    pose proof (ge_modulus_01 u) as Hgm01.
    rewrite conditional_subtract_modulus_spec by auto.
    destruct Hgm01 as [Hgm0 | Hgm1]; rewrite ?Hgm0, ?Hgm1.
    + apply ge_modulus_spec in Hgm0; auto.
      omega.
    + pose proof (bounded_mul2_modulus u); specialize_by auto.
      omega.
  Qed.
End ConditionalSubtractModulusProofs.