Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix debug tests #21

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 8 additions & 5 deletions Sources/HomomorphicEncryption/PolyRq/PolyRq+Ntt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ extension PolyContext {
let rootOfUnityPowers = nttContext.rootOfUnityPowers
// The forward butterfly transforms `x,y` in
// `[0, k * modulus)` -> `[0, (k + 2) * modulus)`.
// We delay modular reduction until overflowing T, i.e.
// `(kMax + 2) * modulus > T.max`, so `kMax = floor(T.max / modulus) - 2
// We delay modular reduction until overflowing the maximum input to `T.subtractIfExceeds`, i.e.,
// we find the largest `kMax` such that `(kMax + 2) * modulus <= (Self.max >> 1) + modulus`.
// So, we have `kMax = T.max / (2 * modulus) - 1`
var lazyReductionCounter = -1 // k
// kMax
let maxLazyReductionCounter = modulusReduceFactor.singleWordModulus.factor.low &- 2
let maxLazyReductionCounter = T.max / (2 * modulus) - 1 // kMax

func applyFinalStageOp(m: Int, op: (_ x: inout T, _ y: inout T) -> Void) {
for i in 0..<m {
Expand Down Expand Up @@ -292,14 +292,17 @@ extension PolyContext {
let timeToReduce = lazyReductionCounter > maxLazyReductionCounter
if timeToReduce {
if t == 1 {
lazyReductionCounter &-= 2
// if lazyReductionCounter == 3, `subtractIfExceeds(twiceModulus)`
// only ensures `x in [0, 2 * p - 1]`
lazyReductionCounter = max(lazyReductionCounter - 2, 2)
} else {
lazyReductionCounter = 1
}
}
switch (t, timeToReduce) {
case (1, true):
applyFinalStageOp(m: m) { x, _ in
// Ensure butterfly doesn't overflow
x = x.subtractIfExceeds(twiceModulus)
}
case (1, false):
Expand Down
3 changes: 2 additions & 1 deletion Sources/HomomorphicEncryption/Scalar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ extension ScalarType {
///
/// Computes a conditional subtraction, `if self >= modulus ? self - modulus : self`, which can be used for modular
/// reduction of `self` from range `[0, 2 * modulus - 1]` to `[0, modulus - 1]`. The computation is constant-time.
/// `self` must be less than or equal to `(Self.max >> 1) + modulus``
/// - Parameter modulus: Modulus.
/// - Returns: `self >= modulus ? self - modulus : self`.
@inlinable
public func subtractIfExceeds(_ modulus: Self) -> Self {
assert(self < 2 * modulus)
assert(self <= (Self.max &>> 1) + modulus) // difference mask fails otherwise
let difference = self &- modulus
let mask = Self(0) &- (difference >> (bitWidth - 1))
return difference &+ (modulus & mask)
Expand Down
54 changes: 53 additions & 1 deletion Tests/HomomorphicEncryptionTests/NttTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,55 @@ final class NttTests: XCTestCase {
])
}

func testNtt16() throws {
// modulus near top of range
try runNttTest(
moduli: [UInt32(536_870_849)],
coeffData: [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
evalData: [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
try runNttTest(
moduli: [UInt32(536_870_849)],
coeffData: [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
evalData: [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
try runNttTest(
moduli: [UInt32(536_870_849)],
coeffData: [[
477_051_601,
421_524_611,
456_257_859,
247_136_825,
128_775_020,
76_785_070,
49_764_016,
525_812_772,
325_605_371,
88_935_943,
255_470_762,
39_507_048,
404_978_219,
379_383_003,
244_420_585,
346_826_612,
]], evalData: [[
230_846_094,
480_599_401,
157_364_576,
360_442_736,
531_052_463,
294_311_347,
432_899_854,
219_721_533,
286_807_067,
260_650_843,
362_842_688,
315_862_017,
493_042_020,
520_739_674,
167_758_416,
370_401_491,
]])
}

func testNtt32() throws {
let modulus = UInt32(769)

Expand Down Expand Up @@ -175,8 +224,11 @@ final class NttTests: XCTestCase {
return try xEval.inverseNtt()
}

let moduli = [UInt64(576_460_752_303_436_801)]
let degree = 128
let moduli = try UInt32.generatePrimes(
significantBitCounts: [30],
preferringSmall: false,
nttDegree: degree)
let context = try PolyContext(degree: degree, moduli: moduli)
let x = PolyRq<_, Coeff>.random(context: context)
let y = PolyRq<_, Coeff>.random(context: context)
Expand Down
14 changes: 14 additions & 0 deletions Tests/HomomorphicEncryptionTests/ScalarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ import TestUtilities
import XCTest

class ScalarTests: XCTestCase {
func testSubtractIfExceeds() {
do {
let modulus: UInt32 = (1 << 29) - 63
XCTAssertEqual(UInt32(2 * modulus + 1).subtractIfExceeds(modulus), modulus + 1)
XCTAssertEqual(UInt32(modulus - 1).subtractIfExceeds(modulus), modulus - 1)
}
do {
let modulus: UInt32 = (1 << 31) - 10
let max = (UInt32.max >> 1) + modulus
XCTAssertEqual(UInt32(max).subtractIfExceeds(modulus), max - modulus)
XCTAssertEqual(UInt32(modulus - 1).subtractIfExceeds(modulus), modulus - 1)
}
}

func testAddMod() {
XCTAssertEqual(UInt32(0).addMod(1, modulus: 3), 1)
XCTAssertEqual(UInt32(1).addMod(2, modulus: 3), 0)
Expand Down