-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfull_yao.oc
105 lines (76 loc) · 3.33 KB
/
full_yao.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
98
99
100
101
102
103
104
105
#include "yao.h"
#include "wrapper.h"
#include "stdio.h"
#include <obliv.oh>
#include "obliv_math_def.h"
#define USE_OBLIV_INT
#include "obliv_math_func.h"
#undef USE_OBLIV_INT
void do_full_train(void* args) {
fullProtocolIO* io = args;
int** my_features = io->features;
int* my_labels = io->labels;
configuration_t* config = io->config;
party_t* party = io->party;
struct timeval t1, t2;
gettimeofday(&t1, NULL);
int num_entries = GetDataRowCount(party);
int num_features = GetDataFeatureCount(party);
/* first the parties need to share their data */
obliv long* labels_a = calloc(num_entries, sizeof(obliv long));
obliv long* labels_b = calloc(num_entries, sizeof(obliv long));
obliv long* features_data_a = calloc(num_entries*num_features, sizeof(obliv long));
obliv long* features_data_b = calloc(num_entries*num_features, sizeof(obliv long));
obliv long** features_a = calloc(num_entries, sizeof(obliv long*));
obliv long** features_b = calloc(num_entries, sizeof(obliv long*));
for (int i = 0; i < num_entries; i++) {
features_a[i] = &features_data_a[i*num_features];
features_b[i] = &features_data_b[i*num_features];
}
feedOblivLongArray(labels_a, my_labels, num_entries, 1);
feedOblivLongArray(labels_b, my_labels, num_entries, 2);
feedOblivLongArray(features_data_a, *my_features, num_features * num_entries, 1);
feedOblivLongArray(features_data_b, *my_features, num_features * num_entries, 2);
// we'll need this for a lot of the math
// precompute the taylor coefficients for the sigmoid function
load_sigmoid_taylor_coefficients();
obliv long* model = calloc(num_features+1, sizeof(obliv long));
int iterations = GetIterationCount(config);
int batch_size = GetBatchSize(config);
int data_i = 0;
for (int i = 0; i < iterations; i++) {
printf("Iteration: %d\n", i);
obliv long* gradient = calloc(num_features+1, sizeof(obliv long));
// don't worry about any privacy here, just do vanilla gradient
// descent.
for (int j = 0; j < batch_size; j++) {
add_to_gradient(gradient, model, features_a[data_i], labels_a[data_i], num_features);
add_to_gradient(gradient, model, features_b[data_i], labels_b[data_i], num_features);
data_i = (data_i + 1) % num_entries;
}
int learning_rate = GetLearningRate(config, i) * (1 << PRECISION);
mult_ovec_p(gradient, -learning_rate / batch_size / 2, num_features+1);
add_ovecs(model, gradient, num_features+1);
}
// add noise to the model based on the regularization constant. We
// are using the gaussian mechanism
double noise_sd = GetRegularizedRegressionNoise(config);
int noise_prec = GetQuantizeBitsPrecision(config);
obliv long* noise_vec = generate_noise_vec(num_features+1, noise_sd, noise_prec);
for (int i = 0; i < num_features+1; i++) {
noise_vec[i] = noise_vec[i] << (PRECISION - noise_prec);
}
add_ovecs(model, noise_vec, num_features+1);
long* reveal_model = calloc(num_features+1, sizeof(long));
for (int i = 0; i < num_features+1; i++) {
revealOblivLong(reveal_model + i, model[i], 0);
}
printf("\n");
io->model = reveal_model;
gettimeofday(&t2, NULL);
printf("Gate Count: %d\n", yaoGateCount());
long elapsed;
elapsed = (t2.tv_sec - t1.tv_sec) * 1000;
elapsed += (t2.tv_usec - t1.tv_usec) / 1000;
printf("Elapsed Time: %dms\n", elapsed);
}