-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathArithmetic.m
118 lines (105 loc) · 3.37 KB
/
Arithmetic.m
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
// Divide the given ciphertext by p
function div_pFunc(c)
return ExactDivisionBy(c, p);
end function;
// Add the given ciphertexts and/or constants together
function addFunc(x, y)
if IsCiphertext(x) and IsCiphertext(y) then
return Add(x, y);
elif IsCiphertext(x) then
return AddConstant(x, y);
elif IsCiphertext(y) then
return AddConstant(y, x);
end if;
end function;
// Subtract the given ciphertexts and/or constants
function subFunc(x, y)
if IsCiphertext(x) and IsCiphertext(y) then
return Sub(x, y);
elif IsCiphertext(x) then
return SubCiphertextConstant(x, y);
elif IsCiphertext(y) then
return SubConstantCiphertext(y, x);
end if;
end function;
// Multiply the given ciphertexts and/or integer constants together
// This function cannot be used for lazy relinearization
function mulFunc(x, y, rk)
if IsCiphertext(x) and IsCiphertext(y) then
return Mul(x, y, rk);
elif IsCiphertext(x) then
return MulConstant(x, y);
elif IsCiphertext(y) then
return MulConstant(y, x);
end if;
end function;
// Multiply the given ciphertexts and/or integer constants together
// This function can be used for lazy relinearization
function mulLazyFunc(x, y, rk)
if IsCiphertext(x) and IsCiphertext(y) then
// First relinearize both ciphertexts if necessary
if GetNbParts(x) eq 3 then
AutomaticModSwitchRelin(~x);
x := Relin(x, rk);
end if;
if GetNbParts(y) eq 3 then
AutomaticModSwitchRelin(~y);
y := Relin(y, rk);
end if;
// Modulus switching is necessary to keep noise low
AutomaticModSwitchMul(~x);
AutomaticModSwitchMul(~y);
return MulNR(x, y);
elif IsCiphertext(x) then
return MulConstant(x, y);
elif IsCiphertext(y) then
return MulConstant(y, x);
end if;
end function;
// Relinearize the given ciphertext if necessary
function relinFunc(x, rk)
if GetNbParts(x) eq 3 then
AutomaticModSwitchRelin(~x);
x := Relin(x, rk);
DynamicModSwitch(~x);
end if;
return x;
end function;
/*** Overwrite basic arithmetic functions to count number of operations and total depth ***/
collision_param := 2^256;
function addCountFunc(x, y)
if IsCiphertext(x) and IsCiphertext(y) then
return <x[1] join y[1], x[2] and y[2]>;
elif IsCiphertext(x) then
return x;
elif IsCiphertext(y) then
return y;
end if;
end function;
function mulCountFunc(x, y)
if IsCiphertext(x) and IsCiphertext(y) then
return <x[1] join y[1] join {Random(collision_param)}, true>;
elif IsCiphertext(x) then
return x;
elif IsCiphertext(y) then
return y;
end if;
end function;
function mulLazyCountFunc(x, y)
if IsCiphertext(x) and IsCiphertext(y) then
if x[2] and y[2] then
return <x[1] join y[1], false>;
elif x[2] or y[2] then
return <x[1] join y[1] join {Random(collision_param)}, false>;
else
return <x[1] join y[1] join {Random(collision_param)} join {Random(collision_param)}, false>;
end if;
elif IsCiphertext(x) then
return x;
elif IsCiphertext(y) then
return y;
end if;
end function;
function relinCountFunc(x)
return x[2] select x else <x[1] join {Random(collision_param)}, true>;
end function;