Skip to content

Commit

Permalink
🏪 all: upgrade to go 1.24
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed Feb 13, 2025
1 parent dcec674 commit 601e310
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 410 deletions.
89 changes: 26 additions & 63 deletions csprng/csprng_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,32 @@ const (
func BenchmarkCryptoRandomSmall(b *testing.B) {
buf := make([]byte, testSmallSize)

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := rand.Read(buf)
if err != nil {
b.Fatal(err)
}
for b.Loop() {
rand.Read(buf)
}
}

func BenchmarkCryptoRandomBig(b *testing.B) {
buf := make([]byte, testBigSize)

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := rand.Read(buf)
if err != nil {
b.Fatal(err)
}
for b.Loop() {
rand.Read(buf)
}
}

func initBlake3KeyedHash(b *testing.B, size int) io.Reader {
func initBlake3KeyedHash(size int) io.Reader {
key := make([]byte, 32)
_, err := rand.Read(key)
if err != nil {
b.Fatal(err)
}
rand.Read(key)

h := blake3.New(size, key)
return h.XOF()
}

func BenchmarkBlake3KeyedHashSmall(b *testing.B) {
buf := make([]byte, testSmallSize)
r := initBlake3KeyedHash(b, testSmallSize)

b.ResetTimer()
r := initBlake3KeyedHash(testSmallSize)

for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := r.Read(buf)
if err != nil {
b.Fatal(err)
Expand All @@ -69,11 +54,9 @@ func BenchmarkBlake3KeyedHashSmall(b *testing.B) {

func BenchmarkBlake3KeyedHashBig(b *testing.B) {
buf := make([]byte, testBigSize)
r := initBlake3KeyedHash(b, testBigSize)
r := initBlake3KeyedHash(testBigSize)

b.ResetTimer()

for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := r.Read(buf)
if err != nil {
b.Fatal(err)
Expand All @@ -82,22 +65,18 @@ func BenchmarkBlake3KeyedHashBig(b *testing.B) {
}

func initAesCtr(b *testing.B, keySize int) cipher.Stream {
b.Helper()

key := make([]byte, keySize)
_, err := rand.Read(key)
if err != nil {
b.Fatal(err)
}
rand.Read(key)

cb, err := aes.NewCipher(key)
if err != nil {
panic(err)
b.Fatal(err)
}

iv := make([]byte, cb.BlockSize())
_, err = rand.Read(iv)
if err != nil {
b.Fatal(err)
}
rand.Read(iv)

return cipher.NewCTR(cb, iv)
}
Expand All @@ -106,9 +85,7 @@ func BenchmarkAes128CtrSmall(b *testing.B) {
buf := make([]byte, testSmallSize)
cs := initAesCtr(b, 16)

b.ResetTimer()

for i := 0; i < b.N; i++ {
for b.Loop() {
cs.XORKeyStream(buf, buf)
}
}
Expand All @@ -117,9 +94,7 @@ func BenchmarkAes128CtrBig(b *testing.B) {
buf := make([]byte, testBigSize)
cs := initAesCtr(b, 16)

b.ResetTimer()

for i := 0; i < b.N; i++ {
for b.Loop() {
cs.XORKeyStream(buf, buf)
}
}
Expand All @@ -128,9 +103,7 @@ func BenchmarkAes256CtrSmall(b *testing.B) {
buf := make([]byte, testSmallSize)
cs := initAesCtr(b, 32)

b.ResetTimer()

for i := 0; i < b.N; i++ {
for b.Loop() {
cs.XORKeyStream(buf, buf)
}
}
Expand All @@ -139,25 +112,19 @@ func BenchmarkAes256CtrBig(b *testing.B) {
buf := make([]byte, testBigSize)
cs := initAesCtr(b, 32)

b.ResetTimer()

for i := 0; i < b.N; i++ {
for b.Loop() {
cs.XORKeyStream(buf, buf)
}
}

func initChaCha20(b *testing.B) cipher.Stream {
key := make([]byte, 32)
_, err := rand.Read(key)
if err != nil {
b.Fatal(err)
}
b.Helper()

key := make([]byte, chacha20.KeySize)
rand.Read(key)

nonce := make([]byte, chacha20.NonceSize)
_, err = rand.Read(nonce)
if err != nil {
b.Fatal(err)
}
rand.Read(nonce)

cs, err := chacha20.NewUnauthenticatedCipher(key, nonce)
if err != nil {
Expand All @@ -170,9 +137,7 @@ func BenchmarkChaCha20Small(b *testing.B) {
buf := make([]byte, testSmallSize)
cs := initChaCha20(b)

b.ResetTimer()

for i := 0; i < b.N; i++ {
for b.Loop() {
cs.XORKeyStream(buf, buf)
}
}
Expand All @@ -181,9 +146,7 @@ func BenchmarkChaCha20Big(b *testing.B) {
buf := make([]byte, testBigSize)
cs := initChaCha20(b)

b.ResetTimer()

for i := 0; i < b.N; i++ {
for b.Loop() {
cs.XORKeyStream(buf, buf)
}
}
18 changes: 6 additions & 12 deletions ecdh/ecdh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ func BenchmarkX25519(b *testing.B) {
pubkey := key.PublicKey()
pubkeyHeader := (*publicKeyHeader)(unsafe.Pointer(pubkey))

b.ResetTimer()

for i := 0; i < b.N; i++ {
for b.Loop() {
pubkeyHeader.publicKey, err = key.ECDH(pubkey)
if err != nil {
b.Fatal(err)
Expand All @@ -36,9 +34,7 @@ func BenchmarkX25519(b *testing.B) {

func handshakeSetup() (h *blake3.Hasher, clientKey, serverKey *ecdh.PrivateKey, clientPubkey, serverPubkey *ecdh.PublicKey, err error) {
psk := make([]byte, 32)
if _, err = rand.Read(psk); err != nil {
return
}
rand.Read(psk)
h = blake3.New(64, psk)

clientKey, err = ecdh.X25519().GenerateKey(rand.Reader)
Expand Down Expand Up @@ -125,7 +121,7 @@ func serverRespond(h *blake3.Hasher, serverKey *ecdh.PrivateKey, clientPubkey, c
return
}

func clientRespond(h *blake3.Hasher, clientKey, clientEphemeralKey *ecdh.PrivateKey, serverPubkey, serverEphemeralPubkey *ecdh.PublicKey) (sum3 []byte, err error) {
func clientRespond(h *blake3.Hasher, clientKey, clientEphemeralKey *ecdh.PrivateKey, serverEphemeralPubkey *ecdh.PublicKey) (sum3 []byte, err error) {
// ee
result, err := clientEphemeralKey.ECDH(serverEphemeralPubkey)
if err != nil {
Expand Down Expand Up @@ -164,7 +160,7 @@ func TestHandshake(t *testing.T) {
t.Fatal("sum0 != sum1")
}

sum3, err := clientRespond(h, clientKey, clientEphemeralKey, serverPubkey, serverEphemeralPubkey)
sum3, err := clientRespond(h, clientKey, clientEphemeralKey, serverEphemeralPubkey)
if err != nil {
t.Fatal(err)
}
Expand All @@ -179,9 +175,7 @@ func BenchmarkHandshake(b *testing.B) {
b.Fatal(err)
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
for b.Loop() {
clientEphemeralKey, clientEphemeralPubkey, sum0, err := clientInitiate(h, clientKey, serverPubkey)
if err != nil {
b.Fatal(err)
Expand All @@ -195,7 +189,7 @@ func BenchmarkHandshake(b *testing.B) {
b.Fatal("sum0 != sum1")
}

sum3, err := clientRespond(h, clientKey, clientEphemeralKey, serverPubkey, serverEphemeralPubkey)
sum3, err := clientRespond(h, clientKey, clientEphemeralKey, serverEphemeralPubkey)
if err != nil {
b.Fatal(err)
}
Expand Down
2 changes: 0 additions & 2 deletions escape/loop/.gitignore

This file was deleted.

22 changes: 0 additions & 22 deletions escape/loop/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/database64128/cubic-go-playground

go 1.23.0
go 1.24.0

require (
github.com/aromatt/netipds v0.1.8
Expand Down
16 changes: 8 additions & 8 deletions ip/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,55 @@ var (

func BenchmarkAddrPortMappedEqual(b *testing.B) {
b.Run("Equal", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
AddrPortMappedEqual(addrPort4, addrPort4)
}
})

b.Run("NotEqual", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
AddrPortMappedEqual(addrPort4, addrPort4in6)
}
})
}

func BenchmarkAddrPortMappedEqualUnsafe(b *testing.B) {
b.Run("Equal", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
AddrPortMappedEqualUnsafe(addrPort4, addrPort4)
}
})

b.Run("NotEqual", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
AddrPortMappedEqualUnsafe(addrPort4, addrPort4in6)
}
})
}

func BenchmarkAddrPortv4Mappedv6(b *testing.B) {
b.Run("Is4", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
AddrPortv4Mappedv6(addrPort4)
}
})

b.Run("Is4In6", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
AddrPortv4Mappedv6(addrPort4in6)
}
})
}

func BenchmarkAddrPortv4Mappedv6Unsafe(b *testing.B) {
b.Run("Is4", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
AddrPortv4Mappedv6Unsafe(addrPort4)
}
})

b.Run("Is4In6", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
AddrPortv4Mappedv6Unsafe(addrPort4in6)
}
})
Expand Down
Loading

0 comments on commit 601e310

Please sign in to comment.