From 1434e3c0dedef61ef12d31c52caa8b40a5ba1d55 Mon Sep 17 00:00:00 2001 From: mleku Date: Thu, 28 Nov 2024 07:47:11 +0000 Subject: [PATCH] fixes on things that were not compiling on go build ./... --- ec/secp256k1/precomps/genprecomps.go | 15 ++++---- ec/secp256k1/precomps/util.go | 21 +++++++++++ keys/keys.go | 41 +++++++++++++--------- layer2/badgerbadger/tester/badgerbadger.go | 22 +++++++----- tests/generate.go | 6 ++-- 5 files changed, 67 insertions(+), 38 deletions(-) create mode 100644 ec/secp256k1/precomps/util.go diff --git a/ec/secp256k1/precomps/genprecomps.go b/ec/secp256k1/precomps/genprecomps.go index 4e56a40..a8bd5a8 100644 --- a/ec/secp256k1/precomps/genprecomps.go +++ b/ec/secp256k1/precomps/genprecomps.go @@ -10,7 +10,6 @@ package main import ( "fmt" - "log" "math/big" "os" @@ -28,9 +27,8 @@ func bigAffineToJacobian(x, y *big.Int, result *secp256k1.JacobianPoint) { result.Z.SetInt(1) } -// serializedBytePoints returns a serialized byte slice which contains all of -// the possible points per 8-bit window. This is used to when generating -// compressedbytepoints.go. +// serializedBytePoints returns a serialized byte slice which contains all possible points per +// 8-bit window. This is used to when generating compressedbytepoints.go. func serializedBytePoints() []byte { // Calculate G^(2^i) for i in 0..255. These are used to avoid recomputing // them for each digit of the 8-bit windows. @@ -310,11 +308,12 @@ func main() { os.Exit(1) } serialized := serializedBytePoints() - embeded, err := os.Create("secp256k1/rawbytepoints.bin") + embedded, err := os.Create("secp256k1/rawbytepoints.bin") if err != nil { - log.Fatal(err) + log.F.Ln(err) + os.Exit(1) } - n, err := embeded.Write(serialized) + n, err := embedded.Write(serialized) if err != nil { panic(err) } @@ -323,5 +322,5 @@ func main() { n, len(serialized)) panic("fail") } - _ = embeded.Close() + _ = embedded.Close() } diff --git a/ec/secp256k1/precomps/util.go b/ec/secp256k1/precomps/util.go new file mode 100644 index 0000000..63785f0 --- /dev/null +++ b/ec/secp256k1/precomps/util.go @@ -0,0 +1,21 @@ +package main + +import ( + "bytes" + + "realy.lol/context" + "realy.lol/lol" +) + +type ( + B = []byte + S = string + E = error + N = int + Ctx = context.T +) + +var ( + log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf + equals = bytes.Equal +) diff --git a/keys/keys.go b/keys/keys.go index a8fd6cf..3e895bd 100644 --- a/keys/keys.go +++ b/keys/keys.go @@ -3,43 +3,50 @@ package keys import ( "strings" - btcec "realy.lol/ec" "realy.lol/ec/schnorr" "realy.lol/hex" "realy.lol/p256k" ) -var GeneratePrivateKey = func() B { return GenerateSecretKeyHex() } +// GeneratePrivateKey - deprecated, use GenerateSecretKeyHex +var GeneratePrivateKey = func() S { return GenerateSecretKeyHex() } -func GenerateSecretKeyHex() (sks B) { - var err E - var skb B +func GenerateSecretKey() (skb B, err E) { signer := &p256k.Signer{} if err = signer.Generate(); chk.E(err) { return } skb = signer.Sec() - sks = B(hex.Enc(skb)) return } -func GetPublicKeyHex(sk S) (S, E) { - b, err := hex.Dec(sk) +func GenerateSecretKeyHex() (sks S) { + skb, err := GenerateSecretKey() if chk.E(err) { - return "", err + return } - _, pk := btcec.PrivKeyFromBytes(b) - return hex.Enc(schnorr.SerializePubKey(pk)), nil + return hex.Enc(skb) } -func SecretBytesToPubKeyHex(skb B) (pk S, err E) { - _, pkk := btcec.SecKeyFromBytes(skb) - return hex.Enc(schnorr.SerializePubKey(pkk)), nil +func GetPublicKeyHex(sk S) (pk S, err E) { + var b B + if b, err = hex.Dec(sk); chk.E(err) { + return + } + signer := &p256k.Signer{} + if err = signer.InitSec(b); chk.E(err) { + return + } + + return hex.Enc(signer.Pub()), nil } -func SecretToPubKeyBytes(skb B) (pk B, err E) { - _, pkk := btcec.SecKeyFromBytes(skb) - return schnorr.SerializePubKey(pkk), nil +func SecretBytesToPubKeyHex(skb B) (pk S, err E) { + signer := &p256k.Signer{} + if err = signer.InitSec(skb); chk.E(err) { + return + } + return hex.Enc(signer.Pub()), nil } func IsValid32ByteHex(pk string) bool { diff --git a/layer2/badgerbadger/tester/badgerbadger.go b/layer2/badgerbadger/tester/badgerbadger.go index da3dab0..0ecdbce 100644 --- a/layer2/badgerbadger/tester/badgerbadger.go +++ b/layer2/badgerbadger/tester/badgerbadger.go @@ -1,8 +1,12 @@ package main import ( - "lukechampine.com/frand" "os" + "sync" + "time" + + "lukechampine.com/frand" + "realy.lol/bech32encoding" "realy.lol/context" "realy.lol/event" @@ -16,8 +20,6 @@ import ( "realy.lol/tag" "realy.lol/tests" "realy.lol/units" - "sync" - "time" ) type Counter struct { @@ -43,7 +45,9 @@ func main() { // fill rate capped to size of difference between high and low water mark diff = TotalSize * units.Gb * (HW - LW) / 100 ) - sec = keys.GenerateSecretKeyHex() + if sec, err = keys.GenerateSecretKey(); chk.E(err) { + panic(err) + } var nsec B if nsec, err = bech32encoding.HexToNsec(sec); chk.E(err) { panic(err) @@ -51,7 +55,7 @@ func main() { log.T.Ln("signing with", nsec) c, cancel := context.Cancel(context.Bg()) var wg sync.WaitGroup - //defer cancel() + // defer cancel() // create L1 with cache management settings enabled; we do it in the current dir // because os.TempDir can point to a ramdisk which is very impractical for this // test. @@ -62,7 +66,7 @@ func main() { // create L2 with no cache management b2 := ratel.GetBackend(c, &wg, false, units.Gb, lol.Trace, 4*units.Mb) // Respond to interrupt signal and clean up after interrupt or end of test. - //defer chk.E(os.RemoveAll(path)) + // defer chk.E(os.RemoveAll(path)) interrupt.AddHandler(func() { cancel() chk.E(os.RemoveAll(path)) @@ -78,7 +82,7 @@ func main() { os.Exit(1) } // start GC - //go b1.GarbageCollector() + // go b1.GarbageCollector() end: for { select { @@ -126,9 +130,9 @@ end: sum += counter[i].size } } - //if len(fetchIDs) > 0 { + // if len(fetchIDs) > 0 { // log.T.Ln("fetchIDs", len(fetchIDs), fetchIDs) - //} + // } mx.Unlock() case <-ticker.C: // copy out current list of events to request diff --git a/tests/generate.go b/tests/generate.go index 263ec83..5e1cae5 100644 --- a/tests/generate.go +++ b/tests/generate.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "lukechampine.com/frand" + "realy.lol/event" "realy.lol/kind" "realy.lol/p256k" @@ -21,14 +22,11 @@ func GenerateEvent(maxSize int) (ev *event.T, binSize int, err E) { if err = signer.Generate(); chk.E(err) { return } - //if err = signer.InitSec(sec); chk.E(err) { - // return - //} if err = ev.Sign(signer); chk.E(err) { return } var bin []byte - bin, err = ev.MarshalBinary(bin) + bin, err = ev.MarshalJSON(bin) binSize = len(bin) return }