Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tdammers authored and iquerejeta committed Nov 29, 2022
1 parent cab9777 commit dad62bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Expand Up @@ -120,8 +120,10 @@ module Cardano.Crypto.EllipticCurve.BLS12_381.Internal

-- * Utility
, integerAsCStr
, integerAsCStrL
, cstrToInteger
, integerToBS
, padBS

-- * P1/G1 operations
, onCurve
Expand Down Expand Up @@ -443,7 +445,7 @@ cstrToInteger p l = do
go n ptr = do
val <- peek ptr
res <- go (pred n) (plusPtr ptr 1)
return $ shiftL res 8 .|. fromIntegral val
return $ res .|. shiftL (fromIntegral val) (8 * pred n)

integerToBS :: Integer -> ByteString
integerToBS 0 = BS.empty
Expand Down
10 changes: 10 additions & 0 deletions cardano-crypto-tests/src/Test/Crypto/EllipticCurve.hs
Expand Up @@ -25,6 +25,7 @@ import Test.Tasty.HUnit (testCase, assertBool, assertEqual)
import Data.Proxy (Proxy (..))
import qualified Data.ByteString as BS
import System.IO.Unsafe (unsafePerformIO)
import Data.Bits (shiftL)

tests :: TestTree
tests =
Expand All @@ -45,6 +46,15 @@ testUtil name =
\n ->
n >= 0 ==>
n === unsafePerformIO (BLS.integerAsCStr n BLS.cstrToInteger)
, testProperty "Integer / C-String 32 round-trip" $
\n ->
n >= 0 ==>
n < (1 `shiftL` 32 * 8) ==>
n === unsafePerformIO (BLS.integerAsCStrL 32 n BLS.cstrToInteger)
, testProperty "padBS min length" $ \n bsw ->
BS.length (BLS.padBS n (BS.pack bsw)) >= n
, testProperty "padBS adds zeroes to front" $ \bsw ->
BS.index (BLS.padBS (length bsw + 1) (BS.pack bsw)) 0 === 0
, testCase "integerToBS" $ do
assertEqual "0x1234" (BS.pack [0x12, 0x34]) (BLS.integerToBS 0x1234)
assertEqual "0x12345678" (BS.pack [0x12, 0x34, 0x56, 0x78]) (BLS.integerToBS 0x12345678)
Expand Down

0 comments on commit dad62bd

Please sign in to comment.