Skip to content

Instantly share code, notes, and snippets.

@joachifm
Created November 29, 2014 22:02
Show Gist options
  • Save joachifm/66b68c06f0643e22f67d to your computer and use it in GitHub Desktop.
Save joachifm/66b68c06f0643e22f67d to your computer and use it in GitHub Desktop.
Commit to a message without leaking information (?)
{-# LANGUAGE OverloadedStrings #-}
module Commitment ( Message, Commitment, Opening, commit, reveal ) where
import Crypto.Cipher.AES (initAES, encryptCTR, decryptCTR)
import qualified Data.ByteString.Lazy as LB
import qualified Data.ByteString as SB
--
-- Type aliases to avoid mistakes
--
newtype Message = Message SB.ByteString
deriving (Eq)
newtype Commitment = Commitment SB.ByteString
deriving (Eq)
newtype Opening = Opening SB.ByteString
deriving (Eq)
--
-- Commitment and revelation
--
commit :: Message -> IO (Commitment, Opening)
commit (Message msg) = do
-- AES128 in CTR mode with random key & constant IV
key <- (LB.toStrict . LB.take 16) `fmap` LB.readFile "/dev/urandom"
return $! (Commitment $! encryptCTR (initAES key) kNONCE msg, Opening key)
reveal :: Commitment -> Opening -> Message
reveal (Commitment c) (Opening o) = Message $ decryptCTR (initAES o) kNONCE c
kNONCE :: SB.ByteString
kNONCE = SB.replicate 16 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment