private Polynomial xgcd(Polynomial polynomial1, Polynomial polynomial2, Polynomial a, Polynomial b) {

        if (polynomial2.getSize() > polynomial1.getSize()) {
            //swap
            Polynomial swapPoly = polynomial1;
            polynomial1 = polynomial2;
            polynomial2 = swapPoly;
            swapPoly = a;
            a = b;
            b = swapPoly;
        }

        //prepare for division
        Polynomial result = new Polynomial(polynomial2.getSize());
        Polynomial remainder = polynomial2;
        Polynomial numerator = polynomial1;
        Polynomial denumerator = polynomial2;

        //prepare to find Bezout's identity
        Polynomial temp;
        ArrayList<Polynomial> resultList = new ArrayList<>();
        ArrayList<Polynomial> bezoutIdentity = new ArrayList<>();


        //find greatest greatest common divisor, last positive remainder
        while (!(remainder.getSize() == 0)) {
            result = remainder;
            remainder = new Polynomial(Math.max(numerator.getSize(), denumerator.getSize()));
            temp = divide(numerator, denumerator, remainder);

            //resultList data neccessary to find Bezout's identity
            resultList.add(numerator);
            resultList.add(denumerator);
            resultList.add(temp);

            remainder.clearZeroValues();
            numerator = denumerator;
            denumerator = remainder;
        }

        if (resultList.size() > 3) {
            //we don't need last 3 values
            resultList.remove(resultList.size() - 1);
            resultList.remove(resultList.size() - 1);
            resultList.remove(resultList.size() - 1);
        }

        Polynomial value = new Polynomial(1, 1);
        bezoutIdentity.add(value);
        bezoutIdentity.add(resultList.get(resultList.size() - 3));
        bezoutIdentity.add(resultList.get(resultList.size() - 2));
        bezoutIdentity.add(resultList.get(resultList.size() - 1));


        //find Bezout's identity from resultList data counted in Euclidean algorithm
        while (resultList.size() != 3) {
            resultList.remove(resultList.size() - 1);
            resultList.remove(resultList.size() - 1);
            resultList.remove(resultList.size() - 1);

            temp = bezoutIdentity.get(0);
            bezoutIdentity.set(0, bezoutIdentity.get(3));
            bezoutIdentity.set(1, resultList.get(resultList.size() - 3));
            bezoutIdentity.set(2, resultList.get(resultList.size() - 2));
            bezoutIdentity.set(3, add(temp, multiply(bezoutIdentity.get(3), resultList.get(resultList.size() - 1))));
        }

        //save Bezout's coefficients and return gcd
        for (int x = 0; x < bezoutIdentity.get(0).getSize(); x++) {
            a.setElement(x, bezoutIdentity.get(0).getElement(x));
        }
        for (int x = 0; x < bezoutIdentity.get(3).getSize(); x++) {
            b.setElement(x, bezoutIdentity.get(3).getElement(x));
        }

        //gcd, not normalized
        return result;
    }