-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyvec.c
More file actions
147 lines (134 loc) · 4.41 KB
/
polyvec.c
File metadata and controls
147 lines (134 loc) · 4.41 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdint.h>
#include "params.h"
#include "poly.h"
#include "polyvec.h"
/*************************************************
* Name: polyvec_tobytes
*
* Description: Serialize vector of polynomials
*
* Arguments: - uint8_t *r: pointer to output byte array
* - const poly *a: pointer to input vector of polynomials
* - int vlen: vector dimension
**************************************************/
void polyvec_tobytes(uint8_t *r, const poly *a, int vlen)
{
int i;
for(i=0;i<vlen;i++)
poly_tobytes(r+i*PQMX_POLYBYTES, &a[i]);
}
/*************************************************
* Name: polyvec_frombytes
*
* Description: De-serialize vector of polynomials;
* inverse of polyvec_tobytes
*
* Arguments: - uint8_t *r: pointer to output byte array
* - const poly *a: pointer to input vector of polynomials
* - int vlen: vector dimension
**************************************************/
void polyvec_frombytes(poly *r, const uint8_t *a, int vlen)
{
int i;
for(i=0;i<vlen;i++)
poly_frombytes(&r[i], a+i*PQMX_POLYBYTES);
}
/*************************************************
* Name: polyvec_ntt
*
* Description: Apply forward NTT to all elements of a vector of polynomials
*
* Arguments: - poly *r: pointer to in/output vector of polynomials
* - int vlen: vector dimension
**************************************************/
void polyvec_ntt(poly *r, int vlen)
{
int i;
for(i=0;i<vlen;i++)
poly_ntt(&r[i]);
}
/*************************************************
* Name: polyvec_invntt_tomont
*
* Description: Apply inverse NTT to all elements of a vector of polynomials
* and multiply by Montgomery factor 2^16
*
* Arguments: - poly *r: pointer to in/output vector of polynomials
* - int vlen: vector dimension
**************************************************/
void polyvec_invntt_tomont(poly *r, int vlen)
{
int i;
for(i=0;i<vlen;i++)
poly_invntt_tomont(&r[i]);
}
/*************************************************
* Name: polyvec_basemul_acc_montgomery
*
* Description: Multiply elements of a and b in NTT domain, accumulate into r,
* and multiply by 2^-16.
*
* Arguments: - poly *r: pointer to output polynomial
* - const poly *a: pointer to first input vector of polynomials
* - const poly *b: pointer to second input vector of polynomials
* - int vlen: vector dimension
**************************************************/
void polyvec_basemul_acc_montgomery(poly *r, const poly *a, const poly *b, int vlen)
{
int i;
poly t;
poly_basemul_montgomery(r, &a[0], &b[0]);
for(i=1;i<vlen;i++) {
poly_basemul_montgomery(&t, &a[i], &b[i]);
poly_add(r, r, &t);
poly_reduce(r);
}
}
/*************************************************
* Name: polyvec_reduce
*
* Description: Applies Barrett reduction to each coefficient
* of each element of a vector of polynomials;
* for details of the Barrett reduction see comments in reduce.c
*
* Arguments: - poly *r: pointer to input/output polynomial
* - int vlen: vector dimension
**************************************************/
void polyvec_reduce(poly *r, int vlen)
{
int i;
for(i=0;i<vlen;i++)
poly_reduce(&r[i]);
}
/*************************************************
* Name: polyvec_reduce_mont
*
* Description: Applies Montgomery reduction to each coefficient
* of each element of a vector of polynomials;
* for details of the Montgomery reduction see comments in reduce.c
*
* Arguments: - poly *r: pointer to input/output polynomial
* - int vlen: vector dimension
**************************************************/
void polyvec_reduce_mont(poly *r, int vlen)
{
int i;
for(i=0;i<vlen;i++)
poly_reduce_mont(&r[i]);
}
/*************************************************
* Name: polyvec_add
*
* Description: Add vectors of polynomials
*
* Arguments: - polyvec *r: pointer to output vector of polynomials
* - const poly *a: pointer to first input vector of polynomials
* - const poly *b: pointer to second input vector of polynomials
* - int vlen: vector dimension
**************************************************/
void polyvec_add(poly *r, const poly *a, const poly *b, int vlen)
{
int i;
for(i=0;i<vlen;i++)
poly_add(&r[i], &a[i], &b[i]);
}