/ tests / TestGoldilocksExt.hs
TestGoldilocksExt.hs
  1  
  2  
  3  module TestGoldilocksExt where
  4  
  5  --------------------------------------------------------------------------------
  6  
  7  import Control.Monad
  8  import System.IO.Unsafe
  9  
 10  import Semantics
 11  import Common
 12  
 13  --------------------------------------------------------------------------------
 14  -- global parameters
 15  
 16  circomFile :: FilePath
 17  circomFile = circuitSourceDir </> "test_wrapper_ext.circom"
 18  
 19  data Op
 20    = Neg
 21    | Add
 22    | Sub
 23    | Sqr
 24    | Mul
 25    | Inv
 26    | Div
 27    deriving (Eq,Show,Bounded,Enum)
 28  
 29  enumerateOps :: [Op]
 30  enumerateOps = enumFromTo minBound maxBound
 31  
 32  ----------------------------------------
 33  
 34  mainComponent :: Op -> MainComponent
 35  mainComponent op = 
 36    case op of
 37      Neg -> unary  "NegExt"
 38      Add -> binary "AddExt"
 39      Sub -> binary "SubExt"
 40      Sqr -> unary  "SqrExt"
 41      Mul -> binary "MulExt"
 42      Inv -> unary  "InvExt"
 43      Div -> binary "DivExt"
 44    where
 45  
 46      unary name = MainComponent 
 47        { _templateName   = name ++ "Wrapper"
 48        , _templateParams = []
 49        , _publicInputs   = ["A"]
 50        }
 51  
 52      binary name = MainComponent 
 53        { _templateName   = name ++ "Wrapper"
 54        , _templateParams = []
 55        , _publicInputs   = ["A","B"]
 56        }
 57  
 58  --------------------------------------------------------------------------------
 59  -- test cases and expected semantics
 60  
 61  type TestCase1 =  (Int,Int)
 62  type TestCase2 = ((Int,Int),(Int,Int))
 63  
 64  type Output = (Int,Int)
 65  
 66  nNestCases = 10
 67  
 68  randomTestCasesUnary :: [TestCase1]
 69  randomTestCasesUnary = unsafePerformIO $ replicateM nNestCases rndFExt
 70  
 71  randomTestCasesBinary :: [TestCase2]
 72  randomTestCasesBinary = unsafePerformIO $ replicateM nNestCases $ do { x <- rndFExt ; y <- rndFExt ; return (x,y) }
 73  
 74  ----------------------------------------
 75  
 76  semantics_neg :: FExt -> Expected FExt
 77  semantics_neg x = Expecting $ Semantics.negExt x
 78  
 79  semantics_add :: (FExt,FExt) -> Expected FExt
 80  semantics_add (x,y) = Expecting $ Semantics.addExt x y
 81  
 82  semantics_sub :: (FExt,FExt) -> Expected FExt
 83  semantics_sub (x,y) = Expecting $ Semantics.subExt x y
 84  
 85  semantics_sqr :: FExt -> Expected FExt
 86  semantics_sqr x = Expecting $ Semantics.sqrExt x
 87  
 88  semantics_mul :: (FExt,FExt) -> Expected FExt
 89  semantics_mul (x,y) = Expecting $ Semantics.mulExt x y
 90  
 91  semantics_inv :: FExt -> Expected FExt
 92  semantics_inv x = Expecting $ Semantics.invExt x
 93  
 94  semantics_div :: (FExt,FExt) -> Expected FExt
 95  semantics_div (x,y) = Expecting $ Semantics.divExt x y
 96  
 97  --------------------------------------------------------------------------------
 98  -- inputs and outputs
 99  
100  inputsA :: TestCase1 -> Inputs Name Integer
101  inputsA a = Inputs $  toMapping "A" a
102                      
103  inputsAB :: TestCase2 -> Inputs Name Integer
104  inputsAB (a,b) = Inputs $  toMapping "A" a
105                          <> toMapping "B" b
106  
107  outputsC :: Output -> Outputs Name Integer
108  outputsC y = Outputs $ toMapping "C" y
109  
110  --------------------------------------------------------------------------------
111  
112  specUnary :: Op -> (FExt -> Expected FExt) -> TestSpec TestCase1 Output
113  specUnary op semantics = TestSpec circomFile (mainComponent op) inputsA outputsC semantics randomTestCasesUnary
114  
115  specBinary :: Op -> ((FExt,FExt) -> Expected FExt) -> TestSpec TestCase2 Output
116  specBinary op semantics = TestSpec circomFile (mainComponent op) inputsAB outputsC semantics randomTestCasesBinary
117  
118  --------------------------------------------------------------------------------