Skip to content

Commit

Permalink
LibConfigGenerate:re-implement ValidFilter for invalid testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hyerinshelly committed Mar 23, 2024
1 parent 6ed8534 commit 854e6a4
Show file tree
Hide file tree
Showing 10 changed files with 886 additions and 235 deletions.
7 changes: 4 additions & 3 deletions src/main/scala/fhetest/Command.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ case object CmdGen extends Command("gen") {
def runJob(config: Config): Unit =
val encType = config.encType.getOrElseThrow("No encType given.")
val genCount = config.genCount.getOrElse(10)
val generator = Generate(encType, Strategy.Random, config.filter)
val generator = Generate(encType, Strategy.Random, config.validFilter)
generator.show(List(Backend.SEAL, Backend.OpenFHE), genCount, encType)
}

Expand Down Expand Up @@ -217,7 +217,8 @@ case object CmdTest extends BackendCommand("test") {
val encType = config.encType.getOrElseThrow("No encType given.")
val genStrategy = config.genStrategy.getOrElse(Strategy.Random)
val genCount = config.genCount
val generator = Generate(encType, genStrategy, config.filter)
val validFilter = config.validFilter
val generator = Generate(encType, genStrategy, validFilter)
val programs = generator(genCount)
val backendList = List(Backend.SEAL, Backend.OpenFHE)
val encParamsOpt = config.libConfigOpt.map(_.encParams)
Expand All @@ -236,7 +237,7 @@ case object CmdTest extends BackendCommand("test") {
toJson,
sealVersion,
openfheVersion,
config.filter,
validFilter,
config.debug,
config.timeLimit,
)
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/fhetest/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class Config(
var encType: Option[ENC_TYPE] = None,
var genStrategy: Option[Strategy] = None,
var genCount: Option[Int] = None,
var validFilter: Boolean = true,
var toJson: Boolean = false,
var sealVersion: Option[String] = None,
var openfheVersion: Option[String] = None,
var libConfigOpt: Option[LibConfig] = None,
var fromJson: Option[String] = None,
var filter: Boolean = true,
var silent: Boolean = false,
var debug: Boolean = false,
var timeLimit: Option[Int] = None,
Expand Down Expand Up @@ -67,7 +67,7 @@ object Config {
case "libconfig" =>
config.libConfigOpt = Some(LibConfig())
case "fromjson" => config.fromJson = Some(value)
case "filter" => config.filter = value.toBoolean
case "filter" => config.validFilter = value.toBoolean
case "silent" => config.silent = value.toBoolean
case "debug" => config.debug = value.toBoolean
case "timeout" => config.timeLimit = Some(value.toInt)
Expand Down
30 changes: 15 additions & 15 deletions src/main/scala/fhetest/Generate/AbsProgram.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ case class AbsProgram(
}

// TODO: Change these filters to assertions?
lazy val isValid: Boolean =
mulDepthIsSmall(mulDepth, encParams.mulDepth) &&
firstModSizeIsLargest(libConfig.firstModSize, libConfig.scalingModSize) &&
modSizeIsUpto60bits(libConfig.firstModSize, libConfig.scalingModSize) &&
openFHEBFVModuli(
libConfig.scheme,
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) &&
boundIsLessThanPlainMod(bound, encParams.plainMod)
// lazy val isValid: Boolean =
// mulDepthIsSmall(mulDepth, encParams.mulDepth) &&
// firstModSizeIsLargest(libConfig.firstModSize, libConfig.scalingModSize) &&
// modSizeIsUpto60bits(libConfig.firstModSize, libConfig.scalingModSize) &&
// openFHEBFVModuli(
// libConfig.scheme,
// 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) &&
// boundIsLessThanPlainMod(bound, encParams.plainMod)

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

Expand Down
64 changes: 35 additions & 29 deletions src/main/scala/fhetest/Generate/AbsProgramGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,71 @@ enum Strategy:
extension (s: Strategy)
def getGenerator(
encType: ENC_TYPE,
validCheck: Boolean,
validFilter: Boolean,
): AbsProgramGenerator = s match {
case Strategy.Exhaustive =>
ExhaustiveGenerator(encType: ENC_TYPE, validCheck: Boolean)
ExhaustiveGenerator(encType: ENC_TYPE, validFilter: Boolean)
case Strategy.Random =>
RandomGenerator(encType: ENC_TYPE, validCheck: Boolean)
RandomGenerator(encType: ENC_TYPE, validFilter: Boolean)
}

// AbsProgram Generator
trait AbsProgramGenerator(encType: ENC_TYPE, validCheck: Boolean) {
trait AbsProgramGenerator(encType: ENC_TYPE, validFilter: Boolean) {
def generateAbsPrograms(): LazyList[AbsProgram]
val lcGen =
if validCheck then ValidLibConfigGenerator(encType)
else RandomLibConfigGenerator(encType)
if validFilter then ValidLibConfigGenerator(encType)
else InvalidLibConfigGenerator(encType)
}

case class ExhaustiveGenerator(encType: ENC_TYPE, validCheck: Boolean)
extends AbsProgramGenerator(encType: ENC_TYPE, validCheck: Boolean) {
case class ExhaustiveGenerator(encType: ENC_TYPE, validFilter: Boolean)
extends AbsProgramGenerator(encType: ENC_TYPE, validFilter: Boolean) {
val libConfigGens = lcGen.getLibConfigGenerators()
def generateAbsPrograms(): LazyList[AbsProgram] = {
def allAbsProgramsOfSize(n: Int): LazyList[AbsProgram] = {
def allAbsProgramsOfSize(n: Int): LazyList[AbsProgram] =
n match {
case 1 =>
allAbsStmts
.map(stmt => List(stmt))
.map(
AbsProgram(
_,
lcGen.generateLibConfig(),
),
)
for {
stmt <- allAbsStmts
libConfigGen <- libConfigGens
} yield {
val stmts = List(stmt)
AbsProgram(stmts, libConfigGen(stmts))
}
case _ =>
for {
stmt <- allAbsStmts
program <- allAbsProgramsOfSize(n - 1)
} yield AbsProgram(
stmt :: program.absStmts,
lcGen.generateLibConfig(),
)
libConfigGen <- libConfigGens
} yield {
val stmts = stmt :: program.absStmts
AbsProgram(stmts, libConfigGen(stmts))
}
}
}
LazyList.from(1).flatMap(allAbsProgramsOfSize)
}

}

case class RandomGenerator(encType: ENC_TYPE, validCheck: Boolean)
extends AbsProgramGenerator(encType: ENC_TYPE, validCheck: Boolean) {
case class RandomGenerator(encType: ENC_TYPE, validFilter: Boolean)
extends AbsProgramGenerator(encType: ENC_TYPE, validFilter: Boolean) {
def generateAbsPrograms(): LazyList[AbsProgram] = {
def randomAbsProgramOfSize(n: Int): AbsProgram = {
val absStmts = (1 to n).map(_ => Random.shuffle(allAbsStmts).head).toList
AbsProgram(absStmts, lcGen.generateLibConfig())
}
def randomAbsStmtsOfSize(n: Int): List[AbsStmt] =
(1 to n).map(_ => Random.shuffle(allAbsStmts).head).toList
val libConfigGens = lcGen.getLibConfigGenerators()

// Generate Lengths from 1 to inf
// LazyList.from(1)

// Generate Random Lengths from 1 to 20
val randomLength: LazyList[Int] =
LazyList.continually(Random.nextInt(20) + 1)

randomLength.map(randomAbsProgramOfSize)
for {
len <- randomLength
libConfigGen <- libConfigGens
} yield {
val stmts = randomAbsStmtsOfSize(len)
AbsProgram(stmts, libConfigGen(stmts))
}
}
}
37 changes: 37 additions & 0 deletions src/main/scala/fhetest/Generate/LibConfigDomain.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package fhetest.Generate

import fhetest.Utils.*

// import java.util.stream.Stream
// import scala.jdk.CollectionConverters._

type RingDim = Int
type FirstModSize = Int
type PlainMod = Int

case class LibConfigDomain(
scheme: Scheme,
ringDim: List[Int],
mulDepth: Int => List[Int],
plainMod: RingDim => List[Int],
firstModSize: Scheme => List[Int],
scalingModSize: Scheme => FirstModSize => List[Int],
securityLevel: List[SecurityLevel],
scalingTechnique: Scheme => List[ScalingTechnique],
lenMin: Scheme => RingDim => Int,
lenMax: Scheme => RingDim => Int,
boundMin: Scheme => PlainMod => FirstModSize => Int | Double,
boundMax: Scheme => PlainMod => FirstModSize => Int | Double,
rotateBound: List[Int],
) {
def stringify(): String =
s"""{scheme: ${scheme}}
{encParams: EncParams(${ringDim}, ${mulDepth}, ${plainMod})}
{(firstModSize, scalingModSize): (${firstModSize}, ${scalingModSize})}
{securityLevel: ${securityLevel}}
{scalingTechnique: ${scalingTechnique}}
{lenMax: ${lenMax}}
{boundMax: ${boundMax}}
{rotateBoundOpt: ${rotateBound}}
"""
}
Loading

0 comments on commit 854e6a4

Please sign in to comment.