Skip to content

Commit

Permalink
Generate: Add ValidFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
hyerinshelly committed Mar 6, 2024
1 parent fbc2bd4 commit ebd4441
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/scala/fhetest/Generate/AbsProgram.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ case class AbsProgram(
absStmts: List[AbsStmt],
libConfig: LibConfig,
) {
val encParams = libConfig.encParams
val len = libConfig.len
val bound = libConfig.bound
val mulDepth: Int = absStmts.count {
case Mul(_, _) | MulP(_, _) => true; case _ => false
}

lazy val isValid: Boolean =
(mulDepth < libConfig.encParams.mulDepth)
mulDepthIsSmall(mulDepth, encParams.mulDepth) &&
firstModSizeIsLargest(libConfig.firstModSize, libConfig.scalingModSize) &&
modSizeIsUpto60bits(libConfig.firstModSize, libConfig.scalingModSize) &&
ringDimIsPowerOfTwo(encParams.ringDim) &&
plainModIsPositive(encParams.plainMod) &&
plainModEnableBatching(encParams.plainMod, encParams.ringDim) &&
lenIsLessThanRingDim(len, encParams.ringDim, libConfig.scheme) &&
boundIsLessThanPowerOfModSize(bound, libConfig.firstModSize)

def stringify: String = absStmts.map(_.stringify()).mkString("")

Expand Down
33 changes: 33 additions & 0 deletions src/main/scala/fhetest/Generate/ValidFilter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fhetest.Generate

import fhetest.LibConfig
import fhetest.Utils.*

def mulDepthIsSmall(realMulDepth: Int, configMulDepth: Int): Boolean =
realMulDepth < configMulDepth

def firstModSizeIsLargest(firstModSize: Int, scalingModSize: Int): Boolean =
scalingModSize <= firstModSize

def modSizeIsUpto60bits(firstModSize: Int, scalingModSize: Int): Boolean =
(firstModSize <= 60) && (scalingModSize <= 60)

def ringDimIsPowerOfTwo(n: Int): Boolean = (n > 0) && ((n & (n - 1)) == 0)

def plainModIsPositive(m: Int): Boolean = m > 0

def plainModEnableBatching(m: Int, n: Int): Boolean = (m % n) == 1

def lenIsLessThanRingDim(len: Int, n: Int, scheme: Scheme): Boolean =
scheme match {
case Scheme.CKKS => len <= (n / 2)
case _ => len <= n
}

def boundIsLessThanPowerOfModSize(
bound: Int | Double,
firstModSize: Int,
): Boolean = bound match {
case intBound: Int => intBound < math.pow(2, firstModSize)
case doubleBound: Double => doubleBound < math.pow(2, firstModSize)
}

0 comments on commit ebd4441

Please sign in to comment.