From 00f096fc078d1aa06927901d888261d4d13de495 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sat, 4 Feb 2006 16:19:03 +0000 Subject: [PATCH] gf: Fix gf_irreduciblep() for small-degree polynomials. Fix division-by-zero error for argument zero, and segfaults for arguments with degree less than 2 due to skipping the main loop. Handle these as a special case. --- gf-arith.c | 10 ++++++++-- tests/gf | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/gf-arith.c b/gf-arith.c index f380a35..eabbff8 100644 --- a/gf-arith.c +++ b/gf-arith.c @@ -179,11 +179,17 @@ void gf_div(mp **qq, mp **rr, mp *a, mp *b) int gf_irreduciblep(mp *f) { - unsigned long m = mp_bits(f) - 1; + unsigned long m; mp *u = MP_TWO; mp *v = MP_NEW; - m /= 2; + if (MP_ZEROP(f)) + return (0); + else if (MP_LEN(f) == 1) { + if (f->v[0] < 2) return (0); + if (f->v[0] < 4) return (1); + } + m = (mp_bits(f) - 1)/2; while (m) { u = gf_sqr(u, u); gf_div(0, &u, u, f); diff --git a/tests/gf b/tests/gf index 801993d..fded680 100644 --- a/tests/gf +++ b/tests/gf @@ -59,6 +59,9 @@ exp { } irred { + 0 0; + 1 0; + 2 1; 0xc1a7bd3b4e853fc92d4e1588719986aa 0; 0x800000000000000000000000000000000000000c9 1; 0x2000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001 1; -- 2.11.0