1
1
// Choosing encryption parameters.
2
2
3
3
// snippet.hide
4
- // Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors
4
+ // Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
5
5
//
6
6
// Licensed under the Apache License, Version 2.0 (the "License");
7
7
// you may not use this file except in compliance with the License.
@@ -37,22 +37,20 @@ import HomomorphicEncryption
37
37
// encryption parameters. Generally, the coefficient moduli should all be as
38
38
// large as possible, while remaining under the maxLog2CoefficientModulus, and
39
39
// as close to the same size as possible.
40
- let params4096 =
41
- try EncryptionParameters < Bfv < UInt64 > > ( from: . n_4096_logq_27_28_28_logt_5)
42
- let params8192 =
43
- try EncryptionParameters < Bfv < UInt64 > > ( from: . n_8192_logq_3x55_logt_24)
40
+ let params4096 = try EncryptionParameters < UInt64 > ( from: . n_4096_logq_27_28_28_logt_5)
41
+ let params8192 = try EncryptionParameters < UInt64 > ( from: . n_8192_logq_3x55_logt_24)
44
42
45
43
// We can also create custom parameters.
46
44
// For instance, for a very small HE circuit, N=2048 might suffice.
47
45
// To maintain security, we ensure the coefficient modulus does not exceed the
48
46
// maximum log2 coefficient modulus for post-quantum 128-bit security.
49
47
let degree = 2048
50
- let maxLog2Q = try EncryptionParameters< Bfv < UInt64> >. maxLog2CoefficientModulus(
48
+ let maxLog2Q = try EncryptionParameters< UInt64> . maxLog2CoefficientModulus(
51
49
degree: degree,
52
50
securityLevel: . quantum128)
53
51
precondition ( maxLog2Q == 41 )
54
52
// We can also stay in the range for 32-bit moduli, for faster runtimes.
55
- let max2BitModulus = EncryptionParameters< Bfv < UInt32> >. maxSingleModulus
53
+ let max2BitModulus = EncryptionParameters< UInt32> . maxSingleModulus
56
54
precondition ( max2BitModulus. ceilLog2 == 30 )
57
55
58
56
// The coefficient moduli must be NTT-friendly, so we set nttDegree: degree.
@@ -73,21 +71,21 @@ let plaintextModulus = try UInt32.generatePrimes(
73
71
preferringSmall: false ) [ 0 ]
74
72
// This custom parameter set has only a single coefficient modulus, so it is not
75
73
// compatible with evaluation key operations.
76
- let customParams = try EncryptionParameters < Bfv < UInt32 > > (
74
+ let customUInt32Params = try EncryptionParameters < UInt32 > (
77
75
polyDegree: degree,
78
76
plaintextModulus: plaintextModulus,
79
77
coefficientModuli: coefficientModuli,
80
78
errorStdDev: . stdDev32,
81
79
securityLevel: . quantum128)
82
- precondition ( !customParams . supportsSimdEncoding)
83
- precondition ( !customParams . supportsEvaluationKey)
80
+ precondition ( !customUInt32Params . supportsSimdEncoding)
81
+ precondition ( !customUInt32Params . supportsEvaluationKey)
84
82
85
83
// snippet.hide
86
84
func summarize< Scheme: HeScheme > (
87
- parameters: EncryptionParameters < Scheme > ) throws
85
+ parameters: EncryptionParameters < Scheme . Scalar > , _ : Scheme . Type ) throws
88
86
{
89
87
let values = ( 0 ..< 8 ) . map { Scheme . Scalar ( $0) }
90
- let context = try Context ( encryptionParameters: parameters)
88
+ let context = try Context < Scheme > ( encryptionParameters: parameters)
91
89
let plaintext : Scheme . CoeffPlaintext = try context. encode (
92
90
values: values,
93
91
format: . coefficient)
@@ -106,6 +104,6 @@ func summarize<Scheme: HeScheme>(
106
104
""" )
107
105
}
108
106
109
- try summarize ( parameters: customParams )
110
- try summarize ( parameters: params4096)
111
- try summarize ( parameters: params8192)
107
+ try summarize ( parameters: customUInt32Params , Bfv < UInt32 > . self )
108
+ try summarize ( parameters: params4096, Bfv < UInt64 > . self )
109
+ try summarize ( parameters: params8192, Bfv < UInt64 > . self )
0 commit comments