Skip to content

Commit

Permalink
Params -> Parameters (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
fboemer authored Sep 17, 2024
1 parent b593b9a commit 6cb205d
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ struct ProcessBenchmarkContext<Scheme: HeScheme> {
preferringSmall: false,
nttDegree: parameterConfig.polyDegree)

let encryptionParams = try EncryptionParameters<Scheme>(
let encryptionParameters = try EncryptionParameters<Scheme>(
polyDegree: parameterConfig.polyDegree,
plaintextModulus: plaintextModuli[0],
coefficientModuli: coefficientModuli,
Expand All @@ -130,15 +130,15 @@ struct ProcessBenchmarkContext<Scheme: HeScheme> {
plaintextMatrixDimensions: MatrixDimensions(
rowCount: databaseConfig.rowCount,
columnCount: databaseConfig.vectorDimension),
encryptionParameters: encryptionParams,
encryptionParameters: encryptionParameters,
maxQueryCount: batchSize)
let scalingFactor = ClientConfig<Scheme>
.maxScalingFactor(
distanceMetric: .cosineSimilarity,
vectorDimension: databaseConfig.vectorDimension,
plaintextModuli: Array(plaintextModuli[1...]))
let clientConfig = try ClientConfig(
encryptionParams: encryptionParams,
encryptionParameters: encryptionParameters,
scalingFactor: scalingFactor,
queryPacking: .denseRow,
vectorDimension: databaseConfig.vectorDimension,
Expand All @@ -151,8 +151,8 @@ struct ProcessBenchmarkContext<Scheme: HeScheme> {
self.serverConfig = serverConfig

self.database = getDatabaseForTesting(config: databaseConfig)
self.contexts = try serverConfig.encryptionParameters.map { encryptionParams in
try Context(encryptionParameters: encryptionParams)
self.contexts = try serverConfig.encryptionParameters.map { encryptionParameters in
try Context(encryptionParameters: encryptionParameters)
}
}
}
Expand Down Expand Up @@ -224,7 +224,7 @@ struct PnnsBenchmarkContext<Scheme: HeScheme> {
significantBitCounts: parameterConfig.coefficientModulusBits,
preferringSmall: false,
nttDegree: parameterConfig.polyDegree)
let encryptionParams = try EncryptionParameters<Scheme>(
let encryptionParameters = try EncryptionParameters<Scheme>(
polyDegree: parameterConfig.polyDegree,
plaintextModulus: plaintextModuli[0],
coefficientModuli: coefficientModuli,
Expand All @@ -235,15 +235,15 @@ struct PnnsBenchmarkContext<Scheme: HeScheme> {
plaintextMatrixDimensions: MatrixDimensions(
rowCount: databaseConfig.rowCount,
columnCount: databaseConfig.vectorDimension),
encryptionParameters: encryptionParams,
encryptionParameters: encryptionParameters,
maxQueryCount: queryCount)
let scalingFactor = ClientConfig<Scheme>
.maxScalingFactor(
distanceMetric: .cosineSimilarity,
vectorDimension: databaseConfig.vectorDimension,
plaintextModuli: plaintextModuli)
let clientConfig = try ClientConfig(
encryptionParams: encryptionParams,
encryptionParameters: encryptionParameters,
scalingFactor: scalingFactor,
queryPacking: .denseRow,
vectorDimension: databaseConfig.vectorDimension,
Expand All @@ -258,7 +258,7 @@ struct PnnsBenchmarkContext<Scheme: HeScheme> {

let database = getDatabaseForTesting(config: databaseConfig)
let contexts = try clientConfig.encryptionParameters
.map { encryptionParams in try Context(encryptionParameters: encryptionParams) }
.map { encryptionParameters in try Context(encryptionParameters: encryptionParameters) }
self.processedDatabase = try database.process(config: serverConfig, contexts: contexts)
self.client = try Client(config: clientConfig, contexts: contexts)
self.server = try Server(database: processedDatabase)
Expand Down
1 change: 1 addition & 0 deletions Sources/HomomorphicEncryption/Plaintext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// Plaintext type.
public struct Plaintext<Scheme: HeScheme, Format: PolyFormat>: Equatable, Sendable {
public typealias SignedScalar = Scheme.SignedScalar
Expand Down
2 changes: 1 addition & 1 deletion Sources/PNNSProcessDatabase/ProcessDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ struct ProcessDatabase: ParsableCommand {

let encryptionParameters = try EncryptionParameters<Scheme>(from: config.rlweParameters)
let clientConfig = try ClientConfig<Scheme>(
encryptionParams: encryptionParameters,
encryptionParameters: encryptionParameters,
scalingFactor: config.scalingFactor,
queryPacking: config.queryPacking,
vectorDimension: vectorDimension,
Expand Down
18 changes: 9 additions & 9 deletions Sources/PrivateNearestNeighborsSearch/CiphertextMatrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public struct CiphertextMatrix<Scheme: HeScheme, Format: PolyFormat>: Equatable,
guard let context = ciphertexts.first?.context else {
throw PnnsError.emptyCiphertextArray
}
let encryptionParams = context.encryptionParameters
guard let simdDimensions = encryptionParams.simdDimensions else {
throw PnnsError.simdEncodingNotSupported(for: encryptionParams)
let encryptionParameters = context.encryptionParameters
guard let simdDimensions = encryptionParameters.simdDimensions else {
throw PnnsError.simdEncodingNotSupported(for: encryptionParameters)
}
let expectedCiphertextCount = try PlaintextMatrix<Scheme, Format>.plaintextCount(
encryptionParameters: encryptionParams,
encryptionParameters: encryptionParameters,
dimensions: dimensions,
packing: packing)
guard ciphertexts.count == expectedCiphertextCount else {
Expand Down Expand Up @@ -158,22 +158,22 @@ extension CiphertextMatrix {
extension CiphertextMatrix {
/// Computes the evaluation key configuration for calling `extractDenseRow`.
/// - Parameters:
/// - encryptionParams: Encryption parameters; must support `.simd` encoding.
/// - encryptionParameters: Encryption parameters; must support `.simd` encoding.
/// - dimensions: Dimensions of the matrix to call `extractDenseRow` on.
/// - Returns: The evaluation key configuration.
/// - Throws: Error upon failure to generate the evaluation key configuration.
@inlinable
static func extractDenseRowConfig(for encryptionParams: EncryptionParameters<Scheme>,
static func extractDenseRowConfig(for encryptionParameters: EncryptionParameters<Scheme>,
dimensions: MatrixDimensions) throws -> EvaluationKeyConfig
{
if dimensions.rowCount == 1 {
// extractDenseRow is a No-op, so no evaluation key required
return EvaluationKeyConfig()
}
guard let simdDimensions = encryptionParams.simdDimensions else {
throw PnnsError.simdEncodingNotSupported(for: encryptionParams)
guard let simdDimensions = encryptionParameters.simdDimensions else {
throw PnnsError.simdEncodingNotSupported(for: encryptionParameters)
}
let degree = encryptionParams.polyDegree
let degree = encryptionParameters.polyDegree
var galoisElements = [GaloisElement.swappingRows(degree: degree)]
let columnCountPowerOfTwo = dimensions.columnCount.nextPowerOfTwo
if columnCountPowerOfTwo != simdDimensions.columnCount {
Expand Down
4 changes: 2 additions & 2 deletions Sources/PrivateNearestNeighborsSearch/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public struct Client<Scheme: HeScheme> {

var contexts = contexts
if contexts.isEmpty {
contexts = try config.encryptionParameters.map { encryptionParams in
try Context(encryptionParameters: encryptionParams)
contexts = try config.encryptionParameters.map { encryptionParameters in
try Context(encryptionParameters: encryptionParameters)
}
}
try config.validateContexts(contexts: contexts)
Expand Down
20 changes: 10 additions & 10 deletions Sources/PrivateNearestNeighborsSearch/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,25 @@ public struct ClientConfig<Scheme: HeScheme>: Codable, Equatable, Hashable, Send
public let distanceMetric: DistanceMetric
/// For plaintext CRT, the list of extra plaintext moduli.
///
/// The first plaintext modulus will be the one in ``ClientConfig/encryptionParams``.
/// The first plaintext modulus will be the one in ``ClientConfig/encryptionParameters``.
public let extraPlaintextModuli: [Scheme.Scalar]

/// The plaintext CRT moduli.
public var plaintextModuli: [Scheme.Scalar] { encryptionParameters.map(\.plaintextModulus) }

/// Creates a new ``ClientConfig``.
/// - Parameters:
/// - encryptionParams: Encryption parameters.
/// - encryptionParameters: Encryption parameters.
/// - scalingFactor: Factor by which to scale floating-point entries before rounding to integers.
/// - queryPacking: Packing for the query.
/// - vectorDimension: Number of entries in each vector.
/// - evaluationKeyConfig: Evaluation key configuration for nearest neighbors computation.
/// - distanceMetric: Metric for nearest neighbors computation
/// - extraPlaintextModuli: For plaintext CRT, the list of extra plaintext moduli. The first plaintext modulus
/// will be the one in ``ClientConfig/encryptionParams``.
/// will be the one in ``ClientConfig/encryptionParameters``.
/// - Throws: Error upon failure to create a new client config.
public init(
encryptionParams: EncryptionParameters<Scheme>,
encryptionParameters: EncryptionParameters<Scheme>,
scalingFactor: Int,
queryPacking: MatrixPacking,
vectorDimension: Int,
Expand All @@ -88,13 +88,13 @@ public struct ClientConfig<Scheme: HeScheme>: Codable, Equatable, Hashable, Send
{
let extraEncryptionParams = try extraPlaintextModuli.map { plaintextModulus in
try EncryptionParameters<Scheme>(
polyDegree: encryptionParams.polyDegree,
polyDegree: encryptionParameters.polyDegree,
plaintextModulus: plaintextModulus,
coefficientModuli: encryptionParams.coefficientModuli,
errorStdDev: encryptionParams.errorStdDev,
securityLevel: encryptionParams.securityLevel)
coefficientModuli: encryptionParameters.coefficientModuli,
errorStdDev: encryptionParameters.errorStdDev,
securityLevel: encryptionParameters.securityLevel)
}
self.encryptionParameters = [encryptionParams] + extraEncryptionParams
self.encryptionParameters = [encryptionParameters] + extraEncryptionParams
self.scalingFactor = scalingFactor
self.queryPacking = queryPacking
self.vectorDimension = vectorDimension
Expand Down Expand Up @@ -145,7 +145,7 @@ public struct ServerConfig<Scheme: HeScheme>: Codable, Equatable, Hashable, Send

/// For plaintext CRT, the list of extra plaintext moduli.
///
/// The first plaintext modulus will be the one in ``ClientConfig/encryptionParams``.
/// The first plaintext modulus will be the one in ``ClientConfig/encryptionParameters``.
public var extraPlaintextModuli: [Scheme.Scalar] {
clientConfig.extraPlaintextModuli
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/PrivateNearestNeighborsSearch/PlaintextMatrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ public struct PlaintextMatrix<Scheme: HeScheme, Format: PolyFormat>: Equatable,
throw PnnsError.emptyPlaintextArray
}
let context = plaintexts[0].context
let encryptionParams = context.encryptionParameters
guard let simdDimensions = encryptionParams.simdDimensions else {
throw PnnsError.simdEncodingNotSupported(for: encryptionParams)
let encryptionParameters = context.encryptionParameters
guard let simdDimensions = encryptionParameters.simdDimensions else {
throw PnnsError.simdEncodingNotSupported(for: encryptionParameters)
}
let expectedPlaintextCount = try PlaintextMatrix.plaintextCount(
encryptionParameters: encryptionParams,
encryptionParameters: encryptionParameters,
dimensions: dimensions,
packing: packing)
guard plaintexts.count == expectedPlaintextCount else {
Expand Down Expand Up @@ -278,9 +278,9 @@ public struct PlaintextMatrix<Scheme: HeScheme, Format: PolyFormat>: Equatable,
throw PnnsError.simdEncodingNotSupported(for: context.encryptionParameters)
}

let encryptionParams = context.encryptionParameters
let encryptionParameters = context.encryptionParameters
let expectedPlaintextCount = try PlaintextMatrix.plaintextCount(
encryptionParameters: encryptionParams,
encryptionParameters: encryptionParameters,
dimensions: dimensions,
packing: .denseColumn)
var plaintexts: [Scheme.CoeffPlaintext] = []
Expand Down
8 changes: 4 additions & 4 deletions Sources/PrivateNearestNeighborsSearch/ProcessedDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public struct ProcessedDatabase<Scheme: HeScheme>: Equatable, Sendable {
public init(from serialized: SerializedProcessedDatabase<Scheme>, contexts: [Context<Scheme>] = []) throws {
var contexts = contexts
if contexts.isEmpty {
contexts = try serialized.serverConfig.encryptionParameters.map { encryptionParams in
try Context(encryptionParameters: encryptionParams)
contexts = try serialized.serverConfig.encryptionParameters.map { encryptionParameters in
try Context(encryptionParameters: encryptionParameters)
}
}
try serialized.serverConfig.validateContexts(contexts: contexts)
Expand Down Expand Up @@ -195,8 +195,8 @@ extension Database {
}
var contexts = contexts
if contexts.isEmpty {
contexts = try config.encryptionParameters.map { encryptionParams in
try Context(encryptionParameters: encryptionParams)
contexts = try config.encryptionParameters.map { encryptionParameters in
try Context(encryptionParameters: encryptionParameters)
}
}
try config.validateContexts(contexts: contexts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ extension Apple_SwiftHomomorphicEncryption_Pnns_V1_ClientConfig {
throw ConversionError.unsetField(\Self.encryptionParameters, in: Self.self)
}
return try ClientConfig<Scheme>(
encryptionParams: encryptionParameters.native(),
encryptionParameters: encryptionParameters.native(),
scalingFactor: Int(scalingFactor),
queryPacking: queryPacking.native(),
vectorDimension: Int(vectorDimension),
Expand Down
5 changes: 3 additions & 2 deletions Tests/HomomorphicEncryptionTests/HeAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

@testable import HomomorphicEncryption
import TestUtilities
import XCTest
Expand Down Expand Up @@ -1074,8 +1075,8 @@ class HeAPITests: XCTestCase {
errorStdDev: ErrorStdDev.stdDev32,
securityLevel: SecurityLevel.unchecked)

for encryptionParams in predefined + [custom] {
let context = try Context<Bfv<T>>(encryptionParameters: encryptionParams)
for encryptionParameters in predefined + [custom] {
let context = try Context<Bfv<T>>(encryptionParameters: encryptionParameters)
try schemeEncodeDecodeTest(context: context)
try schemeEncryptDecryptTest(context: context)
try schemeEncryptZeroDecryptTest(context: context)
Expand Down
4 changes: 2 additions & 2 deletions Tests/PrivateInformationRetrievalTests/PirUtilTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PirUtilTests: XCTestCase {
_ keyCompression: PirKeyCompressionStrategy) throws
{
let degree = 32
let encryptionParams = try EncryptionParameters<Scheme>(
let encryptionParameters = try EncryptionParameters<Scheme>(
polyDegree: degree,
plaintextModulus: Scheme.Scalar(17),
coefficientModuli: Scheme.Scalar
Expand All @@ -36,7 +36,7 @@ class PirUtilTests: XCTestCase {
errorStdDev: ErrorStdDev.stdDev32,
securityLevel: SecurityLevel.unchecked)

let context: Context<Scheme> = try Context(encryptionParameters: encryptionParams)
let context: Context<Scheme> = try Context(encryptionParameters: encryptionParameters)
let plaintextModulus = context.plaintextModulus
let logDegree = degree.log2
for logStep in 1...logDegree {
Expand Down
Loading

0 comments on commit 6cb205d

Please sign in to comment.