-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpoly.cpp
More file actions
executable file
·130 lines (103 loc) · 2.22 KB
/
Copy pathpoly.cpp
File metadata and controls
executable file
·130 lines (103 loc) · 2.22 KB
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
/*
* Copyright 2012, 2013 naehrwert
* Licensed under the terms of the GNU GPL, version 2
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*/
#include <stdlib.h>
#include "poly.h"
poly_t *poly_read(FILE *fp, poly_t *dst)
{
u32 i;
for(i = 0; i <= dst->degree; i++)
bn_read(fp, dst->coeffs[i]);
return dst;
}
poly_t *poly_write(FILE *fp, poly_t *p)
{
u32 i;
for(i = 0; i <= p->degree; i++)
bn_write(fp, p->coeffs[i]);
return p;
}
void poly_print(FILE *fp, const s8 *pre, poly_t *p, const s8 *post)
{
u32 i;
if(p == NULL)
{
fprintf(fp, "%s(NULL)%s", pre, post);
return;
}
fputs(pre, fp);
for(i = 0; i <= p->degree; i++)
{
bn_print(fp, "", bn_from_mon(p->coeffs[i], p->N), "");
bn_to_mon(p->coeffs[i], p->N);
if(i > 0)
{
if(i > 1)
fprintf(fp, "*X^%d", i);
else
fputs("*X", fp);
}
if(i < p->degree)
fputs(" + ", fp);
}
fputs(post, fp);
}
poly_t *poly_alloc(u32 degree, bn_t *N)
{
poly_t *res;
if((res = (poly_t *)malloc(sizeof(poly_t))) == NULL)
return NULL;
//Degree + 1 coeffs.
if((res->coeffs = (bn_t **)malloc(sizeof(bn_t *) * (degree + 1))) == NULL)
{
free(res);
return NULL;
}
res->degree = degree;
res->N = N;
return res;
}
void poly_free(poly_t *p)
{
free(p->coeffs);
free(p);
}
int poly_set_coeff(poly_t *p, u32 i, bn_t *coeff)
{
if(i > p->degree)
return 0;
//Convert coefficients for faster eval.
p->coeffs[i] = bn_to_mon(coeff, p->N);
return 1;
}
int poly_free_coeff(poly_t *p, u32 i)
{
if(i > p->degree)
return 0;
bn_free(p->coeffs[i]);
p->coeffs[i] = NULL;
return 1;
}
bn_t *poly_eval(poly_t *p, bn_t *dst, bn_t *x)
{
u32 i;
bn_t *e = BN_INIT(4), *t = BN_INIT(p->N->n), *tx = bn_to_mon(bn_copy(BN_INIT(x->n), x), p->N);
for(i = 0; i <= p->degree; i++)
{
//TODO: endianess!!!
e->v[3] = i & 0x000000FF;
e->v[2] = (i & 0x0000FF00) >> 8;
e->v[1] = (i & 0x00FF0000) >> 16;
e->v[0] = (i & 0xFF000000) >> 24;
//t = x^e
bn_mon_exp(t, tx, p->N, e);
//dst += t * a_i
bn_add(dst, dst, bn_from_mon(bn_mon_mul(t, t, p->coeffs[i], p->N), p->N), p->N);
}
bn_free(e);
bn_free(t);
bn_free(tx);
return dst;
}