codecrypt/lib/mce.cpp
2012-04-05 16:25:43 +02:00

94 lines
1.7 KiB
C++

#include "codecrypt.h"
using namespace ccr;
using namespace ccr::mce;
int ccr::mce::generate (pubkey&pub, privkey&priv, prng&rng, uint m, uint t)
{
//finite field
priv.fld.create (m);
//goppa polynomial
priv.g.generate_random_irreducible (t, priv.fld, rng);
//check and generator matrix
matrix generator;
permutation hp;
priv.g.compute_goppa_check_matrix (priv.h, priv.fld);
int attempts_left = 1 << m;
for (;;) {
if (priv.h.create_goppa_generator (generator, hp, rng) ) break;
--attempts_left;
}
if (!attempts_left) return 1;
hp.compute_inversion (priv.hperm);
//scramble matrix
matrix S;
S.generate_random_invertible (generator.height(), rng);
S.compute_inversion (priv.Sinv);
//scramble permutation
permutation P;
P.generate_random (generator.width(), rng);
P.compute_inversion (priv.Pinv);
//public key
pub.t = t;
S.mult (generator);
P.permute (S, pub.G);
return 0;
}
int pubkey::encrypt (const bvector& in, bvector&out, prng&rng)
{
uint s = cipher_size();
if (t > s) return 1;
if (in.size() != plain_size() ) return 2;
//make a codeword
G.mult_vecT_left (in, out);
//add error vector
bvector e;
e.resize (s, 0);
for (uint n = t; n > 0;) {
uint p = rng.random (s);
if (!e[p]) {
e[p] = 1;
--n;
}
}
out.add (e);
return 0;
}
int privkey::decrypt (const bvector&in, bvector&out)
{
return -1; //TODO
}
int privkey::prepare ()
{
g.compute_goppa_check_matrix (h, fld);
g.compute_square_root_matrix (sqInv, fld);
return 0;
}
int privkey::sign (const bvector&in, bvector&out, uint delta, uint h, prng&rng)
{
return -1; //TODO
}
int pubkey::verify (const bvector&in, const bvector&hash, uint delta, uint h)
{
return -1; //TODO
}