-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutilities.jl
159 lines (140 loc) · 3.45 KB
/
utilities.jl
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
using SEAL
using Printf
function print_example_banner(title)
if isempty(title)
return nothing
end
title_length = length(title)
banner_length = title_length + 2 * 10
banner_border = "+" * "-"^(banner_length - 2) * "+"
banner_center = "|" * " "^9 * title * " "^9 * "|"
println()
println(banner_border)
println(banner_center)
println(banner_border)
end
function print_parameters(context::SEALContext)
context_data = key_context_data(context)
encryption_parms = parms(context_data)
scheme_type = scheme(encryption_parms)
if scheme_type == SchemeType.bfv
scheme_name = "BFV"
elseif scheme_type == SchemeType.ckks
scheme_name = "CKKS"
else
error("unsupported scheme")
end
println("/")
println("| Encryption parameters :")
println("| scheme: ", scheme_name)
println("| poly_modulus_degree: ", poly_modulus_degree(encryption_parms))
print("| coeff_modulus size: ", total_coeff_modulus_bit_count(context_data), " (")
bit_counts = [bit_count(modulus) for modulus in coeff_modulus(encryption_parms)]
print(join(bit_counts, " + "))
println(") bits")
if scheme_type == SchemeType.bfv
println("| plain_modulus: ", value(plain_modulus(encryption_parms)))
end
println("\\")
end
function print_line(line_number)
if line_number < 10
padding = " "
elseif line_number < 100
padding = " "
else
padding = ""
end
print("Line ", padding, line_number, " --> ")
end
function print_vector(vector, print_size=4, prec=3)
slot_count = length(vector)
println()
if slot_count <= 2 * print_size
print(" [ ")
if prec == 7
@printf "%.7f" first(vector)
else
@printf "%.3f" first(vector)
end
for i in 2:slot_count
if prec == 7
@printf ", %.7f" vector[i]
else
@printf ", %.3f" vector[i]
end
end
println(" ]")
else
print(" [ ")
if prec == 7
@printf "%.7f" first(vector)
else
@printf "%.3f" first(vector)
end
for i in 2:print_size
if prec == 7
@printf ", %.7f" vector[i]
else
@printf ", %.3f" vector[i]
end
end
if slot_count > 2 * print_size
print(", ...")
end
for i in (slot_count - print_size + 1):slot_count
if prec == 7
@printf ", %.7f" vector[i]
else
@printf ", %.3f" vector[i]
end
end
println(" ]")
end
println()
end
function print_matrix(matrix, row_size)
print_size = 5
println()
# First row
print(" [")
for i in 1:print_size
@printf("%3d,", matrix[i])
end
print(" ...,")
for i in (row_size - print_size + 1):row_size
if i != row_size
@printf("%3d,", matrix[i])
else
@printf("%3d ]\n", matrix[i])
end
end
# Second row
print(" [")
for i in (row_size + 1):(row_size + print_size)
@printf("%3d,", matrix[i])
end
print(" ...,")
for i in (2 * row_size - print_size + 1):(2 * row_size)
if i != 2 * row_size
@printf("%3d,", matrix[i])
else
@printf("%3d ]\n", matrix[i])
end
end
println()
end
"""
@elapsedus(ex)
Return integer number of elapsed microseconds required for executing `ex`. Modified from
`Base.@elapsed`, which returns the number of seconds as a floating point number.
See also: [`@elapsed`](@ref)
"""
macro elapsedus(ex)
quote
while false; end # compiler heuristic: compile this block (alter this if the heuristic changes)
local t0 = time_ns()
$(esc(ex))
Int(div(time_ns() - t0, 1000))
end
end