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

gf2m working under polys

This commit is contained in:
Mirek Kratochvil 2012-04-03 12:13:51 +02:00
parent cd65834a92
commit 5928e45a71
3 changed files with 33 additions and 33 deletions

@ -104,7 +104,7 @@ public:
uint add (uint, uint);
uint mult (uint, uint);
uint exp (uint, int);
uint exp (uint, sint);
uint inv (uint);
};

@ -17,12 +17,25 @@ int gf2p_degree (uint p)
return r;
}
inline uint gf2p_add (uint a, uint b)
{
return a ^ b;
}
void outbin (const char*n, uint x)
{
cout << n << " = ";
for (int i = 31; i >= 0; --i) cout << (1 & (x>>i) );
cout << endl;
}
uint gf2p_mod (uint a, uint p)
{
if (!p) return 0;
int t, degp = gf2p_degree (p);
while ( (t = gf2p_degree (a) ) >= degp)
while ( (t = gf2p_degree (a) ) >= degp) {
a ^= p << (t - degp);
}
return a;
}
@ -48,7 +61,7 @@ uint gf2p_modmult (uint a, uint b, uint p)
if (a & 1) r ^= b;
a >>= 1;
b <<= 1;
if (b <= d) b ^= p;
if (b >= d) b ^= p;
}
return r;
}
@ -81,38 +94,36 @@ bool gf2m::create (uint M)
return false;
}
/*
uint gfn_mult(uint a, uint b, uint n)
uint gf2m::add (uint a, uint b)
{
uint irp=0;
while(n) { irp=(irp<<1)|1; n>>=1;}
uint r=a*b;
//TODO probably move this to own file
return gf2p_add (a, b);
}
uint gfn_inv (uint a, uint n);
uint gf2m::mult (uint a, uint b)
{
return gf2p_modmult (a, b, poly);
}
uint gfn_exp (uint a, sint k, uint n)
uint gf2m::exp (uint a, sint k)
{
if (!a) return 0;
if (a == 1) return 1;
if (k < 0) {
a = gfn_inv (a, n);
a = inv (a);
k = -k;
}
uint r = 1;
while (k) {
if (k & 1) r=gfn_mult(r,a,n);
a=gfn_mult(a,a,n);
k >>= 2;
if (k & 1) r = mult (r, a);
a = mult (a, a);
k >>= 1;
}
return r;
}
uint gfn_inv (uint a, uint n)
uint gf2m::inv (uint a)
{
if (n == 2) return a;
return gfn_exp (a, ( (sint) n) - 2, n);
return exp (a, n - 2);
}
*/

@ -1,5 +1,4 @@
#if 0
#include "codecrypt.h"
using namespace ccr;
@ -34,7 +33,7 @@ void polynomial::add (const polynomial&f, gf2m&fld)
{
int df = f.degree();
if (df > degree() ) resize (df + 1);
for (int i = 0; i <= df; ++i) item (i) = item (i) ^ f[i];
for (int i = 0; i <= df; ++i) item (i) = fld.add (item (i), f[i]);
}
void polynomial::mod (const polynomial&f, gf2m&fld)
@ -42,21 +41,14 @@ void polynomial::mod (const polynomial&f, gf2m&fld)
int df = f.degree();
int d;
uint hi = fld.inv (f[df]);
cout << "mod by inv " << hi << endl;
dump (*this);
dump (f);
// while there's place to substract, reduce by x^(d-df)-multiply of f
for (d = degree(); d >= df; --d)
if (item (d) ) {
uint t = fld.mult (item (d), hi);
cout << "mult " << t << endl;
for (int i = 0; i <= df; ++i)
item (i + d - df) = fld.add (item (i + d - df)
, fld.mult (t, f[i]) );
cout << "now ";
dump (*this);
item (i + d - df) = fld.add (item (i + d - df),
fld.mult (t, f[i]) );
}
cout << "end mod" << endl;
strip();
}
@ -119,16 +111,14 @@ bool polynomial::is_irreducible (gf2m&fld)
return true;
}
void polynomial::generate_random_irreducible (uint s, gf2m&fld, prng & rng)
void polynomial::generate_random_irreducible (uint s, gf2m&fld, prng& rng)
{
resize (s + 1);
item (s) = 1; //degree s
item (0) = 1 + rng.random (fld.n - 1); //not divisible by x^1
for (uint i = 1; i < s; ++i) item (i) = rng.random (fld.n);
cout << "start ";
dump (*this);
while (!is_irreducible (fld) ) {
cout << "retry ";
dump (*this);
uint pos = rng.random (s);
item (pos) = pos == 0 ?
@ -155,4 +145,3 @@ void polynomial::compute_square_root_matrix (vector<polynomial>&r, gf2m&fld)
//TODO gauss
}
#endif