Created
April 11, 2017 03:41
-
-
Save kazu-yamamoto/25b7b4ee7884e30a93355e20f5efe0ac to your computer and use it in GitHub Desktop.
A test of P256 C backend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE BangPatterns #-} | |
-- % ghc -Wall -threaded -O A.hs | |
-- % ./A +RTS -N2 | |
-- | |
-- illegal hardware instruction | |
-- segmentation fault | |
module Main where | |
import Control.Concurrent | |
import Crypto.PubKey.ECC.P256 | |
import qualified Data.ByteString.Char8 as B | |
import Data.Hex | |
repeatN :: Int | |
repeatN = 10000 | |
main :: IO () | |
main = do | |
var0 <- newEmptyMVar | |
var1 <- newEmptyMVar | |
_ <- forkIO $ server var0 var1 | |
client var0 var1 | |
client :: MVar Point -> MVar Point -> IO () | |
client var0 var1 = loop repeatN | |
where | |
loop 0 = return () | |
loop !n = do | |
clientPrivate <- scalarGenerate | |
let clientPublic = toPoint clientPrivate | |
putMVar var0 clientPublic | |
serverPublic <- takeMVar var1 | |
let clientSecret = pointDh clientPrivate serverPublic | |
B.putStrLn $ hex clientSecret | |
loop (n - 1) | |
server :: MVar Point -> MVar Point -> IO () | |
server var0 var1 = loop repeatN | |
where | |
loop 0 = return () | |
loop !n = do | |
clientPublic <- takeMVar var0 | |
serverPrivate <- scalarGenerate | |
let serverPublic = toPoint serverPrivate | |
putMVar var1 serverPublic | |
let serverSecret = pointDh serverPrivate clientPublic | |
B.putStrLn $ hex serverSecret | |
loop (n - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment