-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
BIP32_Ed25519
module importing from cardano-crypto
- Loading branch information
1 parent
eac1706
commit 4a1abb4
Showing
4 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
lib/customer-deposit-wallet-pure/agda/Cardano/Wallet/Address/BIP32_Ed25519.agda
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
{-# OPTIONS --erasure #-} | ||
|
||
module Cardano.Wallet.Address.BIP32_Ed25519 where | ||
|
||
open import Haskell.Prelude | ||
|
||
open import Haskell.Data.ByteString using | ||
( ByteString | ||
) | ||
open import Haskell.Data.Word.Odd using | ||
( Word31 | ||
) | ||
|
||
{-# FOREIGN AGDA2HS | ||
{-# LANGUAGE UnicodeSyntax #-} | ||
import Cardano.Crypto.Wallet | ||
( XPrv | ||
, XPub | ||
, XSignature | ||
, toXPub | ||
) | ||
import Data.ByteString | ||
( ByteString | ||
) | ||
import Data.Maybe | ||
( fromJust | ||
) | ||
import Data.Word | ||
( Word32 | ||
) | ||
import Data.Word.Odd | ||
( Word31 | ||
) | ||
import qualified Cardano.Crypto.Wallet as CC | ||
import qualified Data.ByteString as BS | ||
#-} | ||
|
||
dummy : Int | ||
dummy = 12 -- needed for Agda2hs to add sufficient imports | ||
|
||
{----------------------------------------------------------------------------- | ||
Extended private and public keys | ||
------------------------------------------------------------------------------} | ||
|
||
-- TODO: Extend to encrypted keys | ||
postulate | ||
XPub : Set -- plaintext public key | ||
XPrv : Set -- plaintext private key | ||
|
||
toXPub : XPrv → XPub | ||
|
||
XSignature : Set | ||
sign : XPrv → ByteString → XSignature | ||
verify : XPub → ByteString → XSignature → Bool | ||
|
||
prop-verify-sign | ||
: ∀ (xprv : XPrv) | ||
(msg : ByteString) | ||
→ let xpub = toXPub xprv | ||
in verify xpub msg (sign xprv msg) ≡ True | ||
|
||
{-# FOREIGN AGDA2HS | ||
sign :: XPrv → ByteString → XSignature | ||
sign = CC.sign BS.empty | ||
verify :: XPub → ByteString → XSignature → Bool | ||
verify = CC.verify | ||
#-} | ||
|
||
{----------------------------------------------------------------------------- | ||
Key derivation | ||
------------------------------------------------------------------------------} | ||
|
||
postulate | ||
deriveXPubSoft : XPub → Word31 → XPub | ||
deriveXPrvSoft : XPrv → Word31 → XPrv | ||
deriveXPrvHard : XPrv → Word31 → XPrv | ||
|
||
prop-derive-soft | ||
: ∀ (xprv : XPrv) | ||
(ix : Word31) | ||
→ deriveXPubSoft (toXPub xprv) ix | ||
≡ toXPub (deriveXPrvSoft xprv ix) | ||
|
||
-- The following properties about injectivity | ||
-- are not true in the strict sense, | ||
-- only cryptographically hard. | ||
prop-deriveXPubSoft-injective | ||
: ∀ (xpub : XPub) | ||
(ix1 ix2 : Word31) | ||
→ deriveXPubSoft xpub ix1 ≡ deriveXPubSoft xpub ix2 | ||
→ ix1 ≡ ix2 | ||
|
||
prop-deriveXPrvSoft-injective | ||
: ∀ (xprv : XPrv) | ||
(ix1 ix2 : Word31) | ||
→ deriveXPrvSoft xprv ix1 ≡ deriveXPrvSoft xprv ix2 | ||
→ ix1 ≡ ix2 | ||
|
||
prop-deriveXPrvHard-injective | ||
: ∀ (xprv : XPrv) | ||
(ix1 ix2 : Word31) | ||
→ deriveXPrvHard xprv ix1 ≡ deriveXPrvHard xprv ix2 | ||
→ ix1 ≡ ix2 | ||
|
||
{-# FOREIGN AGDA2HS | ||
word32fromWord31 :: Word31 → Word32 | ||
word32fromWord31 = fromInteger . toInteger | ||
deriveXPubSoft :: XPub → Word31 → XPub | ||
deriveXPubSoft xpub ix = | ||
fromJust | ||
(CC.deriveXPub | ||
CC.DerivationScheme2 | ||
xpub | ||
(word32fromWord31 ix) | ||
) | ||
-- deriveXPub always returns Just on Word31 | ||
deriveXPrvSoft :: XPrv → Word31 → XPrv | ||
deriveXPrvSoft xprv ix = | ||
CC.deriveXPrv | ||
CC.DerivationScheme2 | ||
BS.empty | ||
xprv | ||
(word32fromWord31 ix) | ||
deriveXPrvHard :: XPrv → Word31 → XPrv | ||
deriveXPrvHard xprv ix = | ||
CC.deriveXPrv | ||
CC.DerivationScheme2 | ||
BS.empty | ||
xprv | ||
(0x80000000 + word32fromWord31 ix) | ||
#-} |
2 changes: 2 additions & 0 deletions
2
lib/customer-deposit-wallet-pure/agda/Cardano/Wallet/Deposit/Everything.agda
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
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
58 changes: 58 additions & 0 deletions
58
lib/customer-deposit-wallet-pure/haskell/Cardano/Wallet/Address/BIP32_Ed25519.hs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{-# LANGUAGE UnicodeSyntax #-} | ||
|
||
module Cardano.Wallet.Address.BIP32_Ed25519 where | ||
|
||
|
||
import Cardano.Crypto.Wallet | ||
( XPrv | ||
, XPub | ||
, XSignature | ||
, toXPub | ||
) | ||
import Data.ByteString | ||
( ByteString | ||
) | ||
import Data.Maybe | ||
( fromJust | ||
) | ||
import Data.Word.Odd | ||
( Word31 | ||
) | ||
import qualified Cardano.Crypto.Wallet as CC | ||
import qualified Data.ByteString as BS | ||
|
||
sign :: XPrv ->ByteString ->XSignature | ||
sign = CC.sign mempty | ||
|
||
verify :: XPub ->ByteString ->XSignature ->Bool | ||
verify = CC.verify | ||
|
||
word32fromWord31 :: Word31 ->Word32 | ||
word32fromWord31 = fromInteger . toInteger | ||
|
||
deriveXPubSoft :: XPub ->Word31 ->XPub | ||
deriveXPubSoft xpub ix = | ||
fromJust | ||
(CC.deriveXPub | ||
CC.DerivationScheme2 | ||
xpub | ||
(word32fromWord31 ix) | ||
) | ||
-- deriveXPub always returns Just on Word31 | ||
|
||
deriveXPrvSoft :: XPrv ->Word31 ->XPrv | ||
deriveXPrvSoft xprv ix = | ||
CC.deriveXPrv | ||
CC.DerivationScheme2 | ||
mempty | ||
xprv | ||
(word32fromWord31 ix) | ||
|
||
deriveXPrvHard :: XPrv ->Word31 ->XPrv | ||
deriveXPrvHard xprv ix = | ||
CC.deriveXPrv | ||
CC.DerivationScheme2 | ||
mempty | ||
xprv | ||
(0x80000000 + word32fromWord31 ix) | ||
|