我有一个简单的 BFV 上下文,我所做的只是 SquareInplace。我得到错误的答案取决于我的输入值以及我如何从明文中提取值。
/
| 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
因此,当输入 4 个明文 eval 到 10 但第一个索引是 16 的正确答案时。
然后当我输入 10 时,相同的代码,明文 eval 为 100,第一个索引是 256。
我应该如何评估纯文本,您是否发现我的设置有任何问题?
代码
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();
}
希望这足以给你这个想法。