1

I have a simple BFV context and all I'm doing is SquareInplace. I get the wrong answer depending on my input values and depending on how I pull the value from plaintext.

/
| Encryption parameters:
|   Scheme: BFV
|   PolyModulusDegree: 8192
|   CoeffModulus size: 218 (43 + 43 + 44 + 44 + 44) bits
|   PlainModulus: 4173162863
\
4^2
plaintext:10
plaintext[0]:16
CoeffCount:1

-----------

/
| Encryption parameters:
|   Scheme: BFV
|   PolyModulusDegree: 8192
|   CoeffModulus size: 218 (43 + 43 + 44 + 44 + 44) bits
|   PlainModulus: 4173162863
\
10^2
plaintext:100
plaintext[0]:256
CoeffCount:1

So when have a input of 4 plaintext evals to 10 but the first index is the correct answer of 16.

Then same code when I have an input of 10, plaintext evals to 100 and the first index is 256.

How should I be evaluating Plaintext and do you see any issues with my set up?

Code

        public BFVController()
        {
            _sealContext = Utilities.GetContext();


            // Initialize key generator and encryptor
            // Initialize key Generator that will be use to get the Public and Secret keys
            _keyGenerator = new KeyGenerator(_sealContext);
            // Initializing encryptor
            _keyGenerator.CreatePublicKey(out _public);
            _encryptor = new Encryptor(_sealContext, _public);
            // Initialize evaluator
            _evaluator = new Evaluator(_sealContext);
            
            _keyGenerator.CreateRelinKeys(out RelinKeys rks);
            relinKeys = rks;
        }
        public IActionResult test([FromBody] int value)
        {
         
            var result = Utilities.CreateCiphertextFromInt(value, _encryptor);
            
            var resultSq= new Ciphertext();
            _evaluator.SquareInplace(result);
            Console.WriteLine($"{value}^2");
            var decryptor = new Decryptor(_sealContext,_keyGenerator.SecretKey);
          
        
            
            var pt = new Plaintext();
            decryptor.Decrypt(result,pt);
            var answer = Utilities.PlaintextToString(pt);
            return Ok(answer);
        }
        
    }

-----
Utilities.cs

public static SEALContext GetContext()
        {

            ulong mod = 8192;
            var encryptionParameters = new EncryptionParameters(SchemeType.BFV)
            {
                PlainModulus = new Modulus(4173162863),
                PolyModulusDegree = mod,
                CoeffModulus = CoeffModulus.BFVDefault(mod)
            };
          
            

            Debug.WriteLine("[COMMON]: Successfully created context");

            Console.WriteLine("Set encryption parameters and print");
            
            var context = new SEALContext(encryptionParameters);
            PrintParameters(context);
            return context;
        }

        public static Ciphertext CreateCiphertextFromInt(int val, Encryptor encryptor)
        {  
            var value = Convert.ToUInt64(val);
            var plaintext = new Plaintext(value.ToString());
            var ciphertext = new Ciphertext();
            encryptor.Encrypt(plaintext, ciphertext);
            return ciphertext;
        }
        public static string PlaintextToString(Plaintext pt)
        {
         
            Console.WriteLine($"plaintext:{pt}");
            Console.WriteLine($"plaintext[0]:{pt[0]}");
            Console.WriteLine($"CoeffCount:{pt.CoeffCount}");
            return pt.ToString();
        }

Hopefully thats enough to give you the idea.

0 Answers0