From 5ba94ca423dde243f4020369e274bd44632c7b3e Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Thu, 5 Apr 2012 15:20:01 +0200 Subject: [PATCH] vector helpers --- include/codecrypt.h | 17 +++++++++++++++++ lib/matrix.cpp | 28 ++++++++++++++++++++++++++++ src/main.cpp | 25 +++++++++++++------------ 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/include/codecrypt.h b/include/codecrypt.h index e4d527c..9287494 100644 --- a/include/codecrypt.h +++ b/include/codecrypt.h @@ -77,6 +77,9 @@ public: void extend_left_compact (matrix&); bool create_goppa_generator (matrix&, permutation&, prng&); bool create_goppa_generator (matrix&, const permutation&); + + bool mult_vecT_left (const bvector&, bvector&); + bool mult_vec_right (const bvector&, bvector&); }; /* @@ -158,6 +161,13 @@ public: int prepare(); int decrypt (const bvector&, bvector&); int sign (const bvector&, bvector&, uint, uint, prng&); + + uint cipher_size() { + return Pinv.size(); + } + uint plain_size() { + return Sinv.width(); + } }; class pubkey @@ -168,6 +178,13 @@ public: int encrypt (const bvector&, bvector&, prng&); int verify (const bvector&, const bvector&, uint, uint); + + uint cipher_size() { + return G.width(); + } + uint plain_size() { + return G.height(); + } }; int generate (pubkey&, privkey&, prng&, uint m, uint t); diff --git a/lib/matrix.cpp b/lib/matrix.cpp index 79ad54f..743d263 100644 --- a/lib/matrix.cpp +++ b/lib/matrix.cpp @@ -161,3 +161,31 @@ bool matrix::create_goppa_generator (matrix&g, const permutation&p) s.extend_left_compact (g); return true; } + +bool matrix::mult_vecT_left (const bvector&a, bvector&r) +{ + uint w = width(), h = height(); + if (a.size() != h) return false; + r.resize (w, 0); + for (uint i = 0; i < w; ++i) { + bool t = 0; + for (uint j = 0; j < h; ++j) + t ^= item (i) [j] & a[j]; + r[i] = t; + } + return true; +} + +bool matrix::mult_vec_right (const bvector&a, bvector&r) +{ + uint w = width(), h = height(); + if (a.size() != w) return false; + r.resize (h, 0); + for (uint i = 0; i < h; ++i) { + bool t = 0; + for (uint j = 0; j < w; ++j) + t ^= item (j) [i] & a[j]; + r[i] = t; + } + return true; +} diff --git a/src/main.cpp b/src/main.cpp index 9c98f6c..efc5400 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,35 +7,36 @@ #include using namespace std; -ostream& operator<<(ostream&o, ccr::polynomial p) +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] << ' '; + for (int i = 0, e = p.degree(); i <= e; ++i) o << p[i] << ' '; o << endl; return o; } -ostream& operator<<(ostream&o, ccr::permutation p) +ostream& operator<< (ostream&o, ccr::permutation p) { o << "permutation over " << p.size() << " elements:" << endl; - for(uint i=0;i