Advertisement
mrAnderson33

Поля Галуа

Jun 8th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Lab02_5GF_
  8. {
  9.     class GF
  10.     {
  11.         private byte binaryPolynom;
  12.  
  13.         public GF(byte input) =>
  14.             binaryPolynom = input;
  15.  
  16.         public byte Polynom => binaryPolynom;
  17.  
  18.         static public GF operator +(GF left, GF right) => new GF((byte)(left.Polynom ^ right.Polynom));
  19.  
  20.         static public GF operator*(GF left, GF right)
  21.         {
  22.             var result = default(ushort);
  23.             for (var i = 0; i < 8; i++)
  24.                 for (var j = 0; j < 8; j++)
  25.                     result ^= (ushort)((left[i] & right[j]) << (i + j));
  26.  
  27.             return new GF((byte)DivisionRemainder(result,11));
  28.         }
  29.  
  30.         static public ushort DivisionRemainder(ushort firstOperand, ushort secondOperand)
  31.         {
  32.             if (secondOperand == 0)
  33.                 throw new ArgumentException("secondOperand");
  34.  
  35.             if (firstOperand == 0)
  36.                 return 0;
  37.  
  38.             if (firstOperand == 1)
  39.             {
  40.                 if (secondOperand == 1)
  41.                     return 0;
  42.                 return 1;
  43.             }
  44.  
  45.             var firstOperandMaxPower = getMaxPow(firstOperand);
  46.             var secondOperandMaxPower = getMaxPow(secondOperand);
  47.  
  48.             while (firstOperandMaxPower >= secondOperandMaxPower)
  49.             {
  50.                 firstOperand ^= (ushort)(secondOperand << (firstOperandMaxPower - secondOperandMaxPower));
  51.                 firstOperandMaxPower = getMaxPow(firstOperand);
  52.             }
  53.  
  54.             return firstOperand;
  55.         }
  56.  
  57.         static public byte getMaxPow(ushort num)
  58.         {
  59.             var numCopy = num;
  60.             byte pow = 0;
  61.  
  62.             while(numCopy != 1)
  63.             {
  64.                 numCopy >>= 1;
  65.                 pow++;
  66.             }
  67.        
  68.             return pow;
  69.         }
  70.  
  71.  
  72.        public  static void modInverse(int a, int m)
  73.         {
  74.             int g = gcd(a, m);
  75.             if (g != 1)
  76.                 Console.Write("Inverse doesn't exist");
  77.             else
  78.             {
  79.                 // If a and m are relatively
  80.                 // prime, then modulo inverse
  81.                 // is a^(m-2) mode m
  82.                 Console.Write("Modular multiplicative inverse is " +
  83.                                                  power(a, m - 2, m));
  84.             }
  85.         }
  86.  
  87.         // To compute x^y under
  88.         // modulo m
  89.         static int power(int x, int y, int m)
  90.         {
  91.             if (y == 0)
  92.                 return 1;
  93.  
  94.             int p = power(x, y / 2, m) % m;
  95.             p = (p * p) % m;
  96.  
  97.             if (y % 2 == 0)
  98.                 return p;
  99.             else
  100.                 return (x * p) % m;
  101.         }
  102.  
  103.         // Function to return
  104.         // gcd of a and b
  105.         static int gcd(int a, int b)
  106.         {
  107.             if (a == 0)
  108.                 return b;
  109.             return gcd(b % a, a);
  110.         }
  111.  
  112.         public int this[int index] => ((1 << index) & binaryPolynom) >> index;
  113.  
  114.         public override string ToString()
  115.         {
  116.             var GFInStr = string.Empty;
  117.  
  118.             for (int i = 7; i >= 0; i--)
  119.             {
  120.                 if(this[i] != 0)
  121.                     GFInStr += i == 0 ? $"1" : i > 1 ? $"x^{i}" : $"x";
  122.                 if (i != 0 && i!= 7) GFInStr += "+";              
  123.             }
  124.  
  125.             return GFInStr;
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement