Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update repository #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

- uses: actions/setup-go@v1
with:
go-version: 1.17
go-version: 1.21

- name: Run Tests
run: |
Expand Down
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,15 @@ It contains the following general packages:
- `bip39` implements the [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) specification and mnemonic [word lists](https://github.com/bitcoin/bips/blob/master/bip-0039/bip-0039-wordlists.md).
- `bech32` implements Bech32 addresses based on the format described in [BIP-173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki).
- `ed25519` implements Ed25519 signatures with particular validation rules around edge cases as described in [ZIP-215](https://zips.z.cash/zip-0215).
- `curl` implements the Curl ternary hash function in its batched mode. It relies on [`avo`](https://github.com/mmcloughlin/avo) to generate high-performance x86 assembly.
- `merkle` implements a simple Merkle tree hash.
- `pow` implements the Curl-based proof of work for arbitrary binary data as mentioned in [TIP-12](https://iotaledger.github.io/tips/tips/TIP-0012/tip-0012.html).
- `encoding/b1t6` implements the binary-to-ternary encoding which uses 6 trits to represent each byte.
- `encoding/b1t8` implements the binary-to-ternary encoding which uses 8 trits to represent each byte.
- `migration` implements the migration address computation as described in this document: https://hackmd.io/@wollac/H1tZoCK0w

All these packages are tested against the full test vectors provided in the corresponding specifications.

## Examples
- `bech32` encode and decode addresses using the bech32 address scheme.<br>
Run the example with `go run examples/bech32/main.go` and use `-help` to see the available commands.
- `kdf` shows the private and public key derivation using SLIP-10 and BIP-39 mnemonics + passphrase.<br>
It performs the legacy IOTA seed derivation (as implemented in the Ledger App) based on BIP-32 and the Ed25519 key derivation following SLIP-10.<br>
It performs the Ed25519 key derivation following SLIP-10.<br>
Run with `go run examples/kdf/main.go` and use `-help` to see the available command-line flags.
- `merkle` prints the Merkle tree of several random transaction hashes on the console.<br>
Run with `go run examples/merkle/main.go` and use `-help` to see the available command-line flags.
- `mnemseed` presents the extension of BIP-0039 to decode and encode 81-tryte legacy IOTA seeds using mnemonics.<br>
Run with `go run examples/mnemseed/main.go` and use `-help` to see the available command-line flags.
8 changes: 4 additions & 4 deletions examples/bech32/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"os"
"strings"

"github.com/wollac/iota-crypto-demo/internal/rand"
"github.com/wollac/iota-crypto-demo/pkg/bech32"
"github.com/wollac/iota-crypto-demo/pkg/bech32/address"
"github.com/wollac/iota-crypto-demo/pkg/ed25519"
"github.com/iotaledger/iota-crypto-demo/internal/rand"
"github.com/iotaledger/iota-crypto-demo/pkg/bech32"
"github.com/iotaledger/iota-crypto-demo/pkg/bech32/address"
"github.com/iotaledger/iota-crypto-demo/pkg/ed25519"
)

// default values
Expand Down
58 changes: 7 additions & 51 deletions examples/kdf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ import (
"os"
"strings"

"github.com/iotaledger/iota.go/consts"
"github.com/iotaledger/iota.go/kerl"
"github.com/iotaledger/iota.go/kerl/sha3"
"github.com/iotaledger/iota.go/trinary"
"github.com/wollac/iota-crypto-demo/pkg/bech32/address"
"github.com/wollac/iota-crypto-demo/pkg/bip32path"
"github.com/wollac/iota-crypto-demo/pkg/bip39"
"github.com/wollac/iota-crypto-demo/pkg/slip10"
"github.com/wollac/iota-crypto-demo/pkg/slip10/eddsa"
"github.com/wollac/iota-crypto-demo/pkg/slip10/elliptic"
"github.com/iotaledger/iota-crypto-demo/pkg/bech32/address"
"github.com/iotaledger/iota-crypto-demo/pkg/bip32path"
"github.com/iotaledger/iota-crypto-demo/pkg/bip39"
"github.com/iotaledger/iota-crypto-demo/pkg/slip10"
"github.com/iotaledger/iota-crypto-demo/pkg/slip10/eddsa"
)

var (
Expand Down Expand Up @@ -94,25 +89,10 @@ func run() error {
fmt.Printf(" optional passphrase:\t\"%s\"\n", *passphrase)
fmt.Printf(" master seed (%d-byte):\t%x\n", len(seed), seed)

fmt.Println("\n==> Legacy IOTA Seed Derivation (Ledger App)")

curve := elliptic.Secp256k1()
key, err := slip10.DeriveKeyFromPath(seed, curve, path)
if err != nil {
return fmt.Errorf("failed deriving %s key: %w", curve.Name(), err)
}

fmt.Printf(" SLIP-10 curve seed:\t%s\n", curve.HmacKey())
fmt.Printf(" SLIP-10 address path:\t%s\n", path)

fmt.Printf(" private key (%d-byte):\t%x\n", slip10.PrivateKeySize, key.Key)
fmt.Printf(" chain code (%d-byte):\t%x\n", slip10.ChainCodeSize, key.ChainCode)
fmt.Printf(" IOTA seed (%d-tryte):\t%s\n", consts.HashTrytesSize, iotaSeedFromKey(key))

fmt.Println("\n==> Ed25519 Private Key Derivation")

curve = eddsa.Ed25519()
key, err = slip10.DeriveKeyFromPath(seed, curve, path)
curve := eddsa.Ed25519()
key, err := slip10.DeriveKeyFromPath(seed, curve, path)
if err != nil {
return fmt.Errorf("failed deriving %s key: %w", curve.Name(), err)
}
Expand Down Expand Up @@ -143,27 +123,3 @@ func generateEntropy(size int) ([]byte, error) {
}
return entropy, nil
}

// Legacy IOTA seed derivation as implemented in the blue-app-iota:
// https://github.com/IOTA-Ledger/blue-app-iota/blob/master/docs/specification.md#iota-seed
func iotaSeedFromKey(key *slip10.ExtendedKey) trinary.Hash {
// the 512 bits extended private key (k, c) of the provided address path is then hashed using Keccak-384.
hash := sha3.NewLegacyKeccak384()

k := key.Key.Bytes()
c := key.ChainCode

// as Kerl usually expects multiples of 48 bytes as input, the following 98 bytes are absorbed:
// k[0:32] + c[0:16] + k[16:32] + c[0:32]
hash.Write(k[0:32])
hash.Write(c[0:16])
hash.Write(k[16:32])
hash.Write(c[0:32])

// derive the final 243 trit IOTA seed from the resulting hash
seed, err := kerl.KerlBytesToTrytes(hash.Sum(nil))
if err != nil {
panic(err)
}
return seed
}
61 changes: 29 additions & 32 deletions examples/merkle/README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
Print the Merkle tree (as described in the IOTA protocol RFC-0012) of several random transaction hashes on the console.
Print the Merkle tree (as described in the IOTA protocol RFC-0012 for Chrysalis part 2) of several random message IDs on the console.

```
go run examples/merkle/main.go -hashes 7

==> input tx hashes
d[0]: NOBKDFGZMOWYUKDZITTWBRWA9YPSXCVFENCQFPC9GMJIAIPSSURYIOMYZLGNZXLUAQHHNBSRHNOIJDYZO
d[1]: IPATPTEZSBMFJRDCRPTCVUQWBAVCAXAVZIDEDL9TSILDFWDMIIFPZIYHKRFFZDYQNKBQBVGYSKMLCYBMR
d[2]: MXOIOFOGLIHCHMDRCWAIYCWIUCMGEZWXFJZFWBRCNSNBWIGFJXBCACPKMLLANYNXSGYKANYFTVGTLFXXX
d[3]: EXZTJAXJMZJBBIZGUTMBOEUQDNVHJPXCLFUXNLPLSBATDMKYUZOFMHCOBWUABYDMNGMKIXLIUFXNVY9PN
d[4]: SJXYVFUDCDPPAOALVXDQUKAWLLOQO99OSJQT9TUNILQ9VLFLCZMLZAKUTIZFHOLPMGPYHKMMUUSURIOCF
d[5]: Q9GHMAITEZCWKFIESJARYQYMF9XWFPQTTFRXULLHQDWEZLYBSFYHSLPXEHBORDDFYZRFYFGDCM9VJKEFR
d[6]: GMNECTSPSLSPPEITCHBXSN9KZD9OZPVPOET9TVQJDZMFGN9SGPRPMUQARNXUVKMWAFAKLKWBZLWZCTPCP
==> input message ids
d[0]: 52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649
d[1]: 81855ad8681d0d86d1e91e00167939cb6694d2c422acd208a0072939487f6999
d[2]: eb9d18a44784045d87f3c67cf22746e995af5a25367951baa2ff6cd471c483f1
d[3]: 5fb90badb37c5821b6d95526a41a9504680b4e7c8b763a1b1d49d4955c848621
d[4]: 6325253fec738dd7a9e28bf921119c160f0702448615bbda08313f6a8eb668d2
d[5]: 0bf5059875921e668a5bdf2c7fc4844592d2572bcd0668d2d6c52f5054e2d083
d[6]: 6bf84c7174cb7476364cc3dbd968b0f7172ed85794bb358b0c3b525da1786f9f

==> Merkle tree with 7 leafs
Htri: FYEDPDNYFXZB9XHXQZDXP9CXV9YAUWEYEDKCNYIWW9MWXBHYEXTWVDBXWZQAGDWYT9PBHZTBWC9YYWTYBDTWCAMZMDLWRYHWUXZZJ9QAHBKWDCKAI9C9LBDWVWMV9ZDB
root: d07161bdb535afb7dbb3f5b2fb198ecf715cbd9dfca133d2b48d67b1e11173c6f92bed2f4dca92c36e8d1ef279a0c19ca9e40a113e9f5526090342988f86e53a
├─ node: 1448659e74c870013900a3012842b1e5fb2cfecde299d7bbe272ce0968b95546f7bbce242ebd39cd7ea965bd25c51e007212ecd999af17530ef68843311ef403
│ ├─ node: ea4f73b420757c426e5f166066d9207ca4a49f878a1ba6d420367c7f9b946b6dcb35121b619c374a0a8b647623b391c54087b29401d2a9bc864b9816a53cdf27
│ │ │ ┌ tx hash: NOBKDFGZMOWYUKDZITTWBRWA9YPSXCVFENCQFPC9GMJIAIPSSURYIOMYZLGNZXLUAQHHNBSRHNOIJDYZO
│ │ ├──┴ leaf: 470afd417b1b3cdd4d876f1e636cb41e5a0f2c38d2160348cf0b8971144e5d20b118c08c3f65956f8d98949bf89bea8da3b34fa2cab1fba299512a9e573c0854
│ │ │ ┌ tx hash: IPATPTEZSBMFJRDCRPTCVUQWBAVCAXAVZIDEDL9TSILDFWDMIIFPZIYHKRFFZDYQNKBQBVGYSKMLCYBMR
│ │ └──┴ leaf: efefcba97952a5cad857b53f015c3d95c6c38ef9cc97b4b622a9f9f56b396627a6c3fd6f737428ed9c1487e834abedf83561f58c356071279068bdd53b85ffa8
│ └─ node: 183cc0b9a79965986a12003af8b0be0ee3c3980853a99fb571a39fa394f56cb071db6487029b4d7c6ecdb72ae65fafa9e446c0bdca0f18c7f1eeea5170f5aca4
│ │ ┌ tx hash: MXOIOFOGLIHCHMDRCWAIYCWIUCMGEZWXFJZFWBRCNSNBWIGFJXBCACPKMLLANYNXSGYKANYFTVGTLFXXX
│ ├──┴ leaf: 95200ea45cebbe7b582cf23caf53224be98be9a553d4801ed804715afeb9b4b0db4c6a4b3de9852d2cef0712144196c18a7290936fea48208fb417b8d6fe56d0
│ │ ┌ tx hash: EXZTJAXJMZJBBIZGUTMBOEUQDNVHJPXCLFUXNLPLSBATDMKYUZOFMHCOBWUABYDMNGMKIXLIUFXNVY9PN
│ └──┴ leaf: b162e61d41a83ec238871d2a3ed2fbcfea5001b04b363c704bd3a29923ccfc701850ed9911bad3cf9bcb11c510955f8a16ff06f6cbe8d8c887275a83e9232483
└─ node: 7ee54d71bd7958241bfba8a7817fe8eff006d5d7a84edc7358d0ce5639fc9a6cbf38e77bb96656e37189be922fc04090a5a306988f4d1060c2e4f011ff0b7470
├─ node: f2a80742a2b9f03cbf54878c50c6d79df79fe53809de55f236e9ce45f82a2ed9d4bb3a41f6254e2a24955bd6ce7cde5ff6178836029902819de20d0fce3add87
│ │ ┌ tx hash: SJXYVFUDCDPPAOALVXDQUKAWLLOQO99OSJQT9TUNILQ9VLFLCZMLZAKUTIZFHOLPMGPYHKMMUUSURIOCF
│ ├──┴ leaf: a32b588ed56c6823ab9677c5c910b274886b8bd49db9e3a5af24bddbad83dd2b801c744c3b690c99dab3d33a156bb076b4c047163010064235b9268568121e78
│ │ ┌ tx hash: Q9GHMAITEZCWKFIESJARYQYMF9XWFPQTTFRXULLHQDWEZLYBSFYHSLPXEHBORDDFYZRFYFGDCM9VJKEFR
│ └──┴ leaf: 7405aa17eaec13f23b9dc2faf635bf2688bdb7582296880453a930b0716265c93a12b823d5b2ed0a62459f80df3f347b44e7a8d290ff6c1051f34afe63d3827d
│ ┌ tx hash: GMNECTSPSLSPPEITCHBXSN9KZD9OZPVPOET9TVQJDZMFGN9SGPRPMUQARNXUVKMWAFAKLKWBZLWZCTPCP
└──┴ leaf: 282f3dc49046480e118f697bc90d37f19efb633d6e92cb27e53c4a3c69735e6e66e698b810c20e8e7c4d5b5f0b04946fc779a0c817ee587c01f80e44d3e69f84
root: bf67ce7ba23e8c0951b5abaec4f5524360d2c26d971ff226d3359fa70cdb0beb
├─ node: 03bcbb3cf4314eab2f5ae68c767ff0a5fec4573c865728231f71d596fd867b56
│ ├─ node: ae4505f4cfae93586e23958ca88d35d2f34d43def49786b6d0d4224b819f4cda
│ │ │ ┌ msg id: 52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649
│ │ ├──┴ leaf: 3d1399c64ff0ae6a074afa4cd2ce4eab8d5c499c1da6afdd1d84b7447cc00544
│ │ │ ┌ msg id: 81855ad8681d0d86d1e91e00167939cb6694d2c422acd208a0072939487f6999
│ │ └──┴ leaf: 83b0b255014e9a3656f0004a3f17943a20b715ef9c3e7cb85a6b2abac15e00d0
│ └─ node: 54d51291aca22ce5b04cd3e6584fa3026ebe86ef86f0a6dfb47ab843801d4b38
│ │ ┌ msg id: eb9d18a44784045d87f3c67cf22746e995af5a25367951baa2ff6cd471c483f1
│ ├──┴ leaf: ad4bc0a34b27f37810f2ff3a8177ecc98402f8f59a06270f9d285fdf764e45fe
│ │ ┌ msg id: 5fb90badb37c5821b6d95526a41a9504680b4e7c8b763a1b1d49d4955c848621
│ └──┴ leaf: ffb3a7c6bea8f9fdcfb26f4701ad6e912a6076e1a40663607dbe110ebfc9a571
└─ node: ce22d5bc728023e7ab6a9eb8f58baf62b9565fc8baeef4b377daa6709dbe598c
├─ node: e14c8af1258005cd0dbed88f0c5885c6988f319bb8f24272a7495592b873c169
│ │ ┌ msg id: 6325253fec738dd7a9e28bf921119c160f0702448615bbda08313f6a8eb668d2
│ ├──┴ leaf: 1c062628a7a147cc6a4defa655ce6c4ae5b838b4b4cd81b12e8924b5b4b5cca6
│ │ ┌ msg id: 0bf5059875921e668a5bdf2c7fc4844592d2572bcd0668d2d6c52f5054e2d083
│ └──┴ leaf: 2ef4e2ad06b8c8ae1fd4b28b5ed166829533fbfff1f6c14218358537da277fa3
│ ┌ msg id: 6bf84c7174cb7476364cc3dbd968b0f7172ed85794bb358b0c3b525da1786f9f
└──┴ leaf: 7ec774ebc33ed4ca298e8a1cf1f569e36c6784467d63b055efd7612abe2858a4
```
43 changes: 21 additions & 22 deletions examples/merkle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ package main
import (
"crypto"
"encoding"
"encoding/hex"
"flag"
"fmt"
"math/bits"
"math/rand"
"strings"

"github.com/iotaledger/iota.go/consts"
"github.com/iotaledger/iota.go/encoding/t5b1"
"github.com/iotaledger/iota.go/trinary"
"github.com/wollac/iota-crypto-demo/pkg/merkle"
"github.com/iotaledger/iota-crypto-demo/pkg/merkle"

_ "golang.org/x/crypto/blake2b" // BLAKE2b_512 is the default hashing algorithm
_ "golang.org/x/crypto/blake2b" // BLAKE2b_256 is the default hashing algorithm
)

var (
Expand All @@ -25,34 +23,35 @@ var (
)
)

type Hash trinary.Hash
type ID [32]byte

func (h Hash) MarshalBinary() ([]byte, error) { return t5b1.EncodeTrytes(trinary.Trytes(h)), nil }
func (h Hash) String() string { return string(h) }
func (i ID) MarshalBinary() ([]byte, error) {
return i[:], nil
}

func (i ID) String() string {
return hex.EncodeToString(i[:])
}

func main() {
flag.Parse()

var hashes []encoding.BinaryMarshaler
var data []encoding.BinaryMarshaler
for i := 0; i < *numHashes; i++ {
hashes = append(hashes, randomHash(consts.HashTrytesSize))
data = append(data, randomID())
}

fmt.Println("==> input tx hashes")
for i := range hashes {
fmt.Printf(" d[%d]: %s\n", i, hashes[i])
fmt.Println("==> input message ids")
for i := range data {
fmt.Printf(" d[%d]: %s\n", i, data[i])
}
fmt.Printf("\n==> Merkle tree with %d leafs\n", len(hashes))
printTree(merkle.NewHasher(crypto.BLAKE2b_512), hashes)
fmt.Printf("\n==> Merkle tree with %d leafs\n", len(data))
printTree(merkle.NewHasher(crypto.BLAKE2b_256), data)
}

func randomHash(n int) Hash {
var trytes strings.Builder
trytes.Grow(n)
for i := 0; i < n; i++ {
trytes.WriteByte(consts.TryteAlphabet[rand.Intn(len(consts.TryteAlphabet))])
}
return Hash(trytes.String())
func randomID() (id ID) {
rand.Read(id[:])
return id
}

// printTree pretty prints the Merkle tree.
Expand Down
34 changes: 0 additions & 34 deletions examples/merkle/pt2/README.md

This file was deleted.

Loading
Loading