forked from laser/go-merkle-tree
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
535 additions
and
274 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,2 +1,74 @@ | ||
# go-merkle-tree | ||
A Merkle tree, implemented in Golang | ||
|
||
> A Merkle Tree, implemented in Golang | ||
Many people have written many things about Merkle Trees. For a good overview (uses, characteristics, etc.), read Marc | ||
Clifton's [_Understanding Merkle Trees - Why use them, who uses them, and how to use them_][1]. | ||
|
||
## Warning | ||
|
||
*Warning: This is alpha software.* | ||
|
||
## Usage | ||
|
||
### Construction | ||
|
||
```go | ||
data := [][]byte{[]byte("alpha"), []byte("beta"), []byte("kappa")} | ||
|
||
tree := CreateTree(Sha256DoubleHash, data) | ||
|
||
fmt.Println(tree.AsString(hex.EncodeToString, 0)) | ||
|
||
/* | ||
output: | ||
(B root: 1add1cfdf5df28414b715199a740f80b7f559bd558a3f0c0186e60149ee86620 | ||
(B root: 65492c0681df09eb403160136bb648de17f67bd8efc441467c0fc23b8d2950e9 | ||
(L root: aa86be763e41db7eaae266afc79ab46d02343c5d3b05da171d351afbd25c1525) | ||
(L root: 05e3bc756e005c1bc5e4daf8a3da95d435af52476b0a0e6d52e719a2b1e3434a)) | ||
(B root: 3ae3330dcf932104d42b75b4da386896a628926f411737b34430fa65e526824d | ||
(L root: 4cc9e99389b5f729cbef6fe79e97a6f562841a2852e25e508e3bd06ce0de9c26) | ||
(L root: 4cc9e99389b5f729cbef6fe79e97a6f562841a2852e25e508e3bd06ce0de9c26))) | ||
*/ | ||
``` | ||
|
||
### Audit Proof | ||
|
||
```go | ||
|
||
blocks := [][]byte{ | ||
[]byte("alpha"), | ||
[]byte("beta"), | ||
[]byte("kappa"), | ||
[]byte("gamma"), | ||
[]byte("epsilon"), | ||
[]byte("omega"), | ||
[]byte("mu"), | ||
[]byte("zeta"), | ||
} | ||
|
||
tree := NewTree(IdentityHashForTest, blocks) | ||
checksum := treeA.checksumFunc([]byte("omega")) | ||
|
||
// for printing checksums | ||
f := func(xs []byte) string { | ||
return string(xs) | ||
} | ||
|
||
fmt.Println(tree.GetProofString(tree.root.GetChecksum(), checksum, f)) | ||
|
||
/* | ||
output: | ||
epsilon + omega = epsilonomega | ||
epsilonomega + muzeta = epsilonomegamuzeta | ||
alphabetakappagamma + epsilonomegamuzeta = alphabetakappagammaepsilonomegamuzeta | ||
*/ | ||
``` | ||
|
||
[1]: https://www.codeproject.com/Articles/1176140/Understanding-Merkle-Trees-Why-use-them-who-uses-t |
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,14 @@ | ||
package merkletree | ||
|
||
import "crypto/sha256" | ||
|
||
func Sha256DoubleHash(data []byte) []byte { | ||
first := sha256.Sum256(data) | ||
secnd := sha256.Sum256(first[:]) | ||
|
||
return secnd[:] | ||
} | ||
|
||
func IdentityHashForTest(strbytes []byte) []byte { | ||
return strbytes | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.