Section const_1_cs.
Variable (w u u1 u2 : nat).
Hypothesis (H18 : 1+(r-1)*w = power (S (power (S l) 2)) r)
(H19 : u*u = u1 + u2)
(H20 : u1 = (u*u) ⇣ w)
(H21 : u2 = (u*u) ⇣ (2*w))
(H22 : power 2 r + u1 = u + power (power (S l) 2) r)
(H23 : divides (power 4 r) u1).
Let Hw_0 : w = ∑ (S (power (S l) 2)) (fun i => power i r).
Let m := projT1 mk_full.
Let k := proj1_sig (projT2 mk_full).
Let Hu1 : u1 = ∑ (S m) (fun i => power (k i) r).        Proof. apply (proj2_sig (projT2 mk_full)). Qed.
Let Hm : m <= (power (S l) 2).                          Proof. apply (proj2_sig (projT2 mk_full)). Qed.
Let Hk1 : forall i, i < S m -> k i <= power (S l) 2.    Proof. apply (proj2_sig (projT2 mk_full)). Qed.
Let Hk2 : forall i j, i < j < S m -> k i < k j.         Proof. apply (proj2_sig (projT2 mk_full)). Qed.
Let Hh_0 : 4 <= k 0.
Let f1 i := match i with 0 => 2 | S i => k i end.
Let f2 i := if le_lt_dec i m then power (S l) 2 else k i.
Let Hf1_0 : forall i, i <= S m -> f1 i < S (power (S l) 2).
Let Hf1_1 : forall i j, i < j <= S m -> f1 i < f1 j.
Let Hf1_2 : ∑ (S (S m)) (fun i => power (f1 i) r) = u + power (power (S l) 2) r.
Let Hh_1 : k m = power (S l) 2.
Let Hu : u = ∑ (S m) (fun i => power (f1 i) r).
Let Huu : u*u = ∑ (S m) (fun i => power (2*f1 i) r)
+ ∑ (S m) (fun i => ∑ i (fun j => 2*power (f1 i + f1 j) r)).
(* This one should not be that hard given S l < q but to check *)
Let HSl_q : 2 * S (power (S l) 2) < power (2 * q) 2.
Let Hu1_1 : { d | d <= S m /\ u1 = ∑ d (fun i => power (2*f1 i) r) }.
Let Hk_final : k 0 = 4 /\ forall i, i < m -> k (S i) = 2*k i.
Let Hk_is_power i : i <= m -> k i = power (S (S i)) 2.
Let Hm_is_l : S m = l.
Fact obtain_u_u1_value :  u  = ∑ l (fun i => power (power (S i) 2) r)
/\ u1 = ∑ l (fun i => power (power (S (S i)) 2) r).
End const_1_cs.
End const_1.
Variable (l q : nat).
Notation r := (power (4*q) 2).
Definition seqs_of_ones u u1 :=
l+1 < q
/\ u  = ∑ l (fun i => power (power (S i) 2) r)
/\ u1 = ∑ l (fun i => power (power (S (S i)) 2) r).
(* This lemma shows that seqs_of_ones can be encoded by a diophantine expression *)
Definition is_cipher_of f a :=
l+1 < q
/\ (forall i, i < l -> f i < power q 2)
/\ a = ∑ l (fun i => f i * power (power (S i) 2) r).
Fact is_cipher_of_0 f a : l = 0 -> is_cipher_of f a <-> 1 < q /\ a = 0.
Fact is_cipher_of_inj f1 f2 a : is_cipher_of f1 a -> is_cipher_of f2 a -> forall i, i < l -> f1 i = f2 i.
Fact is_cipher_of_fun f1 f2 a b :
(forall i, i < l -> f1 i = f2 i)
-> is_cipher_of f1 a
-> is_cipher_of f2 b
-> a = b.
Fact is_cipher_of_u : l+1 < q -> is_cipher_of (fun _ => 1) (∑ l (fun i => power (power (S i) 2) r)).
(* Fact is_cipher_of_u1 : l+1 < q -> is_cipher_of (fun _ => 1) (∑ l (fun i => power (power (S (S i)) 2) r)). *)
Definition the_cipher f : l+1 < q -> (forall i, i < l -> f i < power q 2) -> { c | is_cipher_of f c }.
Definition Code a := exists f, is_cipher_of f a.

Lemma Code_dio a : Code a <-> l = 0 /\ 1 < q /\ a = 0
\/ 0 < l /\ l+1 < q /\ exists p u u1, p+1 = power q 2 /\ seqs_of_ones u u1 /\ a ≲ p*u.