-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from halseth/matt-demo
Matt demo
- Loading branch information
Showing
26 changed files
with
1,851 additions
and
66 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
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,50 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/btcsuite/btcd/btcec/v2" | ||
"github.com/btcsuite/btcd/btcec/v2/schnorr" | ||
flags "github.com/jessevdk/go-flags" | ||
) | ||
|
||
const usage = "Returns n keys on the format 'privkey,pubkey'" | ||
|
||
type config struct { | ||
Num int `short:"n" long:"num" description:"number of keys to generate"` | ||
} | ||
|
||
var cfg = config{} | ||
|
||
func main() { | ||
parser := flags.NewParser(&cfg, flags.Default) | ||
parser.Usage = usage | ||
if _, err := parser.Parse(); err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
err := run() | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
} | ||
|
||
func run() error { | ||
if cfg.Num < 1 { | ||
return fmt.Errorf("number of keys mus be positive") | ||
} | ||
|
||
for i := 0; i < cfg.Num; i++ { | ||
privKey, err := btcec.NewPrivateKey() | ||
if err != nil { | ||
return err | ||
} | ||
privKeyBytes := privKey.Serialize() | ||
pubKey := privKey.PubKey() | ||
pubKeyBytes := schnorr.SerializePubKey(pubKey) | ||
fmt.Printf("%x,%x\n", privKeyBytes, pubKeyBytes) | ||
} | ||
return nil | ||
} |
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,92 @@ | ||
package build | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func Merkle(leaves []string) ([]string, error) { | ||
|
||
var level [][32]byte | ||
for _, l := range leaves { | ||
if l == "" { | ||
continue | ||
} | ||
|
||
var bs []byte | ||
|
||
// Grouped leaves | ||
if strings.HasPrefix(l, "(") && | ||
strings.HasSuffix(l, ")") { | ||
|
||
l = strings.TrimPrefix(l, "(") | ||
l = strings.TrimSuffix(l, ")") | ||
|
||
els := strings.Split(l, ",") | ||
|
||
group := bytes.Buffer{} | ||
for _, el := range els { | ||
h, err := hex.DecodeString(el) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse '%s': %w", l, err) | ||
} | ||
|
||
hash := sha256.Sum256(h) | ||
group.Write(hash[:]) | ||
} | ||
|
||
bs = group.Bytes() | ||
} else if l == "<>" { | ||
bs = []byte{} | ||
} else { | ||
var err error | ||
bs, err = hex.DecodeString(l) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse '%s': %w", l, err) | ||
} | ||
|
||
} | ||
|
||
hash := sha256.Sum256(bs) | ||
level = append(level, hash) | ||
} | ||
|
||
var tree []string | ||
addLevel := func(level [][32]byte) { | ||
s := "" | ||
for i, l := range level { | ||
s += fmt.Sprintf("%x", l) | ||
if i < len(level)-1 { | ||
s += " " | ||
} | ||
} | ||
|
||
tree = append(tree, s) | ||
|
||
} | ||
|
||
addLevel(level) | ||
for len(level) > 1 { | ||
if len(level)%2 != 0 { | ||
return nil, fmt.Errorf("invalid number of leaves") | ||
} | ||
|
||
var nextLevel [][32]byte | ||
for i := 1; i < len(level); i += 2 { | ||
item := make([]byte, 64) | ||
copy(item[:32], level[i-1][:]) | ||
copy(item[32:], level[i][:]) | ||
hash := sha256.Sum256(item) | ||
|
||
nextLevel = append(nextLevel, hash) | ||
} | ||
|
||
level = nextLevel | ||
addLevel(level) | ||
} | ||
|
||
return tree, nil | ||
} |
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,43 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/halseth/tapsim/cmd/merkle/build" | ||
flags "github.com/jessevdk/go-flags" | ||
) | ||
|
||
type config struct { | ||
Leaves string `short:"l" long:"leaves" description:"space separated string of hex values to commit to (must be power of 2). To group more values in single leaf, use (val1,val2)"` | ||
} | ||
|
||
var cfg = config{} | ||
|
||
func main() { | ||
if _, err := flags.Parse(&cfg); err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
err := run() | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
} | ||
|
||
func run() error { | ||
leaves := strings.Split(cfg.Leaves, " ") | ||
|
||
tree, err := build.Merkle(leaves) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for i := len(tree) - 1; i >= 0; i-- { | ||
fmt.Printf("%s\n", tree[i]) | ||
} | ||
|
||
return nil | ||
} |
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,49 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/halseth/mattlab/commitment" | ||
) | ||
|
||
func main() { | ||
val := os.Args[1] | ||
var ( | ||
num uint64 | ||
err error | ||
) | ||
|
||
if val == "from" { | ||
scriptNumHex := os.Args[2] | ||
v, err := hex.DecodeString(scriptNumHex) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
scriptNum, err := commitment.MakeScriptNum(v, true, len(v)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("%d\n", scriptNum) | ||
return | ||
} | ||
|
||
base := 10 | ||
if strings.HasPrefix(val, "0x") { | ||
base = 16 | ||
val = strings.TrimPrefix(val, "0x") | ||
} | ||
num, err = strconv.ParseUint(val, base, 64) | ||
if err != nil { | ||
panic(err) | ||
} | ||
s := commitment.ScriptNum(num) | ||
|
||
fmt.Printf("%x\n", s.Bytes()) | ||
|
||
} |
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
Oops, something went wrong.