Encryption Pyfhel减法

Encryption Pyfhel减法,encryption,Encryption,我正在尝试使用Pyfhel库对加密的整数列表执行一些操作。但在执行减法运算时,当期望值为负值时,我得到的值不同 """Import all the packages useful for the Demo. #-Pyfhel is useful to generate keys, encrypt and decrypt. #-PyPtxt is useful to tranform the input vectors into plain text objects that could be e

我正在尝试使用Pyfhel库对加密的整数列表执行一些操作。但在执行减法运算时,当期望值为负值时,我得到的值不同

"""Import all the packages useful for the Demo.
#-Pyfhel is useful to generate keys, encrypt and decrypt.
#-PyPtxt is useful to tranform the input vectors into plain text objects that could be encrypted.
#-PyCtxt is useful to tranform the plain text object in PyCtxt object that are encrypted (Cypher texts). PyCtxt can be add, multiply etc with homeomorphic operations."""
from Pyfhel import Pyfhel
from PyPtxt import PyPtxt
from PyCtxt import PyCtxt

"""Other imports useful for the demo."""
from itertools import izip
import itertools
from operator import sub
import numpy as np
import matplotlib.pyplot as plt
import sys
import argparse
import copy
import datetime
import os

#Instantiate a Pyfhel object called HE.
HE = Pyfhel()

print("******Generation of the keys for encryption******")

#Create the Key Generator parameters.
KEYGEN_PARAMS={ "p":257,      "r":1,
                "d":1,        "c":2,
                "sec":80,     "w":64,
                "L":10,       "m":-1,
                "R":3,        "s":0,
                "gens":[],    "ords":[]}

"""Print the Key Generator parameters to let the user knows how his vectors will be encrypted."""
print("  Running KeyGen with params:")
print(KEYGEN_PARAMS)

"""Generate the keys that will be use to encrypted the vectors. The generation of the keys uses the Key Generator parameters. Then print a message to inform the user that the key generation has been completed."""
HE.keyGen(KEYGEN_PARAMS)
print("  KeyGen completed")

var1 = HE.encrypt(PyPtxt([130], HE))
var2 = HE.encrypt(PyPtxt([10], HE))

xyz = var2 - var1
abc = HE.decrypt(xyz)
abc[0][0] # 137
print(abc[0][0] - 257) # output: -120
在上面的代码中,我注意到,如果在生成键时减去使用的'p'值,我会得到预期的输出,但这没有多大帮助,尤其是当差值大于257时

有人能告诉我这是否是预期的行为,或者可以做些什么来获得预期的输出吗

谢谢


无法添加相关标记,但它是使用Pyfhel库进行同态加密的,HElib的Python实现[0][0]-p会产生预期的输出,因为我们使用的是模p

我可以向您确认这是预期的行为。您执行的所有操作都是模p,因此加密值将始终保持在间隔[0,p-1]内。请注意,尽管Pyfhel支持减法,但在其当前实现中不支持负数

然而,一项重大升级正在进行中,这将解决这个问题。请继续收看