reserve x for set;
reserve i,j for Integer;
reserve n,n1,n2,n3 for Nat;
reserve K,K1,K2,K3 for Field;
reserve SK1,SK2 for Subfield of K;
reserve ek,ek1,ek2 for Element of K;
reserve p for Prime;
reserve a,b,c for Element of GF(p);
reserve F for FinSequence of GF(p);

theorem Th39:
  2 < p implies card({b : b|^2 = a}) = 1 + Lege_p(a)
  proof
    assume A1:2 < p;
    per cases;
    suppose A2: a is quadratic_residue; then
      consider x being Element of GF(p) such that
  A3: x|^2= a;
  A4: x in {b : b|^2 = a} by A3;
      (-x) |^2 = (-x)*(-x) by Lm2
      .=x*x by VECTSP_1:10
      .=a by A3,Lm2; then
      -x in {b : b|^2 = a}; then
      A5: {x,-x } c= {b : b|^2 = a} by A4,ZFMISC_1:32;
      A6: x <> -x
      proof
        assume x = -x; then
        x+x = 0.(GF(p)) by VECTSP_1:16; then
        A7: x = 0.(GF(p)) by A1,Th27;
        x|^2 = (0.(GF(p))) * (0.(GF(p))) by A7,Lm2
        .= 0.(GF(p))
        .= 0 by Th11;
        hence contradiction by A3,A2;
      end;
      now let y be object;
        assume y in {b : b|^2 = a}; then
        consider z be Element of GF(p) such that
   A8: y=z & z|^2 = a;
        z*z = z|^2 by Lm2
        .=x*x by A3,A8,Lm2; then
        z = x or z = -x by Th26;
        hence y in {x,-x } by A8,TARSKI:def 2;
      end; then
      {b : b|^2 = a} c={x,-x };
      hence card ({b : b|^2 = a}) = card ({x,-x }) by A5,XBOOLE_0:def 10
      .= 1+1 by A6,CARD_2:57
      .= 1+Lege_p(a) by A2,Def5;
    end;
    suppose A9: not a is quadratic_residue;
      now per cases;
        suppose A10: a = 0;
          thus card({b : b|^2 = a})=1+Lege_p(a)
          proof
            now let x be object;
              assume x in {b : b|^2 = a};
              then consider b such that
              A11: x = b & b|^2 = 0 by A10;
              b = 0 by Th25,A11
              .= 0.(GF(p)) by Th11;
              hence x in {0.(GF(p))} by A11,TARSKI:def 1;
            end; then
            A12: {b : b|^2 = a} c= {0.(GF(p))};
            (0.(GF(p))) |^2 = (0.(GF(p))) * (0.(GF(p))) by Lm2
            .= 0.(GF(p))
            .= 0 by Th11; then
            0.(GF(p)) in {b : b|^2 = a} by A10; then
            {0.(GF(p))} c= {b : b|^2 = a} by ZFMISC_1:31; then
            {b : b|^2 = a} = {0.(GF(p))} by A12,XBOOLE_0:def 10;
            hence card ({b : b|^2 = a}) = 1+0 by CARD_2:42
            .= 1+ Lege_p(a) by A10,Def5;
          end;
        end;
        suppose A13: a <> 0;
          A14: {b : b|^2 = a} = {}
          proof
            assume {b : b|^2 = a} <> {}; then
            consider x be object
            such that A15: x in {b : b|^2 = a} by XBOOLE_0:def 1;
            ex b st x = b & b|^2 = a by A15;
            hence contradiction by A9,A13;
          end;
          thus card({b : b|^2 = a}) = 1 + - 1 by A14
          .= 1+ Lege_p(a) by A9,A13,Def5;
        end;
      end;
      hence card({b : b|^2 = a})=1+Lege_p(a);
    end;
  end;
