Esempio n. 1
0
        static void Main()
        {
            // Application.EnableVisualStyles();
            // Application.SetCompatibleTextRenderingDefault(false);
            // Application.Run(new Form1());
			cpolinom a = new cpolinom ("", 2);
        }
Esempio n. 2
0
 private static List<cpolinom> Table(cpolinom p, cpolinom ir, Multi mul, int w)
 {
     List<cpolinom> table = new List<cpolinom>();
     table.Add(p % ir);
     for (int i = 0; i < Math.Pow(2, w) - 2; i++)
         table.Add(mul(table[i], p) % ir);
     return table;
 }
Esempio n. 3
0
        private static List<cpolinom> NAFLRTable(cpolinom p, cpolinom ir, Multi mul, int power, int w)
        {
            List<cpolinom> table = new List<cpolinom>();

            for (int i = 1; i <= power; i += 2)
                table.Add(BinaryLR(p, ir, mul, i));
            return table;
        }
Esempio n. 4
0
 private static List<cpolinom> SlideLRTable(cpolinom p, cpolinom ir, Multi mul, int power, int w)
 {
     List<cpolinom> table = new List<cpolinom>();
     table.Add(BinaryLR(p, ir, mul, power));
     for (int i = 0; i < power - 1; i++)
         table.Add(mul(table[i], p) % ir);
     return table;
 }
Esempio n. 5
0
 public static cpolinom CreateRand(int n, int m)
 {
     cpolinom c = new cpolinom("", m);
     for (int i = 0; i < n; i++)
         c.Add(Mod(r.Next(), m));
     while (c[0] == 0)
         c.RemoveAt(0);
     return c;
 }
Esempio n. 6
0
 public cpolinom InvPolinom(cpolinom n)
 {
     cpolinom x = new cpolinom("1", n.mod);
     cpolinom y = Copy(this);
     cpolinom r;
     while (true)
     {
         r = (x * y) % n;
         if (r.x[0] == 1 && r.Count == 1)
             break;
         else
             x++;
     }
     return x;
 }
Esempio n. 7
0
        public static cpolinom Point_Multiplication_Affine_Coord_19(cpolinom p, cpolinom ir, Multi mul, BigInteger n, int A, int B, Inverse inv)
        {
            BigInteger[,] mas_k;
            mas_k = Convert_to_DBNS_1(n, A, B);

            int lastindex = mas_k.GetLength(0) - 1;
            cpolinom t = cpolinom.Copy(p);
            cpolinom res = new cpolinom("1", p.mod);

            for (int i = 0; i < mas_k[lastindex, 1]; i++)
                t = mul(t, t) % ir;


            for (int i = 0; i < mas_k[lastindex, 2]; i++)
                t = mul(t, mul(t, t)) % ir;

            if (mas_k[lastindex, 0] == -1)
                res = mul(res, inv(t, ir)) % ir;
            else if (mas_k[lastindex, 0] == 1)
                res = mul(res, t) % ir;

            for (int i = lastindex - 1; i >= 0; i--)
            {
                BigInteger u = mas_k[i, 1] - mas_k[i + 1, 1];
                BigInteger v = mas_k[i, 2] - mas_k[i + 1, 2];
                for (int j = 0; j < u; j++)                
                    t = mul(t, t) % ir;                

                for (int j = 0; j < v; j++)                
                    t = mul(t, mul(t, t)) % ir;

                if (mas_k[i, 0] == -1)
                    res = mul(res, inv(t, ir)) % ir;
                else if (mas_k[i, 0] == 1)
                    res = mul(res, t) % ir;
            }
            return res;
        }
Esempio n. 8
0
        public static cpolinom Point_Multiplication_Affine_Coord_20(cpolinom p, cpolinom ir, Multi mul, BigInteger n, int A, int B, Inverse inv)
        {
            BigInteger[,] mas_k;

            cpolinom t = cpolinom.Copy(p);
            cpolinom res = new cpolinom("1", p.mod);

            mas_k = Convert_to_DBNS_2(n, A, B);

            if (mas_k[0, 0] == -1)
                res = inv(t, ir);
            else if (mas_k[0, 0] == 1)
                res = t;

            for (int i = 0; i < mas_k.GetLength(0) - 1; i++)
            {
                BigInteger u = mas_k[i, 1] - mas_k[i + 1, 1];
                BigInteger v = mas_k[i, 2] - mas_k[i + 1, 2];
                for (int j = 0; j < u; j++)                
                    res = mul(res, res) % ir;

                for (int j = 0; j < v; j++)
                    res = mul(res, mul(res, res)) % ir;

                if (mas_k[i+1, 0] < 0)
                    res = mul(res, inv(t, ir)) % ir;
                else
                    res = mul(res, t) % ir;     
            }

            for (int i = 0; i < mas_k[mas_k.GetLength(0) - 1, 1]; i++)
                res = mul(res, res) % ir;

            for (int i = 0; i < mas_k[mas_k.GetLength(0) - 1, 2]; i++)
                res = mul(res, mul(res, res)) % ir;
            return res;
        }
Esempio n. 9
0
        public static List<cpolinom> Primes(int n, int mod)
        {
            List<cpolinom> r = new List<cpolinom>();
            cpolinom t = new cpolinom("", mod);

            cpolinom temp = new cpolinom("1 0", mod);

            r.Add(Copy(temp));
            temp++;

            while (temp.Count != n + 2)
            {
                for (int i = 0; i < r.Count; i++)
                {
                    t = temp % r[i];
                    if (t.Count == 1 && t[0] == 0)
                        break;
                }
                if (t.Count > 1 || (t.Count == 1 && t[0] != 0))
                    r.Add(Copy(temp));
                temp++;
            }
            return r;
        }
Esempio n. 10
0
 public static cpolinom DBNS1LR(cpolinom p, cpolinom ir, Multi mul, BigInteger n, int A, int B, Inverse inv)
 {
     return methods.Point_Multiplication_Affine_Coord_20(p, ir, mul, n, A, B, inv);
 }
Esempio n. 11
0
        public static cpolinom MontgomeryLadder(cpolinom p, cpolinom ir, Multi mul, BigInteger n)
        {
            cpolinom res = new cpolinom("1", p.mod);
            cpolinom t = cpolinom.Copy(p);

            string Binary = bif.ToBin(n);
            for (int i = 0; i < Binary.Length; i++)
            {
                if (Binary[i] == '0')
                {
                    t = mul(t, res) % ir;
                    res = mul(res, res) % ir;
                }
                else
                {
                    res = mul(res, t) % ir;
                    t = mul(t, t) % ir;
                }
            }
            return res;
        }
Esempio n. 12
0
        public static cpolinom Joye_double_and_add(cpolinom p, cpolinom ir, Multi mul, BigInteger n)
        {
            cpolinom res = new cpolinom("1", p.mod);
            cpolinom t = cpolinom.Copy(p);

            string Binary = bif.ToBin(n);
            for (int i = Binary.Length - 1; i >= 0 ; i--)
            {
                if (Binary[i] == '1')
                {
                    res = mul(res, res) % ir;
                    res = mul(res, t) % ir;
                }
                else
                {
                    t = mul(t, t) % ir;
                    t = mul(res, t) % ir;
                }
            }
            return res;
        }
Esempio n. 13
0
        public static cpolinom AddSubLR(cpolinom p, cpolinom ir, Multi mul, BigInteger n, Inverse inv)
        {
            cpolinom res = new cpolinom("1", p.mod);
            cpolinom c = cpolinom.Copy(p);

            string BinaryT = bif.ToBin(3 * n);
            string BinaryN = bif.ToBin(n);
            while (BinaryN.Length < BinaryT.Length) BinaryN = "0" + BinaryN;

            for (int i = 0; i < BinaryN.Length - 1; i++)
            {
                res = mul(res, res) % ir;
                if (BinaryT[i] != '0' && BinaryN[i] == '0') res = mul(res, c) % ir;
                else if (BinaryT[i] == '0' && BinaryN[i] != '0') res = mul(res, inv(c, ir)) % ir;
            }
            
            return res;
        }
Esempio n. 14
0
        public static cpolinom wNAFSlideLR(cpolinom p, cpolinom ir, Multi mul, BigInteger n, int w, Inverse inv)
        {
            cpolinom res = new cpolinom("1", p.mod);
            List<BigInteger> x = bif.wNAF(n, w);
            int pow = 2 * ((int)Math.Pow(2, w) - (int)Math.Pow((-1), w)) / 3 - 1;

            List<cpolinom> table = NAFLRTable(p, ir, mul, pow, w);

            for (int i = 0; i < x.Count; )
            {
                List<BigInteger> max = new List<BigInteger>();
                if (x[i] == 0)
                {
                    max.Add(0);
                    max.Add(1);
                }
                else
                    max = FindLargest2(x, i, w);

                for (int d = 0; d < max[1]; d++)
                    res = mul(res, res) % ir;

                if (max[0] > 0)
                    res = mul(res, table[(int)(bif.Abs(x[i]) / 2)]) % ir;
                else if (max[0] < 0)
                    res = mul(res, inv(table[(int)(bif.Abs(x[i]) / 2)], ir)) % ir;

                i = i + (int)max[1];
            }
            return res;
        }
Esempio n. 15
0
        public static cpolinom SlideRL(cpolinom p, cpolinom ir, Multi mul, BigInteger n, int w)
        {
            int power = (int)Math.Pow(2, w - 1);
            List<cpolinom> table = SlideRLTable(p, ir, mul, power, w);
            
            cpolinom res = new cpolinom("1", p.mod);
            cpolinom temp = cpolinom.Copy(p);

            string binary = bif.ToBin(n);

            while(binary.Length > 0)
            {
                int index = binary.Length - 1;
                if (binary.Length < w || binary[index - w + 1] == '0')
                {
                    if (binary[index] == '1')
                        res = mul(res, temp) % ir;

                    for (int j = 0; j < table.Count; j++)
                        table[j] = mul(table[j], table[j]) % ir;
                    
                    temp = mul(temp, temp) % ir;

                    binary = binary.Remove(index, 1);
                }
                else
                {
                    int c = Convert.ToInt32(binary.Substring(index - w + 1, w), 2);
                    res = mul(res, table[c - power]) % ir;

                    temp = mul(temp, table[table.Count - 1]) % ir;

                    for (int k = 0; k < w; k++)
                        for (int j = 0; j < table.Count; j++)
                            table[j] = mul(table[j], table[j]) % ir;

                    binary = binary.Remove(index - w + 1, w);
                }
            }
            return res;
        }
Esempio n. 16
0
 public static cpolinom BinaryRL(cpolinom p, cpolinom ir, Multi mul, BigInteger n)
 {
     cpolinom res = new cpolinom("1", p.mod);
     cpolinom t = cpolinom.Copy(p);
     string Binary = bif.ToBin(n);
     for (int i = Binary.Length - 1; i >= 0; i--)
     {
         if (Binary[i] == '1')
             res = mul(t, res) % ir;
         t = mul(t, t) % ir;
     }
     return res;
 }
Esempio n. 17
0
 public static cpolinom BinaryLR(cpolinom p, cpolinom ir, Multi mul, BigInteger n)
 {
     cpolinom res = new cpolinom("1", p.mod);
     cpolinom t = cpolinom.Copy(p);
     string Binary = bif.ToBin(n);
     for (int i = 0; i < Binary.Length; i++)
     {
         res = mul(res, res) % ir;
         if (Binary[i] == '1')
             res = mul(t, res) % ir;
     }
     return res;
 }
Esempio n. 18
0
 public cpolinom Inverve(cpolinom a)
 {
     cpolinom b = new cpolinom("", a.mod);
     for (int i = 0; i < a.Count; i++)
         b.Insert(0, a[i]);
     return b;
 }
Esempio n. 19
0
        public static cpolinom meth7_2(cpolinom p, cpolinom ir, Multi mul, BigInteger n, Inverse inv)
        {
            cpolinom res = new cpolinom("1", p.mod);
            cpolinom c = cpolinom.Copy(p);
            BigInteger k = n;
            while (k >= 1)
            {
                BigInteger temp = 0;
                if (k % 2 != 0)
                {
                    temp = 2 - k % 4;
                    k -= temp;
                }

                if (temp == 1) res = mul(res, c) % ir;
                else if (temp == -1) res = mul(res, inv(c, ir)) % ir;

                k = k / 2;
                c = mul(c, c) % ir;

            }
            return res;
        }
Esempio n. 20
0
 public static cpolinom NAFBinaryLR(cpolinom p, cpolinom ir, Multi mul, BigInteger n, Inverse inv)
 {
     cpolinom res = new cpolinom("1", p.mod);
     cpolinom c = cpolinom.Copy(p);
     List<BigInteger> x = bif.NAF(n);
     for (int i = x.Count - 1; i > -1; i--)
     {
         res = mul(res, res) % ir;
         if (x[i] == 1) res = mul(res, c) % ir;
         else if (x[i] == -1) res = mul(res, inv(c, ir)) % ir;
     }
     return res;
 }
Esempio n. 21
0
        public static cpolinom WindowRL(cpolinom p, cpolinom ir, Multi mul, BigInteger n, int w)
        {
            List<cpolinom> table = Table(p, ir, mul, w);
            cpolinom res = new cpolinom("1", p.mod);

            List<string> bins = windows(bif.ToBin(n), w);

            for (int i = bins.Count - 1; i > -1; i--)
            {
                int c = Convert.ToInt32(bins[i], 2);
                if (c != 0) res = mul(res, table[c - 1]) % ir;

                for (int k = 0; k < w; k++)
                    for (int j = 0; j < table.Count; j++)
                        table[j] = mul(table[j], table[j])%ir;
            }
            return res;
        }
Esempio n. 22
0
 public static cpolinom NAFBinaryRL(cpolinom p, cpolinom ir, Multi mul, BigInteger n, Inverse inv)
 {
     cpolinom res = new cpolinom("1", p.mod);
     cpolinom c = cpolinom.Copy(p);
     List<BigInteger> x = bif.NAF(n);
     for (int i = 0; i < x.Count; i++)            
     {
         if (x[i] == 1) res = mul(res, c) % ir;
         else if (x[i] == -1) res = mul(res, inv(c, ir)) % ir;
         c = mul(c, c) % ir;
     }
     return res;
 }
Esempio n. 23
0
        public static cpolinom SlideLR(cpolinom p, cpolinom ir, Multi mul, BigInteger n, int w)
        {
            int power = (int)Math.Pow(2, w - 1);
            List<cpolinom> table = SlideLRTable(p, ir, mul, power, w);

            cpolinom res = new cpolinom("1", p.mod);
            string binary = bif.ToBin(n);

            while (binary.Length > 0)
            {
                if (binary.Length < w || binary[0] == '0')
                {
                    res = mul(res, res) % ir;
                    if (binary[0] == '1')
                        res = mul(res, p) % ir;
                    binary = binary.Remove(0, 1);
                }
                else
                {
                    int c = Convert.ToInt32(binary.Substring(0, w), 2);

                    for (int k = 0; k < w; k++)
                        res = mul(res, res) % ir;

                    res = mul(res, table[c - power]) % ir;
                    binary = binary.Remove(0, w);
                }
            }
            return res;
        }
Esempio n. 24
0
        public static cpolinom DBNS2RL(cpolinom p, cpolinom ir, Multi mul, BigInteger n, Inverse inv)
        {
            List<int[]> x = ToDBNS2RL(n);

            cpolinom res = new cpolinom("1", p.mod);
            cpolinom t = cpolinom.Copy(p);
            for (int i = x.Count - 1; i > -1; i--)
            {
                for (int j = 0; j < x[i][1]; j++)
                    t = mul(t, t) % ir;
                for (int j = 0; j < x[i][2]; j++)
                    t = mul(t, mul(t, t)) % ir;

                if (x[i][0] == 1)
                    res = mul(res, t) % ir;
                else if (x[i][0] == -1)
                    res = mul(res, inv(t, ir)) % ir;              
            }
            return res;
        }
Esempio n. 25
0
        public static cpolinom DBNS2LR(cpolinom p, cpolinom ir, Multi mul, BigInteger n, Inverse inv)
        {
            List<int[]> x = ToDBNS2LR(n);
            cpolinom res = cpolinom.Copy(p);

            for (int i = x.Count - 1; i > 0 ; i--)
            {
                for (int j = 0; j < x[i][1]; j++)
                    res = mul(res, res) % ir;
                for (int j = 0; j < x[i][2]; j++)
                    res = mul(res, mul(res, res)) % ir;

                if (x[i][0] == 1)
                    res = mul(res, p) % ir;
                else if (x[i][0] == -1)
                    res = mul(res, inv(p, ir)) % ir;
            }
            for (int j = 0; j < x[0][1]; j++)
                res = mul(res, res) % ir;
            for (int j = 0; j < x[0][2]; j++)
                res = mul(res, mul(res, res)) % ir;
            return res;
        }
Esempio n. 26
0
        public static cpolinom NAFWindowLR(cpolinom p, cpolinom ir, Multi mul, BigInteger n, int w, Inverse inv)
        {
            cpolinom res = new cpolinom("1", p.mod);
            List<BigInteger> x = bif.NAF(n);
            int pow = (int)Math.Pow(2, w - 1);
            List<cpolinom> table = NAFLRTable(p, ir, mul, pow, w);

            for (int i = x.Count - 1; i > -1 ; i--)
            {
                res = mul(res, res) % ir;
                if (x[i] > 0)
                    res = mul(res, table[(int)(x[i] / 2)]) % ir;
                else if (x[i] < 0)
                    res = mul(res, inv(table[(int)(-x[i] / 2)], ir)) % ir;
            }
            return res;
        }
Esempio n. 27
0
        public static cpolinom wNAFSlideRL(cpolinom p, cpolinom ir, Multi mul, BigInteger n, int w, Inverse inv)
        {
            cpolinom res = new cpolinom("1", p.mod);
            List<BigInteger> x = bif.wNAF(n, w);
            int pow = 2 * ((int)Math.Pow(2, w) - (int)Math.Pow((-1), w)) / 3 - 1;

            List<cpolinom> table = NAFRLTable(p, ir, mul, pow, w);

            for (int i = x.Count - 1; i > -1; )
            {
                List<BigInteger> max = FindLargest1(x, i, w);

                if (max[0] > 0)
                    res = mul(res, table[(int)(bif.Abs(x[i]) / 2)]) % ir;
                else if (max[0] < 0)
                    res = mul(res, inv(table[(int)(bif.Abs(x[i]) / 2)], ir)) % ir;

                for (int d = 0; d < max[1]; d++)
                    for (int j = 0; j < table.Count; j++)
                        table[j] = mul(table[j], table[j]) % ir;

                i = i - (int)max[1];
            }
            return res;
        }
Esempio n. 28
0
        public static cpolinom WindowLR(cpolinom p, cpolinom ir, Multi mul, BigInteger n, int w)
        {
            List<cpolinom> table = Table(p, ir, mul, w);
            cpolinom res = new cpolinom("1", p.mod);

            List<string> bins = windows(bif.ToBin(n), w);

            for (int i = 0; i < bins.Count; i++)
            {
                for (int k = 0; k < w; k++)
                    res = mul(res, res) % ir;

                int c = Convert.ToInt32(bins[i], 2);
                if (c != 0) res = mul(res, table[c - 1]) % ir;
            }
            return res;
        }
Esempio n. 29
0
        public cpolinom ParseString(string str)
        {
            this.pointer = 0;

            int baseField = ParseNumber(str, '(', '^');
            int powerField = ParseNumber(str, '^', ')');

            bool firstNumber = true;

            cpolinom polinom = new cpolinom("", baseField);
            List<int> coefficients = new List<int>();
            while ((this.pointer >= str.Length) == false)
            {
                int coefficient = ParseCoefficients(str);
                int power = ParsePower(str);

                if (firstNumber == true)
                {
                    polinom = new cpolinom(power, baseField);
                    firstNumber = false;
                }

                polinom[power] = coefficient;
            }

            return Inverve(polinom);
        }
Esempio n. 30
0
        public static cpolinom NAFWindowRL(cpolinom p, cpolinom ir, Multi mul, BigInteger n, int w, Inverse inv)
        {
            cpolinom res = new cpolinom("1", p.mod);
            List<BigInteger> x = bif.NAF(n);
            int pow = (int)Math.Pow(2, w - 1);
            List<cpolinom> table = NAFRLTable(p, ir, mul, pow, w);            

            for (int i = 1; i < pow; i += 2)
                table.Add(BinaryRL(p, ir, mul, i));

            for (int i = 0; i < x.Count; i++)
            {
                if (x[i] > 0)
                    res = mul(res, table[(int)(x[i] / 2)]) % ir;
                else if (x[i] < 0)
                    res = mul(res, inv(table[(int)(-x[i] / 2)], ir)) % ir;
                
                for (int j = 0; j < table.Count; j++)
                    table[j] = mul(table[j], table[j]) % ir;
            }
            return res;
        }