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

hpsc-lab / SecureArithmetic.jl / 13070730225

31 Jan 2025 10:16AM UTC coverage: 99.847%. First build
13070730225

Pull #59

github

web-flow
Merge 35a2aa843 into daf7aabfa
Pull Request #59: Add Secure Array

488 of 507 new or added lines in 8 files covered. (96.25%)

651 of 652 relevant lines covered (99.85%)

355.26 hits per line

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

98.36
/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
30✔
10
    # No data fields required
11
end
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})
36✔
22
    PublicKey(context, nothing), PrivateKey(context, nothing)
36✔
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
30✔
34

35
"""
36
    init_rotation!(context::SecureContext{<:Unencrypted}, private_key::PrivateKey,
37
                   shape, rotation_index...)
38

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

41
See also: [`SecureContext`](@ref), [`Unencrypted`](@ref), [`PrivateKey`](@ref),
42
[`init_rotation!`](@ref)
43
"""
44
init_rotation!(context::SecureContext{<:Unencrypted}, private_key::PrivateKey,
54✔
45
               shape, rotation_index...) = nothing
46

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

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

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

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

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

72
"""
73
    PlainMatrix(data::Matrix{<:Real}, context::SecureContext{<:Unencrypted})
74

75
Constructor for data type [`PlainMatrix`](@ref) takes an unencrypted `data` matrix and a `context`
76
object of type `SecureContext{<:Unencrypted}`. Returns [`PlainMatrix`](@ref) with not encoded and
77
not encrypted data. The context can be utilized later for encryption using [`encrypt`](@ref),
78
resulting in [`SecureMatrix`](@ref).
79
        
80
See also: [`PlainMatrix`](@ref), [`SecureMatrix`](@ref), [`encrypt`](@ref), [`decrypt`](@ref)
81
"""
82
function PlainMatrix(data::Matrix{<:Real}, context::SecureContext{<:Unencrypted})
30✔
83
    PlainArray(data, context)
30✔
84
end
85

86
"""
87
    PlainArray(data::Array{<:Real}, context::SecureContext{<:Unencrypted})
88

89
Constructor for data type [`PlainArray`](@ref) takes an unencrypted `data` array and a `context`
90
object of type `SecureContext{<:Unencrypted}`. Returns [`PlainArray`](@ref) with not encoded and
91
not encrypted data. The context can be utilized later for encryption using [`encrypt`](@ref),
92
resulting in [`SecureArray`](@ref).
93
        
94
See also: [`PlainArray`](@ref), [`SecureArray`](@ref), [`encrypt`](@ref), [`decrypt`](@ref)
95
"""
96
function PlainArray(data::Array{<:Real}, context::SecureContext{<:Unencrypted})
126✔
97
    PlainArray(data, size(data), length(data), context)
126✔
98
end
99

100
function PlainArray(data::Vector{<:Real}, context::SecureContext{<:Unencrypted},
12✔
101
                    shape::Tuple)
102
    reshaped_data = Array(reshape(data, shape))
18✔
103
    PlainArray(reshaped_data, context)
12✔
104
end
105

106
function Base.show(io::IO, pa::PlainArray{<:Unencrypted})
204✔
107
    print(io, pa.data)
204✔
108
end
109

110
function Base.show(io::IO, ::MIME"text/plain", pa::PlainArray{<:Unencrypted})
18✔
111
    print(io, pa.shape, "-shaped PlainArray{Unencrypted}:\n")
18✔
112
    Base.print_matrix(io, pa.data)
18✔
113
end
114

115
"""
116
    collect(pa::PlainArray{<:Unencrypted})
117

118
Return the real-valued data contained in `pa`.
119

120
See also: [`PlainArray`](@ref)
121
"""
122
function Base.collect(pa::PlainArray{<:Unencrypted})
132✔
123
    pa.data
132✔
124
end
125

126
"""
127
    level(a::Union{SecureArray{<:Unencrypted}, PlainArray{<:Unencrypted}})
128

129
Return the number of scalings, referred to as the level, performed over `a`. For data type derived
130
from `Unencrypted`, the level is always equal to 0.
131

132
See also: [`PlainArray`](@ref), [`SecureArray`](@ref)
133
"""
134
function level(a::Union{SecureArray{<:Unencrypted}, PlainArray{<:Unencrypted}})
18✔
135
    0
×
136
end
137

138
function encrypt_impl(data::Array{<:Real}, public_key::PublicKey,
18✔
139
                      context::SecureContext{<:Unencrypted})
140
    SecureArray(data, size(data), length(data), context)
18✔
141
end
142

143
function encrypt_impl(plain_array::PlainArray{<:Unencrypted}, public_key::PublicKey)
108✔
144
    SecureArray(plain_array.data, size(plain_array), capacity(plain_array),
108✔
145
                plain_array.context)
146
end
147

148
function decrypt_impl!(plain_array::PlainArray{<:Unencrypted},
258✔
149
                       secure_array::SecureArray{<:Unencrypted}, private_key::PrivateKey)
150
    plain_array.data .= secure_array.data
387✔
151

NEW
152
    plain_array
129✔
153
end
154

155
function decrypt_impl(secure_array::SecureArray{<:Unencrypted}, private_key::PrivateKey)
258✔
156
    plain_array = PlainArray(similar(secure_array.data), size(secure_array), capacity(secure_array),
387✔
157
                             secure_array.context)
158

159
    decrypt!(plain_array, secure_array, private_key)
258✔
160
end
161

162
"""
163
    bootstrap!(secure_array::SecureArray{<:Unencrypted}, num_iterations = 1,
164
               precision = 0)
165
     
166
An empty duplicate of [`bootstrap!`](@ref) for unencrypted data.
167

168
See also: [`SecureArray`](@ref), [`Unencrypted`](@ref), [`bootstrap!`](@ref),
169
[`init_bootstrapping!`](@ref)
170
"""
171
bootstrap!(secure_array::SecureArray{<:Unencrypted}, num_iterations = 1,
36✔
172
           precision = 0) = secure_array
173

174

175
############################################################################################
176
# Arithmetic operations
177
############################################################################################
178

179
function add(sa1::SecureArray{<:Unencrypted}, sa2::SecureArray{<:Unencrypted})
36✔
180
    SecureArray(sa1.data .+ sa2.data, size(sa1), capacity(sa1), sa1.context)
36✔
181
end
182

183
function add(sa::SecureArray{<:Unencrypted}, pa::PlainArray{<:Unencrypted})
36✔
184
    SecureArray(sa.data .+ pa.data, size(sa), capacity(sa), sa.context)
36✔
185
end
186

187
function add(sa::SecureArray{<:Unencrypted}, scalar::Real)
36✔
188
    SecureArray(sa.data .+ scalar, size(sa), capacity(sa), sa.context)
36✔
189
end
190

191
function subtract(sa1::SecureArray{<:Unencrypted}, sa2::SecureArray{<:Unencrypted})
36✔
192
    SecureArray(sa1.data .- sa2.data, size(sa1), capacity(sa1), sa1.context)
36✔
193
end
194

195
function subtract(sa::SecureArray{<:Unencrypted}, pa::PlainArray{<:Unencrypted})
18✔
196
    SecureArray(sa.data .- pa.data, size(sa), capacity(sa), sa.context)
18✔
197
end
198

199
function subtract(pa::PlainArray{<:Unencrypted}, sa::SecureArray{<:Unencrypted})
18✔
200
    SecureArray(pa.data .- sa.data, size(sa), capacity(sa), sa.context)
18✔
201
end
202

203
function subtract(sa::SecureArray{<:Unencrypted}, scalar::Real)
18✔
204
    SecureArray(sa.data .- scalar, size(sa), capacity(sa), sa.context)
18✔
205
end
206

207
function subtract(scalar::Real, sa::SecureArray{<:Unencrypted})
18✔
208
    SecureArray(scalar .- sa.data, size(sa), capacity(sa), sa.context)
18✔
209
end
210

211
function negate(sa::SecureArray{<:Unencrypted})
18✔
212
    SecureArray(-sa.data, size(sa), capacity(sa), sa.context)
18✔
213
end
214

215
function multiply(sa1::SecureArray{<:Unencrypted}, sa2::SecureArray{<:Unencrypted})
36✔
216
    SecureArray(sa1.data .* sa2.data, size(sa1), capacity(sa1), sa1.context)
36✔
217
end
218

219
function multiply(sa::SecureArray{<:Unencrypted}, pa::PlainArray{<:Unencrypted})
36✔
220
    SecureArray(sa.data .* pa.data, size(sa), capacity(sa), sa.context)
36✔
221
end
222

223
function multiply(sa::SecureArray{<:Unencrypted}, scalar::Real)
54✔
224
    SecureArray(sa.data .* scalar, size(sa), capacity(sa), sa.context)
54✔
225
end
226

227
function rotate(sa::SecureArray{<:Unencrypted, N}, shift) where N
156✔
228
    SecureArray(circshift(sa.data, shift), size(sa), capacity(sa), sa.context)
162✔
229
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