Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sources/PNNSProcessDatabase/ProcessDatabase.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
// Copyright 2024-2026 Apple Inc. and the Swift Homomorphic Encryption project authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -228,7 +228,8 @@ struct ProcessDatabase: ParsableCommand {
maxQueryCount: config.batchSize,
encryptionParameters: encryptionParameters,
scheme: Scheme.self),
distanceMetric: config.distanceMetric)
distanceMetric: config.distanceMetric,
extraPlaintextModuli: config.extraPlaintextModuli.map { Scheme.Scalar($0) })
let serverConfig = ServerConfig<Scheme>(
clientConfig: clientConfig,
databasePacking: config.databasePacking)
Expand Down
14 changes: 10 additions & 4 deletions Sources/PrivateNearestNeighborSearch/MatrixMultiplication.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
// Copyright 2024-2026 Apple Inc. and the Swift Homomorphic Encryption project authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,6 +32,12 @@ public struct BabyStepGiantStep: Codable, Equatable, Hashable, Sendable {

Comment thread
feimaomiao marked this conversation as resolved.
public init(vectorDimension: Int, babyStep: Int, giantStep: Int) {
self.vectorDimension = vectorDimension

// Ensure babyStep >= giantStep for correct algorithm behavior.
// The baby-step giant-step algorithm requires this ordering.
precondition(
babyStep >= giantStep, "babyStep should be larger than giant step")
Comment thread
feimaomiao marked this conversation as resolved.
Outdated

self.babyStep = babyStep
self.giantStep = giantStep
}
Expand Down Expand Up @@ -126,7 +132,9 @@ extension PlaintextMatrix {
vector ciphertextVector: CiphertextMatrix<Scheme, Scheme.CanonicalCiphertextFormat>,
using evaluationKey: EvaluationKey<Scheme>) async throws -> [Scheme.CanonicalCiphertext]
{
guard case .diagonal = packing else {
// Extract BabyStepGiantStep from the packing to ensure consistency
// between how data was packed and how it's accessed during multiplication
guard case let .diagonal(babyStepGiantStep: babyStepGiantStep) = packing else {
Comment thread
fboemer marked this conversation as resolved.
let expectedBsgs = BabyStepGiantStep(vectorDimension: dimensions.columnCount)
throw PnnsError.wrongMatrixPacking(got: packing, expected: .diagonal(babyStepGiantStep: expectedBsgs))
}
Expand Down Expand Up @@ -166,8 +174,6 @@ extension PlaintextMatrix {
// We extend this basic idea using baby-step giant-step logic from Section 6.3 of
// https://eprint.iacr.org/2018/244.pdf.

let babyStepGiantStep = BabyStepGiantStep(vectorDimension: dimensions.columnCount)

// 1) Compute v_j = theta^j(v)
var rotatedStates: [Scheme.CanonicalCiphertext] = []
rotatedStates.reserveCapacity(babyStepGiantStep.babyStep)
Expand Down