-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkmp.oc
97 lines (79 loc) · 2.17 KB
/
kmp.oc
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
#include <obliv.oh>
#include <stdio.h>
#include <oram.oh>
#include <linearoram.oh>
#include <sqrtoram.oh>
#include <circuit_oram.oh>
#include "kmp.h"
obliv bool search(obliv char *txt, obliv int txtLen, obliv int *dfa, obliv int patLen){
/*
* Knuth Morris Pratt algorithm for searching string using dfa
*
* Return:
* bool - found pattern in text
*
*/
obliv int index;
obliv int found = 0;
obliv char state = 0;
int dfa_len;
dfa_len = CHAR_SET_LENGTH * MAX_PATTERN_LENGTH;
const int block = 1;
OcCopy cpy = ocCopyIntN(block);
OcOram *ram = (OcOram*)ocSqrtOramNew(&cpy,dfa,dfa_len);
for (int i = 0; i < MAX_TEXT_LENGTH; ++i)
{
// translate double array indices to single array index
index = (txt[i] * MAX_PATTERN_LENGTH) + state;
ocOramRead(&state, ram, index);
obliv if(state == patLen) found = 1;
}
return found;
}
void kmp(void*args) {
/*
* Reveals to party 2 (pattern) whether the string is
* located in the text
*/
protocolIO *io;
obliv char txt[MAX_TEXT_LENGTH];
obliv int *dfa;
obliv int txtLenObliv, patLenObliv;
obliv bool match;
int txtLen, patLen;
int index;
bool ret;
// allocate dfa
dfa = (obliv int*) calloc(CHAR_SET_LENGTH * MAX_PATTERN_LENGTH,
sizeof(obliv int));
io = args;
// read in txt as obliv
feedOblivCharArray(txt,io->txt,MAX_TEXT_LENGTH,1);
// read in dfa as obliv 1d array
for (int r = 0; r < CHAR_SET_LENGTH; ++r)
{
for (int c = 0; c < MAX_PATTERN_LENGTH; ++c)
{
// translate double array indices to single array index
index = r * MAX_PATTERN_LENGTH + c;
dfa[index] = feedOblivInt(io->dfa[r][c], 2);
}
}
if (ocCurrentParty()==1)
txtLen = strlen(io->txt);
else
patLen = strlen(io->pat);
// read in text and pattern lengths as obliv
txtLenObliv = feedOblivInt(txtLen, 1);
patLenObliv = feedOblivInt(patLen, 2);
// search for a pattern in text
match = search(txt, txtLenObliv, dfa, patLenObliv);
free(dfa);
// reveal index of start of first instance of pattern or -1
revealOblivBool(&ret, match, 2);
if(ocCurrentParty() == 2)
fprintf(stdout,"%d,", ret);
fprintf(stdout, "%d,", MAX_PATTERN_LENGTH);
fprintf(stdout, "%d,", MAX_TEXT_LENGTH);
fprintf(stdout,"%u,",yaoGateCount());
}