Skip to content

Commit

Permalink
Fix a bug in generating Libconfig and restrict the scheme field of Li…
Browse files Browse the repository at this point in the history
…bconfig according to EncType of command line argument
  • Loading branch information
jaeho committed Mar 6, 2024
1 parent ac2ba61 commit 4c69c6d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 37 deletions.
31 changes: 9 additions & 22 deletions src/main/scala/fhetest/Command.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ case object CmdGen extends Command("gen") {
val examples = List(
"fhetest gen -type:int -count:10",
"fhetest gen -type:double -count:10",
"fhetest gen -type:int -stg:exhaust -c:10",
"fhetest gen -type:double -stg:random -c:10",
"fhetest gen -type:int -stg:exhaust -count:10",
"fhetest gen -type:double -stg:random -count:10",
)
def runJob(config: Config): Unit =
val encType = config.encType.getOrElseThrow("No encType given.")
Expand All @@ -182,10 +182,7 @@ case object CmdCheck extends BackendCommand("check") {
)
def runJob(config: Config): Unit =
val dir = config.dirName.getOrElseThrow("No directory given.")
val encParamsOpt = config.libConfigOpt match {
case Some(libConfig) => Some(libConfig.encParams)
case None => None
}
val encParamsOpt = config.libConfigOpt.map(_.encParams)
val backends = List(Backend.SEAL, Backend.OpenFHE)
val toJson = config.toJson
val sealVersion = config.sealVersion
Expand All @@ -201,30 +198,20 @@ case object CmdCheck extends BackendCommand("check") {
case object CmdTest extends BackendCommand("test") {
val help = "Check after Generate random T2 programs."
val examples = List(
"fhetest test -stg:random",
"fhetest test -stg:random -count:10",
"fhetest test -stg:exhaust -count:10",
"fhetest test -stg:random -json:true -seal:4.0.0 -openfhe:1.0.4",
"fhetest test -type:int -stg:random",
"fhetest test -type:int -stg:random -count:10",
"fhetest test -type:double -stg:exhaust -count:10",
"fhetest test -type:double -stg:random -json:true -seal:4.0.0 -openfhe:1.0.4",
)

def runJob(config: Config): Unit =
val encType = config.libConfigOpt match {
case Some(libConfig) =>
libConfig.scheme match {
case Scheme.CKKS => ENC_TYPE.ENC_DOUBLE
case _ => ENC_TYPE.ENC_INT
}
case None => config.encType.getOrElseThrow("No encType given.")
}
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 programs = generator(genCount)
val backendList = List(Backend.SEAL, Backend.OpenFHE)
val encParamsOpt = config.libConfigOpt match {
case Some(libConfig) => Some(libConfig.encParams)
case None => None
}
val encParamsOpt = config.libConfigOpt.map(_.encParams)
val toJson = config.toJson
val sealVersion = config.sealVersion
val openfheVersion = config.openfheVersion
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/fhetest/Generate/AbsProgram.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import scala.util.Random
import fhetest.Generate.Utils.*
import fhetest.Utils.ENC_TYPE
import fhetest.LibConfig
import fhetest.Utils.*

case class AbsProgram(
absStmts: List[AbsStmt],
Expand Down
21 changes: 12 additions & 9 deletions src/main/scala/fhetest/Generate/AbsProgramGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fhetest.Generate

import scala.util.Random
import fhetest.LibConfig
import fhetest.Utils.ENC_TYPE

// Template Generation Strategy
enum Strategy:
Expand All @@ -15,22 +16,25 @@ extension (s: Strategy)

// AbsProgram Generator
trait AbsProgramGenerator {
def generateAbsPrograms(): LazyList[AbsProgram]
// val libConfig: LibConfig = LibConfig()
def generateAbsPrograms(encType: ENC_TYPE): LazyList[AbsProgram]
}

object ExhaustiveGenerator extends AbsProgramGenerator {
def generateAbsPrograms(): LazyList[AbsProgram] = {
def generateAbsPrograms(encType: ENC_TYPE): LazyList[AbsProgram] = {
def allAbsProgramsOfSize(n: Int): LazyList[AbsProgram] = {
val libConfig = generateLibConfig()
n match {
case 1 =>
allAbsStmts.map(stmt => List(stmt)).map(AbsProgram(_, libConfig))
allAbsStmts
.map(stmt => List(stmt))
.map(AbsProgram(_, generateLibConfig(encType)))
case _ =>
for {
stmt <- allAbsStmts
program <- allAbsProgramsOfSize(n - 1)
} yield AbsProgram(stmt :: program.absStmts, libConfig)
} yield AbsProgram(
stmt :: program.absStmts,
generateLibConfig(encType),
)
}
}
LazyList.from(1).flatMap(allAbsProgramsOfSize)
Expand All @@ -39,11 +43,10 @@ object ExhaustiveGenerator extends AbsProgramGenerator {
}

object RandomGenerator extends AbsProgramGenerator {
def generateAbsPrograms(): LazyList[AbsProgram] = {
def generateAbsPrograms(encType: ENC_TYPE): LazyList[AbsProgram] = {
def randomAbsProgramOfSize(n: Int): AbsProgram = {
val absStmts = (1 to n).map(_ => Random.shuffle(allAbsStmts).head).toList
val libConfig = generateLibConfig()
AbsProgram(absStmts, libConfig)
AbsProgram(absStmts, generateLibConfig(encType))
}
// Generate Lengths from 1 to inf
// LazyList.from(1)
Expand Down
10 changes: 6 additions & 4 deletions src/main/scala/fhetest/Generate/LibConfigGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import fhetest.LibConfig
import fhetest.Utils.*
import scala.util.Random

def generateLibConfig(): LibConfig = {
val randomScheme = Scheme.CKKS
// val randomScheme = Scheme.values(Random.nextInt(Scheme.values.length))
def generateLibConfig(encType: ENC_TYPE): LibConfig = {
val randomScheme =
if encType == ENC_TYPE.ENC_INT then Scheme.values(Random.nextInt(2))
else Scheme.CKKS

val randomEncParams = {
// TODO: Currently only MultDepth is random
val randomRingDim = 32768
Expand All @@ -23,7 +25,7 @@ def generateLibConfig(): LibConfig = {
val randomLenOpt: Option[Int] = Some(Random.nextInt(100000))
val randomBoundOpt: Option[Int | Double] = randomScheme match {
case Scheme.BFV | Scheme.BGV =>
Some(Random.nextInt(BigInt(2).pow(100).toInt))
Some(Random.nextInt(Int.MaxValue))
case Scheme.CKKS => Some(Random.nextDouble() * math.pow(2, 100))
}
LibConfig(
Expand Down
5 changes: 5 additions & 0 deletions src/main/scala/fhetest/Phase/Check.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ case object Check {
dumpResult(program, i, checkResult, sealVersion, openfheVersion)
Some(program, checkResult)
} else {
if (debug) {
println(
s"Program $i is skipped due to HE overflow check: $overflowBound",
)
}
None
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/fhetest/Phase/Generate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ case class Generate(

val absProgGen = strategy.getGenerator

val allAbsPrograms = absProgGen.generateAbsPrograms()
val allAbsPrograms = absProgGen.generateAbsPrograms(encType)

def apply(nOpt: Option[Int]): LazyList[T2Program] = {
println(s"Genrating Strategy: $strategy")
Expand Down Expand Up @@ -100,7 +100,6 @@ case class Generate(
val baseStream = new ByteArrayInputStream(baseStr.getBytes("UTF-8"))
Parse(baseStream)

// TODO: current print only 10 values, it can be changed to larger value
def createNewBaseAbsProgram(): Goal = boilerplate()._1

def parseStmt(stmtStr: String): Statement =
Expand Down
6 changes: 6 additions & 0 deletions src/main/scala/fhetest/Utils/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ enum ENC_TYPE:
enum Scheme:
case BFV, BGV, CKKS

extension (s: Scheme) {
def toEncType: ENC_TYPE = s match
case Scheme.CKKS => ENC_TYPE.ENC_DOUBLE
case _ => ENC_TYPE.ENC_INT
}

enum SecurityLevel:
case HEStd_128_classic, HEStd_192_classic, HEStd_256_classic, HEStd_NotSet

Expand Down

0 comments on commit 4c69c6d

Please sign in to comment.