End sum_sum_regroup.
Section all_ones.
Let equation_inj x y a b : 1 <= x -> 1+x*a = y -> 1+x*b = y -> a = b.
Variables (r : nat) (Hr : 2 <= r).
Fact all_ones_equation l : 1+(r-1)*∑ l (fun i => power i r) = power l r.
Fact all_ones_dio l w : w = ∑ l (fun i => power i r) <-> 1+(r-1)*w = power l r.
End all_ones.
Section const_1.
Variable (l q : nat) (Hl : 0 < l) (Hlq : l+1 < q).
Let Hq : 1 <= q.     Proof. omega. Qed.
Let Hq' : 0 < 4*q.   Proof. omega. Qed.
Let r := (power (4*q) 2).
Let Hr' : 4 <= r.    Proof. apply (@power_mono_l 2 (4*q) 2); omega. Qed.
Let Hr :  2 <= r.    Proof. omega. Qed.
Section all_ones.
Variable (n w : nat) (Hw : w = ∑ n (fun i => power i r)).
Let Hw_0 : w = ∑ n (fun i => 1*power i r).
Let Hw_1 : 2*w = ∑ n (fun i => 2*power i r).
Fact all_ones_2_joins : 2*w = msum nat_join 0 n (fun i => 2*power i r).
End all_ones.
Section increase.
Variable (m k k' u w : nat) (f : nat -> nat)
(Hm : 2*m < r)
(Hf1 : forall i, i < m -> f i <= k)
(Hf2 : forall i j, i < j < m  -> f i < f j)
(Hw : w = ∑ k' (fun i => power i r))
(Hu : u = ∑ m (fun i => power (f i) r)).
Let Hf4 : forall i j, i < m -> j < m -> f i = f j -> i = j.
Let Hu1_0 : u1 = ∑ m (fun i => 1*power (2*f i) r).
Let Hu1 : u1 = msum nat_join 0 m (fun i => 1*power (2*f i) r).
Let Hu2_0 : u2 = 2 * ∑ m (fun i => ∑ i (fun j => power (f i + f j) r)).
(* MAJOR change in the argumentation ... one cannot show in generalize that the powers r^(f i + f j) are distincts powers for the values j < i < n, hence it is not correct than the sum reduces to a join ... it works when f i = 2^i but not for an arbitrary (increasing function) f  So we rewrite ∑ {j < i < n} r^(f i + f j) as ∑ {i < k} (g i)*r^i for some small g i <= n supposing n is low compared to r *)
Let g_full : { g | ∑ m (fun i => ∑ i (fun j => power (f i + f j) r))
= ∑ (2*k) (fun i : nat => g i * power i r)
/\ forall i : nat, g i <= m }.
Let Hg2 i : 2*g i <= 2*m.
Let Hu1_u2_1 : u1 ⇣ u2 = 0.
Let Hu1_u2 : u*u = u1 ⇡ u2.
Let Hw_1 : w = msum nat_join 0 k' (fun i => 1*power i r).
Fact const_u1_prefix : { q | q <= m /\ u*u ⇣ w = ∑ q (fun i => 1*power (2*f i) r) }.
Hypothesis (Hk : 2*k < k').
Let Hu1_w : u1 ⇣ w = u1.
Let Hu1_2w : u1 ⇣ (2*w) = 0.
Fact const_u1_meet p : p = (u*u) ⇣ w <-> p = u1.
Fact const_u1_eq : (u*u) ⇣ w = u1.
(* This cannot be proved anymore without stronger hypothesis on f *)
Let Hu2_2w : u2 ⇣ (2*w) = u2.
Fact const_u2_meet p : p = (u*u) ⇣ (2*w) <-> p = u2.
End increase.
Let Hl'' : 2*l < r.
Section const_1_cn.
(* Perhaps you should encode the predicate that  w = ∑ {i=0..2^{l+1}} r^i u = ∑ {1..l} r^{2^i} and u1 = u*u ⇣ w  as diophantine and use that predicate because it is used for Const1 and the product and CodeNat *)
Variable (u u1 : nat) (Hu  : u = ∑ l (fun i => power (power (S i) 2) r))
(Hu1 : u1 = ∑ l (fun i => power (power (S (S i)) 2) r)).
Let w  := ∑ (S (power (S l) 2)) (fun i => power i r).
(*     Let u1 := ∑ l (fun i => power (power (S (S i)) 2) r). *)
Let u2 := ∑ l (fun i => ∑ i (fun j => 2*power (power (S i) 2 + power (S j) 2) r)).
Let H18 : 1+(r-1)*w = power (S (power (S l) 2)) r.
Let k := S (power (S l) 2).
Let f i := power (S i) 2.
Let Hf1 i : i < l -> 2*f i < k.
Let Hf2 i j : i < j < l -> f i < f j.
Let H20 : u1 = (u*u) ⇣ w.
Let H21 : u2 = (u*u) ⇣ (2*w).
Let H22 : power 2 r + u1 = u + power (power (S l) 2) r.
Let H23 : divides (power 4 r) u1.

(* Specifications: *)
Lemma const1_cn : exists w u2,    1+(r-1)*w = power (S (power (S l) 2)) r
/\ u*u = u1 + u2
/\ u1 = (u*u) ⇣ w
/\ u2 = (u*u) ⇣ (2*w)
/\ power 2 r + u1 = u + power (power (S l) 2) r
/\ divides (power 4 r) u1.