BinaryCompare.hs
1 2 module Circuit.BinaryCompare where 3 4 -------------------------------------------------------------------------------- 5 6 import CircuitCommon 7 8 -------------------------------------------------------------------------------- 9 -- global parameters 10 11 circomFile :: FilePath 12 circomFile = circuitLibSourceDir </> "binary_compare.circom" 13 14 -- | comparing @n@-bit integers 15 type GP = Int 16 17 mainComponent :: GP -> MainComponent 18 mainComponent n = MainComponent 19 { _templateName = "BinaryCompare" 20 , _templateParams = [n] 21 , _publicInputs = ["A","B"] 22 } 23 24 -------------------------------------------------------------------------------- 25 -- test cases and expected semantics 26 27 type TestCase = (Integer,Integer) 28 type Output = Int 29 30 cmp :: Integer -> Integer -> Int 31 cmp a b = case compare a b of 32 LT -> -1 33 EQ -> 0 34 GT -> 1 35 36 semantics :: GP -> TestCase -> Expected Output 37 semantics n (a,b) = Expecting $ cmp a b 38 39 testCases :: GP -> [TestCase] 40 testCases n = [ (a,b) | a<-[0..2^n-1] , b<-[0..2^n-1] ] 41 42 -------------------------------------------------------------------------------- 43 -- inputs and outputs 44 45 inputs :: GP -> TestCase -> Inputs Name Integer 46 inputs n (a,b) = Inputs $ toMapping "A" (toBitsLE' n a) 47 <> toMapping "B" (toBitsLE' n b) 48 49 outputs :: Output -> Outputs Name Integer 50 outputs y = Outputs $ toMapping "out" y 51 52 -------------------------------------------------------------------------------- 53 54 spec :: GP -> TestSpec TestCase Output 55 spec n = TestSpec circomFile (mainComponent n) (inputs n) outputs (semantics n) (testCases n) 56 57 specs :: [ ( GP, TestSpec TestCase Output) ] 58 specs = [ (n, spec n) | n <- [4,5,7] ] 59 60 -------------------------------------------------------------------------------- 61