Coder Social home page Coder Social logo

Comments (4)

ibarrond avatar ibarrond commented on June 1, 2024

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.

Shakemilktea avatar Shakemilktea commented on June 1, 2024

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.

Shakemilktea avatar Shakemilktea commented on June 1, 2024

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.

ibarrond avatar ibarrond commented on June 1, 2024

Closing as the OP found a fix

from pyfhel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.