Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decrypt(circshift(c, gk)) does not work with Julia 1.5.0-beta1 #16

Closed
sloede opened this issue Jun 12, 2020 · 4 comments
Closed

decrypt(circshift(c, gk)) does not work with Julia 1.5.0-beta1 #16

sloede opened this issue Jun 12, 2020 · 4 comments

Comments

@sloede
Copy link
Member

sloede commented Jun 12, 2020

I've tried to run the introductory examples on https://juliacomputing.com/blog/2019/11/22/encrypted-machine-learning.html with Julia 1.5.0-beta1. When I reached the circshift test, Julia threw the following error:

julia> decrypt(circshift(c, gk))
ERROR: MethodError: no method matching decrypt(::CipherText{CKKSEncoding{FixedRational{1099511627776,T} where T},CKKSParams,ToyFHE.NTT.RingElement{ℤ₁₃₂₉₂₂₇₉₉₇₅₆₈₀₈₁₄₅₇₄₀₂₇₀₁₂₀₇₁₀₄₂₄₈₂₅₇/(x¹⁶ + 1),ToyFHE.CRTEncoded{3,Tuple{𝔽₁₀₉₉₅₁₁₆₂₇₈₇₃,𝔽₁₀₉₉₅₁₁₆₂₈₁₆₁,𝔽₁₀₉₉₅₁₁₆₂₈₇₆₉}},StructArrays.StructArray{ToyFHE.CRTEncoded{3,Tuple{𝔽₁₀₉₉₅₁₁₆₂₇₈₇₃,𝔽₁₀₉₉₅₁₁₆₂₈₁₆₁,𝔽₁₀₉₉₅₁₁₆₂₈₇₆₉}},1,Tuple{Array{𝔽₁₀₉₉₅₁₁₆₂₇₈₇₃,1},Array{𝔽₁₀₉₉₅₁₁₆₂₈₁₆₁,1},Array{𝔽₁₀₉₉₅₁₁₆₂₈₇₆₉,1}},Int64}},2})
Closest candidates are:
  decrypt(::PrivKey, ::CipherText{T,P,T1,N} where N where T1 where P<:ToyFHE.SHEShemeParams) where T at /home/mschlott/gdrive/work/code/CryptoSim.jl/ToyFHE.jl/src/rlwe_she.jl:199
  decrypt(::KeyPair, ::Any) at /home/whatever/ToyFHE.jl/src/rlwe_she.jl:217
Stacktrace:
 [1] top-level scope at REPL[21]:1

Here's an MWE:

julia> using ToyFHE

julia> N = 8;

julia>= NegacyclicRing(2N, (40, 40, 40))
ℤ₁₃₂₉₂₂₇₉₉₇₅₆₈₀₈₁₄₅₇₄₀₂₇₀₁₂₀₇₁₀₄₂₄₈₂₅₇/(x¹⁶ + 1)

julia> params = CKKSParams(ℛ)
CKKS parameters

julia> Tscale = FixedRational{2^40}
FixedRational{1099511627776,T} where T

julia> plain = CKKSEncoding{Tscale}(zero(ℛ))
8-element CKKSEncoding{FixedRational{1099511627776,T} where T} with indices 0:7:
 0.0 + 0.0im
 0.0 + 0.0im
 0.0 + 0.0im
 0.0 + 0.0im
 0.0 + 0.0im
 0.0 + 0.0im
 0.0 + 0.0im
 0.0 + 0.0im

julia> kp = keygen(params)
CKKS key pair

julia> kp.priv
CKKS private key

julia> kp.pub
CKKS public key

julia> foreach(i->plain[i] = i+1, 0:7); plain
8-element CKKSEncoding{FixedRational{1099511627776,T} where T} with indices 0:7:
 1.0 + 0.0im
 2.0 + 0.0im
 3.0 + 0.0im
 4.0 + 0.0im
 5.0 + 0.0im
 6.0 + 0.0im
 7.0 + 0.0im
 8.0 + 0.0im

julia> c = encrypt(kp.pub, plain)
CKKS ciphertext (length 2, encoding CKKSEncoding{FixedRational{1099511627776,T} where T})

julia> gk = keygen(GaloisKey, kp.priv; steps=2)
CKKS galois key (element 25)

julia> decrypt(circshift(c, gk))

Any idea what the problem could be?

@sloede
Copy link
Member Author

sloede commented Jun 12, 2020

By the way, kudos to implementing this package in pure Julia, this has been a great help to kick around a few ideas 👍

@Keno
Copy link
Collaborator

Keno commented Jun 22, 2020

Whoops, there's a typo in the blog post, obviously, you need to pass the private key to decrypt it. In your MWE:

julia> decrypt(kp.priv, circshift(c, gk))
8-element CKKSEncoding{FixedRational{1099511627776,T} where T} with indices 0:7:
 -18.506166149368628 + 7.60456192425713im
   40.80057950134403 + 8.450631478608264im
 -20.786099653978496 - 5.443269584828531im
  -16.50816701251562 - 0.3042282041572637im
  -19.25951679089231 - 29.497772481885757im
  21.089374991774747 - 16.74889104138221im
  2.5942574658725626 + 9.605495431441932im
   9.309505110847219 - 15.199594797590228im

I'll fix the blog post. Sorry for missing this issue. In the future, please feel free to just ping me directly on issues here.

@Keno Keno closed this as completed Jun 22, 2020
@Keno
Copy link
Collaborator

Keno commented Jun 22, 2020

Although now that I actually look at the output, the noise obviously took over here. It's been a while since I thought about FHE noise growth, so I'm not quite sure what the issue is.

@Keno Keno reopened this Jun 22, 2020
@Keno
Copy link
Collaborator

Keno commented Jun 22, 2020

Hmm works fine with the parameter choices in the test suite: https://github.com/JuliaCrypto/ToyFHE.jl/blob/master/test/ckks_rotate.jl. Not sure whether there's an actual issue here. Unfortunately, if there is, it'd be something subtle about the noise behavior, which is beyond the level of time commitment I have available to look into issues here. That said, hopefully you should be able to use the parameters in the test suite for experimentation if you want.

@Keno Keno closed this as completed Jun 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants