reserve i,s,t,m,n,k for Nat,
        c,d,e for Element of NAT,
        fn for FinSequence of NAT,
        x,y for Integer;
reserve p for Prime;
 reserve fp,fr for FinSequence of NAT;

theorem
  n>1 & i,n are_coprime implies (i is_primitive_root_of n iff
  for fn st len fn = Euler n &
   (for d being Nat st d in dom fn holds fn.d = i|^d)
 holds rng fn is_RRS_of n)
proof assume A1:n>1 & i,n are_coprime;
  then A2:i <> 0 by Th5;
  reconsider z = 0, j = 1 as Element of REAL by XREAL_0:def 1;
  i - 1 >= 0 by A2,INT_1:52;then
A3:i - 1 + j >= z + j by XREAL_1:7;
A4:i gcd n = 1 by A1,INT_2:def 3;
  thus i is_primitive_root_of n implies for fn st len fn = Euler n
    & (for d being Nat st d in dom fn holds fn.d = i|^d) holds
      rng fn is_RRS_of n
  proof assume i is_primitive_root_of n;
    then A5: order(i,n) = Euler n;
      for fn st len fn = Euler n
     & (for d be Nat st d in dom fn holds fn.d = i|^d) holds
       rng fn is_RRS_of n
   proof let fn;
     assume A6:len fn = Euler n
               & (for d being Nat st d in dom fn holds fn.d = i|^d);
      fn is one-to-one
    proof per cases by A3,XXREAL_0:1;
      suppose
A7:      i = 1;
         reconsider fn as FinSequence of REAL by FINSEQ_2:24, NUMBERS:19;
         fn = <*fn.1*> by FINSEQ_5:14,A6,A5,A1,PEPIN:46,A7;
        hence thesis;
      end;
      suppose A8:i > 1;
        for x1,x2 be object st x1 in dom fn & x2 in dom fn & fn.x1 = fn.x2
         holds x1 = x2
        proof let x1,x2 be object;
          assume A9:x1 in dom fn & x2 in dom fn & fn.x1 = fn.x2;
          then reconsider x1,x2 as Element of NAT;
          i|^x1 = fn.x2 by A9,A6    .= i|^x2 by A9,A6;
         hence thesis by A8,PEPIN:30;
        end;
        hence thesis by FUNCT_1:def 4;
       end;
    end;
    then A10:card(rng fn) = Euler n by A6,FINSEQ_4:62;
A11:  for x,y be Integer st x in rng fn & y in rng fn & x <> y
      holds not x,y are_congruent_mod n
    proof let x,y be Integer;
      assume A12:x in rng fn & y in rng fn & x <> y
                 & x,y are_congruent_mod n;
      then consider s such that
A13:  s in dom fn & fn.s = x by FINSEQ_2:10;
      consider t such that
A14:  t in dom fn & fn.t = y by A12,FINSEQ_2:10;
A15:  x = i|^s & y = i|^t by A13,A14,A6;
A16:  1 <= s & s <= order(i,n) & 1 <= t & t <= order(i,n)
                                            by A13,A14,A5,A6,FINSEQ_3:25;
       per cases by A12,A13,A14,XXREAL_0:1;
      suppose s > t;
        then A17:s - t > 0 by XREAL_1:50;
        then reconsider k = s - t as Element of NAT by INT_1:3;
        i|^s - i|^t = i|^(t+k) - i|^t  .= i|^t*i|^k - i|^t*1 by NEWTON:8
                   .= i|^t * (i|^k - 1);
        then A18:n divides (i|^t * (i|^k - 1)) by A15,A12,INT_2:15;
        i|^t gcd n = 1 by A2,A4,A1,NAT_4:10;
        then n divides (i|^k - 1) by A18,WSIERP_1:29;
        then i|^k,1 are_congruent_mod n by INT_2:15;
        then i|^k mod n = 1 mod n by NAT_D:64  .= 1 by A1,PEPIN:5;then
A19:    order(i,n) <= k by A17,A1,PEPIN:47,NAT_D:7;
        k <= order(i,n)-1 & order(i,n)-1<order(i,n) by A16,XREAL_1:13,146;
       hence contradiction by A19,XXREAL_0:2;
      end;
      suppose s < t;
        then A20:t - s > 0 by XREAL_1:50;
        then reconsider k = t - s as Element of NAT by INT_1:3;
A21:    i|^t - i|^s = i|^(s+k) - i|^s  .= i|^s*i|^k - i|^s*1 by NEWTON:8
                   .= i|^s * (i|^k - 1);
        i|^t,i|^s are_congruent_mod n by A15,A12,INT_1:14;
        then A22:n divides (i|^s * (i|^k - 1)) by A21,INT_2:15;
        i|^s gcd n = 1 by A2,A4,A1,NAT_4:10;
        then n divides (i|^k - 1) by A22,WSIERP_1:29;
        then i|^k,1 are_congruent_mod n by INT_2:15;
        then i|^k mod n = 1 mod n by NAT_D:64  .= 1 by A1,PEPIN:5;then
A23:    order(i,n) <= k by A20,A1,PEPIN:47,NAT_D:7;
        k <= order(i,n)-1 & order(i,n)-1<order(i,n) by A16,XREAL_1:13,146;
       hence contradiction by A23,XXREAL_0:2;
      end;
    end;
A24: for x be Integer st x in rng fn holds x,n are_coprime
    proof let x be Integer;
      assume x in rng fn;
      then consider s such that
A25:  s in dom fn & fn.s = x by FINSEQ_2:10;
A26:  x = i|^s by A25,A6;
      i|^s gcd n = 1 by A2,A4,A1,NAT_4:10;
     hence thesis by A26,INT_2:def 3;
    end;
    rng fn c= INT by RELAT_1:def 19;
    hence thesis by A1,A10,A11,A24,Th26;
   end;
  hence thesis;
  end;
  assume A27:for fn st len fn = Euler n
    & (for d being Nat st d in dom fn holds fn.d = i|^d) holds
      rng fn is_RRS_of n;
    deffunc G(Nat) = i|^$1;
    consider f be FinSequence such that
A28:len f = Euler n & for d being Nat st d in dom f holds f.d=G(d)
       from FINSEQ_1:sch 2;
     for s st s in dom f holds f.s in NAT
     proof let s;
       assume A29:s in dom f;
       f.s = i|^s by A28,A29;
      hence thesis by ORDINAL1:def 12;
     end;
    then reconsider f as FinSequence of NAT by FINSEQ_2:12;
A30:rng f is_RRS_of n by A28,A27;then
card rng f = Euler n & (for x,y be Integer st x in rng f
      & y in rng f & x<>y holds not x,y are_congruent_mod n) by Th24;
    then A31:f is one-to-one by A28,FINSEQ_4:62;
    Euler n <> 0 by A1,PEPIN:42;
then
A33:Euler n > 0;
A34:Euler n >= 1 by A1,PEPIN:42,NAT_1:14;
A35:(i |^ Euler n) mod n = 1 by A1,Th5,EULER_2:18;
       for s st s > 0 & (i|^s) mod n = 1 holds Euler n <= s
     proof let s;
        assume A36:s > 0 & (i|^s) mod n = 1;
          now assume s < Euler n;
           then A37:Euler n - s > 0 by XREAL_1:50;
           then reconsider k = Euler n - s as Element of NAT by INT_1:3;
A38:       k < Euler n & k >= 1 by A36,A37,XREAL_1:44,NAT_1:14;then
A39:       k in dom f & Euler n in dom f by A28,A34,FINSEQ_3:25; then
A40:       f.k in rng f & f.(Euler n) in rng f by FUNCT_1:3;
           f.k <> f.(Euler n) by A39,A31,A38,FUNCT_1:def 4;
           then not f.k,f.(Euler n) are_congruent_mod n by A40,A30,Th24;
           then not i|^k,f.(Euler n) are_congruent_mod n by A39,A28;then
A41:       not i|^k,i|^(Euler n) are_congruent_mod n by A39,A28;
           i|^(Euler n),i|^s are_congruent_mod n by A36,A35,A1,NAT_D:64;
           then A42:n divides (i|^(Euler n) - i|^s) by INT_2:15;
A43:       i|^(Euler n) - i|^s = i|^(s+k)-i|^s
                              .= i|^s*i|^k - i|^s*1 by NEWTON:8
                              .= i|^s * (i|^k - 1);
           i|^s gcd n = 1 by A2,A4,A1,NAT_4:10;then
A44:       n divides (i|^k - 1) by A42,A43,WSIERP_1:29;
           (i |^ Euler n) mod n = 1 mod n by A1,A35,PEPIN:5;
           then i|^Euler n,1 are_congruent_mod n by A1,NAT_D:64;
           then n divides (i|^Euler n - 1) by INT_2:15;
           then n divides (i|^k - 1 - (i|^Euler n - 1)) by A44,INT_5:1;
           then n divides (i|^k - i|^Euler n);
          hence contradiction by A41,INT_2:15;
          end;
       hence thesis;
    end;
    then order(i,n) = Euler n by A1,A33,A35,PEPIN:def 2;
  hence thesis;
end;
