• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hpsc-lab / SecureArithmetic.jl / 9684152832

26 Jun 2024 05:31PM UTC coverage: 100.0%. Remained the same
9684152832

push

github

web-flow
New version 0.1.5-dev (#40)

* set version 0.1.4

* Set 0.1.5-dev

594 of 594 relevant lines covered (100.0%)

226.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/unencrypted.jl
1
"""
2
    Unencrypted
3

4
An alternative backend to use instead of [`OpenFHEBackend`](@ref) to experiment with
5
algorithms on unencrypted data.
6

7
See also: [`SecureContext`](@ref), [`OpenFHEBackend`](@ref)
8
"""
9
struct Unencrypted <: AbstractCryptoBackend
12✔
10
    # No data fields required
11
end
20✔
12

13
"""
14
    generate_keys(context::SecureContext{<:Unencrypted})
15

16
Return public and private keys for use with unencrypted data.
17

18
See also: [`PublicKey`](@ref), [`PrivateKey`](@ref), [`SecureContext`](@ref),
19
[`Unencrypted`](@ref)
20
"""
21
function generate_keys(context::SecureContext{<:Unencrypted})
40✔
22
    PublicKey(context, nothing), PrivateKey(context, nothing)
40✔
23
end
24

25
"""
26
    init_multiplication!(context::SecureContext{<:Unencrypted}, private_key::PrivateKey)
27

28
An empty duplicate of [`init_multiplication!`](@ref) for unencrypted data.
29

30
See also: [`SecureContext`](@ref), [`Unencrypted`](@ref), [`PrivateKey`](@ref),
31
[`init_multiplication!`](@ref)
32
"""
33
init_multiplication!(context::SecureContext{<:Unencrypted}, private_key::PrivateKey) = nothing
32✔
34

35
"""
36
    init_rotation!(context::SecureContext{<:Unencrypted}, private_key::PrivateKey, shifts)
37

38
An empty duplicate of [`init_rotation!`](@ref) for unencrypted data.
39

40
See also: [`SecureContext`](@ref), [`Unencrypted`](@ref), [`PrivateKey`](@ref),
41
[`init_rotation!`](@ref)
42
"""
43
init_rotation!(context::SecureContext{<:Unencrypted}, private_key::PrivateKey, shifts) = nothing
16✔
44

45
"""
46
    init_bootstrapping!(context::SecureContext{<:Unencrypted}, private_key::PrivateKey)
47
              
48

49
An empty duplicate of [`init_bootstrapping!`](@ref) for unencrypted data.
50

51
See also: [`SecureContext`](@ref), [`Unencrypted`](@ref), [`PrivateKey`](@ref),
52
[`init_bootstrapping!`](@ref)
53
"""
54
init_bootstrapping!(context::SecureContext{<:Unencrypted}, private_key::PrivateKey) = nothing
16✔
55

56
"""
57
    PlainVector(data::Vector{<:Real}, context::SecureContext{<:Unencrypted})
58

59
Constructor for data type [`PlainVector`](@ref) takes an unencrypted `data` vector and a `context`
60
object of type `SecureContext{<:Unencrypted}`. Returns [`PlainVector`](@ref) with not encoded and
61
not encrypted data. The context can be utilized later for encryption using [`encrypt`](@ref),
62
resulting in [`SecureVector`](@ref).
63
        
64
See also: [`PlainVector`](@ref), [`SecureVector`](@ref), [`encrypt`](@ref), [`decrypt`](@ref)
65
"""
66
function PlainVector(data::Vector{<:Real}, context::SecureContext{<:Unencrypted})
56✔
67
    PlainVector(data, length(data), length(data), context)
56✔
68
end
69

70
function Base.show(io::IO, v::PlainVector{<:Unencrypted})
96✔
71
    print(io, v.data[1:v.length])
96✔
72
end
73

74
function Base.show(io::IO, ::MIME"text/plain", v::PlainVector{<:Unencrypted})
8✔
75
    print(io, v.length, "-element PlainVector{Unencrypted}:\n")
8✔
76
    Base.print_matrix(io, v.data[1:v.length])
8✔
77
end
78

79
"""
80
    collect(v::PlainVector{<:Unencrypted})
81

82
Return the real-valued data contained in `v`.
83

84
See also: [`PlainVector`](@ref)
85
"""
86
function Base.collect(v::PlainVector{<:Unencrypted})
40✔
87
    v.data
40✔
88
end
89

90
"""
91
    level(v::Union{SecureVector{<:Unencrypted}, PlainVector{<:Unencrypted}})
92

93
Return the number of scalings, referred to as the level, performed over `v`. For data type derived
94
from `Unencrypted`, the level is always equal to 0.
95

96
See also: [`PlainVector`](@ref), [`SecureVector`](@ref)
97
"""
98
function level(v::Union{SecureVector{<:Unencrypted}, PlainVector{<:Unencrypted}})
16✔
99
    0
10✔
100
end
101

102
function encrypt_impl(data::Vector{<:Real}, public_key::PublicKey,
16✔
103
                      context::SecureContext{<:Unencrypted})
104
    SecureVector(data, length(data), length(data), context)
16✔
105
end
106

107
function encrypt_impl(plain_vector::PlainVector{<:Unencrypted}, public_key::PublicKey)
48✔
108
    SecureVector(plain_vector.data, length(plain_vector), capacity(plain_vector),
48✔
109
                 plain_vector.context)
110
end
111

112
function decrypt_impl!(plain_vector::PlainVector{<:Unencrypted},
96✔
113
                       secure_vector::SecureVector{<:Unencrypted}, private_key::PrivateKey)
114
    plain_vector.data .= secure_vector.data
96✔
115

116
    plain_vector
60✔
117
end
118

119
function decrypt_impl(secure_vector::SecureVector{<:Unencrypted}, private_key::PrivateKey)
96✔
120
    plain_vector = PlainVector(similar(secure_vector.data), length(secure_vector),
96✔
121
                               capacity(secure_vector), secure_vector.context)
122

123
    decrypt!(plain_vector, secure_vector, private_key)
96✔
124
end
125

126
"""
127
    bootstrap!(secure_vector::SecureVector{<:Unencrypted})
128
     
129
An empty duplicate of [`bootstrap!`](@ref) for unencrypted data.
130

131
See also: [`SecureVector`](@ref), [`Unencrypted`](@ref), [`bootstrap!`](@ref),
132
[`init_bootstrapping!`](@ref)
133
"""
134
bootstrap!(secure_vector::SecureVector{<:Unencrypted}) = secure_vector
8✔
135

136

137
############################################################################################
138
# Arithmetic operations
139
############################################################################################
140

141
function add(sv1::SecureVector{<:Unencrypted}, sv2::SecureVector{<:Unencrypted})
16✔
142
    SecureVector(sv1.data .+ sv2.data, length(sv1), capacity(sv1), sv1.context)
16✔
143
end
144

145
function add(sv::SecureVector{<:Unencrypted}, pv::PlainVector{<:Unencrypted})
16✔
146
    SecureVector(sv.data .+ pv.data, length(sv), capacity(sv), sv.context)
16✔
147
end
148

149
function add(sv::SecureVector{<:Unencrypted}, scalar::Real)
16✔
150
    SecureVector(sv.data .+ scalar, length(sv), capacity(sv), sv.context)
16✔
151
end
152

153
function subtract(sv1::SecureVector{<:Unencrypted}, sv2::SecureVector{<:Unencrypted})
16✔
154
    SecureVector(sv1.data .- sv2.data, length(sv1), capacity(sv1), sv1.context)
16✔
155
end
156

157
function subtract(sv::SecureVector{<:Unencrypted}, pv::PlainVector{<:Unencrypted})
8✔
158
    SecureVector(sv.data .- pv.data, length(sv), capacity(sv), sv.context)
8✔
159
end
160

161
function subtract(pv::PlainVector{<:Unencrypted}, sv::SecureVector{<:Unencrypted})
8✔
162
    SecureVector(pv.data .- sv.data, length(sv), capacity(sv), sv.context)
8✔
163
end
164

165
function subtract(sv::SecureVector{<:Unencrypted}, scalar::Real)
8✔
166
    SecureVector(sv.data .- scalar, length(sv), capacity(sv), sv.context)
8✔
167
end
168

169
function subtract(scalar::Real, sv::SecureVector{<:Unencrypted})
8✔
170
    SecureVector(scalar .- sv.data, length(sv), capacity(sv), sv.context)
8✔
171
end
172

173
function negate(sv::SecureVector{<:Unencrypted})
8✔
174
    SecureVector(-sv.data, length(sv), capacity(sv), sv.context)
8✔
175
end
176

177
function multiply(sv1::SecureVector{<:Unencrypted}, sv2::SecureVector{<:Unencrypted})
16✔
178
    SecureVector(sv1.data .* sv2.data, length(sv1), capacity(sv1), sv1.context)
16✔
179
end
180

181
function multiply(sv::SecureVector{<:Unencrypted}, pv::PlainVector{<:Unencrypted})
16✔
182
    SecureVector(sv.data .* pv.data, length(sv), capacity(sv), sv.context)
16✔
183
end
184

185
function multiply(sv::SecureVector{<:Unencrypted}, scalar::Real)
24✔
186
    SecureVector(sv.data .* scalar, length(sv), capacity(sv), sv.context)
24✔
187
end
188

189
function rotate(sv::SecureVector{<:Unencrypted}, shift; wrap_by)
144✔
190
    # `wrap_by` can be ignored since here length is always equal to capacity
191
    SecureVector(circshift(sv.data, shift), length(sv), capacity(sv), sv.context)
72✔
192
end
193

194

195
############################################################################################
196
# Matrix
197
############################################################################################
198
init_matrix_rotation!(context::SecureContext{<:Unencrypted}, private_key::PrivateKey,
9✔
199
                      shifts, shape) = nothing
15✔
200

201
function PlainMatrix(data::Matrix{<:Real}, context::SecureContext{<:Unencrypted})
48✔
202
    PlainMatrix(data, size(data), length(data), context)
48✔
203
end
204

205
function PlainMatrix(data::Vector{<:Real}, context::SecureContext{<:Unencrypted},
8✔
206
                     shape::Tuple{Int, Int})
207
    reshaped_data = Matrix(reshape(data, shape))
8✔
208
    PlainMatrix(reshaped_data, context)
8✔
209
end
210

211
function Base.show(io::IO, m::PlainMatrix{<:Unencrypted})
88✔
212
    print(io, m.data[1:m.shape[1], 1:m.shape[2]])
88✔
213
end
214

215
function Base.show(io::IO, ::MIME"text/plain", m::PlainMatrix{<:Unencrypted})
8✔
216
    print(io, m.shape, "-shaped PlainMatrix{Unencrypted}:\n")
8✔
217
    Base.print_matrix(io, m.data[1:m.shape[1], 1:m.shape[2]])
8✔
218
end
219

220
function Base.collect(m::PlainMatrix{<:Unencrypted})
56✔
221
    m.data
56✔
222
end
223

224
function level(m::Union{SecureMatrix{<:Unencrypted}, PlainMatrix{<:Unencrypted}})
16✔
225
    0
10✔
226
end
227

228
function encrypt_impl(data::Matrix{<:Real}, public_key::PublicKey,
8✔
229
                      context::SecureContext{<:Unencrypted})
230
    SecureMatrix(data, size(data), length(data), context)
8✔
231
end
232

233
function encrypt_impl(plain_matrix::PlainMatrix{<:Unencrypted}, public_key::PublicKey)
40✔
234
    SecureMatrix(plain_matrix.data, size(plain_matrix), capacity(plain_matrix),
40✔
235
                 plain_matrix.context)
236
end
237

238
function decrypt_impl!(plain_matrix::PlainMatrix{<:Unencrypted},
112✔
239
                       secure_matrix::SecureMatrix{<:Unencrypted}, private_key::PrivateKey)
240
    plain_matrix.data .= secure_matrix.data
112✔
241

242
    plain_matrix
70✔
243
end
244

245
function decrypt_impl(secure_matrix::SecureMatrix{<:Unencrypted}, private_key::PrivateKey)
112✔
246
    plain_matrix = PlainMatrix(similar(secure_matrix.data), size(secure_matrix),
112✔
247
                               capacity(secure_matrix), secure_matrix.context)
248

249
    decrypt!(plain_matrix, secure_matrix, private_key)
112✔
250
end
251

252
bootstrap!(secure_matrix::SecureMatrix{<:Unencrypted}) = secure_matrix
8✔
253

254

255
############################################################################################
256
# Arithmetic operations
257
############################################################################################
258

259
function add(sm1::SecureMatrix{<:Unencrypted}, sm2::SecureMatrix{<:Unencrypted})
16✔
260
    SecureMatrix(sm1.data .+ sm2.data, size(sm1), capacity(sm1), sm1.context)
16✔
261
end
262

263
function add(sm::SecureMatrix{<:Unencrypted}, pm::PlainMatrix{<:Unencrypted})
16✔
264
    SecureMatrix(sm.data .+ pm.data, size(sm), capacity(sm), sm.context)
16✔
265
end
266

267
function add(sm::SecureMatrix{<:Unencrypted}, scalar::Real)
16✔
268
    SecureMatrix(sm.data .+ scalar, size(sm), capacity(sm), sm.context)
16✔
269
end
270

271
function subtract(sm1::SecureMatrix{<:Unencrypted}, sm2::SecureMatrix{<:Unencrypted})
16✔
272
    SecureMatrix(sm1.data .- sm2.data, size(sm1), capacity(sm1), sm1.context)
16✔
273
end
274

275
function subtract(sm::SecureMatrix{<:Unencrypted}, pm::PlainMatrix{<:Unencrypted})
8✔
276
    SecureMatrix(sm.data .- pm.data, size(sm), capacity(sm), sm.context)
8✔
277
end
278

279
function subtract(pm::PlainMatrix{<:Unencrypted}, sm::SecureMatrix{<:Unencrypted})
8✔
280
    SecureMatrix(pm.data .- sm.data, size(sm), capacity(sm), sm.context)
8✔
281
end
282

283
function subtract(sm::SecureMatrix{<:Unencrypted}, scalar::Real)
8✔
284
    SecureMatrix(sm.data .- scalar, size(sm), capacity(sm), sm.context)
8✔
285
end
286

287
function subtract(scalar::Real, sm::SecureMatrix{<:Unencrypted})
8✔
288
    SecureMatrix(scalar .- sm.data, size(sm), capacity(sm), sm.context)
8✔
289
end
290

291
function negate(sm::SecureMatrix{<:Unencrypted})
8✔
292
    SecureMatrix(-sm.data, size(sm), capacity(sm), sm.context)
8✔
293
end
294

295
function multiply(sm1::SecureMatrix{<:Unencrypted}, sm2::SecureMatrix{<:Unencrypted})
16✔
296
    SecureMatrix(sm1.data .* sm2.data, size(sm1), capacity(sm1), sm1.context)
16✔
297
end
298

299
function multiply(sm::SecureMatrix{<:Unencrypted}, pm::PlainMatrix{<:Unencrypted})
16✔
300
    SecureMatrix(sm.data .* pm.data, size(sm), capacity(sm), sm.context)
16✔
301
end
302

303
function multiply(sm::SecureMatrix{<:Unencrypted}, scalar::Real)
24✔
304
    SecureMatrix(sm.data .* scalar, size(sm), capacity(sm), sm.context)
24✔
305
end
306

307
function rotate(sm::SecureMatrix{<:Unencrypted}, shift)
56✔
308
    SecureMatrix(circshift(sm.data, shift), size(sm), capacity(sm), sm.context)
56✔
309
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc