/usr/include/m4rie/mzd_poly.h is in libm4rie-dev 20140914-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | /**
* \file mzd_poly.h
*
* \brief Matrices over \GF2[x]
*
* @warning This code is experimental.
*/
#ifndef M4RIE_MZD_POLY_H
#define M4RIE_MZD_POLY_H
/******************************************************************************
*
* M4RIE: Linear Algebra over GF(2^e)
*
* Copyright (C) 2011 Martin Albrecht <martinralbrecht@googlemail.com>
*
* Distributed under the terms of the GNU General Public License (GEL)
* version 2 or higher.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* The full text of the GPL is available at:
*
* http://www.gnu.org/licenses/
******************************************************************************/
#include <m4ri/m4ri.h>
#include "mzd_ptr.h"
#include "gf2x.h"
#include "blm.h"
/**
* \brief will be the data type for matrices over \GF2[x] in the future
*/
typedef struct {
mzd_t **x; /**< Coefficients. */
rci_t nrows; /**< Number of rows. */
rci_t ncols; /**< Number of columns. */
deg_t depth; /**< Degree +1 */
} mzd_poly_t;
/**
* \brief C += (A+B)*x^offset
*
* \param C Target polynomial.
* \param A Source polynomial.
* \param B Source polynomial.
* \param offset The result is shifted offset entries upwards.
*
* \ingroup Addition
*
* \warning No bounds checks are performed.
*/
static inline mzd_poly_t *_mzd_poly_add(mzd_poly_t *C, const mzd_poly_t *A, const mzd_poly_t *B, unsigned int offset) {
_mzd_ptr_add(C->x+offset, (const mzd_t**)A->x, (const mzd_t**)B->x, A->depth);
return C;
}
/**
* \brief C += (A+B)
*
* \param C Target polynomial.
* \param A Source polynomial.
* \param B Source polynomial.
*
* \ingroup Addition
*/
static inline mzd_poly_t *mzd_poly_add(mzd_poly_t *C, const mzd_poly_t *A, const mzd_poly_t *B) {
assert(C->depth >= A->depth && A->depth == B->depth);
return _mzd_poly_add(C, A, B, 0);
}
/**
* \brief Create a new polynomial of degree d with m x n matrices as coefficients.
*
* \param d Degree.
* \param m Number of rows.
* \param n Number of columns.
*
* \ingroup Constructions
*/
static inline mzd_poly_t *mzd_poly_init(const deg_t d, const rci_t m, const rci_t n) {
mzd_poly_t *A = (mzd_poly_t*)m4ri_mm_malloc(sizeof(mzd_poly_t));
A->x = (mzd_t**)m4ri_mm_malloc(sizeof(mzd_t*)*(d+1));
A->nrows = m;
A->ncols = n;
A->depth = d+1;
for(int i=0; i<A->depth; i++)
A->x[i] = mzd_init(m,n);
return A;
}
/**
* \brief Free polynomial A
*
* \param A Polynomial.
*
* \ingroup Constructions
*/
static inline void mzd_poly_free(mzd_poly_t *A) {
for(int i=0; i<A->depth; i++)
mzd_free(A->x[i]);
m4ri_mm_free(A->x);
m4ri_mm_free(A);
}
/**
* \brief change depth of A to new_depth.
*
* \param A Polynomial.
* \param new_depth New depth (may be <,=,> than current depth).
*
* \ingroup Constructions
*/
static inline mzd_poly_t *_mzd_poly_adapt_depth(mzd_poly_t *A, const deg_t new_depth) {
if (new_depth < A->depth) {
for(int i=new_depth; i<A->depth; i++) {
mzd_free(A->x[i]);
A->x[i] = NULL;
}
} else {
for(int i=A->depth; i<new_depth; i++) {
A->x[i] = mzd_init(A->nrows,A->ncols);
}
}
A->depth = new_depth;
return A;
}
/**
* \brief C += A*B using naive polynomial multiplication
*
* \param C Target polynomial.
* \param A Source polynomial.
* \param B Source polynomial.
*
* \ingroup Multiplication
*/
static inline mzd_poly_t *_mzd_poly_addmul_naive(mzd_poly_t *C, const mzd_poly_t *A, const mzd_poly_t *B) {
if (C == NULL)
C = mzd_poly_init(A->depth+B->depth-1, A->nrows, B->ncols);
for(unsigned int i=0; i<A->depth; i++) {
for(unsigned int j=0; j<B->depth; j++) {
mzd_addmul(C->x[i+j], A->x[i], B->x[j], 0);
}
}
return C;
}
/**
* \brief C += A*B using Karatsuba multiplication on balanced inputs
*
* \param C Target polynomial.
* \param A Source polynomial.
* \param B Source polynomial.
*
* \ingroup Multiplication
*/
static inline mzd_poly_t *_mzd_poly_addmul_karatsubs_balanced(mzd_poly_t *C, const mzd_poly_t *A, const mzd_poly_t *B) {
assert(A->depth == B->depth);
if (C == NULL)
C = mzd_poly_init(A->depth+B->depth-1, A->nrows, B->ncols);
switch(A->depth) {
case 0:
m4ri_die("depth 0: seriously?");
case 1: mzd_addmul(C->x[0], A->x[0], B->x[0], 0); break;
case 2: _mzd_ptr_addmul_karatsuba2(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 3: _mzd_ptr_addmul_karatsuba3(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 4: _mzd_ptr_addmul_karatsuba4(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 5: _mzd_ptr_addmul_karatsuba5(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 6: _mzd_ptr_addmul_karatsuba6(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 7: _mzd_ptr_addmul_karatsuba7(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 8: _mzd_ptr_addmul_karatsuba8(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 9: _mzd_ptr_addmul_karatsuba9(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 10: _mzd_ptr_addmul_karatsuba10(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 11: _mzd_ptr_addmul_karatsuba11(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 12: _mzd_ptr_addmul_karatsuba12(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 13: _mzd_ptr_addmul_karatsuba13(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 14: _mzd_ptr_addmul_karatsuba14(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 15: _mzd_ptr_addmul_karatsuba15(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
case 16: _mzd_ptr_addmul_karatsuba16(NULL, C->x, (const mzd_t**)A->x, (const mzd_t**)B->x); break;
default:
m4ri_die("Not implemented\n");
}
return C;
}
/**
* \brief C += A*B by applying the bilinear maps f, i.e. f->H*((f->F*A) x (f->G*B)).
*/
static inline mzd_poly_t *_mzd_poly_addmul_blm(mzd_poly_t *C, mzd_poly_t *A, mzd_poly_t *B, const blm_t *f) {
assert(f!=NULL);
assert(f->F->ncols == A->depth && f->G->ncols == B->depth);
if (C == NULL)
C = mzd_poly_init(A->depth+B->depth-1, A->nrows, B->ncols);
_mzd_ptr_apply_blm(C->x, (const mzd_t**)A->x, (const mzd_t**)B->x, f);
return C;
}
/**
* \brief C += A*B using the Chinese Remainder Theorem.
*/
static inline mzd_poly_t *_mzd_poly_addmul_crt(mzd_poly_t *C, mzd_poly_t *A, mzd_poly_t *B) {
int *p = crt_init(A->depth, B->depth);
blm_t *f = blm_init_crt(NULL, A->depth, B->depth, p, 1);
_mzd_poly_addmul_blm(C, A, B, f);
blm_free(f);
m4ri_mm_free(p);
return C;
}
/**
* \brief C += A*B using arithmetic in GF(2^log2(d)) if C has degree d.
*/
mzd_poly_t *_mzd_poly_addmul_ext1(mzd_poly_t *C, mzd_poly_t *A, mzd_poly_t *B);
/**
* \brief Return -1,0,1 if if A < B, A == B or A > B respectively.
*
* \param A Matrix.
* \param B Matrix.
*
* \note This comparison is not well defined (except for !=0) mathematically and relatively
* arbitrary.
*
* \ingroup Comparison
*/
static inline int mzd_poly_cmp(mzd_poly_t *A, mzd_poly_t *B) {
int r = 0;
if ((A->depth != B->depth) ) {
if (A->depth < B->depth)
return -1;
else
return 1;
}
for(int i=0; i<A->depth; i++)
r |= mzd_cmp(A->x[i],B->x[i]);
return r;
}
/**
* \brief Fill matrix A with random elements.
*
* \param A Matrix
*
* \todo Allow the user to provide a RNG callback.
*
* \ingroup Assignment
*/
static inline void mzd_poly_randomize(mzd_poly_t *A) {
for(int i=0; i<A->depth; i++)
mzd_randomize(A->x[i]);
}
#endif //M4RIE_MZD_POLY_H
|