|
@@ -5,13 +5,13 @@ def main():
|
|
SwitchCKKSToFHEW()
|
|
SwitchCKKSToFHEW()
|
|
SwitchFHEWtoCKKS()
|
|
SwitchFHEWtoCKKS()
|
|
FloorViaSchemeSwitching()
|
|
FloorViaSchemeSwitching()
|
|
- # FuncViaSchemeSwitching()
|
|
|
|
- # PolyViaSchemeSwitching()
|
|
|
|
|
|
+ FuncViaSchemeSwitching()
|
|
|
|
+ PolyViaSchemeSwitching()
|
|
ComparisonViaSchemeSwitching()
|
|
ComparisonViaSchemeSwitching()
|
|
ArgminViaSchemeSwitching()
|
|
ArgminViaSchemeSwitching()
|
|
ArgminViaSchemeSwitchingAlt()
|
|
ArgminViaSchemeSwitchingAlt()
|
|
- #ArgminViaSchemeSwitchingUnit()
|
|
|
|
- #ArgminViaSchemeSwitchingAltUnit()
|
|
|
|
|
|
+ ArgminViaSchemeSwitchingUnit()
|
|
|
|
+ ArgminViaSchemeSwitchingAltUnit()
|
|
|
|
|
|
def SwitchCKKSToFHEW():
|
|
def SwitchCKKSToFHEW():
|
|
|
|
|
|
@@ -57,9 +57,18 @@ def SwitchCKKSToFHEW():
|
|
keys = cc.KeyGen()
|
|
keys = cc.KeyGen()
|
|
|
|
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
- FHEWparams = cc.EvalCKKStoFHEWSetup(sl, slBin, False, logQ_ccLWE, False, slots)
|
|
|
|
- ccLWE = FHEWparams[0]
|
|
|
|
- privateKeyFHEW = FHEWparams[1]
|
|
|
|
|
|
+ # FHEWparams = cc.EvalCKKStoFHEWSetup(sl, slBin, False, logQ_ccLWE, False, slots)
|
|
|
|
+ params = SchSwchParams()
|
|
|
|
+ params.SetSecurityLevelCKKS(sl)
|
|
|
|
+ params.SetSecurityLevelFHEW(slBin)
|
|
|
|
+ params.SetCtxtModSizeFHEWLargePrec(logQ_ccLWE)
|
|
|
|
+ params.SetNumSlotsCKKS(slots)
|
|
|
|
+
|
|
|
|
+ privateKeyFHEW = cc.EvalCKKStoFHEWSetup(params)
|
|
|
|
+ ccLWE = cc.GetBinCCForSchemeSwitch()
|
|
|
|
+
|
|
|
|
+ # ccLWE = FHEWparams[0]
|
|
|
|
+ # privateKeyFHEW = FHEWparams[1]
|
|
cc.EvalCKKStoFHEWKeyGen(keys, privateKeyFHEW)
|
|
cc.EvalCKKStoFHEWKeyGen(keys, privateKeyFHEW)
|
|
|
|
|
|
print(f"FHEW scheme is using a lattice parameter {ccLWE.Getn()},")
|
|
print(f"FHEW scheme is using a lattice parameter {ccLWE.Getn()},")
|
|
@@ -67,18 +76,13 @@ def SwitchCKKSToFHEW():
|
|
print(f"and modulus q {ccLWE.Getq()}\n")
|
|
print(f"and modulus q {ccLWE.Getq()}\n")
|
|
|
|
|
|
# Compute the scaling factor to decrypt correctly in FHEW; the LWE mod switch is performed on the ciphertext at the last level
|
|
# Compute the scaling factor to decrypt correctly in FHEW; the LWE mod switch is performed on the ciphertext at the last level
|
|
- modulus_CKKS_from = cc.GetModulusCKKS()
|
|
|
|
-
|
|
|
|
pLWE1 = ccLWE.GetMaxPlaintextSpace() # Small precision
|
|
pLWE1 = ccLWE.GetMaxPlaintextSpace() # Small precision
|
|
modulus_LWE = 1 << logQ_ccLWE
|
|
modulus_LWE = 1 << logQ_ccLWE
|
|
beta = ccLWE.GetBeta()
|
|
beta = ccLWE.GetBeta()
|
|
pLWE2 = modulus_LWE / (2*beta) # Large precision
|
|
pLWE2 = modulus_LWE / (2*beta) # Large precision
|
|
|
|
|
|
- scFactor = cc.GetScalingFactorReal(0)
|
|
|
|
- # if (cc.GetScalingTechnique() == FLEXIBLEAUTOEXT):
|
|
|
|
- # scFactor = cc.GetScalingFactorReal(1)
|
|
|
|
- scale1 = modulus_CKKS_from / (scFactor * pLWE1)
|
|
|
|
- scale2 = modulus_CKKS_from / (scFactor * pLWE2)
|
|
|
|
|
|
+ scale1 = 1 / pLWE1
|
|
|
|
+ scale2 = 1 / pLWE2
|
|
|
|
|
|
# Perform the precomputation for switching
|
|
# Perform the precomputation for switching
|
|
cc.EvalCKKStoFHEWPrecompute(scale1)
|
|
cc.EvalCKKStoFHEWPrecompute(scale1)
|
|
@@ -226,7 +230,7 @@ def SwitchFHEWtoCKKS():
|
|
x2.extend(zeros)
|
|
x2.extend(zeros)
|
|
|
|
|
|
# Encrypt
|
|
# Encrypt
|
|
- # Encrypted nder small plaintext modulus p = 4 and ciphertext modulus:
|
|
|
|
|
|
+ # Encrypted under small plaintext modulus p = 4 and ciphertext modulus:
|
|
ctxtsLWE1 = [ccLWE.Encrypt(lwesk, x1[i]) for i in range(slots)]
|
|
ctxtsLWE1 = [ccLWE.Encrypt(lwesk, x1[i]) for i in range(slots)]
|
|
# Encrypted under larger plaintext modulus p = 16 but small ciphertext modulus:
|
|
# Encrypted under larger plaintext modulus p = 16 but small ciphertext modulus:
|
|
ctxtsLWE2 = [ccLWE.Encrypt(lwesk, x1[i], FRESH, pLWE1) for i in range(slots)]
|
|
ctxtsLWE2 = [ccLWE.Encrypt(lwesk, x1[i], FRESH, pLWE1) for i in range(slots)]
|
|
@@ -314,13 +318,17 @@ def FloorViaSchemeSwitching():
|
|
|
|
|
|
# Generate encryption keys.
|
|
# Generate encryption keys.
|
|
keys = cc.KeyGen()
|
|
keys = cc.KeyGen()
|
|
-
|
|
|
|
|
|
+
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
- arbFunc = False
|
|
|
|
- FHEWparams = cc.EvalSchemeSwitchingSetup(sl, slBin, arbFunc, logQ_ccLWE, False, slots)
|
|
|
|
-
|
|
|
|
- ccLWE = FHEWparams[0]
|
|
|
|
- privateKeyFHEW = FHEWparams[1]
|
|
|
|
|
|
+ params = SchSwchParams()
|
|
|
|
+ params.SetSecurityLevelCKKS(sl)
|
|
|
|
+ params.SetSecurityLevelFHEW(slBin)
|
|
|
|
+ params.SetCtxtModSizeFHEWLargePrec(logQ_ccLWE)
|
|
|
|
+ params.SetNumSlotsCKKS(slots)
|
|
|
|
+ params.SetNumValues(slots)
|
|
|
|
+
|
|
|
|
+ privateKeyFHEW = cc.EvalSchemeSwitchingSetup(params)
|
|
|
|
+ ccLWE = cc.GetBinCCForSchemeSwitch()
|
|
|
|
|
|
cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW)
|
|
cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW)
|
|
|
|
|
|
@@ -336,10 +344,7 @@ def FloorViaSchemeSwitching():
|
|
beta = ccLWE.GetBeta()
|
|
beta = ccLWE.GetBeta()
|
|
pLWE = int(modulus_LWE / (2 * beta)) # Large precision
|
|
pLWE = int(modulus_LWE / (2 * beta)) # Large precision
|
|
|
|
|
|
- scFactor = cc.GetScalingFactorReal(0)
|
|
|
|
- if cc.GetScalingTechnique() == FLEXIBLEAUTOEXT:
|
|
|
|
- scFactor = cc.GetScalingFactorReal(1)
|
|
|
|
- scaleCF = int(modulus_CKKS_from) / (scFactor * pLWE)
|
|
|
|
|
|
+ scaleCF = 1.0 / pLWE
|
|
|
|
|
|
cc.EvalCKKStoFHEWPrecompute(scaleCF)
|
|
cc.EvalCKKStoFHEWPrecompute(scaleCF)
|
|
|
|
|
|
@@ -348,14 +353,14 @@ def FloorViaSchemeSwitching():
|
|
x1 = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]
|
|
x1 = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]
|
|
|
|
|
|
# Encoding as plaintexts
|
|
# Encoding as plaintexts
|
|
- ptxt1 = cc.MakeCKKSPackedPlaintext(x1, 1, 0)#, None)
|
|
|
|
|
|
+ ptxt1 = cc.MakeCKKSPackedPlaintext(x1, 1, 0, None)#, None)
|
|
|
|
|
|
# Encrypt the encoded vectors
|
|
# Encrypt the encoded vectors
|
|
c1 = cc.Encrypt(keys.publicKey, ptxt1)
|
|
c1 = cc.Encrypt(keys.publicKey, ptxt1)
|
|
|
|
|
|
# Step 4: Scheme switching from CKKS to FHEW
|
|
# Step 4: Scheme switching from CKKS to FHEW
|
|
- cTemp = cc.EvalCKKStoFHEW(c1)
|
|
|
|
-
|
|
|
|
|
|
+ cTemp = cc.EvalCKKStoFHEW(c1, slots)
|
|
|
|
+
|
|
# Step 5: Evaluate the floor function
|
|
# Step 5: Evaluate the floor function
|
|
bits = 2
|
|
bits = 2
|
|
|
|
|
|
@@ -417,10 +422,16 @@ def FuncViaSchemeSwitching():
|
|
keys = cc.KeyGen()
|
|
keys = cc.KeyGen()
|
|
|
|
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
- FHEWparams = cc.EvalSchemeSwitchingSetup(sl, slBin, arbFunc, logQ_ccLWE, False, slots)
|
|
|
|
-
|
|
|
|
- ccLWE = FHEWparams[0]
|
|
|
|
- privateKeyFHEW = FHEWparams[1]
|
|
|
|
|
|
+ params = SchSwchParams()
|
|
|
|
+ params.SetSecurityLevelCKKS(sl)
|
|
|
|
+ params.SetSecurityLevelFHEW(slBin)
|
|
|
|
+ params.SetArbitraryFunctionEvaluation(True)
|
|
|
|
+ params.SetCtxtModSizeFHEWLargePrec(logQ_ccLWE)
|
|
|
|
+ params.SetNumSlotsCKKS(slots)
|
|
|
|
+ params.SetNumValues(slots)
|
|
|
|
+
|
|
|
|
+ privateKeyFHEW = cc.EvalSchemeSwitchingSetup(params)
|
|
|
|
+ ccLWE = cc.GetBinCCForSchemeSwitch()
|
|
|
|
|
|
cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW)
|
|
cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW)
|
|
|
|
|
|
@@ -430,12 +441,8 @@ def FuncViaSchemeSwitching():
|
|
print(f"FHEW scheme is using lattice parameter {ccLWE.Getn()},\n logQ {logQ_ccLWE},\n and modulus q {ccLWE.Getq()}\n")
|
|
print(f"FHEW scheme is using lattice parameter {ccLWE.Getn()},\n logQ {logQ_ccLWE},\n and modulus q {ccLWE.Getq()}\n")
|
|
|
|
|
|
# Set the scaling factor to be able to decrypt; the LWE mod switch is performed on the ciphertext at the last level
|
|
# Set the scaling factor to be able to decrypt; the LWE mod switch is performed on the ciphertext at the last level
|
|
- modulus_CKKS_from = cc.GetModulusCKKS()
|
|
|
|
pLWE = ccLWE.GetMaxPlaintextSpace() # Small precision because GenerateLUTviaFunction needs p < q
|
|
pLWE = ccLWE.GetMaxPlaintextSpace() # Small precision because GenerateLUTviaFunction needs p < q
|
|
- scFactor = cc.GetScalingFactorReal(0)
|
|
|
|
- if cc.GetScalingTechnique() == FLEXIBLEAUTOEXT:
|
|
|
|
- scFactor = cc.GetScalingFactorReal(1)
|
|
|
|
- scaleCF = modulus_CKKS_from / (scFactor * pLWE)
|
|
|
|
|
|
+ scaleCF = 1.0 / pLWE
|
|
|
|
|
|
cc.EvalCKKStoFHEWPrecompute(scaleCF)
|
|
cc.EvalCKKStoFHEWPrecompute(scaleCF)
|
|
|
|
|
|
@@ -461,7 +468,7 @@ def FuncViaSchemeSwitching():
|
|
c1 = cc.Encrypt(keys.publicKey, ptxt1)
|
|
c1 = cc.Encrypt(keys.publicKey, ptxt1)
|
|
|
|
|
|
# Step 5: Scheme switching from CKKS to FHEW
|
|
# Step 5: Scheme switching from CKKS to FHEW
|
|
- cTemp = cc.EvalCKKStoFHEW(c1)
|
|
|
|
|
|
+ cTemp = cc.EvalCKKStoFHEW(c1, slots)
|
|
|
|
|
|
print(f"Input x1: {ptxt1.GetRealPackedValue()}")
|
|
print(f"Input x1: {ptxt1.GetRealPackedValue()}")
|
|
print("FHEW decryption: ", end="")
|
|
print("FHEW decryption: ", end="")
|
|
@@ -487,7 +494,7 @@ def FuncViaSchemeSwitching():
|
|
|
|
|
|
plaintextDec2 = cc.Decrypt(keys.secretKey, cTemp2)
|
|
plaintextDec2 = cc.Decrypt(keys.secretKey, cTemp2)
|
|
plaintextDec2.SetLength(slots)
|
|
plaintextDec2.SetLength(slots)
|
|
- print(f"\nSwitched decryption modulus_LWE mod {pLWE}\nwoeks only for messages << p: {plaintextDec2}")
|
|
|
|
|
|
+ print(f"\nSwitched decryption modulus_LWE mod {pLWE}\nworks only for messages << p: {plaintextDec2}")
|
|
|
|
|
|
# Transform through arcsine
|
|
# Transform through arcsine
|
|
cTemp2 = cc.EvalFHEWtoCKKS(cFunc, slots, slots, 4, 0, 2)
|
|
cTemp2 = cc.EvalFHEWtoCKKS(cFunc, slots, slots, 4, 0, 2)
|
|
@@ -545,27 +552,27 @@ def PolyViaSchemeSwitching():
|
|
keys = cc.KeyGen()
|
|
keys = cc.KeyGen()
|
|
|
|
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
- FHEWparams = cc.EvalSchemeSwitchingSetup(sl, slBin, False, logQ_ccLWE, False, slots)
|
|
|
|
-
|
|
|
|
- ccLWE = FHEWparams[0]
|
|
|
|
- privateKeyFHEW = FHEWparams[1]
|
|
|
|
|
|
+ params = SchSwchParams()
|
|
|
|
+ params.SetSecurityLevelCKKS(sl)
|
|
|
|
+ params.SetSecurityLevelFHEW(slBin)
|
|
|
|
+ params.SetCtxtModSizeFHEWLargePrec(logQ_ccLWE)
|
|
|
|
+ params.SetNumSlotsCKKS(slots)
|
|
|
|
+ params.SetNumValues(slots)
|
|
|
|
+
|
|
|
|
+ privateKeyFHEW = cc.EvalSchemeSwitchingSetup(params)
|
|
|
|
+ ccLWE = cc.GetBinCCForSchemeSwitch()
|
|
|
|
|
|
- # Step 3: Precompute the necessary keys and information for switching from FHEW to CKKS
|
|
|
|
- cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW, slots)
|
|
|
|
|
|
+ cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW)
|
|
|
|
|
|
print(f"FHEW scheme is using lattice parameter {ccLWE.Getn()},\n logQ {logQ_ccLWE},\n and modulus q {ccLWE.Getq()}\n")
|
|
print(f"FHEW scheme is using lattice parameter {ccLWE.Getn()},\n logQ {logQ_ccLWE},\n and modulus q {ccLWE.Getq()}\n")
|
|
|
|
|
|
pLWE1 = ccLWE.GetMaxPlaintextSpace() # Small precision
|
|
pLWE1 = ccLWE.GetMaxPlaintextSpace() # Small precision
|
|
modulus_LWE = 1 << logQ_ccLWE
|
|
modulus_LWE = 1 << logQ_ccLWE
|
|
beta = ccLWE.GetBeta()
|
|
beta = ccLWE.GetBeta()
|
|
- pLWE2 = modulus_LWE // (2 * beta) # Large precision
|
|
|
|
|
|
+ pLWE2 = int(modulus_LWE / (2 * beta)) # Large precision
|
|
|
|
|
|
- modulus_from_CKKS = cc.GetModulusCKKS()
|
|
|
|
- scFactor = cc.GetScalingFactorReal(0)
|
|
|
|
- if cc.GetScalingTechnique() == FLEXIBLEAUTOEXT:
|
|
|
|
- scFactor = cc.GetScalingFactorReal(1)
|
|
|
|
- scale1 = modulus_from_CKKS // (scFactor * pLWE1)
|
|
|
|
- scale2 = modulus_from_CKKS // (scFactor * pLWE2)
|
|
|
|
|
|
+ scale1 = 1.0 / pLWE1
|
|
|
|
+ scale2 = 1.0 / pLWE2
|
|
|
|
|
|
# Generate keys for the CKKS intermediate computation
|
|
# Generate keys for the CKKS intermediate computation
|
|
cc.EvalMultKeyGen(keys.secretKey)
|
|
cc.EvalMultKeyGen(keys.secretKey)
|
|
@@ -636,10 +643,6 @@ def PolyViaSchemeSwitching():
|
|
print("\n")
|
|
print("\n")
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
def ComparisonViaSchemeSwitching():
|
|
def ComparisonViaSchemeSwitching():
|
|
print("\n-----ComparisonViaSchemeSwitching-----\n")
|
|
print("\n-----ComparisonViaSchemeSwitching-----\n")
|
|
print("Output precision is only wrt the operations in CKKS after switching back.\n")
|
|
print("Output precision is only wrt the operations in CKKS after switching back.\n")
|
|
@@ -686,11 +689,15 @@ def ComparisonViaSchemeSwitching():
|
|
keys = cc.KeyGen()
|
|
keys = cc.KeyGen()
|
|
|
|
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
- FHEWparams = cc.EvalSchemeSwitchingSetup(sl, slBin, False, logQ_ccLWE, False, slots)
|
|
|
|
-
|
|
|
|
- ccLWE = FHEWparams[0]
|
|
|
|
- privateKeyFHEW = FHEWparams[1]
|
|
|
|
- ccLWE.BTKeyGen(privateKeyFHEW)
|
|
|
|
|
|
+ params = SchSwchParams()
|
|
|
|
+ params.SetSecurityLevelCKKS(sl)
|
|
|
|
+ params.SetSecurityLevelFHEW(slBin)
|
|
|
|
+ params.SetCtxtModSizeFHEWLargePrec(logQ_ccLWE)
|
|
|
|
+ params.SetNumSlotsCKKS(slots)
|
|
|
|
+ params.SetNumValues(slots)
|
|
|
|
+
|
|
|
|
+ privateKeyFHEW = cc.EvalSchemeSwitchingSetup(params)
|
|
|
|
+ ccLWE = cc.GetBinCCForSchemeSwitch()
|
|
|
|
|
|
cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW)
|
|
cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW)
|
|
|
|
|
|
@@ -700,14 +707,10 @@ def ComparisonViaSchemeSwitching():
|
|
pLWE1 = ccLWE.GetMaxPlaintextSpace() # Small precision
|
|
pLWE1 = ccLWE.GetMaxPlaintextSpace() # Small precision
|
|
modulus_LWE = 1 << logQ_ccLWE
|
|
modulus_LWE = 1 << logQ_ccLWE
|
|
beta = ccLWE.GetBeta()
|
|
beta = ccLWE.GetBeta()
|
|
- pLWE2 = modulus_LWE // (2 * beta) # Large precision
|
|
|
|
|
|
+ pLWE2 = int(modulus_LWE / (2 * beta)) # Large precision
|
|
|
|
|
|
- scaleSignFHEW = 1
|
|
|
|
- init_level = 0
|
|
|
|
- if cc.GetScalingTechnique() == FLEXIBLEAUTOEXT:
|
|
|
|
- init_level = 1
|
|
|
|
-
|
|
|
|
- cc.EvalCompareSwitchPrecompute(pLWE2, init_level, scaleSignFHEW)
|
|
|
|
|
|
+ scaleSignFHEW = 1.0
|
|
|
|
+ cc.EvalCompareSwitchPrecompute(pLWE2, scaleSignFHEW)
|
|
|
|
|
|
# Step 3: Encoding and encryption of inputs
|
|
# Step 3: Encoding and encryption of inputs
|
|
x1 = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]
|
|
x1 = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]
|
|
@@ -756,10 +759,10 @@ def ComparisonViaSchemeSwitching():
|
|
print()
|
|
print()
|
|
|
|
|
|
# Step 5'': Direct comparison via CKKS->FHEW->CKKS
|
|
# Step 5'': Direct comparison via CKKS->FHEW->CKKS
|
|
- cResult = cc.EvalCompareSchemeSwitching(c1, c2, slots, 0, scaleSignFHEW)
|
|
|
|
|
|
+ cResult = cc.EvalCompareSchemeSwitching(c1, c2, slots, slots)
|
|
plaintextDec3 = cc.Decrypt(keys.secretKey, cResult)
|
|
plaintextDec3 = cc.Decrypt(keys.secretKey, cResult)
|
|
plaintextDec3.SetLength(slots)
|
|
plaintextDec3.SetLength(slots)
|
|
- print(f"Decrypted swutched result: {plaintextDec3}\n")
|
|
|
|
|
|
+ print(f"Decrypted switched result: {plaintextDec3}\n")
|
|
|
|
|
|
|
|
|
|
def ArgminViaSchemeSwitching():
|
|
def ArgminViaSchemeSwitching():
|
|
@@ -809,11 +812,18 @@ def ArgminViaSchemeSwitching():
|
|
keys = cc.KeyGen()
|
|
keys = cc.KeyGen()
|
|
|
|
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
- FHEWparams = cc.EvalSchemeSwitchingSetup(sl, slBin, arbFunc, logQ_ccLWE, False, slots)
|
|
|
|
- ccLWE = FHEWparams[0]
|
|
|
|
- privateKeyFHEW = FHEWparams[1]
|
|
|
|
|
|
+ params = SchSwchParams()
|
|
|
|
+ params.SetSecurityLevelCKKS(sl)
|
|
|
|
+ params.SetSecurityLevelFHEW(slBin)
|
|
|
|
+ params.SetCtxtModSizeFHEWLargePrec(logQ_ccLWE)
|
|
|
|
+ params.SetNumSlotsCKKS(slots)
|
|
|
|
+ params.SetNumValues(numValues)
|
|
|
|
+ params.SetComputeArgmin(True)
|
|
|
|
+
|
|
|
|
+ privateKeyFHEW = cc.EvalSchemeSwitchingSetup(params)
|
|
|
|
+ ccLWE = cc.GetBinCCForSchemeSwitch()
|
|
|
|
|
|
- cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW, numValues, oneHot)
|
|
|
|
|
|
+ cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW)
|
|
|
|
|
|
print(f"FHEW scheme is using lattice parameter {ccLWE.Getn()},\n logQ {logQ_ccLWE},\n and modulus q {ccLWE.Getq()}\n")
|
|
print(f"FHEW scheme is using lattice parameter {ccLWE.Getn()},\n logQ {logQ_ccLWE},\n and modulus q {ccLWE.Getq()}\n")
|
|
|
|
|
|
@@ -821,12 +831,9 @@ def ArgminViaSchemeSwitching():
|
|
scaleSign = 512
|
|
scaleSign = 512
|
|
modulus_LWE = 1 << logQ_ccLWE
|
|
modulus_LWE = 1 << logQ_ccLWE
|
|
beta = ccLWE.GetBeta()
|
|
beta = ccLWE.GetBeta()
|
|
- pLWE = modulus_LWE // (2 * beta) # Large precision
|
|
|
|
|
|
+ pLWE = int(modulus_LWE / (2 * beta)) # Large precision
|
|
|
|
|
|
- init_level = 0
|
|
|
|
- if cc.GetScalingTechnique() == FLEXIBLEAUTOEXT:
|
|
|
|
- init_level = 1
|
|
|
|
- cc.EvalCompareSwitchPrecompute(pLWE, init_level, scaleSign)
|
|
|
|
|
|
+ cc.EvalCompareSwitchPrecompute(pLWE, scaleSign)
|
|
|
|
|
|
# Step 3: Encoding and encryption of inputs
|
|
# Step 3: Encoding and encryption of inputs
|
|
x1 = [-1.125, -1.12, 5.0, 6.0, -1.0, 2.0, 8.0, -1.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.25, 15.30]
|
|
x1 = [-1.125, -1.12, 5.0, 6.0, -1.0, 2.0, 8.0, -1.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.25, 15.30]
|
|
@@ -839,7 +846,7 @@ def ArgminViaSchemeSwitching():
|
|
c1 = cc.Encrypt(keys.publicKey, ptxt1)
|
|
c1 = cc.Encrypt(keys.publicKey, ptxt1)
|
|
|
|
|
|
# Step 4: Argmin evaluation
|
|
# Step 4: Argmin evaluation
|
|
- result = cc.EvalMinSchemeSwitching(c1, keys.publicKey, numValues, slots, oneHot)
|
|
|
|
|
|
+ result = cc.EvalMinSchemeSwitching(c1, keys.publicKey, numValues, slots)
|
|
|
|
|
|
ptxtMin = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMin = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMin.SetLength(1)
|
|
ptxtMin.SetLength(1)
|
|
@@ -853,7 +860,7 @@ def ArgminViaSchemeSwitching():
|
|
ptxtMin.SetLength(1)
|
|
ptxtMin.SetLength(1)
|
|
print("Argmin: ", ptxtMin)
|
|
print("Argmin: ", ptxtMin)
|
|
|
|
|
|
- result = cc.EvalMaxSchemeSwitching(c1, keys.publicKey, numValues, slots, oneHot)
|
|
|
|
|
|
+ result = cc.EvalMaxSchemeSwitching(c1, keys.publicKey, numValues, slots)
|
|
|
|
|
|
ptxtMax = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMax = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMax.SetLength(1)
|
|
ptxtMax.SetLength(1)
|
|
@@ -909,27 +916,30 @@ def ArgminViaSchemeSwitchingAlt():
|
|
print(f"number of slots {slots}, and supports a multiplicative depth of {multDepth}\n")
|
|
print(f"number of slots {slots}, and supports a multiplicative depth of {multDepth}\n")
|
|
|
|
|
|
keys = cc.KeyGen()
|
|
keys = cc.KeyGen()
|
|
-
|
|
|
|
|
|
+
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
- FHEWparams = cc.EvalSchemeSwitchingSetup(sl, slBin, arbFunc, logQ_ccLWE, False, slots)
|
|
|
|
- ccLWE = FHEWparams[0]
|
|
|
|
- privateKeyFHEW = FHEWparams[1]
|
|
|
|
|
|
+ params = SchSwchParams()
|
|
|
|
+ params.SetSecurityLevelCKKS(sl)
|
|
|
|
+ params.SetSecurityLevelFHEW(slBin)
|
|
|
|
+ params.SetCtxtModSizeFHEWLargePrec(logQ_ccLWE)
|
|
|
|
+ params.SetNumSlotsCKKS(slots)
|
|
|
|
+ params.SetNumValues(numValues)
|
|
|
|
+ params.SetComputeArgmin(True)
|
|
|
|
+ params.SetUseAltArgmin(True)
|
|
|
|
+
|
|
|
|
+ privateKeyFHEW = cc.EvalSchemeSwitchingSetup(params)
|
|
|
|
+ ccLWE = cc.GetBinCCForSchemeSwitch()
|
|
|
|
|
|
- cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW, numValues, oneHot, alt)
|
|
|
|
|
|
+ cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW)
|
|
|
|
|
|
print(f"FHEW scheme is using lattice parameter {ccLWE.Getn()},\n logQ {logQ_ccLWE},\n and modulus q {ccLWE.Getq()}\n")
|
|
print(f"FHEW scheme is using lattice parameter {ccLWE.Getn()},\n logQ {logQ_ccLWE},\n and modulus q {ccLWE.Getq()}\n")
|
|
|
|
|
|
scaleSign = 512
|
|
scaleSign = 512
|
|
modulus_LWE = 1 << logQ_ccLWE
|
|
modulus_LWE = 1 << logQ_ccLWE
|
|
beta = ccLWE.GetBeta()
|
|
beta = ccLWE.GetBeta()
|
|
- pLWE = modulus_LWE // (2 * beta)
|
|
|
|
|
|
+ pLWE = int(modulus_LWE / (2 * beta))
|
|
|
|
|
|
- init_level = 0
|
|
|
|
- if cc.GetScalingTechnique() == FLEXIBLEAUTOEXT:
|
|
|
|
- init_level = 1
|
|
|
|
- cc.EvalCompareSwitchPrecompute(pLWE, init_level, scaleSign)
|
|
|
|
- # But we can also include the scaleSign in pLWE (here we use the fact both pLWE and scaleSign are powers of two)
|
|
|
|
- # cc.EvalCompareSwitchPrecompute(pLWE / scaleSign, init_level, 1)
|
|
|
|
|
|
+ cc.EvalCompareSwitchPrecompute(pLWE, scaleSign)
|
|
|
|
|
|
# Step 3: Encoding and encryption of inputs
|
|
# Step 3: Encoding and encryption of inputs
|
|
|
|
|
|
@@ -947,7 +957,7 @@ def ArgminViaSchemeSwitchingAlt():
|
|
c1 = cc.Encrypt(keys.publicKey, ptxt1)
|
|
c1 = cc.Encrypt(keys.publicKey, ptxt1)
|
|
|
|
|
|
# Step 4: Argmin evaluation
|
|
# Step 4: Argmin evaluation
|
|
- result = cc.EvalMinSchemeSwitchingAlt(c1, keys.publicKey, numValues, slots, oneHot)
|
|
|
|
|
|
+ result = cc.EvalMinSchemeSwitchingAlt(c1, keys.publicKey, numValues, slots)
|
|
|
|
|
|
ptxtMin = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMin = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMin.SetLength(1)
|
|
ptxtMin.SetLength(1)
|
|
@@ -961,7 +971,7 @@ def ArgminViaSchemeSwitchingAlt():
|
|
ptxtMin.SetLength(1)
|
|
ptxtMin.SetLength(1)
|
|
print("Argmin: ", ptxtMin)
|
|
print("Argmin: ", ptxtMin)
|
|
|
|
|
|
- result = cc.EvalMaxSchemeSwitchingAlt(c1, keys.publicKey, numValues, slots, oneHot)
|
|
|
|
|
|
+ result = cc.EvalMaxSchemeSwitchingAlt(c1, keys.publicKey, numValues, slots)
|
|
|
|
|
|
ptxtMax = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMax = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMax.SetLength(1)
|
|
ptxtMax.SetLength(1)
|
|
@@ -1023,22 +1033,23 @@ def ArgminViaSchemeSwitchingUnit():
|
|
keys = cc.KeyGen()
|
|
keys = cc.KeyGen()
|
|
|
|
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
- FHEWparams = cc.EvalSchemeSwitchingSetup(sl, slBin, arbFunc, logQ_ccLWE, False, slots)
|
|
|
|
-
|
|
|
|
- ccLWE = FHEWparams[0]
|
|
|
|
- privateKeyFHEW = FHEWparams[1]
|
|
|
|
|
|
+ params = SchSwchParams()
|
|
|
|
+ params.SetSecurityLevelCKKS(sl)
|
|
|
|
+ params.SetSecurityLevelFHEW(slBin)
|
|
|
|
+ params.SetCtxtModSizeFHEWLargePrec(logQ_ccLWE)
|
|
|
|
+ params.SetNumSlotsCKKS(slots)
|
|
|
|
+ params.SetNumValues(numValues)
|
|
|
|
+ params.SetComputeArgmin(True)
|
|
|
|
+
|
|
|
|
+ privateKeyFHEW = cc.EvalSchemeSwitchingSetup(params)
|
|
|
|
+ ccLWE = cc.GetBinCCForSchemeSwitch()
|
|
|
|
|
|
- cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW, numValues, oneHot)
|
|
|
|
|
|
+ cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW)
|
|
|
|
|
|
print(f"FHEW scheme is using lattice parameter {ccLWE.Getn()},\n logQ {logQ_ccLWE},\n and modulus q {ccLWE.Getq()}\n")
|
|
print(f"FHEW scheme is using lattice parameter {ccLWE.Getn()},\n logQ {logQ_ccLWE},\n and modulus q {ccLWE.Getq()}\n")
|
|
|
|
|
|
- init_level = 0
|
|
|
|
-
|
|
|
|
- if cc.GetScalingTechnique() == FLEXIBLEAUTOEXT:
|
|
|
|
- init_level = 1
|
|
|
|
-
|
|
|
|
# Here we assume the message does not need scaling, as they are in the unit circle.
|
|
# Here we assume the message does not need scaling, as they are in the unit circle.
|
|
- cc.EvalCompareSwitchPrecompute(1, init_level, 1)
|
|
|
|
|
|
+ cc.EvalCompareSwitchPrecompute(1, 1)
|
|
|
|
|
|
# Step 3: Encoding and encryption of inputs
|
|
# Step 3: Encoding and encryption of inputs
|
|
|
|
|
|
@@ -1062,7 +1073,7 @@ def ArgminViaSchemeSwitchingUnit():
|
|
c1 = cc.Encrypt(keys.publicKey, ptxt1)
|
|
c1 = cc.Encrypt(keys.publicKey, ptxt1)
|
|
|
|
|
|
# Step 4: Argmin evaluation
|
|
# Step 4: Argmin evaluation
|
|
- result = cc.EvalMinSchemeSwitching(c1, keys.publicKey, numValues, slots, oneHot)
|
|
|
|
|
|
+ result = cc.EvalMinSchemeSwitching(c1, keys.publicKey, numValues, slots)
|
|
|
|
|
|
ptxtMin = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMin = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMin.SetLength(1)
|
|
ptxtMin.SetLength(1)
|
|
@@ -1076,7 +1087,7 @@ def ArgminViaSchemeSwitchingUnit():
|
|
ptxtMin.SetLength(1)
|
|
ptxtMin.SetLength(1)
|
|
print("Argmin: ", ptxtMin)
|
|
print("Argmin: ", ptxtMin)
|
|
|
|
|
|
- result = cc.EvalMaxSchemeSwitching(c1, keys.publicKey, numValues, slots, oneHot)
|
|
|
|
|
|
+ result = cc.EvalMaxSchemeSwitching(c1, keys.publicKey, numValues, slots)
|
|
|
|
|
|
ptxtMax = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMax = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMax.SetLength(1)
|
|
ptxtMax.SetLength(1)
|
|
@@ -1139,21 +1150,24 @@ def ArgminViaSchemeSwitchingAltUnit():
|
|
keys = cc.KeyGen()
|
|
keys = cc.KeyGen()
|
|
|
|
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
# Step 2: Prepare the FHEW cryptocontext and keys for FHEW and scheme switching
|
|
- FHEWparams = cc.EvalSchemeSwitchingSetup(sl, slBin, arbFunc, logQ_ccLWE, False, slots)
|
|
|
|
-
|
|
|
|
- ccLWE = FHEWparams[0]
|
|
|
|
- privateKeyFHEW = FHEWparams[1]
|
|
|
|
|
|
+ params = SchSwchParams()
|
|
|
|
+ params.SetSecurityLevelCKKS(sl)
|
|
|
|
+ params.SetSecurityLevelFHEW(slBin)
|
|
|
|
+ params.SetCtxtModSizeFHEWLargePrec(logQ_ccLWE)
|
|
|
|
+ params.SetNumSlotsCKKS(slots)
|
|
|
|
+ params.SetNumValues(numValues)
|
|
|
|
+ params.SetComputeArgmin(True)
|
|
|
|
+ params.SetUseAltArgmin(True)
|
|
|
|
+
|
|
|
|
+ privateKeyFHEW = cc.EvalSchemeSwitchingSetup(params)
|
|
|
|
+ ccLWE = cc.GetBinCCForSchemeSwitch()
|
|
|
|
|
|
- cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW, numValues, oneHot, alt)
|
|
|
|
|
|
+ cc.EvalSchemeSwitchingKeyGen(keys, privateKeyFHEW)
|
|
|
|
|
|
print(f"FHEW scheme is using lattice parameter {ccLWE.Getn()},\n logQ {logQ_ccLWE},\n and modulus q {ccLWE.Getq()}\n")
|
|
print(f"FHEW scheme is using lattice parameter {ccLWE.Getn()},\n logQ {logQ_ccLWE},\n and modulus q {ccLWE.Getq()}\n")
|
|
|
|
|
|
- init_level = 0
|
|
|
|
-
|
|
|
|
- if cc.GetScalingTechnique() == FLEXIBLEAUTOEXT:
|
|
|
|
- init_level = 1
|
|
|
|
# Here we assume the message does not need scaling, as they are in the unit circle.
|
|
# Here we assume the message does not need scaling, as they are in the unit circle.
|
|
- cc.EvalCompareSwitchPrecompute(1, init_level, 1)
|
|
|
|
|
|
+ cc.EvalCompareSwitchPrecompute(1, 1)
|
|
|
|
|
|
# Step 3: Encoding and encryption of inputs
|
|
# Step 3: Encoding and encryption of inputs
|
|
# Inputs
|
|
# Inputs
|
|
@@ -1177,7 +1191,7 @@ def ArgminViaSchemeSwitchingAltUnit():
|
|
c1 = cc.Encrypt(keys.publicKey, ptxt1)
|
|
c1 = cc.Encrypt(keys.publicKey, ptxt1)
|
|
|
|
|
|
# Step 4: Argmin evaluation
|
|
# Step 4: Argmin evaluation
|
|
- result = cc.EvalMinSchemeSwitchingAlt(c1, keys.publicKey, numValues, slots, oneHot)
|
|
|
|
|
|
+ result = cc.EvalMinSchemeSwitchingAlt(c1, keys.publicKey, numValues, slots)
|
|
|
|
|
|
ptxtMin = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMin = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMin.SetLength(1)
|
|
ptxtMin.SetLength(1)
|
|
@@ -1191,7 +1205,7 @@ def ArgminViaSchemeSwitchingAltUnit():
|
|
ptxtMin.SetLength(1)
|
|
ptxtMin.SetLength(1)
|
|
print("Argmin: ", ptxtMin)
|
|
print("Argmin: ", ptxtMin)
|
|
|
|
|
|
- result = cc.EvalMaxSchemeSwitchingAlt(c1, keys.publicKey, numValues, slots, oneHot)
|
|
|
|
|
|
+ result = cc.EvalMaxSchemeSwitchingAlt(c1, keys.publicKey, numValues, slots)
|
|
|
|
|
|
ptxtMax = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMax = cc.Decrypt(keys.secretKey, result[0])
|
|
ptxtMax.SetLength(1)
|
|
ptxtMax.SetLength(1)
|