Пример #1
0
        public IntArray Multiply(IntArray other, int m)
        {
            // Lenght of c is 2m bits rounded up to the next int (32 bit)
            int t = (m + 31) >> 5;

            if (m_ints.Length < t)
            {
                m_ints = resizedInts(t);
            }

            IntArray b = new IntArray(other.resizedInts(other.Length + 1));
            IntArray c = new IntArray((m + m + 31) >> 5);
            // IntArray c = new IntArray(t + t);
            int testBit = 1;

            for (int k = 0; k < 32; k++)
            {
                for (int j = 0; j < t; j++)
                {
                    if ((m_ints[j] & testBit) != 0)
                    {
                        // The kth bit of m_ints[j] is set
                        c.AddShifted(b, j);
                    }
                }
                testBit <<= 1;
                b.ShiftLeft();
            }
            return(c);
        }
Пример #2
0
        public override ECFieldElement Add(
            ECFieldElement b)
        {
            // No check performed here for performance reasons. Instead the
            // elements involved are checked in ECPoint.F2m
            // checkFieldElements(this, b);
            IntArray        iarrClone = (IntArray)this.x.Copy();
            F2mFieldElement bF2m      = (F2mFieldElement)b;

            iarrClone.AddShifted(bF2m.x, 0);
            return(new F2mFieldElement(m, k1, k2, k3, iarrClone));
        }
Пример #3
0
		public override ECFieldElement Invert()
		{
			// Inversion in F2m using the extended Euclidean algorithm
			// Input: A nonzero polynomial a(z) of degree at most m-1
			// Output: a(z)^(-1) mod f(z)

			// u(z) := a(z)
            IntArray uz = (IntArray)this.x.Copy();

			// v(z) := f(z)
			IntArray vz = new IntArray(t);
			vz.SetBit(m);
			vz.SetBit(0);
			vz.SetBit(this.k1);
			if (this.representation == Ppb)
			{
				vz.SetBit(this.k2);
				vz.SetBit(this.k3);
			}

			// g1(z) := 1, g2(z) := 0
			IntArray g1z = new IntArray(t);
			g1z.SetBit(0);
			IntArray g2z = new IntArray(t);

			// while u != 0
			while (uz.GetUsedLength() > 0)
//            while (uz.bitLength() > 1)
			{
				// j := deg(u(z)) - deg(v(z))
				int j = uz.BitLength - vz.BitLength;

				// If j < 0 then: u(z) <-> v(z), g1(z) <-> g2(z), j := -j
				if (j < 0)
				{
                    IntArray uzCopy = uz;
					uz = vz;
					vz = uzCopy;

                    IntArray g1zCopy = g1z;
					g1z = g2z;
					g2z = g1zCopy;

					j = -j;
				}

				// u(z) := u(z) + z^j * v(z)
				// Note, that no reduction modulo f(z) is required, because
				// deg(u(z) + z^j * v(z)) <= max(deg(u(z)), j + deg(v(z)))
				// = max(deg(u(z)), deg(u(z)) - deg(v(z)) + deg(v(z))
				// = deg(u(z))
				// uz = uz.xor(vz.ShiftLeft(j));
				// jInt = n / 32
				int jInt = j >> 5;
				// jInt = n % 32
				int jBit = j & 0x1F;
				IntArray vzShift = vz.ShiftLeft(jBit);
				uz.AddShifted(vzShift, jInt);

				// g1(z) := g1(z) + z^j * g2(z)
//                g1z = g1z.xor(g2z.ShiftLeft(j));
				IntArray g2zShift = g2z.ShiftLeft(jBit);
				g1z.AddShifted(g2zShift, jInt);
			}
			return new F2mFieldElement(this.m, this.k1, this.k2, this.k3, g2z);
		}
Пример #4
0
        public override ECFieldElement Invert()
        {
            // Inversion in F2m using the extended Euclidean algorithm
            // Input: A nonzero polynomial a(z) of degree at most m-1
            // Output: a(z)^(-1) mod f(z)

            // u(z) := a(z)
            IntArray uz = (IntArray)this.x.Copy();

            // v(z) := f(z)
            IntArray vz = new IntArray(t);

            vz.SetBit(m);
            vz.SetBit(0);
            vz.SetBit(this.k1);
            if (this.representation == Ppb)
            {
                vz.SetBit(this.k2);
                vz.SetBit(this.k3);
            }

            // g1(z) := 1, g2(z) := 0
            IntArray g1z = new IntArray(t);

            g1z.SetBit(0);
            IntArray g2z = new IntArray(t);

            // while u != 0
            while (uz.GetUsedLength() > 0)
//            while (uz.bitLength() > 1)
            {
                // j := deg(u(z)) - deg(v(z))
                int j = uz.BitLength - vz.BitLength;

                // If j < 0 then: u(z) <-> v(z), g1(z) <-> g2(z), j := -j
                if (j < 0)
                {
                    IntArray uzCopy = uz;
                    uz = vz;
                    vz = uzCopy;

                    IntArray g1zCopy = g1z;
                    g1z = g2z;
                    g2z = g1zCopy;

                    j = -j;
                }

                // u(z) := u(z) + z^j * v(z)
                // Note, that no reduction modulo f(z) is required, because
                // deg(u(z) + z^j * v(z)) <= max(deg(u(z)), j + deg(v(z)))
                // = max(deg(u(z)), deg(u(z)) - deg(v(z)) + deg(v(z))
                // = deg(u(z))
                // uz = uz.xor(vz.ShiftLeft(j));
                // jInt = n / 32
                int jInt = j >> 5;
                // jInt = n % 32
                int      jBit    = j & 0x1F;
                IntArray vzShift = vz.ShiftLeft(jBit);
                uz.AddShifted(vzShift, jInt);

                // g1(z) := g1(z) + z^j * g2(z)
//                g1z = g1z.xor(g2z.ShiftLeft(j));
                IntArray g2zShift = g2z.ShiftLeft(jBit);
                g1z.AddShifted(g2zShift, jInt);
            }
            return(new F2mFieldElement(this.m, this.k1, this.k2, this.k3, g2z));
        }
Пример #5
0
		public IntArray Multiply(IntArray other, int m)
		{
			// Lenght of c is 2m bits rounded up to the next int (32 bit)
			int t = (m + 31) >> 5;
			if (m_ints.Length < t)
			{
				m_ints = resizedInts(t);
			}

			IntArray b = new IntArray(other.resizedInts(other.Length + 1));
			IntArray c = new IntArray((m + m + 31) >> 5);
			// IntArray c = new IntArray(t + t);
			int testBit = 1;
			for (int k = 0; k < 32; k++)
			{
				for (int j = 0; j < t; j++)
				{
					if ((m_ints[j] & testBit) != 0)
					{
						// The kth bit of m_ints[j] is set
						c.AddShifted(b, j);
					}
				}
				testBit <<= 1;
				b.ShiftLeft();
			}
			return c;
		}