Comments (4)
Hi @Shakemilktea, the plaintext multiplication of sub_matrix.sum()
with 0.25
seems to be causing the issue (this is the SEAL line of code that raises the error). Could you try encoding that 0.25
in a PyPtxt (e.g., ptxt = HE.encode(0.25)
) and calling HE..align_mod_n_scale(sub_matrix.sum(), ptxt)
? It looks like you caught a bug!
from pyfhel.
Hello @ibarrond, thank you for your response! After testing, using HE.align_mod_n_scale(sub_matrix.sum(), ptxt) successfully adjusted the scale_bits of the sub-matrix and the mod_level of ptxt. Unfortunately, the error still exists and seems to not be related to scale_bits. Additionally, if I move the square layer forward, this problem doesn't occur, and only a UserWarning is generated. The results of the decryption are correct in this case. The following are the results of moving the square layer forward:
Expected answer: [[10932.625]]
E:\NCKU\ICLab\code\my_code\Pyfhel\HE_CNN_CKKS.py:56: UserWarning: Cannot align scales 120 and 30 (available rescalings: [30 60])
new_matrix_row_temp.append(np.array(sub_matrix).sum()*0.25)
E:\NCKU\ICLab\code\my_code\Pyfhel\HE_CNN_CKKS.py:32: UserWarning: Cannot align scales 150 and 30 (available rescalings: [30 60 90])
convolution = sub_matrix * kernel
E:\NCKU\ICLab\code\my_code\Pyfhel\HE_CNN_CKKS.py:56: UserWarning: Cannot align scales 180 and 30 (available rescalings: [ 30 60 90 120])
new_matrix_row_temp.append(np.array(sub_matrix).sum()*0.25)
Encrypt answer [[<Pyfhel Ciphertext at 0x159e301f5b0, scheme=ckks, size=2/2, scale_bits=210, mod_level=5>]]
Decrypt answer: [[array([10932.624508])]]
from pyfhel.
I think I've found the issue. When I multiply the ciphertext with scale=30 by the plaintext, the scale is increased to 60. When multiplying the ciphertext with scale=60 by either scale=30 plaintext or ciphertext, the result can be maintained at scale=60. However, if I multiply the ciphertext with scale=60 by another ciphertext with scale=60, the result becomes scale=120. When multiplying the scale=120 ciphertext by scale=30 plaintext or ciphertext, the scale undergoes further escalation instead of staying at scale=60.
Therefore, it is necessary to align the ciphertext with scale=60 with plaintext or ciphertext with scale=30 first. Taking the final part of the Demo_6_MultDepth code as an example, I modified the last section to look like this:
print(f"B3. Securely multiplying {n_mults} times!")
for step in range(1,n_mults+1):
# do x * y
ctxt_x *= ctxt_y # Multiply in-place --> implicit align_mod_n_scale()
ctxt_x = ~(ctxt_x) # Always relinearize after each multiplication!
print(ctxt_x)
print(f"\tStep {step}: res {_r(HE.decryptFrac(ctxt_x))}")
# ctxt_x = HE.align_mod_n_scale(ctxt_x, ctxt_y)[0] # THIS PART IS NOT NECESSARY(WHY?)
# do x * y
ctxt_x *= ctxt_y # Multiply in-place --> implicit align_mod_n_scale()
ctxt_x = ~(ctxt_x) # Always relinearize after each multiplication!
print(ctxt_x)
print(f"\tStep {step}: res {_r(HE.decryptFrac(ctxt_x))}")
ctxt_x = HE.align_mod_n_scale(ctxt_x, ctxt_y)[0] # MAIN PART
# do x^2
ctxt_x *= ctxt_x
ctxt_x = ~(ctxt_x) # Always relinearize after each multiplication!
print(ctxt_x)
print(f"\tStep {step}: res {_r(HE.decryptFrac(ctxt_x))}")
I use comments to explain the confusing part of this code. When I add #MAIN PART, the program correctly outputs "ValueError: scale out of bounds." Conversely, without it, the program outputs "ValueError: encrypted1 and encrypted2 parameter mismatch."
I suspect the problem is that when the ciphertext undergoes a second round of operations, it doesn't align its scale.
So, I modified the code like this, and the issue was finally resolved! Thanks for your advice!
x = conv2d(ctxt_message)
rescale_matrix(HE, x, ctxt_message[0][0])
# x = square(x)
# relinearize_matrix(HE, x)
x = avg_pooling(x)
rescale_matrix(HE, x, ctxt_message[0][0])
x = conv2d(x)
rescale_matrix(HE, x, ctxt_message[0][0])
x = square(x)
relinearize_matrix(HE, x)
rescale_matrix(HE, x, ctxt_message[0][0])
from pyfhel.
Closing as the OP found a fix
from pyfhel.
Related Issues (20)
- The decrypt answer is not near to the answer it should be. HOT 1
- Invalid encryption parameters for certain primes in BFV HOT 3
- Does the library support bootstrapping for ckks HOT 1
- ImportError occurs when executing "import Pyfhel" HOT 6
- How to calculate squared euclidean distance? HOT 3
- How to duplicate the same number or set of numbers across an encrypted array?
- How to avoid `scale out of bounds` error in repeated multiplication? HOT 6
- Loading Saved ciphertext and decrypting it HOT 1
- ERROR: Could not build wheels for Pyfhel, which is required to install pyproject.toml-based projects HOT 3
- ValueError: encrypted_ntt and plain_ntt parameter mismatch in evaluate_polynomial HOT 1
- Installation error of Pyfhel HOT 2
- [up to 10 words short bug description]: [one-line error message] HOT 1
- How to print the ciphertext for BFV/CKKS scheme? HOT 4
- Not able to install Pyfhel module in Windows 10 HOT 6
- unable to install on windows11 HOT 2
- unable to install on WSL ubuntu
- License ambiguity: GPL or Apache 2.0? HOT 2
- pip Pyfhel went wrong HOT 2
- `ImportError` when running `from Pyfhel import Pyfhel` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyfhel.