1
0
mirror of https://github.com/biergaizi/codecrypt synced 2024-06-30 02:43:06 +00:00

debugging stash 1

This commit is contained in:
Mirek Kratochvil 2012-05-13 00:17:12 +02:00
parent 985c71e831
commit 1c2e807f69
4 changed files with 39 additions and 32 deletions

@ -38,6 +38,7 @@ public:
bool operator* (const bvector&); //dot product
bool zero() const;
void to_poly (polynomial&, gf2m&);
void from_poly (const polynomial&, gf2m&);
};
/*

@ -34,8 +34,16 @@ bool bvector::zero() const
void bvector::to_poly (polynomial&r, gf2m&fld)
{
r.clear();
if(size() % fld.m) return; //impossible
if (size() % fld.m) return; //impossible
r.resize (size() / fld.m, 0);
for (uint i = 0; i < size(); ++i)
if (item (i) ) r[i/fld.m] |= 1 << (i % fld.m);
if (item (i) ) r[i/fld.m] |= (1 << (i % fld.m) );
}
void bvector::from_poly (const polynomial&r, gf2m&fld)
{
clear();
resize (r.size() *fld.m, 0);
for (uint i = 0; i < size(); ++i)
item (i) = (r[i/fld.m] >> (i % fld.m) ) & 1;
}

@ -76,7 +76,7 @@ bool gf2m::create (uint M)
m = M;
n = 1 << m;
if (!n) return false; //too big.
for (uint t = 1 + (1 << m), e = 1 << (1 + m); t < e; t += 2)
for (uint t = (1 << m)+1, e = 1 << (m+1); t < e; t += 2)
if (is_irreducible_gf2_poly (t) ) {
poly = t;
return true;

@ -67,15 +67,14 @@ void polynomial::mod (const polynomial&f, gf2m&fld)
void polynomial::mult (const polynomial&b, gf2m&fld)
{
polynomial a = *this;
clear();
uint i, j;
int da, db;
da = a.degree();
db = b.degree();
if ( (da < 0) || (db < 0) ) { //multiply by zero
clear();
clear();
if ( (da < 0) || (db < 0) ) //multiply by zero
return;
}
resize (da + db + 1, 0);
for (i = 0; i <= da; ++i)
@ -113,7 +112,7 @@ bool polynomial::is_irreducible (gf2m&fld) const
xmodf.mod (*this, fld); //mod f
uint d = degree();
for (uint i = 1; i <= d / 2; ++i) {
for (uint i = 1; i <= (d / 2); ++i) {
for (uint j = 0; j < fld.m; ++j) {
t = xi;
t.mult (xi, fld);
@ -124,7 +123,7 @@ bool polynomial::is_irreducible (gf2m&fld) const
t.add (xmodf, fld);
t = t.gcd (*this, fld);
if (t.degree() > 0) //gcd(f,x^2^i - x mod f) != const
if (!t.one() )
return false;
}
return true;
@ -228,7 +227,7 @@ void polynomial::compute_goppa_check_matrix (matrix&r, gf2m&fld)
{
if (degree() < 0) return; //wrongly initialized polynomial
uint t = degree();
vector<vector<uint> > vd, h;
vector<polynomial> vd, h; //matrix over fld
uint i, j, k;
//construction from Barreto's slides with maximal support L=[0..fld.n)
@ -246,20 +245,18 @@ void polynomial::compute_goppa_check_matrix (matrix&r, gf2m&fld)
for (i = 0; i < fld.n; ++i) {
h[i].resize (t);
for (j = 0; j < t; ++j) { //computing the element h[i][j]
h[i][j]=0;
h[i][j] = 0;
for (k = 0; k <= j; ++k) //k = column index of t
h[i][j] = fld.add (h[i][j],
fld.mult (item (t - j + k),
fld.mult (item (t + k - j),
vd[i][k]) );
}
}
//now convert to binary
r.resize (fld.n);
for (i = 0; i < fld.n; ++i) {
r[i].resize (fld.m * t);
for (j = 0; j < fld.m * t; ++j)
r[i][j] = (h[i][j/fld.m] >> (j % fld.m) ) & 1;
}
for (i = 0; i < fld.n; ++i)
r[i].from_poly (h[i], fld);
}
void polynomial::make_monic (gf2m&fld)
@ -279,7 +276,7 @@ void polynomial::shift (uint n)
void polynomial::square (gf2m&fld)
{
polynomial a = *this;
this->mult (a, fld);
mult (a, fld);
}
void polynomial::sqrt (vector<polynomial>& sqInv, gf2m&fld)
@ -329,10 +326,7 @@ void polynomial::div (polynomial&p, polynomial&m, gf2m&fld)
}
*this = s0;
if (r0.degree() >= 0) {
uint m = fld.inv (r0[r0.degree() ]);
for (uint i = 0; i < size(); ++i) item (i) = fld.mult (item (i), m);
}
make_monic(fld);
}
void polynomial::divmod (polynomial&d, polynomial&res, polynomial&rem, gf2m&fld)
@ -356,31 +350,35 @@ void polynomial::divmod (polynomial&d, polynomial&res, polynomial&rem, gf2m&fld)
void polynomial::inv (polynomial&m, gf2m&fld)
{
polynomial a = *this;
this->resize (1);
resize (1);
item (0) = 1;
div (a, m, fld);
}
void polynomial::mod_to_fracton (polynomial&a, polynomial&b, polynomial&m, gf2m&fld)
void polynomial::mod_to_fracton (polynomial&a, polynomial&b,
polynomial&m, gf2m&fld)
{
int deg = m.degree() / 2;
polynomial a0, a1, b0, b1, t1, t2;
polynomial a0, a1, b0, b1, q, r;
a0 = m;
a1 = *this;
a1.mod (m, fld);
b0.resize (1, 0);
b0.clear();
b1.clear();
b1.resize (1, 1);
while (a1.degree() > deg) {
a0.divmod (a1, t1, t2, fld);
a0.divmod (a1, q, r, fld);
a0.swap (a1);
a1.swap (t2);
a1.swap (r);
t1.mult (b1, fld);
t1.mod (m, fld);
t1.add (b0, fld);
q.mult (b1, fld);
q.mod (m, fld);
q.add (b0, fld);
b0.swap (b1);
b1.swap (t1);
b1.swap (q);
}
a = a1;
b = b1;