From 19225c36658a4cf2bec936049e9f5017959d18f5 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Wed, 4 Apr 2012 23:01:55 +0200 Subject: [PATCH] check matrix --- include/codecrypt.h | 4 +- lib/polynomial.cpp | 103 +++++++++++++++++++++++++++++--------------- 2 files changed, 72 insertions(+), 35 deletions(-) diff --git a/include/codecrypt.h b/include/codecrypt.h index 2138307..a01c0c2 100644 --- a/include/codecrypt.h +++ b/include/codecrypt.h @@ -120,13 +120,15 @@ public: void strip(); int degree() const; bool zero() const; + uint eval (uint, gf2m&) const; void add (const polynomial&, gf2m&); void mod (const polynomial&, gf2m&); void mult (const polynomial&, gf2m&); polynomial gcd (polynomial, gf2m&); - bool is_irreducible (gf2m&); + bool is_irreducible (gf2m&) const; void generate_random_irreducible (uint s, gf2m&, prng&); bool compute_square_root_matrix (std::vector&, gf2m&); + void compute_goppa_check_matrix (matrix&, gf2m&); }; /* diff --git a/lib/polynomial.cpp b/lib/polynomial.cpp index bbade71..e3b8d26 100644 --- a/lib/polynomial.cpp +++ b/lib/polynomial.cpp @@ -3,14 +3,6 @@ using namespace ccr; -#include -using namespace std; -void dump (const polynomial&t) -{ - for (uint i = 0; i < t.size(); ++i) cout << t[i] << ' '; - cout << endl; -} - int polynomial::degree() const { int r = -1; @@ -45,6 +37,7 @@ void polynomial::mod (const polynomial&f, gf2m&fld) for (d = degree(); d >= df; --d) if (item (d) ) { uint t = fld.mult (item (d), hi); + for (int i = 0; i <= df; ++i) item (i + d - df) = fld.add (item (i + d - df), fld.mult (t, f[i]) ); @@ -82,7 +75,7 @@ polynomial polynomial::gcd (polynomial b, gf2m&fld) return polynomial(); } -bool polynomial::is_irreducible (gf2m&fld) +bool polynomial::is_irreducible (gf2m&fld) const { //Ben-Or irreducibility test polynomial xi; //x^(2^i) in our case @@ -103,7 +96,7 @@ bool polynomial::is_irreducible (gf2m&fld) t.add (xmodf, fld); t = t.gcd (*this, fld); - if (t.degree() != 0) //gcd(f,x^2^i - x mod f) != 1 + if (t.degree() > 0) //gcd(f,x^2^i - x mod f) is polynomial return false; } return true; @@ -141,11 +134,11 @@ bool polynomial::compute_square_root_matrix (vector&r, gf2m&fld) l[i] = col; } // step 2, gauss-jordan inverse to unit matrix - r.resize(d); - for(int i=0;i=0;--i) - for(j=0;j= 0; --i) + for (j = 0; j < i; ++j) { + a = l[i][j]; + if (a == 0) continue; + add_row_mult (i, j, a); } return true; } + +uint polynomial::eval (uint x, gf2m&fld) const +{ + uint r = 0; + //horner + for (int i = degree(); i >= 0; --i) + r = fld.add (item (i), fld.mult (r, x) ); + return r; +} + +void polynomial::compute_goppa_check_matrix (matrix&r, gf2m&fld) +{ + if (degree() < 0) return; //wrongly initialized polynomial + uint t = degree(); + vector > yz, h; + uint i, j, k; + yz.resize (t); + h.resize (t); + for (i = 0; i < t; ++i) { + yz[i].resize (fld.n); + h[i].resize (fld.n, 0); + } + //create Y*Z + for (i = 0; i < fld.n; ++i) yz[0][i] = fld.inv (eval (i, fld) ); + for (i = 1; i < t; ++i) for (j = 0; j < fld.n; ++j) + yz[i][j] = fld.mult (yz[i-1][j], j); + //X*Y*Z = h + for (i = 0; i < t; ++i) + for (j = 0; j < fld.n; ++j) + for (k = 0; k <= i; ++k) + h[i][j] = fld.add (h[i][j], fld.mult + (yz[k][j], + item (t + k - i) ) ); + + //now convert to binary + r.resize (fld.n); + for (i = 0; i < fld.n; ++i) { + r[i].resize (fld.m * t, 0); + for (j = 0; j < fld.m * t; ++j) + r[i][j] = (h[j/fld.m][i] >> (j % fld.m) ) & 1; + } +}