Skip to content

Commit

Permalink
Support random generation strategy (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maokami committed Feb 20, 2024
1 parent 0cf400f commit 5c879de
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/main/scala/fhetest/Command.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ case object CmdExecute extends Command("execute") {
}
}

// TODO : Get Strategy from the command line
/** `gen` command */
case object CmdGen extends Command("gen") {
val help = "Generate random T2 programs."
Expand Down Expand Up @@ -189,20 +190,21 @@ case object CmdCheck extends Command("check") {
case object CmdTest extends Command("test") {
val help = "Check after Generate random T2 programs."
val examples = List(
"fhetest test --INT",
"fhetest test --INT 10",
"fhetest test --DOUBLE 10",
"fhetest test --INT --random",
"fhetest test --INT --random 10",
"fhetest test --DOUBLE --exhaust 10",
)
def apply(args: List[String]): Unit = args match {
case Nil => println("No argument given.")
case encTypeString :: remain => {
case encTypeString :: stgString :: remain => {
val nOpt = remain match {
case nString :: _ =>
Some(nString.toInt)
case _ => None
}
val encType = parseEncType(encTypeString)
val generator = Generate(encType)
val strategy = parseStrategy(stgString)
val generator = Generate(encType, strategy)
val programs = generator(nOpt).map(T2Program(_))
val backendList = List(Backend.SEAL, Backend.OpenFHE)
// TODO: temporary encParams. Fix after having parameter genernation.
Expand All @@ -216,5 +218,6 @@ case object CmdTest extends Command("test") {
println("=" * 80)
}
}
case _ => println("EncType and Strategy are required.")
}
}
7 changes: 6 additions & 1 deletion src/main/scala/fhetest/Generate/TemplateGenerator.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fhetest.Generate

import scala.util.Random

// Template Generation Strategy
enum Strategy:
case Exhaustive, Random
Expand Down Expand Up @@ -32,6 +34,9 @@ object ExhaustiveGenerator extends TemplateGenerator {

object RandomGenerator extends TemplateGenerator {
def generateTemplates(): LazyList[Template] = {
???
def randomTemplateOfSize(n: Int): Template = {
(1 to n).map(_ => Random.shuffle(allAbsStmts).head).toList
}
LazyList.from(1).map(randomTemplateOfSize)
}
}
1 change: 1 addition & 0 deletions src/main/scala/fhetest/Phase/Generate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ case class Generate(
val allTemplates = tempGen.generateTemplates()

def apply(nOpt: Option[Int]): LazyList[String] = {
println(s"Genrating Strategy: $strategy")
val templates = nOpt match {
case Some(n) => allTemplates.take(n)
case None => allTemplates
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 @@ -10,6 +10,7 @@ import scala.util.Try
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import java.util.concurrent.atomic.AtomicInteger
import fhetest.Generate.Strategy

enum Backend(val name: String):
case SEAL extends Backend("SEAL")
Expand Down Expand Up @@ -61,6 +62,11 @@ def parseWordSizeAndEncParams(args: List[String]): (Option[Int], EncParams) = {

(wordSizeOpt, EncParams(ringDim, mulDepth, plainMod))
}
def parseStrategy(sString: String): Strategy =
parsePrefixedArg(sString) match
case Some("exhaust") => Strategy.Exhaustive
case Some("random") => Strategy.Random
case _ => throw new Exception("Invalid strategy")

def getWorkspaceDir(backend: Backend): String = backend match
case Backend.SEAL => fhetest.SEAL_DIR
Expand Down

0 comments on commit 5c879de

Please sign in to comment.