Download

GF 1.0 Doc

Galois Fields

 

Definition

Console.WriteLine("2006 Paul Mizel www.darkleo.com");
GF a = new GF("1011");
GF b = new GF("1010"
);
Console.WriteLine("a = " + a+" b = " + b);

Addition
1011+1010=0001
Console.WriteLine("+ = " + (a + b));
out: +=1

Multiplikation
1011*1010=1001110
Console.WriteLine("* = " + (a * b));
out: *=1001110

Rest von GF 2^4
(1011*1010)%10011=0100
Console.WriteLine("rest = " + (a * b % new GF(8)));
out: rest = 100

Rest Algorithmus:
1)Multipliziere:

   1000 * 1001= (x^3)*(x^3+1)
erzeugt eine Liste:
4+4=1000000
4+1=0010000

gleicht vorkommende die 2 mal und das vielfache davon sind, werden entfernt.
die liste wird dann zusammen gesetzt
1000000
0001000
--------
1001000 = x^6+x^3

2)Funktions Mod
1001000 / 10011 =
man setzt so viele 0 ans ende dass es passt und XORt die werte
1001000
1001100
_______
0000100 = Lösch die 0en davor, wiederhole solange die Länge größer oder gleich ist als 10011

Lösung: 100


 

Main
static void Main(string[] args)
        {
            Console.WriteLine("2006 Paul Mizel www.darkleo.com");
            GF a = new GF("1011");
            GF b = new GF("1010");
            Console.WriteLine("a = " + a+" b = " + b);
            Console.WriteLine("+ = " + (a + b));
            Console.WriteLine("* = " + (a * b));

            Console.WriteLine("rest = " + (a * b % new GF(16)));


            //x^4+x+1
            Console.WriteLine("\r\nTestprojekt für GF2^4 mit iPolynom:"+new GF(16));

            bool nstop=true;
            while(nstop)
            {
                Console.Write("\r\nErste GF Zahl   :");
                 GF g1 = new GF(Console.In.ReadLine());
                 if (g1.Data == "")
                     break;
                 Console.Write("Zweite GF Zahl  :");
                 GF g2 = new GF(Console.In.ReadLine());
                 if (g2.Data == "")
                     break;
                 GF r = g1 * g2 % new GF("10011");
                 Console.WriteLine("Rest            :" + r);
            }
            Console.In.ReadLine();
        }

 

GF
//Paul Mizel
// * ist normale multiplokation man verlässt =X^2*x^2=x^4
// % modulo liefert den rest
// + und - sind gleich aber die länge der strings muss gleich sein
// bitweises addieren 
//10000
//11100 = 01100
// div ist nicht implementiert
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace TestGF
{

    public class GF
    {
        string data;
        public string Data
        {
            get { return data; }
            set { data = value; }
        }

        #region CTOR
        public GF()
        {
        }

        public GF(GF g)
        {
            data = g.data;
        }

        public GF(string s)
        {
            this.data = s;
        }
        public GF(int gf_ipolynom)
        {
            this.data = GF.GET_IPOLYNOM(gf_ipolynom);
        }
        #endregion
        public static string i111 = "111";
        public static string i1101 = "1101";
        public static string i10011 = "10011";        
        /// <summary>
        /// 2^2 2^3 und 2^4 können angegeben werden
        /// </summary>
        /// <param name="gf"></param>
        /// <returns></returns>
        public static string GET_IPOLYNOM(int gf)
        {
            string ipolynom = "";
            switch (gf)
            {
                case 4: ipolynom  = GF.i111; break;   //x^2+x+1                
                case 8: ipolynom  = GF.i1101; break;  //x^3+x^2+1
                case 16: ipolynom = GF.i10011; break; // x^4+x+1
            }            
            return ipolynom;
        }


        public static string Rest(string s1, string s2)
        {
            return ((new GF(s1))%(new GF(s2))).Data;
        }
        public static string MultiplikationRestbetrag(string s1, string s2,string ipol)
        {
            string s = Multiplikation(s1, s2);
            return Rest(s, ipol);
        }
        
        public static string Addition(string s1, string s2)
        {
            return ((new GF(s1)) + (new GF(s2))).Data;
        }
        public static string Subtraktion(string s1, string s2)
        {
            return ((new GF(s1)) + (new GF(s2))).Data;
        }
        public static string Multiplikation(string s1, string s2)
        {
            return ((new GF(s1)) * (new GF(s2))).Data;
        }

        #region operationen
        public static GF operator *(GF g1, GF g2)
        {
            string ret = mul(g1.Data, g2.Data).TrimStart('0');
            return new GF(ret);
        }
        public static GF operator +(GF g1, GF g2)
        {
            return new GF(add(g1.Data, g2.Data));
        }

        public static GF operator -(GF g1, GF g2)
        {
            return new GF(add(g1.Data, g2.Data));
        }

        public static GF operator %(GF g1, GF g2)
        {
            string rest = mod(g1.Data, g2.Data);
            return new GF(rest);
        }
        #endregion


        /// <summary>
        /// Modulo Recursiv mit iPolynom
        /// </summary>
        /// <param name="s"></param>
        /// <param name="ipol"></param>
        /// <returns></returns>
        static string mod(string s, string ipol)
        {
            string ret = "";
            if (s.Length >= ipol.Length)
            {
                string tmp = ipol;
                while (s.Length > tmp.Length)
                    tmp = tmp + "0";
                ret = add(s, tmp);
                if (ipol.Length <= ret.Length)
                    ret = mod(ret, ipol);
            }
            else
                ret = s;
            return ret;
        }

        /// <summary>
        /// addiert zwei GFs
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        static string add(string a, string b)
        {
            string ret = "";
            for (int i = 0; i < a.Length; i++)
                if (a[i] == b[i])
                    ret += "0";
                else
                    ret += "1";
            ret = ret.TrimStart('0');
            return ret;
        }

        /// <summary>
        /// Multipliziert zwei GFs
        /// </summary>
        /// <param name="s1"></param>
        /// <param name="s2"></param>
        /// <returns></returns>
        static string mul(string s1, string s2)
        {
            int l1 = s1.Length;
            int l2 = s2.Length;
            int size = l1 + l2 - 1;
            ArrayList al = new ArrayList();
            for (int i = 0; i < s1.Length; i++)
                if (s1[i] == '1')
                    for (int j = 0; j < s2.Length; j++)
                        if (s2[j] == '1')
                            al.Add(create(size, size - (i + j)));


            ArrayList list = cleardoubledata(al);
            return adddata(list);
        }

        /// <summary>
        /// Löscht die doppelt oder vielfachen raus nach der multiplikation
        /// </summary>
        /// <param name="al"></param>
        /// <returns></returns>
        static ArrayList cleardoubledata(ArrayList al)
        {
            ArrayList ret = new ArrayList();
            for (int i = 0; i < al.Count; i++)
            {
                int anzahl = cfind(al, (string)al[i]);//al.IndexOf(,0,al.Count);
                if (anzahl % 2 != 0)
                    ret.Add(al[i]);
            }
            return ret;
        }
        /// <summary>
        /// gibt die anzahl der vorhanden strings in einem array
        /// wie oft ist c^2 drin?
        /// </summary>
        /// <param name="al"></param>
        /// <param name="s"></param>
        /// <returns></returns>
        static int cfind(ArrayList al, string s)
        {
            int ret = 0;
            for (int i = 0; i < al.Count; i++)
                if (al[i].ToString().CompareTo(s) == 0)
                    ret++;
            return ret;
        }

        /// <summary>
        /// fügt die Strings zusammen von allen elementen
        /// und setzt die 1 wo sie in allen strings im array vorhanden sind
        /// </summary>
        /// <param name="al"></param>
        /// <returns></returns>
        static string adddata(ArrayList al)
        {
            ArrayList list = new ArrayList();
            for (int i = 0; i < al.Count; i++)
            {
                string s = (string)al[i];
                if (list.Count == 0)
                    for (int tmp = 0; tmp < s.Length; tmp++)
                        list.Add("0");
                for (int k = 0; k < s.Length; k++)
                    if (s[k] == '1')
                        list[k] = "1";
            }
            char[] str = new char[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                str[i] = list[i].ToString()[0];
            }
            return new String(str);//.ToString();
        }
        /// <summary>
        /// Erzeugt ein String und an der pos setzt er eine 1
        /// </summary>
        /// <param name="size"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        static string create(int size, int pos)
        {
            string ret = "";
            for (int i = 0; i < pos - 1; i++)
            {
                ret += "0";
            }
            ret = "1" + ret;
            while (ret.Length < size)
                ret = "0" + ret;
            return ret;
        }

        public override string ToString()
        {
            return Data;
        }
    }
}