(**************************************************************)
(*   Copyright Dominique Larchey-Wendling [*]                 *)
(*                                                            *)
(*                             [*] Affiliation LORIA -- CNRS  *)
(**************************************************************)
(*      This file is distributed under the terms of the       *)
(*         CeCILL v2 FREE SOFTWARE LICENSE AGREEMENT          *)
(**************************************************************)
(** ** Sparse ciphers *)
Set Implicit Arguments.
Local Notation power := (mscal mult 1).
Local Notation "∑" := (msum plus 0).
Local Infix "≲" := binary_le (at level 70, no associativity).
Local Infix "⇣" := nat_meet (at level 40, left associativity).
Local Infix "⇡" := nat_join (at level 50, left associativity).
Hint Resolve power2_gt_0.
Section stability_of_power.
Fact mult_lt_power_2 u v k : u < power k 2 -> v < power k 2 -> u*v < power (2*k) 2.
Fact mult_lt_power_2_4 u v k : u < power k 2 -> v < power k 2 -> u*v < power (4*k) 2.
Fact mult_lt_power_2_4' u1 v1 u2 v2 k :
u1 < power k 2
-> v1 < power k 2
-> u2 < power k 2
-> v2 < power k 2
-> u1*v1+v2*u2 < power (4*k) 2.
End stability_of_power.
Section power_decomp.
Variable (p : nat) (Hp : 2 <= p).
Let power_nzero x : power x p <> 0.

(* Specifications: *)
Lemma power_decomp_is_digit n a f :
(forall i j, i < j < n -> f i < f j)
-> (forall i, i < n -> a i < p)
->  forall i, i < n -> is_digit (∑ n (fun i => a i * power (f i) p)) p (f i) (a i).
Theorem power_decomp_unique n f a b :
(forall i j, i < j < n -> f i < f j)
-> (forall i, i < n -> a i < p)
-> (forall i, i < n -> b i < p)
-> ∑ n (fun i => a i * power (f i) p)
= ∑ n (fun i => b i * power (f i) p)
-> forall i, i < n -> a i = b i.