Skip to content

Commit db7f1c5

Browse files
authored
Pretty-print contents of PlainVector (#15)
* Add fancy `show` for `PlainVector`s * Update README -> we are a registered package now * Set version to v0.1.1
1 parent eb47482 commit db7f1c5

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SecureArithmetic"
22
uuid = "38cee09b-6d7e-4d21-866e-807a7f642fe9"
33
authors = ["Michael Schlottke-Lakemper <[email protected]>"]
4-
version = "0.1.0"
4+
version = "0.1.1"
55

66
[deps]
77
OpenFHE = "77ce9b8e-ecf5-45d1-bd8a-d31f384f2f95"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ operating system](https://julialang.org/downloads/platform/).
2323
and later on Linux and macOS platforms, and with Julia v1.9 or later on Windows platforms.
2424

2525
### Installation
26-
Since SecureArithmetic.jl is not yet a registered Julia package, you can install it by
27-
executing the following commands in the Julia REPL:
26+
Since SecureArithmetic.jl is a registered Julia package, you can install it by executing
27+
the following commands in the Julia REPL:
2828
```julia
29-
julia> import Pkg; Pkg.add("https://github.com/sloede/SecureArithmetic.jl")
29+
julia> import Pkg; Pkg.add("SecureArithmetic")
3030
```
3131
If you plan on running the examples in the
3232
[`examples`](https://github.com/sloede/SecureArithmetic.jl/tree/main/examples) directory,

src/openfhe.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ function PlainVector(data::Vector{<:Real}, context::SecureContext{<:OpenFHEBacke
5353
plain_vector
5454
end
5555

56+
function Base.show(io::IO, ::MIME"text/plain", v::PlainVector{<:OpenFHEBackend})
57+
print(io, v.length, "-element PlainVector{OpenFHEBackend}:\n")
58+
Base.print_matrix(io, OpenFHE.GetRealPackedValue(v.data))
59+
end
60+
5661
function encrypt(data::Vector{<:Real}, public_key, context::SecureContext{<:OpenFHEBackend})
5762
plain_vector = PlainVector(data, context)
5863
secure_vector = encrypt(plain_vector, public_key)

src/types.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct SecureVector{CryptoBackendT <: AbstractCryptoBackend, DataT}
1919
end
2020

2121
Base.length(v::SecureVector) = v.length
22+
2223
function Base.show(io::IO, v::SecureVector)
2324
print("SecureVector{", backend_name(v), "}(data=<encrypted>, length=$(v.length))")
2425
end
@@ -34,11 +35,10 @@ struct PlainVector{CryptoBackendT <: AbstractCryptoBackend, DataT}
3435
end
3536

3637
Base.length(v::PlainVector) = v.length
37-
function Base.show(io::IO, v::PlainVector{CryptoBackendT}) where CryptoBackendT
38-
print("PlainVector{", backend_name(v), "}(data=<plain>, length=$(v.length))")
39-
end
4038

41-
Base.print(io::IO, plain_vector::PlainVector) = print(io, plain_vector.data)
39+
function Base.show(io::IO, v::PlainVector)
40+
print(io, "PlainVector{", backend_name(v), "}(data=<encoded>, length=$(v.length))")
41+
end
4242

4343
struct PrivateKey{CryptoBackendT <: AbstractCryptoBackend, KeyT}
4444
private_key::KeyT
@@ -49,7 +49,7 @@ struct PrivateKey{CryptoBackendT <: AbstractCryptoBackend, KeyT}
4949
end
5050
end
5151

52-
function Base.show(io::IO, key::PrivateKey{CryptoBackendT}) where CryptoBackendT
52+
function Base.show(io::IO, key::PrivateKey)
5353
print("PrivateKey{", backend_name(key), "}()")
5454
end
5555

@@ -62,7 +62,7 @@ struct PublicKey{CryptoBackendT <: AbstractCryptoBackend, KeyT}
6262
end
6363
end
6464

65-
function Base.show(io::IO, key::PublicKey{CryptoBackendT}) where CryptoBackendT
65+
function Base.show(io::IO, key::PublicKey)
6666
print("PublicKey{", backend_name(key), "}()")
6767
end
6868

@@ -72,6 +72,6 @@ end
7272
# Note: prefixed by `__` since it is really, really dirty black magic internals we use here!
7373
__parameterless_type(T) = Base.typename(T).wrapper
7474

75-
# Convenience method for getting the human-readable backend name
75+
# Convenience method for getting human-readable names
7676
backend_name(x::Union{SecureContext{T}, SecureVector{T}, PlainVector{T}, PrivateKey{T},
7777
PublicKey{T}}) where T = string(__parameterless_type(T))

src/unencrypted.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ function PlainVector(data::Vector{<:Real}, context::SecureContext{<:Unencrypted}
1414
PlainVector(data, length(data), context)
1515
end
1616

17+
function Base.show(io::IO, ::MIME"text/plain", v::PlainVector{<:Unencrypted})
18+
print(io, v.length, "-element PlainVector{Unencrypted}:\n")
19+
Base.print_matrix(io, v.data[1:v.length])
20+
end
21+
1722
function encrypt(data::Vector{<:Real}, public_key::PublicKey,
1823
context::SecureContext{<:Unencrypted})
1924
SecureVector(data, length(data), context)

test/test_unit.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,22 @@ for backend in ((; name = "OpenFHE", BackendT = OpenFHEBackend, context = contex
9090
end
9191

9292
@testset verbose=true showtiming=true "show" begin
93-
@test_nowarn show(context)
93+
@test_nowarn show(stdout, context)
9494
println()
9595

96-
@test_nowarn show(pv1)
96+
@test_nowarn show(stdout, pv1)
9797
println()
9898

99-
@test_nowarn show(sv1)
99+
@test_nowarn show(stdout, MIME"text/plain"(), pv1)
100100
println()
101101

102-
@test_nowarn show(public_key)
102+
@test_nowarn show(stdout, sv1)
103103
println()
104104

105-
@test_nowarn show(private_key)
105+
@test_nowarn show(stdout, public_key)
106+
println()
107+
108+
@test_nowarn show(stdout, private_key)
106109
println()
107110
end
108111
end

0 commit comments

Comments
 (0)