From 09538f45d801532a2d1cfacb2795de3390f5b1ad Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Sun, 30 Sep 2012 11:55:23 +0200 Subject: [PATCH] mce_qd: encryption --- include/codecrypt.h | 3 ++ lib/fwht.cpp | 53 +++++++++++++++++++ lib/fwht.h | 13 +++++ lib/mce_qd.cpp | 120 ++++++++++++++++++++++++++++++++++++++------ 4 files changed, 175 insertions(+), 14 deletions(-) create mode 100644 lib/fwht.cpp create mode 100644 lib/fwht.h diff --git a/include/codecrypt.h b/include/codecrypt.h index a8462e9..ffcc6db 100644 --- a/include/codecrypt.h +++ b/include/codecrypt.h @@ -388,6 +388,8 @@ public: std::vector Hsig; //signature of canonical H matrix std::vector support; //computed goppa support polynomial g; //computed goppa polynomial + std::vector sqInv; + int decrypt (const bvector&, bvector&); int prepare(); @@ -404,6 +406,7 @@ class pubkey { public: uint T; + uint k; std::vector qd_sigs; int encrypt (const bvector&, bvector&, prng&); diff --git a/lib/fwht.cpp b/lib/fwht.cpp new file mode 100644 index 0000000..e913c52 --- /dev/null +++ b/lib/fwht.cpp @@ -0,0 +1,53 @@ + +#include "fwht.h" + +#include +using namespace std; + +/* + * we count on that all integers are sufficiently large. + * They should be, largest value occuring should be O(k*n) if initial vector is + * consisted only from {0,1}^n, and we don't usually have codes of this size. + */ + +static void fwht (vector x, vector&r) +{ + int bs, s; + s = x.size(); + r.resize (s); + bs = s >> 1; + r.swap (x); + while (bs) { + x.swap (r); + for (uint i = 0; i < s; ++i) { + if ( (i / bs) & 1) + r[i] = x[i-bs] - x[i]; + else + r[i] = x[i] + x[i+bs]; + } + bs >>= 1; + } +} + +//we expect correct parameter size and preallocated out. +void fwht_dyadic_multiply (const bvector& a, const bvector& b, bvector& out) +{ + + //lift everyting to Z. + vector t, A, B; + uint i; + + for (i = 0; i < a.size(); ++i) t[i] = a[i]; + fwht (t, A); + + for (i = 0; i < b.size(); ++i) t[i] = b[i]; + fwht (t, B); + + //multiply diagonals to A + for (i = 0; i < A.size(); ++i) A[i] *= B[i]; + fwht (A, t); + + uint bitpos = a.size(); //no problem as a.size() == 1< 0;) { + uint p = rng.random (e.size() ); + if (!e[p]) { + e[p] = 1; + --n; + } + } + + //compute ciphertext + out = in; + out.insert (out.end(), cksum.begin(), cksum.end() ); + out.add (e); + return 0; }