
theorem
  for a,n,m be Element of NAT st a <> 0 & 1 < m holds
  ALGO_BPOW(a,n,m) = a to_power n mod m
  proof
    let a, n,m be Element of NAT;
    assume AS: a <> 0 & 1 < m;
    consider A,B be sequence of NAT such that
    ASC:
    ALGO_BPOW(a,n,m) = B. (LenBSeq n) &
    A.0 = a mod m & B.0 = 1 &
    ( for i be Nat holds
    A.(i+1) = (A.i)*(A.i) mod m &
    B.(i+1) = BinBranch((B.i),(B.i)*(A.i) mod m,(Nat2BL.n).(i+1))) by Def1;
    set NBS = Nat2BL.n;
    NBS = (LenBSeq n)-BinarySequence n by BINARI_6:def 2; then
    reconsider NBS as Tuple of (LenBSeq n),BOOLEAN;
    defpred P[Nat] means
    $1 < LenBSeq n implies
    (
    ex SUBBS be Tuple of ($1+1), BOOLEAN st
    SUBBS = (Nat2BL.n) | ($1 +1) &
    B.($1 +1) = (a to_power Absval(SUBBS)) mod m);
    LC1:P[0]
    proof
      reconsider NBS0 = NBS| (0 +1) as Tuple of 1, BOOLEAN;
      PCJJ:
      B.(0+1) = BinBranch((B.0),(B.0)*(A.0) mod m,(Nat2BL.n).(0+1)) by ASC;
      then
      PCC0:B.1 = BinBranch(1,a mod m, (Nat2BL.n).1) by NAT_D:73,ASC;
      per cases by BINARITH:14;
      suppose LCT: NBS0 = <*TRUE*>; then
        LCT1:Absval(NBS0) = 1 by BINARITH:16;
        <* NBS.1 *>.1 = <* TRUE*>.1 by FINSEQ_5:20,LCT; then
        B.1 = (a to_power 1) mod m by defBB,PCC0,XBOOLEAN:def 2;
        hence thesis by LCT1;
      end;
      suppose LCF: NBS0 = <*FALSE*>; then
        LCF1:Absval(NBS0) = 0 by BINARITH:15;
        <* NBS.1 *>.1 = <* FALSE*>.1 by FINSEQ_5:20,LCF; then
        B.1 = 1 by defBB,ASC,PCJJ,XBOOLEAN:def 1
        .= 1 mod m by NAT_D:14,AS
        .=(a to_power 0) mod m by POWER:24;
        hence thesis by LCF1;
      end;
    end;
    LC2: for i be Nat st P[i] holds P[i+1]
    proof
      let i be Nat;
      assume ASPi:P[i];
      assume I1: i+1 < LenBSeq n;
      i < LenBSeq n
      proof
        assume
    COV:i >= LenBSeq n;
        i + 1 - 1 < LenBSeq n -0 by I1,XREAL_1:14;
        hence contradiction by COV;
      end; then
      consider SUBBS be Tuple of (i+1), BOOLEAN such that
      CLa: SUBBS = (Nat2BL.n) | (i +1) &
      B.(i + 1) = (a to_power Absval(SUBBS)) mod m by ASPi;
      set j = i + 1;
      thus ex SUBBS1 be Tuple of (j+1), BOOLEAN st
      SUBBS1 = (Nat2BL.n) | (j +1) &
        B.(j +1) = (a to_power Absval(SUBBS1)) mod m
      proof
        LINT:j+1 <= LenBSeq n by INT_1:7,I1;
        1 <= j+1 by NAT_1:11; then
        j +1 in Seg(LenBSeq n) by LINT; then
        CHO:j+1 in dom NBS by FINSEQ_1:89;
        set SUBBS1 = (Nat2BL.n) | (j +1);
        Lhyo:j+1 <= len NBS by FINSEQ_3:153,LINT;
        Sho:SUBBS1 is Element of BOOLEAN* by FINSEQ_1:def 11;
        len SUBBS1 = min(j+1,len NBS) by FINSEQ_2:21; then
        len SUBBS1 = j+1 by Lhyo,XXREAL_0:def 9; then
        SUBBS1 in (j+1)-tuples_on BOOLEAN by Sho; then
        reconsider SUBBS1 as Tuple of (j+1), BOOLEAN;
        set BC=IFEQ(NBS.(j+1),FALSE,0,2 to_power j);
        CCL2:SUBBS1= SUBBS^<*NBS.(j+1)*> by FINSEQ_5:10,CLa,CHO;
        PO:NBS.(j+1) is Element of BOOLEAN by FINSEQ_1:84,CHO;
        reconsider X = NBS.(j+1) as Nat;
        ZZ: Absval(SUBBS1) = Absval(SUBBS)+BC by BINARITH:20,CCL2,PO;
        LMT1: B.(j+1) = BinBranch((B.j),(B.j)*(A.j) mod m,X) by ASC;
        take SUBBS1;
        thus SUBBS1 = (Nat2BL.n) | (j +1);
        thus B.(j +1) = (a to_power Absval(SUBBS1)) mod m
        proof
          per cases;
          suppose LPP:X= 0; then
            BC = 0 by FUNCOP_1:def 8,XBOOLEAN:def 1;
            hence thesis by ZZ,CLa,LMT1,defBB,LPP;
          end;
          suppose LPP1:X<>0; then
            B.(j+1) = (B.j)*(A.j) mod m by LMT1,defBB
            .= a to_power ((Absval(SUBBS)) + 2 to_power j) mod m
              by CBPOW2,ASC,AS,CLa;
            hence thesis by FUNCOP_1:def 8,XBOOLEAN:def 1,LPP1,ZZ;
          end;
        end;
      end;
    end;
    LC3: for i be Nat holds P[i] from NAT_1:sch 2(LC1,LC2);
    (LenBSeq n)-1 in NAT by INT_1:5,NAT_1:14;
    then reconsider fs = (LenBSeq n)-1 as Nat;
    consider FSBS be Tuple of (fs+1), BOOLEAN such that
    Lcfs: FSBS = (Nat2BL.n) | (fs +1) &
    B.(fs +1) = (a to_power Absval(FSBS)) mod m by LC3,XREAL_1:44;
    reconsider FSBS as Tuple of LenBSeq n, BOOLEAN;
    dom NBS = Seg LenBSeq n by FINSEQ_1:89;
    hence thesis by ASC,BINARI_6:10,Lcfs;
  end;
