From a14d1088e7c361425c99bc67c0457d711b628141 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Sun, 2 Dec 2012 12:24:29 +0100 Subject: [PATCH] serialization work --- include/codecrypt.h | 35 ++++++ lib/serialization.cpp | 287 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 322 insertions(+) create mode 100644 lib/serialization.cpp diff --git a/include/codecrypt.h b/include/codecrypt.h index 855ed16..2f4beb1 100644 --- a/include/codecrypt.h +++ b/include/codecrypt.h @@ -122,6 +122,9 @@ public: void colex_rank (bvector&) const; void colex_unrank (bvector&, uint n, uint k) const; + + sencode* serialize(); + bool unserialize (sencode*); }; /* @@ -187,6 +190,9 @@ public: bool create_goppa_generator_dyadic (matrix&, uint&, prng&); bool create_goppa_generator_dyadic (matrix&, uint); + + sencode* serialize(); + bool unserialize (sencode*); }; /* @@ -242,6 +248,8 @@ public: return true; } + sencode* serialize(); + bool unserialize (sencode*); }; /* @@ -264,6 +272,9 @@ public: uint exp (int); uint inv (uint); uint sq_root (uint); + + sencode* serialize(); + bool unserialize (sencode*); }; /* @@ -306,6 +317,9 @@ public: bool compute_square_root_matrix (std::vector&, gf2m&); void compute_goppa_check_matrix (matrix&, gf2m&); + + sencode* serialize(); + bool unserialize (sencode*); }; /* @@ -342,6 +356,9 @@ public: uint signature_size() { return plain_size(); } + + sencode* serialize(); + bool unserialize (sencode*); }; class pubkey @@ -365,6 +382,9 @@ public: uint signature_size() { return plain_size(); } + + sencode* serialize(); + bool unserialize (sencode*); }; int generate (pubkey&, privkey&, prng&, uint m, uint t); @@ -405,6 +425,9 @@ public: uint signature_size() { return plain_size(); } + + sencode* serialize(); + bool unserialize (sencode*); }; class pubkey @@ -431,6 +454,9 @@ public: uint signature_size() { return plain_size(); } + + sencode* serialize(); + bool unserialize (sencode*); }; int generate (pubkey&, privkey&, prng&, uint m, uint t); @@ -476,6 +502,9 @@ public: uint plain_size() { return (1 << T) * (block_count - fld.m); } + + sencode* serialize(); + bool unserialize (sencode*); }; class pubkey @@ -541,6 +570,9 @@ public: uint signature_size() { return Sinv.size(); } + + sencode* serialize(); + bool unserialize (sencode*); }; class pubkey @@ -557,6 +589,9 @@ public: uint signature_size() { return G.height(); } + + sencode* serialize(); + bool unserialize (sencode*); }; //n is the number of subcodes used diff --git a/lib/serialization.cpp b/lib/serialization.cpp new file mode 100644 index 0000000..311a7e0 --- /dev/null +++ b/lib/serialization.cpp @@ -0,0 +1,287 @@ + +#include "codecrypt.h" + +using namespace ccr; + +sencode* bvector::serialize() +{ + uint ss = (size() + 7) / 8; + std::string bytes; + bytes.resize (ss, '\0'); + for (uint i = 0; i < size(); ++i) + if (item (i) ) bytes[i / 8] |= 1 << (i % 8); + sencode_list*l = new sencode_list; + l->items.push_back (new sencode_int (size() ) ); + l->items.push_back (new sencode_bytes (bytes) ); + return l; +} + +bool bvector::unserialize (sencode* s) +{ + sencode_list*l = dynamic_cast (s); + if (!l) return false; + if (l->items.size() != 2) return false; + sencode_int*size = dynamic_cast (l->items[0]); + sencode_bytes*bytes = dynamic_cast (l->items[1]); + if (! (size && bytes) ) return false; + if (bytes->b.size() != ( (size->i + 7) / 8) ) return false; + clear(); + resize (size->i, 0); + for (uint i = 0; i < size->i; ++i) + if ( (bytes->b[i / 8] >> (i % 8) ) & 1) + item (i) = 1; + return true; +} + +sencode* matrix::serialize() +{ + uint bits = width() * height(); + uint ss = (bits + 7) / 8; + std::string bytes; + bytes.resize (ss, '\0'); + for (uint i = 0; i < bits; ++i) + if (item (i / height(), i % height() ) ) bytes[i / 8] |= 1 << (i % 8); + sencode_list*l = new sencode_list; + l->items.push_back (new sencode_int (width() ) ); + l->items.push_back (new sencode_int (height() ) ); + l->items.push_back (new sencode_bytes (bytes) ); + return l; +} + +bool matrix::unserialize (sencode* s) +{ + sencode_list*l = dynamic_cast (s); + if (!l) return false; + if (l->items.size() != 3) return false; + sencode_int*w = dynamic_cast (l->items[0]); + sencode_int*h = dynamic_cast (l->items[1]); + sencode_bytes*bytes = dynamic_cast (l->items[2]); + if (! (h && w && bytes) ) return false; + if (bytes->b.size() != ( ( (h->i * w->i) + 7) / 8) ) return false; + clear(); + resize2 (w->i, h->i, 0); + for (uint i = 0; i < w->i * h->i; ++i) + if ( (bytes->b[i / 8] >> (i % 8) ) & 1) + item (i / h->i, i % h->i) = 1; + return true; +} + +sencode* permutation::serialize() +{ + sencode_list*l = new sencode_list; + l->items.resize (size() ); + for (uint i = 0; i < size(); ++i) + l->items[i] = new sencode_int (item (i) ); + return l; +} + +bool permutation::unserialize (sencode* s) +{ + sencode_list*l = dynamic_cast (s); + if (!l) return false; + clear(); + resize (l->items.size() ); + for (uint i = 0; i < size(); ++i) { + sencode_int*x = dynamic_cast (l->items[i]); + if (!x) return false; + if (x->i >= size() ) return false; //small sanity check + item (i) = x->i; + } + return true; +} + +sencode* gf2m::serialize() +{ + return new sencode_int (m); +} + +bool gf2m::unserialize (sencode* s) +{ + sencode_int*p = dynamic_cast (s); + if (!p) return false; + return create (p->i); +} + +sencode* polynomial::serialize() +{ + sencode_list*l = new sencode_list; + l->items.resize (size() ); + for (uint i = 0; i < size(); ++i) + l->items[i] = new sencode_int (item (i) ); + return l; +} + +bool polynomial::unserialize (sencode* s) +{ + sencode_list*l = dynamic_cast (s); + if (!l) return false; + clear(); + resize (l->items.size() ); + for (uint i = 0; i < size(); ++i) { + sencode_int*x = dynamic_cast (l->items[i]); + if (!x) return false; + item (i) = x->i; + } + return true; +} + +sencode* mce::privkey::serialize() +{ + sencode_list*l = new sencode_list; + l->items.resize (5); + l->items[0] = fld.serialize(); + l->items[1] = g.serialize(); + l->items[2] = hperm.serialize(); + l->items[3] = Pinv.serialize(); + l->items[4] = Sinv.serialize(); + return l; +} + +bool mce::privkey::unserialize (sencode* s) +{ + sencode_list*l = dynamic_cast (s); + if (!l) return false; + if (l->items.size() != 5) return false; + + if (! (fld.unserialize (l->items[0]) && + g.unserialize (l->items[1]) && + hperm.unserialize (l->items[2]) && + Pinv.unserialize (l->items[3]) && + Sinv.unserialize (l->items[4]) ) ) return false; + + return true; +} + +sencode* mce::pubkey::serialize() +{ + sencode_list*l = new sencode_list; + l->items.resize (2); + l->items[0] = new sencode_int (t); + l->items[1] = G.serialize(); + return l; +} + +bool mce::pubkey::unserialize (sencode* s) +{ + sencode_list*l = dynamic_cast (s); + if (!l) return false; + if (l->items.size() != 2) return false; + + sencode_int*p = dynamic_cast (l->items[0]); + if (!p) return false; + t = p->i; + + if (!G.unserialize (l->items[1]) ) return false; + + return true; +} + +sencode* nd::privkey::serialize() +{ + sencode_list*l = new sencode_list; + l->items.resize (4); + l->items[0] = fld.serialize(); + l->items[1] = g.serialize(); + l->items[2] = Pinv.serialize(); + l->items[3] = Sinv.serialize(); + return l; +} + +bool nd::privkey::unserialize (sencode* s) +{ + sencode_list*l = dynamic_cast (s); + if (!l) return false; + if (l->items.size() != 4) return false; + + if (! (fld.unserialize (l->items[0]) && + g.unserialize (l->items[1]) && + Pinv.unserialize (l->items[2]) && + Sinv.unserialize (l->items[3]) ) ) return false; + + return true; +} + +sencode* nd::pubkey::serialize() +{ + sencode_list*l = new sencode_list; + l->items.resize (2); + l->items[0] = new sencode_int (t); + l->items[1] = H.serialize(); + return l; +} + +bool nd::pubkey::unserialize (sencode* s) +{ + sencode_list*l = dynamic_cast (s); + if (!l) return false; + if (l->items.size() != 2) return false; + + sencode_int*p = dynamic_cast (l->items[0]); + if (!p) return false; + t = p->i; + + if (!H.unserialize (l->items[1]) ) return false; + + return true; +} + +sencode* mce_qd::privkey::serialize() +{ + +} + +bool mce_qd::privkey::unserialize (sencode* s) +{ + +} + +sencode* mce_qd::pubkey::serialize() +{ + +} + +bool mce_qd::pubkey::unserialize (sencode* s) +{ + +} + +sencode* cfs_qd::privkey::serialize() +{ + +} + +bool cfs_qd::privkey::unserialize (sencode* s) +{ + +} + +sencode* cfs_qd::pubkey::serialize() +{ + +} + +bool cfs_qd::pubkey::unserialize (sencode* s) +{ + +} + +sencode* mce_oc::privkey::serialize() +{ + +} + +bool mce_oc::privkey::unserialize (sencode* s) +{ + +} + +sencode* mce_oc::pubkey::serialize() +{ + +} + +bool mce_oc::pubkey::unserialize (sencode* s) +{ + +} +