-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmachine_memory.c
More file actions
401 lines (354 loc) · 8.67 KB
/
machine_memory.c
File metadata and controls
401 lines (354 loc) · 8.67 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* memory.c
*
* Created on: 20.01.2011
* Author: micha
*/
/*
* Memory.c
*
* Created on: 30.07.2010
* Author: root
*/
/*
* This class implements encrypted memory access.
*
* input
* -----
* cellarray is an array of encrypted memory bit values
* a is the encrypted memory address
*
* output
* ------
* b is the encrypted word at the given address
*
* Access is realized as a series of 1-out-of-n-bit boolean data selectors
*
* Example for 3 address lines and 7 single-column memory rows:
*
* ------and-circuit---->
*
* o= !a0 & !a1 & !a2 & m0 | \ |
* a0 & !a1 & !a2 & m1 | \ |
* !a0 & a1 & !a2 & m2 | \ |
* a0 & a1 & !a2 & m3 | \ or-circuit
* !a0 & !a1 & a2 & m4 | \ |
* a0 & !a1 & a2 & m5 | \ |
* !a0 & a1 & a2 & m6 | \ |
* a0 & a1 & a2 & m7 \/
*
* Writing to a cell applies the following assumption: after read or write
* access, the register and the cell have the same values, so the function
* for a cell-bit and a register-bit in dependency to a READ/WRITE signal
* is:
*
* reg=cell= (cell & WRITE) | (reg & READ)
*
* this can be derived from the following truth table
*
* R = register bit
* M = memory (cell bit)
* W = write signal (inverse common circuit logic: 0=read 1=write)
*
* R M W R' M'
* ------------
* 0 0 0 0 0
* 0 0 1 0 0
* 0 1 0 1 1
* 0 1 1 0 0
* 1 0 0 0 0
* 1 0 1 1 1
* 1 1 0 1 1
* 1 1 1 1 1
*
* this can be simplified to R'=M'=(C and !W) or (R and W)
*
* assuming that the cell has been selected (and-circuit)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmp.h>
#include "machine_memory.h"
#include "machine_function.h"
#include "types.h"
fhe_int_t r[Memory_ARRAY_ROWS];
fhe_int_t nr[Memory_ARRAY_ROWS];
fhe_int_t rowsel;
fhe_int_t nam,nam2,nrw,read,write,nsel,ZERO;
static int inited=0;
void Memory_access(fhe_int_t *a,fhe_int_t *reg,fhe_int_t rw,fhe_int_t *b1,fhe_pk_t pk,fhe_sk_t sk)
{
int row;
int mask;
int m;
int i;
int i2;
int j;
if(!inited)
{
inited=1;
for(i=0;i<Memory_ARRAY_ROWS;i++)
fhe_int_init(r[i]);
fhe_int_init(nam);
fhe_int_init(nam2);
fhe_int_init(nrw);
fhe_int_init(read);
fhe_int_init(write);
fhe_int_init(nsel);
}
puts("memory access 1");fflush(stdout);
//this loop generates a positive row signal for the *one* selected row
if(fhe_decrypt(rw->v,sk)==1)
puts(" read request");
else
puts(" write request");
for(row=0;row<Memory_ARRAY_ROWS;row++)
{
mask=4;
Function_not(nam,a[0],pk);
Function_not(nam2,a[1],pk);
Function_and2(r[row],(row&1)>0?a[0]:nam,(row&2)>0?a[1]:nam2,pk);
for(m=2;m<Memory_ARRAY_COLS;m++,mask<<=1)
{
Function_not(nam,a[m],pk);
Function_and2(r[row],r[row],(row&mask)>0?a[m]:nam,pk);
}
if(fhe_decrypt(r[row]->v,sk)==1)
printf(" row selected: %d\n",row);
}
puts("memory access 2");fflush(stdout);
//now we generate a
for(row=0;row<Memory_ARRAY_ROWS;row++)
{
for(i2=0;i2<Memory_ARRAY_COLS;i2++)
{
Function_not(nam,r[row],pk);
Function_not(nrw,rw,pk);
Function_and2(write,nrw,reg[i2],pk); //write bit
Function_and2(read,rw,Memory_cellarray[row][i2],pk); //read bit
Function_and2(nsel,nam,Memory_cellarray[row][i2],pk); //row not selected
Function_or3(Memory_cellarray[row][i2],read,write,nsel,pk);
}
}
puts("memory access 3");fflush(stdout);
//combine row signals and cell bits (or-circuit)
//load b1
for(i=0;i<Memory_WORD_SIZE;i++)
{
Function_and2(nam ,r[0],Memory_cellarray[0][i],pk);
Function_and2(nam2,r[1],Memory_cellarray[1][i],pk);
Function_or2 (b1[i],nam,nam2,pk);
//b1[i]=Function_or2(Function_and2(r[0],Memory_cellarray[0][i]),Function_and2(r[1],Memory_cellarray[1][i]));
for(j=2;j<Memory_ARRAY_ROWS;j++)
{
Function_and2(nam,r[j],Memory_cellarray[j][i],pk);
Function_or2(b1[i],b1[i],nam,pk);
//b1[i]=Function_or2(b1[i],Function_and2(r[j],Memory_cellarray[j][i]));
}
}
return;
}
void Memory_access2(fhe_int_t *a,fhe_int_t *reg,fhe_int_t rw,fhe_int_t *b1,fhe_pk_t pk,fhe_sk_t sk)
{
int row;
int mask;
int m;
int i;
int i2;
int j;
fhe_int_t b2[Memory_WORD_SIZE];
if(!inited)
{
inited=1;
fhe_int_init(rowsel);
fhe_int_init(nam);
fhe_int_init(nam2);
fhe_int_init(nrw);
fhe_int_init(read);
fhe_int_init(write);
fhe_int_init(nsel);
fhe_int_init(ZERO);
fhe_encrypt(ZERO->v,pk,0);
for(i=0;i<Memory_ARRAY_ROWS;i++)
fhe_int_init(r[i]);
}
// for(i=0;i<Memory_WORD_SIZE;i++)
// fhe_int_init(b2[i]);
// puts("memory access 1");fflush(stdout);
//this loop generates a positive row signal for the *one* selected row
// if(fhe_decrypt(rw->v,sk)==1)
// puts(" read request");
// else
// puts(" write request");
for(i=0;i<13;i++)
{
fhe_int_init(b2[i]);
fhe_int_set(b2[i],ZERO);
}
Function_not(nrw,rw,pk);
for(row=0;row<4/*Memory_ARRAY_ROWS*/;row++)
{
/* SCHRITT 1:
*
* fuer jede row ein select-signal generieren
*
* dieser abschnitt implementiert die AND-logik eines
* selektor-schaltkreises; das vielstellige AND wird
* durch viele zweistellige ANDs umgesetzt:
* AND(a,b,c,d) = AND(AND(AND(a,b),c),d)
*
* es wird durch die binaeren darstellungen der row-nummer
* geritten und mit der angelegten adresse a[0..COLS] "verglichen":
*
* row[1]=row[1000]= a[0] & !a[1] & !a[2] & !a[3]
* row[5]=row[1010]= a[0] & !a[1] & a[2] & !a[3]
*
* am ende enthaelt nur eine row die 1 (sofern mit einer gueltigen
* adresse zugegriffen wird)
*
*/
mask=4;
//das "innere" AND von hand...
Function_not(nam,a[0],pk);
Function_not(nam2,a[1],pk);
Function_and2(r[row],(row&1)>0?a[0]:nam,(row&2)>0?a[1]:nam2,pk);
// ...die weiteren ANDs werden "aufaddiert"
for(m=2;m<Memory_ARRAY_COLS;m++,mask<<=1)
{
Function_not(nam,a[m],pk);
Function_and2(r[row],r[row],(row&mask)>0?a[m]:nam,pk);
}
Function_not(nam,r[row],pk);
/*
* SCHRITT 2:
*
* speicherzelle mit select-signal verknuepfen; dieser abschnitt
* implementiert den zweiten teil der AND-logik
*
*/
for(m=0;m<Memory_WORD_SIZE;m++)
{
Function_and2(read,Memory_cellarray[row][m],r[row],pk);
Function_and2(write,b1[m],r[row],pk);
/*
* SCHRITT 3:
*
* refresh der speicherzellen des data-compartments
* (alter wert wenn row-select=0, sonst neuer wert aus register)
*/
if(m<8)
{
//set memory cell
Function_and3(read,Memory_cellarray[row][m],r[row],rw,pk);
Function_and3(write,b1[m],r[row],nrw,pk);
Function_and2(nsel,Memory_cellarray[row][m],nam,pk);
Function_or3(Memory_cellarray[row][m],read,write,nsel,pk);
}
/*
* SCHRITT 4:
*
* register schreiben; dies ist die OR-logik des selektor-
* schaltkreises
*/
Function_or3(b2[m],b2[m],read,write,pk);
}
}
for(m=0;m<13;m++)
{
fhe_int_set(b1[m],b2[m]);
fhe_int_clear(b2[m]);
}
return;
}
void ALU_add(fhe_int_t *a,fhe_int_t *b,fhe_int_t carry,fhe_int_t *res,fhe_pk_t pk)
{
fhe_int_t t[2];
fhe_int_t c;
int i;
fhe_int_init(t[0]);
fhe_int_init(t[1]);
fhe_int_init(c);
mpz_set(c->v,carry->v);
c->n=carry->n;
for(i=0;i<8;i++)
{
Function_fa(a[i], b[i], c,t,pk);
fhe_int_set(res[i],t[0]);
fhe_int_set(c,t[1]);
}
fhe_int_set(res[8],c);
fhe_int_clear(t[0]);
fhe_int_clear(t[1]);
fhe_int_clear(c);
}
void ALU_addadr(fhe_int_t *a,fhe_int_t *b,fhe_int_t *res,fhe_pk_t pk)
{
fhe_int_t t[2];
fhe_int_t c;
int i;
fhe_int_init(t[0]);
fhe_int_init(t[1]);
fhe_int_init(c);
fhe_encrypt(c->v,pk,0);
for(i=0;i<Memory_ARRAY_COLS;i++)
{
Function_fa(a[i], b[i], c,t,pk);
fhe_int_set(res[i],t[0]);
fhe_int_set(c,t[1]);
}
fhe_int_clear(t[0]);
fhe_int_clear(t[1]);
fhe_int_clear(c);
}
void loadMemory(fhe_int_t *ac,fhe_int_t *pc,char *filename)
{
int i,j;
FILE *f;
char s[16384];
for(i=0;i<Memory_ARRAY_ROWS;i++)
for(j=0;j<Memory_WORD_SIZE;j++)
fhe_int_init(Memory_cellarray[i][j]);
f=fopen(filename,"r");
i=0;
while(!feof(f))
{
fgets(s,16384,f);
if(feof(f))
break;
if(i<8)
mpz_set_str(ac[i]->v,s,10);
else if(i<16)
mpz_set_str(pc[i-8]->v,s,10);
else
{
// if((i-16)%13==0)
// puts("read word");
// printf("read bit at %d,%d (%d)\n",(i-16)/Memory_WORD_SIZE,(i-16)%Memory_WORD_SIZE,i-16);
mpz_set_str(Memory_cellarray[(i-16)/Memory_WORD_SIZE][(i-16)%Memory_WORD_SIZE]->v,s,10);
}
i++;
}
fclose(f);
}
void memdump(int rows,fhe_sk_t sk)
{
int i,j;
int shift,val;
puts("___DUMP_START___");
for(i=0;i<rows;i++)
{
shift=1;val=0;
printf("%d\t",i);
for(j=0;j<Memory_WORD_SIZE;j++)
{
if(j<8)
if(fhe_decrypt(Memory_cellarray[i][j]->v,sk)==1)
val+=shift;
shift*=2;
printf("%d ",fhe_decrypt(Memory_cellarray[i][j]->v,sk));
}
printf(" %d\n",val);
}
puts("___DUMP_END_____");
}