1
+ #include < iostream>
2
+ #include < iomanip>
3
+ #include < vector>
4
+ #include < string>
5
+ #include < chrono>
6
+ #include < random>
7
+ #include < thread>
8
+ #include < mutex>
9
+ #include < random>
10
+ #include < limits>
11
+ #include < fstream>
12
+
13
+
14
+ #include " seal/seal.h"
15
+
16
+ using namespace seal ;
17
+
18
+ auto start = std::chrono::steady_clock::now();
19
+
20
+ const std::vector<int > S_ZAG = { 0 ,1 ,8 ,16 ,9 ,2 ,3 ,10 ,17 ,24 ,32 ,25 ,18 ,11 ,4 ,5 ,12 ,19 ,26 ,33 ,40 ,48 ,41 ,34 ,27 ,20 ,13 ,6 ,7 ,14 ,21 ,28 ,35 ,42 ,49 ,56 ,57 ,50 ,43 ,36 ,29 ,22 ,15 ,23 ,30 ,37 ,44 ,51 ,58 ,59 ,52 ,45 ,38 ,31 ,39 ,46 ,53 ,60 ,61 ,54 ,47 ,55 ,62 ,63 };
21
+ const std::vector<double > S_STD_LUM_QUANT = { 16 ,11 ,12 ,14 ,12 ,10 ,16 ,14 ,13 ,14 ,18 ,17 ,16 ,19 ,24 ,40 ,26 ,24 ,22 ,22 ,24 ,49 ,35 ,37 ,29 ,40 ,58 ,51 ,61 ,60 ,57 ,51 ,56 ,55 ,64 ,72 ,92 ,78 ,64 ,68 ,87 ,69 ,55 ,56 ,80 ,109 ,81 ,87 ,95 ,98 ,103 ,104 ,103 ,62 ,77 ,113 ,121 ,112 ,100 ,120 ,92 ,101 ,103 ,99 };
22
+ const std::vector<double > S_STD_CROMA_QUANT = { 17 ,18 ,18 ,24 ,21 ,24 ,47 ,26 ,26 ,47 ,99 ,66 ,56 ,66 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 ,99 };
23
+
24
+
25
+ void raymond_average ();
26
+ void dct (double *data);
27
+ std::vector<double > read_image (std::string fname);
28
+
29
+ void print_parameters (const SEALContext &context) {
30
+ std::cout << " / Encryption parameters:" << std::endl;
31
+ std::cout << " | poly_modulus: " << context.poly_modulus ().to_string () << std::endl;
32
+
33
+ /*
34
+ Print the size of the true (product) coefficient modulus
35
+ */
36
+ std::cout << " | coeff_modulus size: "
37
+ << context.total_coeff_modulus ().significant_bit_count () << " bits" << std::endl;
38
+
39
+ std::cout << " | plain_modulus: " << context.plain_modulus ().value () << std::endl;
40
+ std::cout << " \\ noise_standard_deviation: " << context.noise_standard_deviation () << std::endl;
41
+ std::cout << std::endl;
42
+ }
43
+
44
+ int main ()
45
+ {
46
+ std::cout << " \n Total memory allocated by global memory pool: "
47
+ << (MemoryPoolHandle::Global ().alloc_byte_count () >> 20 )
48
+ << " MB" << std::endl;
49
+
50
+ raymond_average ();
51
+
52
+ return 0 ;
53
+ }
54
+
55
+ std::vector<double > read_image (std::string fname) {
56
+ int w, h;
57
+ std::vector<double > im;
58
+ std::ifstream myfile;
59
+
60
+ myfile.open (fname.c_str ());
61
+ myfile >> w;
62
+ myfile >> h;
63
+ std::cout << " Read in " << fname << " with dimensions: " << w << " x " << h << std::endl;
64
+
65
+ float tmp;
66
+ for (int i = 0 ; i < w*h; i++) {
67
+ myfile >> tmp;
68
+ im.push_back (tmp);
69
+ }
70
+ return im;
71
+ }
72
+
73
+ const int BLOCK_SIZE = 8 ;
74
+ std::vector<std::vector<double >> split_image_eight_block (std::vector<double > im, int w, int h) {
75
+ std::vector<std::vector<double >> lst;
76
+ for (int i = 0 ; i < w; i += BLOCK_SIZE) {
77
+ for (int j = 0 ; j < h; j += BLOCK_SIZE) {
78
+ std::vector<double > new_lst;
79
+ for (int k = 0 ; k < BLOCK_SIZE; k++)
80
+ for (int l = 0 ; l < BLOCK_SIZE; l++) {
81
+ int index = (j+k)*w + i + l;
82
+ new_lst.push_back (im[index ]);
83
+ }
84
+ lst.push_back (new_lst);
85
+ }
86
+ }
87
+ return lst;
88
+ }
89
+
90
+ void print_image (std::vector<double > &im, int w, int h) {
91
+ std::cout << " Printing Image dim: (" << w << " ," << h << " )" << std::endl;
92
+ for (int i = 0 ; i < im.size (); i++) {
93
+ std::cout << std::setw (5 ) << im[i] << " " ;
94
+ if ((i + 1 ) % w == 0 ) std::cout << std::endl;
95
+ }
96
+ }
97
+
98
+ void print_blocks (std::vector<std::vector<double >> &blocks) {
99
+ for (int a = 0 ; a < blocks.size (); a++) {
100
+ std::cout << " Printing block " << a << std::endl;
101
+ for (int i = 0 ; i < blocks[a].size (); i++) {
102
+ std::cout << std::setw (11 ) << blocks[a][i] << " " ;
103
+ if ((i + 1 ) % BLOCK_SIZE == 0 ) std::cout << std::endl;
104
+ }
105
+ std::cout << " ---------------" << std::endl;
106
+ }
107
+ }
108
+
109
+ void dct_blocks (std::vector<std::vector<double >> &blocks) {
110
+ for (int a = 0 ; a < blocks.size (); a++) {
111
+ dct (&blocks[a][0 ]);
112
+ }
113
+ }
114
+
115
+
116
+ void raymond_average () {
117
+ std::vector<double > im = read_image (" image/kung.txt" );
118
+ print_image (im, 16 , 16 );
119
+ std::vector<std::vector<double >> blocks = split_image_eight_block (im, 16 , 16 );
120
+ dct_blocks (blocks);
121
+ print_blocks (blocks);
122
+ return ;
123
+ }
124
+
125
+
126
+ // Forward DCT
127
+ void dct (double *data)
128
+ {
129
+ double z1, z2, z3, z4, z5, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp10, tmp11, tmp12, tmp13, *data_ptr;
130
+
131
+ data_ptr = data;
132
+
133
+ for (int c=0 ; c < 8 ; c++) {
134
+ tmp0 = data_ptr[0 ] + data_ptr[7 ];
135
+ tmp7 = data_ptr[0 ] - data_ptr[7 ];
136
+ tmp1 = data_ptr[1 ] + data_ptr[6 ];
137
+ tmp6 = data_ptr[1 ] - data_ptr[6 ];
138
+ tmp2 = data_ptr[2 ] + data_ptr[5 ];
139
+ tmp5 = data_ptr[2 ] - data_ptr[5 ];
140
+ tmp3 = data_ptr[3 ] + data_ptr[4 ];
141
+ tmp4 = data_ptr[3 ] - data_ptr[4 ];
142
+ tmp10 = tmp0 + tmp3;
143
+ tmp13 = tmp0 - tmp3;
144
+ tmp11 = tmp1 + tmp2;
145
+ tmp12 = tmp1 - tmp2;
146
+ data_ptr[0 ] = tmp10 + tmp11;
147
+ data_ptr[4 ] = tmp10 - tmp11;
148
+ z1 = (tmp12 + tmp13) * 0.541196100 ;
149
+ data_ptr[2 ] = z1 + tmp13 * 0.765366865 ;
150
+ data_ptr[6 ] = z1 + tmp12 * - 1.847759065 ;
151
+ z1 = tmp4 + tmp7;
152
+ z2 = tmp5 + tmp6;
153
+ z3 = tmp4 + tmp6;
154
+ z4 = tmp5 + tmp7;
155
+ z5 = (z3 + z4) * 1.175875602 ;
156
+ tmp4 *= 0.298631336 ;
157
+ tmp5 *= 2.053119869 ;
158
+ tmp6 *= 3.072711026 ;
159
+ tmp7 *= 1.501321110 ;
160
+ z1 *= -0.899976223 ;
161
+ z2 *= -2.562915447 ;
162
+ z3 *= -1.961570560 ;
163
+ z4 *= -0.390180644 ;
164
+ z3 += z5;
165
+ z4 += z5;
166
+ data_ptr[7 ] = tmp4 + z1 + z3;
167
+ data_ptr[5 ] = tmp5 + z2 + z4;
168
+ data_ptr[3 ] = tmp6 + z2 + z3;
169
+ data_ptr[1 ] = tmp7 + z1 + z4;
170
+ data_ptr += 8 ;
171
+ }
172
+
173
+ data_ptr = data;
174
+
175
+ for (int c=0 ; c < 8 ; c++) {
176
+ tmp0 = data_ptr[8 *0 ] + data_ptr[8 *7 ];
177
+ tmp7 = data_ptr[8 *0 ] - data_ptr[8 *7 ];
178
+ tmp1 = data_ptr[8 *1 ] + data_ptr[8 *6 ];
179
+ tmp6 = data_ptr[8 *1 ] - data_ptr[8 *6 ];
180
+ tmp2 = data_ptr[8 *2 ] + data_ptr[8 *5 ];
181
+ tmp5 = data_ptr[8 *2 ] - data_ptr[8 *5 ];
182
+ tmp3 = data_ptr[8 *3 ] + data_ptr[8 *4 ];
183
+ tmp4 = data_ptr[8 *3 ] - data_ptr[8 *4 ];
184
+ tmp10 = tmp0 + tmp3;
185
+ tmp13 = tmp0 - tmp3;
186
+ tmp11 = tmp1 + tmp2;
187
+ tmp12 = tmp1 - tmp2;
188
+ data_ptr[8 *0 ] = (tmp10 + tmp11) / 8.0 ;
189
+ data_ptr[8 *4 ] = (tmp10 - tmp11) / 8.0 ;
190
+ z1 = (tmp12 + tmp13) * 0.541196100 ;
191
+ data_ptr[8 *2 ] = (z1 + tmp13 * 0.765366865 ) / 8.0 ;
192
+ data_ptr[8 *6 ] = (z1 + tmp12 * -1.847759065 ) / 8.0 ;
193
+ z1 = tmp4 + tmp7;
194
+ z2 = tmp5 + tmp6;
195
+ z3 = tmp4 + tmp6;
196
+ z4 = tmp5 + tmp7;
197
+ z5 = (z3 + z4) * 1.175875602 ;
198
+ tmp4 *= 0.298631336 ;
199
+ tmp5 *= 2.053119869 ;
200
+ tmp6 *= 3.072711026 ;
201
+ tmp7 *= 1.501321110 ;
202
+ z1 *= -0.899976223 ;
203
+ z2 *= -2.562915447 ;
204
+ z3 *= -1.961570560 ;
205
+ z4 *= -0.390180644 ;
206
+ z3 += z5;
207
+ z4 += z5;
208
+ data_ptr[8 *7 ] = (tmp4 + z1 + z3) / 8.0 ;
209
+ data_ptr[8 *5 ] = (tmp5 + z2 + z4) / 8.0 ;
210
+ data_ptr[8 *3 ] = (tmp6 + z2 + z3) / 8.0 ;
211
+ data_ptr[8 *1 ] = (tmp7 + z1 + z4) / 8.0 ;
212
+ data_ptr++;
213
+ }
214
+ }
0 commit comments