Skip to content

Commit

Permalink
Merge pull request #241 from hexresearch/kh/clean
Browse files Browse the repository at this point in the history
Minimal cleanup of sigma expression
  • Loading branch information
anton-k authored Feb 3, 2021
2 parents cfa32bb + a11f664 commit 6f6ef1d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 26 deletions.
1 change: 1 addition & 0 deletions hschain-utxo-compiler/hschain-utxo-compiler.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ library
, aeson
, bytestring
, hschain-utxo-lang
, hschain-crypto
, optparse-applicative
, serialise
, text
Expand Down
3 changes: 2 additions & 1 deletion hschain-utxo-compiler/src/Hschain/Utxo/Compiler/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Hschain.Utxo.Lang.Pretty
import Hschain.Utxo.Lang.Infer
import Hschain.Utxo.Lang.Lib.Base

import HSChain.Crypto (encodeBase58)
import qualified Hschain.Utxo.Lang.Sigma as Sigma

import qualified Codec.Serialise as S
Expand Down Expand Up @@ -86,7 +87,7 @@ getPublicKey input output = do
eSecret <- fmap S.deserialiseOrFail $ LB.readFile input
either (const failToReadInput) onSecret eSecret
where
onSecret secret = saveKey output $ Sigma.publicKeyToText $ Sigma.getPublicKey secret
onSecret secret = saveKey output $ encodeBase58 $ Sigma.getPublicKey secret

failToReadInput = failToRead "secret" input

Expand Down
4 changes: 2 additions & 2 deletions hschain-utxo-lang/src/Hschain/Utxo/Lang/Compile/Hask/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ toSigma loc = foldFix $ \case
toQOp op = H.QVarOp loc (toQName $ VarName loc op)

fromProofInput = \case
Sigma.InputDLog pk -> app loc "pk" [toText loc $ publicKeyToText pk]
Sigma.InputDTuple dt -> app loc "proofDTuple" $ fmap (toText loc . publicKeyToText)
Sigma.InputDLog pk -> app loc "pk" [toText loc $ encodeBase58 pk]
Sigma.InputDTuple dt -> app loc "proofDTuple" $ fmap (toText loc . encodeBase58)
[ Sigma.dtuple'g dt
, Sigma.dtuple'g_x dt
, Sigma.dtuple'g_y dt
Expand Down
26 changes: 3 additions & 23 deletions hschain-utxo-lang/src/Hschain/Utxo/Lang/Sigma.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ module Hschain.Utxo.Lang.Sigma(
, SigmaF(..)
, newProof
, verifyProof
, publicKeyFromText
, publicKeyToText
, emptyProofEnv
, proofEnvFromKeys
, newSecret
, newKeyPair
Expand Down Expand Up @@ -62,11 +59,9 @@ import Data.ByteString (ByteString)
import Data.Boolean
import Data.Bifunctor
import Data.Data
import Data.Default
import Data.Either
import Data.Fix
import Data.Functor.Classes (Eq1(..))
import Data.Maybe
import Data.Set (Set)
import Data.Text (Text)
import Data.Eq.Deriving
Expand Down Expand Up @@ -119,9 +114,6 @@ newtype SigMessage = SigMessage (Hash SHA256)
deriving anyclass (Serialise)
deriving (ToJSON, FromJSON, ToJSONKey, FromJSONKey) via (ViaBase58 "SigMessage" ByteString)

instance Default SigMessage where
def = fromJust $ decodeFromBS ""

-- | Generate new private key.
newSecret :: IO Secret
newSecret = Crypto.generatePrivKey
Expand All @@ -148,14 +140,6 @@ getPublicKey = Sigma.getPublicKey
toProofEnv :: [KeyPair] -> ProofEnv
toProofEnv ks = Sigma.Env ks

-- | Parse public key from text.
publicKeyFromText :: Text -> Maybe PublicKey
publicKeyFromText = serialiseFromText

-- | Convert public key to text.
publicKeyToText :: PublicKey -> Text
publicKeyToText = serialiseToText

instance Serialise a => ToJSON (Sigma a) where
toJSON = serialiseToJSON

Expand Down Expand Up @@ -264,9 +248,9 @@ toSigmaExpr :: Sigma a -> Either Bool (Sigma.SigmaE () a)
toSigmaExpr a = (maybe (Left False) Right . toPrimSigmaExpr) =<< eliminateSigmaBool a

toSigmaExprOrFail :: Sigma a -> Either Text (Sigma.SigmaE () a)
toSigmaExprOrFail a = bimap catchBoolean id $ toSigmaExpr a
where
catchBoolean = const "Expression is constant boolean. It is not a sigma-expression"
toSigmaExprOrFail
= first (const "Expression is constant boolean. It is not a sigma-expression")
. toSigmaExpr

toPrimSigmaExpr :: Sigma a -> Maybe (Sigma.SigmaE () a)
toPrimSigmaExpr = foldFix $ \case
Expand All @@ -275,10 +259,6 @@ toPrimSigmaExpr = foldFix $ \case
SigmaOr as -> fmap (Sigma.OR ()) $ sequence as
SigmaBool _ -> Nothing

-- | Empty proof environment. It holds no keys.
emptyProofEnv :: ProofEnv
emptyProofEnv = Sigma.Env []

-- | Wrapper to contruct proof environment from list of key-pairs.
proofEnvFromKeys :: [KeyPair] -> ProofEnv
proofEnvFromKeys = Sigma.Env
Expand Down

0 comments on commit 6f6ef1d

Please sign in to comment.