@Test
    public void testInvert() {
        /*
         * Test with constant values.
         */
        gf = new GF2N(283);
        PolynomialGF2N polyGF = new PolynomialGF2N(gf);

        Polynomial polynomial1 = new Polynomial(3);
        polynomial1.setElement(0, 195);
        polynomial1.setElement(1, 103);
        polynomial1.setElement(2, 117);

        Polynomial polynomial2 = new Polynomial(6);
        polynomial2.setElement(0, 163);
        polynomial2.setElement(1, 221);
        polynomial2.setElement(2, 7);
        polynomial2.setElement(3, 51);
        polynomial2.setElement(4, 197);
        polynomial2.setElement(5, 172);

        Polynomial resultPolynomial = new Polynomial(5);
        resultPolynomial.setElement(0, 32);
        resultPolynomial.setElement(1, 142);
        resultPolynomial.setElement(2, 85);
        resultPolynomial.setElement(3, 232);
        resultPolynomial.setElement(4, 71);
        assertEquals("Inverse of polynomials with constant values failed.",
                resultPolynomial, polyGF.invert(polynomial1, polynomial2));

        polynomial1.setElement(0, 180);
        polynomial1.setElement(1, 222);
        polynomial1.setElement(2, 52);

        polynomial2.setElement(0, 106);
        polynomial2.setElement(1, 164);
        polynomial2.setElement(2, 241);
        polynomial2.setElement(3, 37);
        polynomial2.setElement(4, 108);
        polynomial2.setElement(5, 123);

        resultPolynomial.setElement(0, 205);
        resultPolynomial.setElement(1, 59);
        resultPolynomial.setElement(2, 72);
        resultPolynomial.setElement(3, 255);
        resultPolynomial.setElement(4, 128);
        assertEquals("Inverse of polynomials with constant values failed.",
                resultPolynomial, polyGF.invert(polynomial1, polynomial2));


        /*
         * Basic test.
         */
        gf = new GF2N(4194307l);
        polyGF = new PolynomialGF2N(gf);
        for (int x = 0; x < 128; x++) {
            try {
                int length1 = rn.nextInt(128) + 1;
                int length2 = rn.nextInt(128) + 1 + length1;
                polynomial1 = new Polynomial(length1, gf.getFieldSize());
                polynomial2 = new Polynomial(length2, gf.getFieldSize());

                //Test: inverse(a) % x = b    =>   ( a * b ) % x = 1
                Polynomial inverse = polyGF.invert(polynomial1, polynomial2);
                Polynomial remainder = new Polynomial(length1);
                Polynomial result = polyGF.divide(polyGF.multiply(polynomial1, inverse), polynomial2, remainder);
                remainder.clearZeroValues();

                assertEquals("Inverse result is wrong.",
                        new Polynomial(1, 1),
                        remainder);

                try {
                    polyGF.invert(polynomial2, polynomial1);
                    fail("MathArithmeticException should be thrown, "
                            + "when moduloPolynomial is shorter than polynomial");
                } catch (MathArithmeticException ex) {
                    //OK
                }
            } catch (MathArithmeticException ex) {
                //OK, some polynomials cannot be inverted with some modulos
            }

        }

        //Tests in binary finite field
        polyGF = new PolynomialGF2N(3);
        for (int x = 0; x < 128; x++) {
            try {
                int length1 = rn.nextInt(128) + 1;
                int length2 = rn.nextInt(128) + 1 + length1;
                polynomial1 = new Polynomial(length1, 1);
                polynomial2 = new Polynomial(length2, 1);

                //Test: inverse(a) % x = b    =>   ( a * b ) % x = 1
                Polynomial inverse = polyGF.invert(polynomial1, polynomial2);
                Polynomial remainder = new Polynomial(length1);
                Polynomial result = polyGF.divide(polyGF.multiply(polynomial1, inverse), polynomial2, remainder);
                remainder.clearZeroValues();

                assertEquals("Inverse result is wrong.",
                        new Polynomial(1, 1),
                        remainder);

                try {
                    polyGF.invert(polynomial2, polynomial1);
                    fail("MathArithmeticException should be thrown, "
                            + "when moduloPolynomial is shorter than polynomial");
                } catch (MathArithmeticException ex) {
                    //OK
                }
            } catch (MathArithmeticException ex) {
                //OK, some polynomials cannot be inverted with some modulos
            }

        }

    }