source: comparacioncriptosistemas/testVarieties/polynomial.h

interfaz
Last change on this file was 130b818, checked in by aaraujo <aaraujo@…>, 8 years ago

Documentación inicial de los archivos fuentes.

  • Property mode set to 100644
File size: 7.9 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    /**
21     * @brief Máxima longitud en bit de un polinomio
22     */
23        const u32_t POL1V_MAXLENGTH = 4096;
24
25    /**
26     * @brief
27     */
28    const u32_t M32 = 0xffffffff;
29
30    /**
31     * @brief Máxima grado del polinomio de la clave privada
32     */
33    const u32_t KEY_DEG = 3;
34
35    /**
36     * @brief Característica del campo finito
37     */
38        const u32_t MODSIZE = 61;
39
40    /**
41     * @brief
42     */
43        const u32_t BUFSIZE = 8;
44
45    /**
46     * @brief Tamaño del campo finito
47     */
48        const u64_t MOD = 2305843009213693951; // 2147483647; //  // 2^61 - 1
49
50
51
52    /**
53     * @brief Operaciones sobre el campo finito
54     */
55        class u61_t {
56        private:
57                u64_t data;
58        public:
59                u61_t(u64_t x = 0);
60                 operator u64_t() const;
61                 u61_t operator+(const u61_t &) const;
62            u61_t operator-(const u61_t &) const;
63                u61_t operator*(const u61_t &) const;
64                u61_t operator/(const u61_t &) const;
65                u61_t& operator+=(const u61_t &);
66                u61_t& operator-=(const u61_t &);
67                u61_t& operator*=(const u61_t &);
68                u61_t& operator/=(const u61_t &);
69                u61_t inverse() const;
70                u61_t pow(u32_t) const;
71                inline u64_t* get_addr();
72                inline const u64_t* get_addr() const;
73                inline u64_t& get_buf();
74                inline const u64_t& get_buf() const;
75                void print(FILE *, u32_t) const;
76        };
77
78    /**
79     * @brief El tipo de representación de coeficientes de un polinomio
80     */
81        enum pol_format_t {
82        BINPR,  // representación polinómica de un número en binario
83        TEXTPR, // representación polinómica de un número en texto
84        HTMLPR  // representación polinómica ...
85        };
86
87    /**
88     * @brief Clase que representa un polinomio de un variable sobre el campo finito
89     */
90        class pol_t {
91        private:
92                std::vector<u61_t> data;
93                u32_t degree;
94        public:
95                typedef std::vector<pol_t> vector_t;
96                pol_t();
97                pol_t& operator=(const pol_t&);
98                void change_degree(u32_t);
99                pol_t& operator+=(const pol_t&);
100                pol_t& operator-=(const pol_t&);
101                pol_t& operator*=(const pol_t&);
102                pol_t& operator%=(const pol_t&);
103                pol_t& operator/=(const pol_t&);
104                pol_t operator+(const pol_t&) const;
105                pol_t operator-(const pol_t&) const;
106                pol_t operator*(const pol_t&) const;
107                pol_t operator%(const pol_t&) const;
108                pol_t operator/(const pol_t&) const;
109                operator bool() const;
110                bool operator==(const pol_t &) const;
111                pol_t pow(u32_t) const;
112                pol_t mpow(u64_t, const pol_t &) const;
113                pol_t mpow(u64_t, u64_t, const pol_t &) const;
114
115        /**
116         * @brief Normaliza el polinomio.
117         *
118         * El coeficiente lider (de máximo grado) sea 1.
119         *
120         * @return polinonio normalizado
121         */
122        pol_t normalize() const;
123
124        /**
125         * @brief Deriva el polinomio
126         *
127         * @return polinomio derivado
128         */
129        pol_t deriv() const;
130
131        /**
132         * @brief Leer un polinomio desde un archivo
133         * @param fin
134         * @param format
135         */
136                void read(FILE * fin, pol_format_t format = TEXTPR);
137
138        /**
139         * @brief Escribir un polinomio hacia un archivo
140         * @param fout
141         * @param format
142         * @param endofline
143         */
144                void write(FILE * fout, pol_format_t format = HTMLPR, bool endofline = true) const;
145
146        u61_t& operator[](u32_t);
147                const u61_t& operator[](u32_t) const;
148                u64_t& operator()(u32_t);
149                const u64_t& operator()(u32_t) const;
150
151        /**
152         * @brief Retorna el grado del polinomio
153         * @return grado del polinomio
154         */
155        u32_t get_deg() const;
156
157        /**
158         * @brief Factoriza el poloniomio
159         */
160                void fact(u32_t, vector_t &) const;
161
162        void ddf(u32_t, vector_t &) const;
163
164        /**
165         * @brief Separa el polinomio en sus factores
166         *
167         */
168        void split(u32_t, vector_t &) const;
169        };
170
171    /**
172     * @brief Estructura para mantener el grado de un polinomio de varias variables
173     */
174        union deg_t {
175                u64_t key;
176                struct {
177                        u32_t x, y;
178                };
179                deg_t();
180                deg_t(u32_t, u32_t);
181                bool operator<(const deg_t &) const;
182                bool operator==(const deg_t &) const;
183                deg_t operator+(const deg_t&) const;
184        };
185
186        typedef std::pair<deg_t, pol_t> pol3v1_t;
187        typedef std::list<pol3v1_t> list_t;
188        typedef std::map<deg_t, pol_t> polmap_t;
189        typedef pol_t::vector_t vector_t;
190
191
192    /**
193     * @brief Clase que representa un polinomio de tres variables
194     */
195        class pol3v_t {
196        private:
197                list_t data;
198                u32_t dx;
199                u32_t dy;
200                u32_t dt;
201        public:
202                pol3v_t();
203                pol3v_t(const polmap_t &);
204                pol3v_t& operator=(const pol3v_t &);
205                pol3v_t operator+(const pol3v_t&) const;
206                pol3v_t operator-(const pol3v_t&) const;
207                pol3v_t operator*(const pol3v_t&) const;
208                pol3v_t& operator+=(const pol3v_t &);
209                pol3v_t& operator-=(const pol3v_t &);
210                pol3v_t& operator*=(const pol3v_t &);
211                pol_t subst(const pol_t &, const pol_t &) const;
212
213        /**
214         * @brief Retorna un polinomio con la misma característica (forma)
215         * @return
216         */
217        pol3v_t get_same_rand_pol() const;
218
219        /**
220         * @brief Leer un polinomio desde un archivo
221         * @param fin
222         * @param format
223         */
224        void read(FILE *, pol_format_t);
225
226        /**
227         * @brief Escribir un polinomio hacia un archivo
228         * @param fout
229         * @param format
230         * @param endofline
231         */
232                void write(FILE * fout, pol_format_t format = HTMLPR, bool endofline = 1) const;
233        };
234
235        enum file_mode_t {
236                READ, WRITE
237        };
238
239    /**
240     * @brief Clase para gestionar un archivo en el sistema de archivos
241     */
242        class File {
243        private:
244                FILE *file;
245                bool opened;
246                file_mode_t mode;
247                u64_t buf;
248                size_t pos, count, read_size;
249        public:
250                File();
251                File(FILE *, file_mode_t);
252                File(const char *, file_mode_t);
253                ~File();
254                bool open(const char *, file_mode_t);
255                void close();
256                void flush();
257                size_t read(u61_t &);
258                size_t write(const u61_t &, size_t = MODSIZE - 1);
259        };
260
261    /**
262     * @brief Clase para generar números pseudo-aleatorios
263     */
264        class random_t {
265        private:
266                std::mt19937_64 generator;
267        public:
268                random_t();
269                u61_t operator()();
270        };
271
272    /**
273     * @brief Retorna el máximo común dividor
274     * @return máximo común dividor
275     */
276        pol_t gcd(const pol_t &, const pol_t &);
277
278    pol_t get_x();
279
280    /**
281     * @brief Retorna un polinomio aleatorio de una variable
282     * @param deg
283     * @param f
284     * @return polinomio aleatorio de una variable
285     */
286    pol_t get_rand_pol(u32_t deg, bool f = true);
287
288    /**
289     * Genera la clave pública a partir de dos polinomios que corresponden a la clave privada
290     */
291    template <unsigned CLOSE_KEY_DEG = KEY_DEG, unsigned OPEN_KEY_DEG = KEY_DEG>
292    pol3v_t get_open_key(const pol_t &, const pol_t &);
293
294    /**
295     * Cifra un archivo dado la clave pública (polinomio de tres variables)
296     */
297    template <unsigned Key_Deg = 2 * KEY_DEG>
298    void encrypt(File &, FILE *, const pol3v_t &);
299
300    /**
301     * Descifra un archivo dada la clave privada en dos polinomios de una variable
302     */
303    template <unsigned CLOSE_KEY_DEG = KEY_DEG, unsigned OPEN_KEY_DEG = KEY_DEG>
304    void decrypt(FILE *, File &, const pol_t &, const pol_t &);
305
306
307    /**
308     * @brief Retorna el polinomio divisor f utilizado para el proceso de cifrado
309     * @param max_deg
310     * @param Key_Deg
311     * @return polinomio divisor f utilizado para el proceso de cifrado
312     */
313    pol3v_t XXX_get_f(const u64_t max_deg, const u32_t Key_Deg);
314
315    /**
316     * @brief Retorna el mensaje convertido en polinomio
317     * @param max_deg
318     * @param Key_Deg
319     * @return mensaje convertido en polinomio
320     */
321    pol3v_t XXX_get_mes(const u64_t max_deg, const u32_t Key_Deg, const u64_t size, const std::vector<u61_t> &);
322
323    /**
324     * @brief
325     * @param fact
326     * @param deg
327     * @param res
328     */
329    void XXX_get_fact(const vector_t &fact, u64_t deg, vector_t &res);
330
331
332        #include "polinomial.hpp"
333};
Note: See TracBrowser for help on using the repository browser.