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

hpsc-lab / SecureArithmetic.jl / 13112159393

03 Feb 2025 11:10AM UTC coverage: 96.262%. First build
13112159393

Pull #59

github

web-flow
Merge bab919a0b into babf72f09
Pull Request #59: Add Secure Array

479 of 498 new or added lines in 8 files covered. (96.18%)

618 of 642 relevant lines covered (96.26%)

62.0 hits per line

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

96.55
/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
5✔
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})
6✔
22
    PublicKey(context, nothing), PrivateKey(context, nothing)
6✔
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
5✔
34

35
"""
36
    init_rotation!(context::SecureContext{<:Unencrypted}, private_key::PrivateKey,
37
                   shape, shifts...)
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,
9✔
45
               shape, shifts...) = 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
3✔
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})
7✔
69
    PlainArray(data, context)
7✔
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})
5✔
83
    PlainArray(data, context)
5✔
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})
19✔
97
    PlainArray(data, size(data), length(data), context)
19✔
98
end
99

100
function Base.show(io::IO, pa::PlainArray{<:Unencrypted})
34✔
101
    print(io, pa.data)
34✔
102
end
103

104
function Base.show(io::IO, ::MIME"text/plain", pa::PlainArray{<:Unencrypted})
3✔
105
    print(io, pa.shape, "-shaped PlainArray{Unencrypted}:\n")
3✔
106
    Base.print_matrix(io, pa.data)
3✔
107
end
108

109
"""
110
    collect(pa::PlainArray{<:Unencrypted})
111

112
Return the real-valued data contained in `pa`.
113

114
See also: [`PlainArray`](@ref)
115
"""
116
function Base.collect(pa::PlainArray{<:Unencrypted})
23✔
117
    pa.data
23✔
118
end
119

120
"""
121
    level(a::Union{SecureArray{<:Unencrypted}, PlainArray{<:Unencrypted}})
122

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

126
See also: [`PlainArray`](@ref), [`SecureArray`](@ref)
127
"""
128
function level(a::Union{SecureArray{<:Unencrypted}, PlainArray{<:Unencrypted}})
6✔
129
    0
×
130
end
131

132
function encrypt_impl(data::Array{<:Real}, public_key::PublicKey,
3✔
133
                      context::SecureContext{<:Unencrypted})
134
    SecureArray(data, size(data), length(data), context)
3✔
135
end
136

137
function encrypt_impl(plain_array::PlainArray{<:Unencrypted}, public_key::PublicKey)
18✔
138
    SecureArray(plain_array.data, size(plain_array), capacity(plain_array),
18✔
139
                plain_array.context)
140
end
141

142
function decrypt_impl!(plain_array::PlainArray{<:Unencrypted},
44✔
143
                       secure_array::SecureArray{<:Unencrypted}, private_key::PrivateKey)
144
    plain_array.data .= secure_array.data
44✔
145

NEW
146
    plain_array
×
147
end
148

149
function decrypt_impl(secure_array::SecureArray{<:Unencrypted}, private_key::PrivateKey)
44✔
150
    plain_array = PlainArray(similar(secure_array.data), size(secure_array), capacity(secure_array),
44✔
151
                             secure_array.context)
152

153
    decrypt!(plain_array, secure_array, private_key)
44✔
154
end
155

156
"""
157
    bootstrap!(secure_array::SecureArray{<:Unencrypted}, num_iterations = 1,
158
               precision = 0)
159
     
160
An empty duplicate of [`bootstrap!`](@ref) for unencrypted data.
161

162
See also: [`SecureArray`](@ref), [`Unencrypted`](@ref), [`bootstrap!`](@ref),
163
[`init_bootstrapping!`](@ref)
164
"""
165
bootstrap!(secure_array::SecureArray{<:Unencrypted}, num_iterations = 1,
6✔
166
           precision = 0) = secure_array
167

168

169
############################################################################################
170
# Arithmetic operations
171
############################################################################################
172

173
function add(sa1::SecureArray{<:Unencrypted}, sa2::SecureArray{<:Unencrypted})
6✔
174
    SecureArray(sa1.data .+ sa2.data, size(sa1), capacity(sa1), sa1.context)
6✔
175
end
176

177
function add(sa::SecureArray{<:Unencrypted}, pa::PlainArray{<:Unencrypted})
6✔
178
    SecureArray(sa.data .+ pa.data, size(sa), capacity(sa), sa.context)
6✔
179
end
180

181
function add(sa::SecureArray{<:Unencrypted}, scalar::Real)
6✔
182
    SecureArray(sa.data .+ scalar, size(sa), capacity(sa), sa.context)
6✔
183
end
184

185
function subtract(sa1::SecureArray{<:Unencrypted}, sa2::SecureArray{<:Unencrypted})
6✔
186
    SecureArray(sa1.data .- sa2.data, size(sa1), capacity(sa1), sa1.context)
6✔
187
end
188

189
function subtract(sa::SecureArray{<:Unencrypted}, pa::PlainArray{<:Unencrypted})
3✔
190
    SecureArray(sa.data .- pa.data, size(sa), capacity(sa), sa.context)
3✔
191
end
192

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

197
function subtract(sa::SecureArray{<:Unencrypted}, scalar::Real)
3✔
198
    SecureArray(sa.data .- scalar, size(sa), capacity(sa), sa.context)
3✔
199
end
200

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

205
function negate(sa::SecureArray{<:Unencrypted})
3✔
206
    SecureArray(-sa.data, size(sa), capacity(sa), sa.context)
3✔
207
end
208

209
function multiply(sa1::SecureArray{<:Unencrypted}, sa2::SecureArray{<:Unencrypted})
6✔
210
    SecureArray(sa1.data .* sa2.data, size(sa1), capacity(sa1), sa1.context)
6✔
211
end
212

213
function multiply(sa::SecureArray{<:Unencrypted}, pa::PlainArray{<:Unencrypted})
6✔
214
    SecureArray(sa.data .* pa.data, size(sa), capacity(sa), sa.context)
6✔
215
end
216

217
function multiply(sa::SecureArray{<:Unencrypted}, scalar::Real)
9✔
218
    SecureArray(sa.data .* scalar, size(sa), capacity(sa), sa.context)
9✔
219
end
220

221
function rotate(sa::SecureArray{<:Unencrypted, N}, shift) where N
25✔
222
    SecureArray(circshift(sa.data, shift), size(sa), capacity(sa), sa.context)
25✔
223
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