source: comparacioncriptosistemas/testVarieties/polynomial.h @ 1404527

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

Agregado directorio testVarieties

  • Property mode set to 100644
File size: 4.5 KB
Line 
1// polynomial.h
2
3#pragma once
4
5#include <cstdio>
6#include <utility>
7#include <vector>
8#include <map>
9#include <list>
10#include <random>
11#include <ctime>
12#include "hash.h"
13
14namespace pol {
15
16        typedef unsigned long long u64_t;
17        typedef unsigned u32_t;
18        typedef unsigned char u8_t;
19
20        const u32_t POL1V_MAXLENGTH = 4096;
21        const u32_t M32 = 0xffffffff;
22        const u32_t KEY_DEG = 3;
23        const u32_t MODSIZE = 61;
24        const u32_t BUFSIZE = 8;
25        const u64_t MOD = 2305843009213693951; // 2147483647; //  // 2^61 - 1
26
27        class u61_t {
28        private:
29                u64_t data;
30        public:
31                u61_t(u64_t x = 0);
32                 operator u64_t() const;
33                 u61_t operator+(const u61_t &) const;
34            u61_t operator-(const u61_t &) const;
35                u61_t operator*(const u61_t &) const;
36                u61_t operator/(const u61_t &) const;
37                u61_t& operator+=(const u61_t &);
38                u61_t& operator-=(const u61_t &);
39                u61_t& operator*=(const u61_t &);
40                u61_t& operator/=(const u61_t &);
41                u61_t inverse() const;
42                u61_t pow(u32_t) const;
43                inline u64_t* get_addr();
44                inline const u64_t* get_addr() const;
45                inline u64_t& get_buf();
46                inline const u64_t& get_buf() const;
47                void print(FILE *, u32_t) const;
48        };
49
50        enum pol_format_t {
51                BINPR,
52                TEXTPR,
53                HTMLPR
54        };
55
56        class pol_t {
57        private:
58                std::vector<u61_t> data;
59                u32_t degree;
60        public:
61                typedef std::vector<pol_t> vector_t;
62                pol_t();
63                pol_t& operator=(const pol_t&);
64                void change_degree(u32_t);
65                pol_t& operator+=(const pol_t&);
66                pol_t& operator-=(const pol_t&);
67                pol_t& operator*=(const pol_t&);
68                pol_t& operator%=(const pol_t&);
69                pol_t& operator/=(const pol_t&);
70                pol_t operator+(const pol_t&) const;
71                pol_t operator-(const pol_t&) const;
72                pol_t operator*(const pol_t&) const;
73                pol_t operator%(const pol_t&) const;
74                pol_t operator/(const pol_t&) const;
75                operator bool() const;
76                bool operator==(const pol_t &) const;
77                pol_t pow(u32_t) const;
78                pol_t mpow(u64_t, const pol_t &) const;
79                pol_t mpow(u64_t, u64_t, const pol_t &) const;
80                pol_t normalize() const;
81                pol_t deriv() const;
82                void read(FILE * fin, pol_format_t format = TEXTPR);
83                void write(FILE * fout, pol_format_t format = HTMLPR, bool endofline = true) const;
84                u61_t& operator[](u32_t);
85                const u61_t& operator[](u32_t) const;
86                u64_t& operator()(u32_t);
87                const u64_t& operator()(u32_t) const;
88                 u32_t get_deg() const;
89                void fact(u32_t, vector_t &) const;
90                void ddf(u32_t, vector_t &) const;
91                void split(u32_t, vector_t &) const;
92        };
93
94        union deg_t {
95                u64_t key;
96                struct {
97                        u32_t x, y;
98                };
99                deg_t();
100                deg_t(u32_t, u32_t);
101                bool operator<(const deg_t &) const;
102                bool operator==(const deg_t &) const;
103                deg_t operator+(const deg_t&) const;
104        };
105
106        typedef std::pair<deg_t, pol_t> pol3v1_t;
107        typedef std::list<pol3v1_t> list_t;
108        typedef std::map<deg_t, pol_t> polmap_t;
109        typedef pol_t::vector_t vector_t;
110
111        class pol3v_t {
112        private:
113                list_t data;
114                u32_t dx;
115                u32_t dy;
116                u32_t dt;
117        public:
118                pol3v_t();
119                pol3v_t(const polmap_t &);
120                pol3v_t& operator=(const pol3v_t &);
121                pol3v_t operator+(const pol3v_t&) const;
122                pol3v_t operator-(const pol3v_t&) const;
123                pol3v_t operator*(const pol3v_t&) const;
124                pol3v_t& operator+=(const pol3v_t &);
125                pol3v_t& operator-=(const pol3v_t &);
126                pol3v_t& operator*=(const pol3v_t &);
127                pol_t subst(const pol_t &, const pol_t &) const;
128                pol3v_t get_same_rand_pol() const;
129                void read(FILE *, pol_format_t);
130                void write(FILE * fout, pol_format_t format = HTMLPR, bool endofline = 1) const;
131        };
132
133        enum file_mode_t {
134                READ, WRITE
135        };
136
137        class File {
138        private:
139                FILE *file;
140                bool opened;
141                file_mode_t mode;
142                u64_t buf;
143                size_t pos, count, read_size;
144        public:
145                File();
146                File(FILE *, file_mode_t);
147                File(const char *, file_mode_t);
148                ~File();
149                bool open(const char *, file_mode_t);
150                void close();
151                void flush();
152                size_t read(u61_t &);
153                size_t write(const u61_t &, size_t = MODSIZE - 1);
154        };
155
156        class random_t {
157        private:
158                std::mt19937_64 generator;
159        public:
160                random_t();
161                u61_t operator()();
162        };
163
164        pol_t gcd(const pol_t &, const pol_t &);
165        pol_t get_x();
166        pol_t get_rand_pol(u32_t deg, bool f = true);
167        template <unsigned CLOSE_KEY_DEG = KEY_DEG, unsigned OPEN_KEY_DEG = KEY_DEG>
168        pol3v_t get_open_key(const pol_t &, const pol_t &);
169        template <unsigned Key_Deg = 2 * KEY_DEG>
170        void encrypt(File &, FILE *, const pol3v_t &);
171        template <unsigned CLOSE_KEY_DEG = KEY_DEG, unsigned OPEN_KEY_DEG = KEY_DEG>
172        void decrypt(FILE *, File &, const pol_t &, const pol_t &);
173
174        pol3v_t XXX_get_f(const u64_t max_deg, const u32_t Key_Deg);
175        pol3v_t XXX_get_mes(const u64_t max_deg, const u32_t Key_Deg, const u64_t size, const std::vector<u61_t> &);
176        void XXX_get_fact(const vector_t &fact, u64_t deg, vector_t &res);
177
178        #include "polinomial.hpp"
179};
Note: See TracBrowser for help on using the repository browser.