From b715459593f6294f233f9b127c4673d1ffd188c5 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Thu, 5 Apr 2012 16:25:43 +0200 Subject: [PATCH] mce encryption --- lib/mce.cpp | 19 ++++++++++++++++++- src/main.cpp | 32 +++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/lib/mce.cpp b/lib/mce.cpp index a78cd08..be3aaa8 100644 --- a/lib/mce.cpp +++ b/lib/mce.cpp @@ -46,8 +46,25 @@ int ccr::mce::generate (pubkey&pub, privkey&priv, prng&rng, uint m, uint t) 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; - return -1; //TODO + //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) diff --git a/src/main.cpp b/src/main.cpp index efc5400..4633094 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,14 @@ ostream& operator<< (ostream&o, ccr::matrix m) 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 { public: @@ -61,7 +69,7 @@ int main() ccr::mce::privkey priv; ccr::mce::pubkey pub; - ccr::mce::generate (pub, priv, r, 9, 15); + ccr::mce::generate (pub, priv, r, 9, 9); cout << "PRIVATE KEY" << endl; cout << priv.fld; @@ -73,6 +81,28 @@ int main() cout << pub.t << endl; cout << pub.G; + ccr::bvector plain; + plain.resize (pub.plain_size() ); + plain[0] = 1; + plain[1] = 1; + plain[2] = 1; + + cout << "PLAINTEXT" << endl; + cout << plain; + + ccr::bvector cipher; + pub.encrypt (plain, cipher, r); + + cout << "CIPHERTEXT" << endl; + cout << cipher; + + priv.prepare(); + + ccr::bvector result; + priv.decrypt (cipher, result); + + cout << "DECRYPTED" << endl; + cout << result; return 0; }