/** * Checking polynomial for irreducibility * * @param p polinomial * @return true if p is irreducible and false otherwise */ public static boolean isIrreducible(int p) { if (p == 0) { return false; } int d = degree(p) >>> 1; int u = 2; for (int i = 0; i < d; i++) { u = modMultiply(u, u, p); if (gcd(u ^ 2, p) != 1) { return false; } } return true; }
public McElieceCCA2KeyGenParameterSpec(int keysize, String digest) { if (keysize < 1) { throw new IllegalArgumentException("key size must be positive"); } int m = 0; int n = 1; while (n < keysize) { n <<= 1; m++; } t = (n >>> 1) / m; this.m = m; this.n = n; this.fieldPoly = PolynomialRingGF2.getIrreduciblePolynomial(m); this.digest = digest; }
/** * create a finite field GF(2^m) with the fixed field polynomial * * @param degree the degree of the field * @param poly the field polynomial */ public GF2mField(int degree, int poly) { if (degree != PolynomialRingGF2.degree(poly)) { throw new IllegalArgumentException( " Error: the degree is not correct"); } if (!PolynomialRingGF2.isIrreducible(poly)) { throw new IllegalArgumentException( " Error: given polynomial is reducible"); } this.degree = degree; polynomial = poly; }
/** * Return the remainder of a polynomial division of two polynomials. * * @param p dividend * @param q divisor * @return <tt>p mod q</tt> */ public static int remainder(int p, int q) { int result = p; if (q == 0) { System.err.println("Error: to be divided by 0"); return 0; } while (degree(result) >= degree(q)) { result ^= q << (degree(result) - degree(q)); } return result; }
/** * Return product of two elements * * @param a * @param b * @return a*b */ public int mult(int a, int b) { return PolynomialRingGF2.modMultiply(a, b, polynomial); }
/** * Return the greatest common divisor of two polynomials * * @param p polinomial * @param q polinomial * @return GCD(p, q) */ public static int gcd(int p, int q) { int a, b, c; a = p; b = q; while (b != 0) { c = remainder(a, b); a = b; b = c; } return a; }
for (int i = a; i < b; i += 2) if (isIrreducible(i))
/** * create a finite field GF(2^m) with the fixed field polynomial * * @param degree the degree of the field * @param poly the field polynomial */ public GF2mField(int degree, int poly) { if (degree != PolynomialRingGF2.degree(poly)) { throw new IllegalArgumentException( " Error: the degree is not correct"); } if (!PolynomialRingGF2.isIrreducible(poly)) { throw new IllegalArgumentException( " Error: given polynomial is reducible"); } this.degree = degree; polynomial = poly; }
/** * Return the remainder of a polynomial division of two polynomials. * * @param p dividend * @param q divisor * @return <tt>p mod q</tt> */ public static int remainder(int p, int q) { int result = p; if (q == 0) { System.err.println("Error: to be divided by 0"); return 0; } while (degree(result) >= degree(q)) { result ^= q << (degree(result) - degree(q)); } return result; }
/** * Return product of two elements * * @param a * @param b * @return a*b */ public int mult(int a, int b) { return PolynomialRingGF2.modMultiply(a, b, polynomial); }
/** * Return the greatest common divisor of two polynomials * * @param p polinomial * @param q polinomial * @return GCD(p, q) */ public static int gcd(int p, int q) { int a, b, c; a = p; b = q; while (b != 0) { c = remainder(a, b); a = b; b = c; } return a; }
for (int i = a; i < b; i += 2) if (isIrreducible(i))
public GF2mField(byte[] enc) { if (enc.length != 4) { throw new IllegalArgumentException( "byte array is not an encoded finite field"); } polynomial = LittleEndianConversions.OS2IP(enc); if (!PolynomialRingGF2.isIrreducible(polynomial)) { throw new IllegalArgumentException( "byte array is not an encoded finite field"); } degree = PolynomialRingGF2.degree(polynomial); }
/** * Checking polynomial for irreducibility * * @param p polinomial * @return true if p is irreducible and false otherwise */ public static boolean isIrreducible(int p) { if (p == 0) { return false; } int d = degree(p) >>> 1; int u = 2; for (int i = 0; i < d; i++) { u = modMultiply(u, u, p); if (gcd(u ^ 2, p) != 1) { return false; } } return true; }
/** * Constructor. * * @param keysize the length of a Goppa code * @throws IllegalArgumentException if <tt>keysize < 1</tt>. */ public McElieceKeyGenParameterSpec(int keysize) { if (keysize < 1) { throw new IllegalArgumentException("key size must be positive"); } m = 0; n = 1; while (n < keysize) { n <<= 1; m++; } t = n >>> 1; t /= m; fieldPoly = PolynomialRingGF2.getIrreduciblePolynomial(m); }
/** * Return the rest of devision two polynomials * * @param p polinomial * @param q polinomial * @return p mod q */ public static int rest(long p, int q) { long p1 = p; if (q == 0) { System.err.println("Error: to be divided by 0"); return 0; } long q1 = q & 0x00000000ffffffffL; while ((p1 >>> 32) != 0) { p1 ^= q1 << (degree(p1) - degree(q1)); } int result = (int)(p1 & 0xffffffff); while (degree(result) >= degree(q)) { result ^= q << (degree(result) - degree(q)); } return result; }
public GF2mField(byte[] enc) { if (enc.length != 4) { throw new IllegalArgumentException( "byte array is not an encoded finite field"); } polynomial = LittleEndianConversions.OS2IP(enc); if (!PolynomialRingGF2.isIrreducible(polynomial)) { throw new IllegalArgumentException( "byte array is not an encoded finite field"); } degree = PolynomialRingGF2.degree(polynomial); }
/** * Constructor. * * @param keysize the length of a Goppa code * @throws IllegalArgumentException if <tt>keysize < 1</tt>. */ public McElieceParameters(int keysize) throws IllegalArgumentException { if (keysize < 1) { throw new IllegalArgumentException("key size must be positive"); } m = 0; n = 1; while (n < keysize) { n <<= 1; m++; } t = n >>> 1; t /= m; fieldPoly = PolynomialRingGF2.getIrreduciblePolynomial(m); }