private void DivTest()
        {
            Random rng = new Random();

            for (int i = 0; i < 10; i++)
            {
                IntegerPolynomial poly = (IntegerPolynomial)PolynomialGeneratorForTesting.GenerateRandom(439, 10000);

                // test special case: division by 2048
                IntegerPolynomial a = poly.Clone();
                a.Divide(2048);
                for (int j = 0; j < poly.Coeffs.Length; j++)
                {
                    if (!Compare.Equals((int)Math.Floor((((double)poly.Coeffs[j]) / 2048) + 0.5), a.Coeffs[j]))
                    {
                        throw new Exception("IntegerPolynomialTest division test failed!");
                    }
                }
                // test the general case
                a = poly.Clone();
                int k = rng.Next(2047) + 1;
                a.Divide(k);
                for (int j = 0; j < poly.Coeffs.Length; j++)
                {
                    if (!Compare.Equals((int)Math.Floor(((double)poly.Coeffs[j] / k) + 0.5), a.Coeffs[j]))
                    {
                        throw new Exception("IntegerPolynomialTest division test failed!");
                    }
                }
            }
        }
Beispiel #2
0
        private IntegerPolynomial sign(IntegerPolynomial i, SignatureKeyPair kp)
        {
            int N = param.N;
            int q = param.q;
            int perturbationBases = param.B;

            IntegerPolynomial s = new IntegerPolynomial(N);
            int iLoop           = perturbationBases;

            while (iLoop >= 1)
            {
                IPolynomial f      = kp.priv.getBasis(iLoop).f;
                IPolynomial fPrime = kp.priv.getBasis(iLoop).fPrime;

                IntegerPolynomial y = f.Multiply(i);
                y.Divide(q);
                y = fPrime.Multiply(y);

                IntegerPolynomial x = fPrime.Multiply(i);
                x.Divide(q);
                x = f.Multiply(x);

                IntegerPolynomial si = y;
                si.Subtract(x);
                s.Add(si);

                IntegerPolynomial hi = kp.priv.getBasis(iLoop).h.Clone();
                if (iLoop > 1)
                {
                    hi.Subtract(kp.priv.getBasis(iLoop - 1).h);
                }
                else
                {
                    hi.Subtract(kp.pub.h);
                }
                i = si.Multiply(hi, q);

                iLoop--;
            }

            IPolynomial f2      = kp.priv.getBasis(0).f;
            IPolynomial fPrime2 = kp.priv.getBasis(0).fPrime;

            IntegerPolynomial y2 = f2.Multiply(i);

            y2.Divide(q);
            y2 = fPrime2.Multiply(y2);

            IntegerPolynomial x2 = fPrime2.Multiply(i);

            x2.Divide(q);
            x2 = f2.Multiply(x2);

            y2.Subtract(x2);
            s.Add(y2);
            s.ModPositive(q);
            return(s);
        }