decryption

actual decryption that doesnt work
This commit is contained in:
Mirek Kratochvil 2012-04-07 15:19:53 +02:00
parent 5f167030cc
commit e4c75fe0e0
4 changed files with 58 additions and 49 deletions

View file

@ -237,5 +237,17 @@ int generate (pubkey&, privkey&, prng&);
} //namespace ccr } //namespace ccr
//global overload for iostream operators
#include <iostream>
std::ostream& operator<< (std::ostream&o, const ccr::polynomial&);
std::ostream& operator<< (std::ostream&o, const ccr::permutation&);
std::ostream& operator<< (std::ostream&o, const ccr::gf2m&);
std::ostream& operator<< (std::ostream&o, const ccr::matrix&);
std::ostream& operator<< (std::ostream&o, const ccr::bvector&);
#endif // _CODECRYPT_H_ #endif // _CODECRYPT_H_

View file

@ -4,6 +4,8 @@
using namespace ccr; using namespace ccr;
using namespace ccr::mce; using namespace ccr::mce;
#include "decoding.h"
int ccr::mce::generate (pubkey&pub, privkey&priv, prng&rng, uint m, uint t) int ccr::mce::generate (pubkey&pub, privkey&priv, prng&rng, uint m, uint t)
{ {
//finite field //finite field
@ -69,7 +71,39 @@ int pubkey::encrypt (const bvector& in, bvector&out, prng&rng)
int privkey::decrypt (const bvector&in, bvector&out) int privkey::decrypt (const bvector&in, bvector&out)
{ {
return -1; //TODO //remove the P permutation
bvector not_permuted;
Pinv.permute (in, not_permuted);
//prepare for decoding
permutation hpermInv;
hperm.compute_inversion (hpermInv);
bvector canonical, syndrome;
hperm.permute (not_permuted, canonical);
h.mult_vec_right (canonical, syndrome);
//decode
bvector ev;
syndrome_decode (syndrome, fld, g, sqInv, ev);
std::cout << "ERRORS " << ev;
//check the error vector. It should have exactly t == deg(g) errors
if ( (int) ev.hamming_weight() != g.degree() )
return 1;
//correct the errors
canonical.add (ev);
//shuffle back into systematic order
hpermInv.permute (canonical, not_permuted);
//get rid of redundancy bits
not_permuted.resize (Sinv.size() );
//unscramble the result
Sinv.mult_vec_right (not_permuted, out);
return 0;
} }
int privkey::prepare () int privkey::prepare ()

View file

@ -57,9 +57,15 @@ void polynomial::mult (const polynomial&b, gf2m&fld)
{ {
polynomial a = *this; polynomial a = *this;
clear(); clear();
uint i, j, da, db; uint i, j;
int da, db;
da = a.degree(); da = a.degree();
db = b.degree(); db = b.degree();
if ( (da < 0) || (db < 0) ) { //multiply by zero
clear();
return;
}
resize (da + db + 1, 0); resize (da + db + 1, 0);
for (i = 0; i <= da; ++i) for (i = 0; i <= da; ++i)
if (a[i]) for (j = 0; j <= db; ++j) if (a[i]) for (j = 0; j <= db; ++j)
@ -325,9 +331,9 @@ void polynomial::mod_to_fracton (polynomial&a, polynomial&b, polynomial&m, gf2m&
a0.swap (a1); a0.swap (a1);
a1.swap (t2); a1.swap (t2);
t1.mult (b1); t1.mult (b1, fld);
t1.mod (m); t1.mod (m, fld);
t1.add (b0); t1.add (b0, fld);
b0.swap (b1); b0.swap (b1);
b1.swap (t1); b1.swap (t1);
} }

View file

@ -7,49 +7,6 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
ostream& operator<< (ostream&o, ccr::polynomial p)
{
o << "polynomial degree " << p.degree() << ':' << endl;
for (int i = 0, e = p.degree(); i <= e; ++i) o << p[i] << ' ';
o << endl;
return o;
}
ostream& operator<< (ostream&o, ccr::permutation p)
{
o << "permutation over " << p.size() << " elements:" << endl;
for (uint i = 0; i < p.size(); ++i) o << p[i] << ' ';
o << endl;
return o;
}
ostream& operator<< (ostream&o, ccr::gf2m f)
{
o << "GF(2^" << f.m << ") of " << f.n << " elements, modulus " << f.poly << endl;
return o;
}
ostream& operator<< (ostream&o, ccr::matrix m)
{
uint i, j, h, w;
h = m.height();
w = m.width();
o << "binary " << h << "x" << w << " matrix:" << endl;
for (i = 0; i < h; ++i) {
for (j = 0; j < w; ++j) o << m[j][i];
o << endl;
}
return o;
}
ostream& operator<< (ostream&o, ccr::bvector v)
{
o << "vector of " << v.size() << " elements:" << endl;
for (uint i = 0, e = v.size(); i < e; ++i) cout << v[i];
cout << endl;
return o;
}
class primitiverng : public ccr::prng class primitiverng : public ccr::prng
{ {
public: public:
@ -69,7 +26,7 @@ int main()
ccr::mce::privkey priv; ccr::mce::privkey priv;
ccr::mce::pubkey pub; ccr::mce::pubkey pub;
ccr::mce::generate (pub, priv, r, 9, 9); ccr::mce::generate (pub, priv, r, 7, 4);
cout << "PRIVATE KEY" << endl; cout << "PRIVATE KEY" << endl;
cout << priv.fld; cout << priv.fld;