Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 21 additions & 5 deletions Sources/PrivateNearestNeighborSearch/MatrixMultiplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,24 @@ 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
self.babyStep = babyStep
self.giantStep = giantStep
// Ensure babyStep >= giantStep for correct algorithm behavior.
// The baby-step giant-step algorithm requires this ordering.
if babyStep >= giantStep {
self.babyStep = babyStep
self.giantStep = giantStep
} else {
self.babyStep = giantStep
self.giantStep = babyStep
}
Comment thread
feimaomiao marked this conversation as resolved.
Outdated
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let vectorDimension = try container.decode(Int.self, forKey: .vectorDimension)
let babyStep = try container.decode(Int.self, forKey: .babyStep)
let giantStep = try container.decode(Int.self, forKey: .giantStep)
// Use the normalizing initializer
self.init(vectorDimension: vectorDimension, babyStep: babyStep, giantStep: giantStep)
}
Comment thread
feimaomiao marked this conversation as resolved.
Outdated

@inlinable
Expand Down Expand Up @@ -126,7 +142,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 +184,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
Loading