Сryptocurrency development analysis

Description

Relacing hs-ed25519 with cryptonite which has support of Ed25519.
After two commits popped up the problem when cardano-sl-crypto testing:
✓ golden_RedeemPublicKey passed 1 test.
✗ golden_RedeemSecretKey failed after 1 test.

┏━━ test/Test/Pos/Crypto/Bi.hs ━━━
144 ┃ golden_RedeemSecretKey :: Property
145 ┃ golden_RedeemSecretKey = goldenTestBi rsk «test/golden/RedeemSecretKey»
┃ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
┃ │ — «00: 58404b6d7977346c4453453553346653»
┃ │ + «00: 58204b6d7977346c4453453553346653»
┃ │ «10: 483665744e6f756958657a4379456a4b»
┃ │ — «20: 6333538960a916726b09f05c9d4d4a92»
┃ │ + «20: 6333»
┃ │ — «30: 2b85b4e11acfcc1434673d95019b7a9a»
┃ │ — «40: 52d2»
146 ┃ where Just rsk = snd <$> redeemDeterministicKeyGen (getBytes 0 32)

This failure can be reproduced by running:
> recheck (Size 0) (Seed 14648883807316199488 6288899416044678831) golden_RedeemSecretKey

✓ golden_RedeemSignature passed 1 test.

It was suprising as *Ed25519Legacy.createKeypairFromSeed_ seed* should be directly replaced with *Ed25519.secretKey $ (BA.convert seed :: BA.Bytes)*

I investigated further and saw that:
1. hs-ed25519 with (getBytes 0 32) gives:
PublicKey: S\137`\169\SYNrk\t\240\\\157MJ\146+\133\180\225\SUB\207\204\DC44g=\149\SOH\155z\154R\210
SecretKey= Kmyw4lDSE5S4fSH6etNouiXezCyEjKc3S\137`\169\SYNrk\t\240\\\157MJ\146+\133\180\225\SUB\207\204\DC44g=\149\SOH\155z\154R\210
2. cryptonite `Ed25519.secretKey $ (BA.convert seed :: BA.Bytes)` gave me:
PublicKey: S\137`\169\SYNrk\t\240\\\157MJ\146+\133\180\225\SUB\207\204\DC44g=\149\SOH\155z\154R\210
SecretKey: Kmyw4lDSE5S4fSH6etNouiXezCyEjKc3
(here converted from ScrubbedBytes to ByteString)

As indicated by Vincent Hanquez hs-ed25519 uses normalized extended secret key, whereas cryptonite uses the normal plain secret key directly, so effectively ed25519’s seed is cryptonite’s secret key

My solution: when implementing Bi instance append publickey bytestring and publickey bytestring when encoding, and substrating publickey bytestring when decoding (taking advantage of constant length of Ed25519.secretKeySize)

I checked:
1. stack test cardano-sl-crypto
2. stack test cardano-crypto
and all tests passed

## Linked issue

https://iohk.myjetbrains.com/youtrack/issue/CO-343

## Type of change

— [ ] 🐞 Bug fix (non-breaking change which fixes an issue)
— [ ] 🛠 New feature (non-breaking change which adds functionality)
— [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to change)
— [x] 🏭 Refactoring that does not change existing functionality but does improve things like code readability, structure etc
— [ ] 🔨 New or improved tests for existing code
— [ ] ⛑ git-flow chore (backport, hotfix, etc)

## Developer checklist

— [x] I have read the [style guide](https://github.com/input-output-hk/cardano-sl/blob/develop/docs/style-guide.md) document, and my code follows the code style of this project.
— [x] If my code deals with exceptions, it follows the [guidelines](https://github.com/input-output-hk/cardano-sl/blob/develop/docs/exceptions.md).
— [x] I have updated any documentation accordingly, if needed. Documentation changes can be reflected in opening a PR on [cardanodocs.com](https://github.com/input-output-hk/cardanodocs.com), amending the inline [Haddock](https://www.haskell.org/haddock/) comments, any relevant README file or one of the document listed in the [docs](https://github.com/input-output-hk/cardano-sl/tree/develop/docs) directory.

## Testing checklist

— [ ] I have added tests to cover my changes.
— [x] All new and existing tests passed.

## QA Steps
1. stack test cardano-sl-crypto
2. stack test cardano-crypto

## Screenshots (if available)

You can view, comment on, or merge this pull request online at:

https://github.com/input-output-hk/cardano-sl/pull/3336

— Commit Summary —

* [CO-343] Replace hs-ed25519 dependency with cryptonite
* [CO-343] Applying hlint suggestions and simplyfing code
* [CO-343] Accomodating normal plain secret key in cryptonite in place of hs-ed25519 normalized extended secret key plus removing hs-ed25519 from default nix

— File Changes —

M crypto/Pos/Crypto/Orphans.hs (136)
M crypto/Pos/Crypto/SafeCopy.hs (7)
M crypto/Pos/Crypto/Signing/Redeem.hs (23)
M crypto/Pos/Crypto/Signing/Types/Redeem.hs (17)
M crypto/cardano-sl-crypto.cabal (1)
M explorer/scripts/fetch_dependencies.sh (1)
M explorer/scripts/generate_cabal2nix.sh (1)
M lib/cardano-sl.cabal (1)
M pkgs/default.nix (17)
M stack.yaml (6)

— Patch Links —

https://github.com/input-output-hk/cardano-sl/pull/3336.patch
https://github.com/input-output-hk/cardano-sl/pull/3336.diff

Добавить комментарий