source: comparacioncriptosistemas/testVarieties/polinomial.hpp @ b469dad

interfaz
Last change on this file since b469dad was b939736, checked in by Fundación CENDITEL <cenditel@…>, 8 years ago

Agregado directorio testVarieties

  • Property mode set to 100644
File size: 2.8 KB
Line 
1#include "polynomial.h"
2//using namespace pol;
3template <unsigned KEY_DEG>
4
5void encrypt(File &in, FILE *out, const pol3v_t &D) {
6        std::vector<u61_t> m;
7        u61_t buf;
8        u64_t size = 0, block_size;
9        while (block_size = in.read(buf), block_size) {
10                m.push_back(buf);
11                size += block_size;
12        }
13        block512_t h = hash_nm::hash((char *)(m.data()), m.size() * sizeof(u61_t));
14        fwrite(&h, sizeof(h), 1, out);
15        u64_t max_deg = (m.size() - 1) / KEY_DEG + 2;
16        pol3v_t f = XXX_get_f(max_deg, KEY_DEG);
17        pol3v_t mes = XXX_get_mes(max_deg, KEY_DEG, size, m);
18        (mes += f * D.get_same_rand_pol() + D * f.get_same_rand_pol()).write(out, TEXTPR);
19        (mes += f * D.get_same_rand_pol() + D * f.get_same_rand_pol()).write(out, TEXTPR);
20}
21
22template <unsigned CLOSE_KEY_DEG, unsigned OPEN_KEY_DEG>
23void decrypt(FILE *in, File &out, const pol_t &x, const pol_t &y) {
24        block512_t h1, h2;
25        fread(&h1, sizeof(h1), 1, in);
26        pol3v_t tmp;
27        tmp.read(in, TEXTPR);
28        pol_t m, m1, fc, pol_x = get_x(), kxy = x * y;
29        m1 = tmp.subst(x, y);
30        tmp.read(in, TEXTPR);
31        fc = tmp.subst(x, y) - m1;
32        vector_t factor;
33        fc.fact(CLOSE_KEY_DEG * OPEN_KEY_DEG * 2 + 1, factor);
34        XXX_get_fact(factor, CLOSE_KEY_DEG * OPEN_KEY_DEG * 2 + 1, factor);
35        std::vector<u61_t> res;
36        u61_t a = x[CLOSE_KEY_DEG].inverse() * y[CLOSE_KEY_DEG].inverse();
37        for (u32_t j = 0; j < factor.size(); ++j) {
38                res.clear();
39                m = m1 % (fc / factor[j]);
40                u32_t deg = m.get_deg();
41                u64_t size = m[deg] * a.pow(deg / (2 * CLOSE_KEY_DEG));
42                m %= kxy.pow(deg / (2 * CLOSE_KEY_DEG)) * pol_x.pow(deg % (2 * CLOSE_KEY_DEG));
43                deg = m.get_deg();
44                for (u32_t i = 0; i <= deg; ++i) {
45                        res.push_back(m[deg - i] * a.pow((deg - i) / (2 * CLOSE_KEY_DEG)));
46                        m %= kxy.pow((deg - i) / (2 * CLOSE_KEY_DEG)) * pol_x.pow((deg - i) % (2 * CLOSE_KEY_DEG));
47                }
48                block512_t h2 = hash_nm::hash((char *)(res.data()), res.size() * sizeof(u61_t));
49                if (h1 == h2) {
50//                      for (u32_t i = 0; i < res.size(); ++i) {
51//                              if (size > MODSIZE - 1) {
52//                                      size -= out.write(res[i]);
53//                              }
54//                              else {
55//                                      size -= out.write(res[i], size);
56//                              }
57//                      }
58                        out.flush();
59                        break;
60                }
61        }
62}
63
64template <unsigned CLOSE_KEY_DEG, unsigned OPEN_KEY_DEG>
65   pol3v_t get_open_key(const pol_t &x, const pol_t &y) {
66        polmap_t tmp;
67        pol3v_t tmp2;
68        for (u32_t i = 1; i <= OPEN_KEY_DEG; ++i) {
69                tmp.insert(pol3v1_t(deg_t(i, i), get_rand_pol(2 * (OPEN_KEY_DEG - i) * CLOSE_KEY_DEG + 1, false)));
70        }
71        tmp.insert(pol3v1_t(deg_t(OPEN_KEY_DEG - 1, OPEN_KEY_DEG), get_rand_pol(1 + CLOSE_KEY_DEG, false)));
72        tmp.insert(pol3v1_t(deg_t(OPEN_KEY_DEG, OPEN_KEY_DEG - 1), get_rand_pol(1 + CLOSE_KEY_DEG, false)));
73        tmp.insert(pol3v1_t(deg_t(0, 1), get_rand_pol(CLOSE_KEY_DEG * (2 * OPEN_KEY_DEG - 1) + 1, false)));
74        tmp.insert(pol3v1_t(deg_t(1, 0), get_rand_pol(CLOSE_KEY_DEG * (2 * OPEN_KEY_DEG - 1) + 1, false)));
75        tmp2 = tmp;
76        tmp.insert(pol3v1_t(deg_t(0, 0), pol_t() - tmp2.subst(x, y)));
77        return tmp;
78}
Note: See TracBrowser for help on using the repository browser.