package org.bouncycastle.math.field;

import java.math.BigInteger;

public abstract class FiniteFields
{
    static final FiniteField GF_2 = new PrimeField(BigInteger.valueOf(2));
    static final FiniteField GF_3 = new PrimeField(BigInteger.valueOf(3));

    public static PolynomialExtensionField getBinaryExtensionField(int[] exponents)
    {
        return new GenericPolynomialExtensionField(GF_2, new GF2Polynomial(exponents));
    }

    public static FiniteField getPrimeField(BigInteger characteristic)
    {
        characteristic = characteristic.abs();

        int bitLength = characteristic.bitLength();
        if (bitLength < 3)
        {
            if (bitLength < 2)
            {
                throw new IllegalArgumentException("'characteristic' must be >= 2");
            }

            switch (characteristic.intValue())
            {
            case 2:
                return GF_2;
            case 3:
                return GF_3;
            }
        }

        return new PrimeField(characteristic);
    }
}

@@ -351,88 +351,692 @@
     
-return new GenericPolynomialExtensionField(GF_2, new GF2Polynomial(exponents));%0A
+if (exponents%5B0%5D != 0)%0A        %7B%0A            throw new IllegalArgumentException(%22Irreducible polynomials in GF(2) must have constant term%22);%0A        %7D%0A        for (int i = 1; i %3C exponents.length; ++i)%0A        %7B%0A            if (exponents%5Bi%5D %3C= exponents%5Bi - 1%5D)%0A            %7B%0A                throw new IllegalArgumentException(%22Polynomial exponents must be montonically increasing%22);%0A            %7D%0A        %7D%0A%0A        return new GenericPolynomialExtensionField(GF_2, new GF2Polynomial(exponents));%0A    %7D%0A%0A//    public static PolynomialExtensionField getTernaryExtensionField(Term%5B%5D terms)%0A//    %7B%0A//        return new GenericPolynomialExtensionField(GF_3, new GF3Polynomial(terms));%0A//
     
@@ -1119,56 +1119,8 @@
   %7B%0A
-        characteristic = characteristic.abs();%0A%0A
     
@@ -1183,49 +1183,40 @@
 if (
-bitLength %3C 3)%0A        %7B%0A            if (
+characteristic.signum() %3C= 0 %7C%7C 
 bitL
@@ -1230,34 +1230,26 @@
  2)%0A        
-    %7B%0A    
+%7B%0A
             
@@ -1321,30 +1321,63 @@
 %22);%0A        
+%7D%0A%0A
     
-%7D%0A
+    if (bitLength %3C 3)%0A        %7B
 %0A           

Improve argument checks and add hint of future ternary extensions