From 16d1ebd78bd17db80d6a1a2575d80b7e029b983f Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Sat, 9 Jun 2012 23:52:20 +0200 Subject: [PATCH] gf2m: fixed defining polynomial generation --- lib/gf2m.cpp | 55 ++++++++++++++++++++++++++++++---------------- lib/polynomial.cpp | 4 ++-- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/lib/gf2m.cpp b/lib/gf2m.cpp index 03645df..1cba5ef 100644 --- a/lib/gf2m.cpp +++ b/lib/gf2m.cpp @@ -64,7 +64,7 @@ bool is_irreducible_gf2_poly (uint p) if (!p) return false; int d = gf2p_degree (p) / 2; uint test = 2; //x^1+0 - for (int i = 0; i <= d; ++i) { + for (int i = 1; i <= d; ++i) { test = gf2p_modmult (test, test, p); if (gf2p_gcd (test ^ 2 /* test - x^1 */, p) != 1) @@ -88,28 +88,45 @@ bool gf2m::create (uint M) n = 1 << m; if (!n) return false; //too big. poly = 0; - //FIXME fails for M>=12. Why? - for (uint t = (1 << m) + 1, e = 1 << (m + 1); t < e; t += 2) - if (is_irreducible_gf2_poly (t) ) { - poly = t; - break; + + /* + * find a conway polynomial for given degree. First we "filter out" the + * possibilities that cannot be conway (reducible ones), then we check + * that Z2[x]/poly is a field. + */ + for (uint t = (1 << m) + 1, e = 1 << (m + 1); t < e; t += 2) { + + if (!is_irreducible_gf2_poly (t) ) continue; + + //try to prepare log and antilog tables + log.resize (n, 0); + antilog.resize (n, 0); + log[0] = n - 1; + antilog[n-1] = 0; + + uint i, xi = 1; //x^0 + for (i = 0; i < n - 1; ++i) { + if (log[xi] != 0) { //not a cyclic group + log.clear(); + antilog.clear(); + break; + } + log[xi] = i; + antilog[i] = xi; + + xi <<= 1; //multiply by x + xi = gf2p_mod (xi, t); } + //if it broke... + if (i < n - 1) continue; + poly = t; + break; + } + if (!poly) return false; - log.resize (n); - antilog.resize (n); - log[0] = n - 1; - antilog[n-1] = 0; - - uint xi = 1; //x^0 - for (uint i = 0; i < n - 1; ++i) { - log[xi] = i; - antilog[i] = xi; - - xi <<= 1; //multiply by x - xi = gf2p_mod (xi, poly); - } + return true; } uint gf2m::add (uint a, uint b) diff --git a/lib/polynomial.cpp b/lib/polynomial.cpp index c323ca1..d88d4cc 100644 --- a/lib/polynomial.cpp +++ b/lib/polynomial.cpp @@ -111,9 +111,9 @@ bool polynomial::is_irreducible (gf2m&fld) const xi = xmodf; xmodf.mod (*this, fld); //mod f - uint d = degree(); + int d = degree(); if (d < 0) return false; - for (uint i = 1; i <= (d / 2); ++i) { + for (uint i = 1; i <= (uint) (d / 2); ++i) { for (uint j = 0; j < fld.m; ++j) { t = xi; t.mult (xi, fld);