Implementation of Ring Learning-with-Errors Encryption and Brakerski–Fan–Vercauteren Fully Homomorphic Encryption Using ChatGPT
Abstract
1. Introduction
2. Preliminaries
2.1. Ring-LWE Encryption
2.2. BFV FHE Scheme
2.3. Security Parameters
3. Overall Design
4. Implementation of Ring-LWE Lattice Cryptography Based on ChatGPT
4.1. Lattice Cryptography Algorithm and Code
- (1)
- Ring Definition: The InitRing function implements the polynomial ring , where q is the modulus, n is the polynomial degree, and the ring’s characteristic is realized through the modulus polynomial . Later, we will discuss that defining this function makes the code redundant and has no practical significance.
- (2)
- Random Polynomial Generation: The RandomPolynomial function generates a random polynomial with coefficients in the range [−q/2, q/2).
- (3)
- Center Modulus: The CenterMod function converts polynomial coefficients modulo q to the center modulus range [−q/2, q/2).
- (4)
- Key Generation: The KeyGen function generates the private key s and the public key (a,b).
- (5)
- Encryption Process: The Encrypt function encrypts the plaintext polynomial mmm using the public key (a,b) to obtain the ciphertext (c1, c2).
- (6)
- Decryption Process: The Decrypt function decrypts the ciphertext (c1, c2) using the private key s to recover the plaintext m.
4.2. Debugging the Code
4.2.1. Parameter Mismatch Issue of the MulMod Function
4.2.2. Remove the InitRing() Function
4.2.3. The Issue of Direct Indexing Without Initializing the Polynomial
4.2.4. Encoding and Decoding of Message Integers
4.2.5. Improvements to the Decryption Function Code
4.2.6. Explanation
- (1)
- From an algorithmic level, ChatGPT is familiar with the Ring-LWE encryption algorithm and can accurately describe it in a professional manner.
- (2)
- From a coding level, ChatGPT can generate complete the Ring-LWE encryption program code, but there may be compilation issues that require debugging. If you inform ChatGPT of the compilation error messages, it will provide possible solutions, although these may not always resolve the issue, indicating that ChatGPT has a certain level of code debugging ability.
- (3)
- When the problem is identified, ChatGPT consistently provides effective solutions and modified code, which is an area in which it excels.
5. Implementation of BFV Fully Homomorphic Encryption Based on ChatGPT
5.1. BFV Fully Homomorphic Encryption Algorithm and Code
- (1)
- In the key generation algorithm, there is no detailed arithmetic representation provided for calculating the public key used for key exchange; it only describes in words that a computation key needs to be generated and applied in the key switching process after the homomorphic multiplication operation.
- (2)
- The description of homomorphic multiplication is incorrect. In homomorphic multiplication, obtaining the triple ciphertext first requires multiplying by (t/q) and then rounding the result. These operations are missing in the homomorphic multiplication operation provided by ChatGPT.
- (3)
- There is a lack of description of the key exchange process.
- (4)
- In the description of noise management techniques, ChatGPT mentions using modulus switching to manage noise, which is clearly incorrect. The BFV algorithm does not use modulus switching; this technique is employed in the BGV fully homomorphic encryption scheme.
- (1)
- The center modulus function is incorrect.
- (2)
- There is a lack of functions to convert a message integer into a t-base polynomial and to convert a t-base polynomial back into an integer after decryption. This can refer to the lattice-based cryptography part in Ring LWE.
- (3)
- The key generation part is missing the generation of the public key (used for key switching).
- (4)
- After performing homomorphic addition, the center modulus reduction is not applied.
- (5)
- In the homomorphic multiplication process, there is no multiplication of the ternary ciphertext by (t/q), followed by rounding.
- (6)
- The process in the key switching function is incomplete and does not consider noise control.
- (7)
- The main function is missing calls to the homomorphic addition and multiplication functions.
- (8)
- The main function lacks output for validating the results of homomorphic addition and multiplication.
5.2. Figures, Tables, and Schemes
5.2.1. Conversion Between Message Integers and t-Base Coefficient Polynomials
5.2.2. Generation of the Relinearization Key
- Let l = int(logw(q)) + 1, and p2(s^2) = (s^2 mod q, t·s^2 mod q, …, t^(l−1)·s^2 mod q). Note that the ‘mod q’ here refers to the center modulus.
- Randomly and uniformly select a polynomial vector ‘a1’ of length ‘l’ and select a noise polynomial vector ‘e_rlk’ of length ‘l’ from a Gaussian distribution. Again, the ‘mod q’ here refers to the center modulus.
- Compute relinearization_key = p2(s^2)+ a1·s + e_rlk mod q, with the ‘mod q’ here referring to the center modulus.
5.2.3. Correction of the Homomorphic Multiplication Code
- Calculate result_c1 = round ((t/q)·(c1_1 · c1_2)) mod q.
- Calculate result_c2 = round ((t/q)·(c1_1 · c2_2 + c1_2 · c2_1)) mod q.
- Calculate result_c3 = round ((t/q)·(c2_1 · c2_2)) mod q.
5.2.4. Implementation of the Key Switching Process
- Let D2(ca3) = (ca3 mod w, int(ca3/w) mod w,…, int(ca3/w^(l − 1))) mod w). Here, “mod w” refers to the center modulus.
- Calculate cb1 = [ca2 + <D2(ca1),a1>] mod q, cb2 = [ca3 + <D2(ca1), relinearization_key>] mod q. Here, “mod q” refers to the center modulus.
- Output (cb1, cb2).
5.2.5. Code Optimization and Improvement
- (1)
- Input two integer messages, then encrypt these two integers separately, and output the two ciphertexts.
- (2)
- Next, test the homomorphic addition. Perform homomorphic addition on these two ciphertexts and output the result of the homomorphic addition.
- (3)
- Then, decrypt the result of the homomorphic addition.
- (4)
- Test homomorphic multiplication. Perform homomorphic multiplication on these two ciphertexts and output the result of the homomorphic multiplication.
- (5)
- Perform relinearization on the result of the homomorphic multiplication and output the result.
- (6)
- Decrypt and output the result.
6. Time Cost Analysis
- Logical errors: Business logic errors in the code that do not meet expectations or align with the algorithm’s intended logic.
- Runtime errors: Errors that cause the program to crash or behave abnormally during execution.
- Compilation errors: Errors that prevent the code from compiling successfully.
- (1)
- Manual Implementation
- Select three experienced developers to manually write and debug the code.
- Record the total time required for the developers to write, debug, and pass all tests.
- Document the total time taken.
- (2)
- ChatGPT-Assisted Implementation
- Use ChatGPT to generate the same algorithm code (providing specific prompts and requiring ChatGPT to generate the implementation code).
- Record the total time taken from generating the code to fixing errors and passing all tests.
7. Conclusions
Author Contributions
Funding
Data Availability Statement
Conflicts of Interest
References
- Gentry, C. Fully homomorphic encryption using ideal lattices. In Proceedings of the 41st Annual ACM Symposium on Theory of Computing, Bethesda, MD, USA, 31 May–2 June 2009; pp. 169–178. [Google Scholar]
- Brakerski, Z.; Vaikuntanathan, V. Efficient Fully Homomorphic Encryption from (Standard) LWE. In Proceedings of the 2011 IEEE 52nd Annual Symposium on Foundations of Computer Science, Palm Springs, CA, USA, 22–25 October 2011; Ostrovsky, R., Ed.; Annual IEEE Symposium on Foundations of Computer Science. IEEE Computer Society: Los Alamitos, CA, USA, 2011; pp. 97–106. [Google Scholar]
- Brakerski, Z.; Gentry, C.; Vaikuntanathan, V. (Leveled) fully homomorphic encryption without bootstrapping. In Proceedings of the 3rd Innovations in Theoretical Computer Science Conference, Cambridge, MA, USA, 8–10 January 2012; pp. 309–325. [Google Scholar]
- Brakerski, Z. Fully Homomorphic Encryption without Modulus Switching from Classical GapSVP. In Advances in Cryptology—CRYPTO 2012; Safavi-Naini, R., Canetti, R., Eds.; Lecture Notes in Computer Science; Springer: Berlin/Heidelberg, Germany, 2012; Volume 7417, pp. 868–886. [Google Scholar]
- Gentry, C.; Sahai, A.; Waters, B. Homomorphic Encryption from Learning with Errors: Conceptually Simpler, Asymptotically Faster, Attribute-Based. In Advances in Cryptology—CRYPTO 2013; Canetti, R., Garay, J., Eds.; Lecture Notes in Computer Science; Springer: Berlin/Heidelberg, Germany, 2013; Volume 8042, pp. 75–92. [Google Scholar]
- Cheon, J.H.; Kim, A.; Kim, M.; Song, Y. Homomorphic Encryption for Arithmetic of Approximate Numbers. In Advances in Cryptology—ASIACRYPT 2017; Takagi, T., Peyrin, T., Eds.; Springer International Publishing: Cham, Switzerland, 2017; pp. 409–437. [Google Scholar]
- Chillotti, I.; Gama, N.; Georgieva, M.; Izabachène, M. TFHE: Fast Fully Homomorphic Encryption Over the Torus. J. Cryptol. 2020, 33, 34–91. [Google Scholar] [CrossRef]
- Halevi, S.; Shoup, V. Algorithms in HElib. In Advances in Cryptology—CRYPTO 2014, Proceedings of the 34th Annual Cryptology Conference, Santa Barbara, CA, USA, 17–21 August 2014; Proceedings, Part I; Garay, J.A., Gennaro, R., Eds.; Springer: Berlin/Heidelberg, Germany, 2014; pp. 554–571. [Google Scholar]
- Microsoft SEAL. 2020. Available online: https://github.com/Microsoft/SEAL (accessed on 1 July 2024).
- Polyakov, Y.; Rohloff, K.; Ryan, G.W.; Cousins, D. Palisade Lattice Cryptography Library User Manual; Cybersecurity Research Center, New Jersey Institute of Technology (NJIT): Newark, NJ, USA, 2017; Technical Report No. 15. [Google Scholar]
- Shahriar, S.; Hayawi, K. Let’s have a chat! A Conversation with ChatGPT: Technology, Applications, and Limitations. In Proceedings of the Artificial Intelligence and Applications, 2024, Corfu, Greece, 27–30 June 2024; pp. 11–20. [Google Scholar]
- Dong, Y.; Jiang, X.; Jin, Z.; Li, G. Self-Collaboration Code Generation via ChatGPT. ACM Trans. Softw. Eng. Methodol. 2024, 33, 1–38. [Google Scholar] [CrossRef]
- Kwon, H.; Sim, M.; Song, G.; Lee, M.; Seo, H. Novel Approach to Cryptography Implementation Using ChatGPT. Cryptol. ePrint Arch. 2023, 606. [Google Scholar]
- Cintas-Canto, A.; Kaur, J.; Mozaffari-Kermani, M.; Azarderakhsh, R. ChatGPT vs. Lightweight Security: First Work Implementing the NIST Cryptographic Standard ASCON. arXiv 2023, arXiv:2306.08178. [Google Scholar] [CrossRef]
- Regev, O. On lattices, learning with errors, random linear codes, and cryptography. In Proceedings of the 37th Annual ACM Symposium on Theory of Computing, Baltimore, MD, USA, 22–24 May 2005; pp. 84–93. [Google Scholar]
- Fan, J.; Vercauteren, F. Somewhat Practical Fully Homomorphic Encryption. Cryptol. ePrint Arch. 2012, 144. [Google Scholar]
Security Level | n | q | σ |
---|---|---|---|
Low Security (AES-128) | 256 | 210 | 3.2 |
Medium Security (AES-192) | 512 | 212 | 3.45 |
High Security (AES-256) | 1024 | 213 | 3.7 |
Security Level | n | q | σ |
---|---|---|---|
Low Security (AES-128) | 2028 | 254 | 3.2 |
Medium Security (AES-192) | 4096 | 2109 | 3.45 |
High Security (AES-256) | 8192 | 2218 | 3.7 |
Type | Ring-LWE Lattice Cryptography | BFV Fully Homomorphic Encryption | ||
---|---|---|---|---|
Manual Implementation (h) | ChatGPT-Assisted (h) | Manual Implementation (h) | ChatGPT-Assisted (h) | |
Coding | 6 | 1 | 20 | 3 |
Debugging | 10 | 4 | 25 | 15 |
Testing | 4 | 2 | 11 | 6 |
Total Time | 20 | 7 | 56 | 24 |
Error Type | Error Description | ChatGPT’s Ability to Identify Issues | Need for Manual Intervention | ChatGPT’s Efficiency in Identifying Issues |
---|---|---|---|---|
Syntax Errors | Missing semicolons, unmatched brackets, undeclared variables, etc. | Very High: ChatGPT can accurately identify and suggest fixes | No manual intervention or minimal manual adjustments needed | Very High: ChatGPT can quickly generate corrected code |
Compiler Warnings | Type conversion warnings, unused variables, etc. | High: ChatGPT can identify and provide optimization suggestions | No manual intervention or minimal adjustments needed | High: Time required to fix warnings is short |
Algorithm Logic Errors | Errors in logic, such as incorrect loop conditions or algorithmic flow errors | Low: ChatGPT struggles with identifying high-level logical issues | High: Manual step-by-step code analysis is needed to identify issues | High: Once the issue is clearly described, ChatGPT can quickly fix it |
Mathematical Formula Implementation Errors | Errors in calculations or boundary conditions of formulas | Moderate: ChatGPT can partially detect complex mathematical errors | Low: Manual checks of mathematical correctness are partially required | High: After identifying issues, ChatGPT can quickly provide improvements |
Data Structure Design Issues | Poorly designed data structures, such as memory allocation issues or out-of-bounds access | Moderate: ChatGPT may provide some hints but cannot identify all issues | Low: Manual analysis and optimization of data structure implementation are partly required | High: With manual guidance, ChatGPT can quickly make fixes |
Runtime Errors | Issues like array out-of-bounds, null pointer access, memory leaks, etc. | High: ChatGPT can provide suggestions based on error messages | Low: Rarely requires further manual verification of the cause | High: After identifying issues, ChatGPT’s fix time is short |
Performance Optimization Issues | Algorithm inefficiency or excessive memory usage | Moderate: ChatGPT can partially identify performance bottlenecks | Moderate: Requires manual verification of the cause of the issue | High: ChatGPT can provide optimization suggestions |
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2025 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).
Share and Cite
Chen, Z.; Song, X.; Chen, L.; Liu, H. Implementation of Ring Learning-with-Errors Encryption and Brakerski–Fan–Vercauteren Fully Homomorphic Encryption Using ChatGPT. Computers 2025, 14, 440. https://doi.org/10.3390/computers14100440
Chen Z, Song X, Chen L, Liu H. Implementation of Ring Learning-with-Errors Encryption and Brakerski–Fan–Vercauteren Fully Homomorphic Encryption Using ChatGPT. Computers. 2025; 14(10):440. https://doi.org/10.3390/computers14100440
Chicago/Turabian StyleChen, Zhigang, Xinxia Song, Liqun Chen, and Hai Liu. 2025. "Implementation of Ring Learning-with-Errors Encryption and Brakerski–Fan–Vercauteren Fully Homomorphic Encryption Using ChatGPT" Computers 14, no. 10: 440. https://doi.org/10.3390/computers14100440
APA StyleChen, Z., Song, X., Chen, L., & Liu, H. (2025). Implementation of Ring Learning-with-Errors Encryption and Brakerski–Fan–Vercauteren Fully Homomorphic Encryption Using ChatGPT. Computers, 14(10), 440. https://doi.org/10.3390/computers14100440