Skip to content

Commit

Permalink
break packages down into smaller pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Jan 5, 2025
1 parent 4a5e8a9 commit fafed9d
Show file tree
Hide file tree
Showing 26 changed files with 1,906 additions and 448 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ require (
)

require (
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
github.com/btcsuite/btcd/btcec/v2 v2.3.4 h1:3EJjcN70HCu/mwqlUsGK8GcNVyLVxFDlWurTXGPFfiQ=
github.com/btcsuite/btcd/btcec/v2 v2.3.4/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/kilic/bls12-381 v0.1.0 h1:encrdjqKMEvabVQ7qYOKu1OvhqpK4s47wDYtNiPtlp4=
Expand Down
100 changes: 100 additions & 0 deletions internal/runtime/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cache

import (
"crypto/sha256"
"sync"

"github.com/tetratelabs/wazero"
)

// Cache manages compiled Wasm modules
type Cache struct {
mu sync.RWMutex
codeCache map[string][]byte // stores raw Wasm bytes
compiledModules map[string]wazero.CompiledModule
pinnedModules map[string]struct{}
moduleHits map[string]uint32
moduleSizes map[string]uint64
}

// New creates a new cache instance
func New() *Cache {
return &Cache{
codeCache: make(map[string][]byte),
compiledModules: make(map[string]wazero.CompiledModule),
pinnedModules: make(map[string]struct{}),
moduleHits: make(map[string]uint32),
moduleSizes: make(map[string]uint64),
}
}

// Save stores a Wasm module in the cache
func (c *Cache) Save(code []byte) []byte {
checksum := sha256.Sum256(code)
key := string(checksum[:])

c.mu.Lock()
defer c.mu.Unlock()

c.codeCache[key] = code
c.moduleSizes[key] = uint64(len(code))
return checksum[:]
}

// Load retrieves a Wasm module from the cache
func (c *Cache) Load(checksum []byte) ([]byte, bool) {
c.mu.RLock()
defer c.mu.RUnlock()

code, exists := c.codeCache[string(checksum)]
if exists {
c.moduleHits[string(checksum)]++
}
return code, exists
}

// Pin marks a module as pinned in memory
func (c *Cache) Pin(checksum []byte) {
c.mu.Lock()
defer c.mu.Unlock()
c.pinnedModules[string(checksum)] = struct{}{}
}

// Unpin removes the pin from a module
func (c *Cache) Unpin(checksum []byte) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.pinnedModules, string(checksum))
}

// Remove deletes a module from the cache if it's not pinned
func (c *Cache) Remove(checksum []byte) bool {
c.mu.Lock()
defer c.mu.Unlock()

key := string(checksum)
if _, isPinned := c.pinnedModules[key]; isPinned {
return false
}

delete(c.codeCache, key)
delete(c.compiledModules, key)
delete(c.moduleHits, key)
delete(c.moduleSizes, key)
return true
}

// SaveCompiledModule stores a compiled module in the cache
func (c *Cache) SaveCompiledModule(checksum []byte, module wazero.CompiledModule) {
c.mu.Lock()
defer c.mu.Unlock()
c.compiledModules[string(checksum)] = module
}

// LoadCompiledModule retrieves a compiled module from the cache
func (c *Cache) LoadCompiledModule(checksum []byte) (wazero.CompiledModule, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
module, exists := c.compiledModules[string(checksum)]
return module, exists
}
152 changes: 0 additions & 152 deletions internal/runtime/crypto.go

This file was deleted.

100 changes: 100 additions & 0 deletions internal/runtime/crypto/bls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package crypto

import (
"fmt"

bls12381 "github.com/kilic/bls12-381"
)

// BLS12381AggregateG1 aggregates multiple G1 points into a single compressed G1 point.
func (v *Verifier) BLS12381AggregateG1(elements [][]byte) ([]byte, error) {
if len(elements) == 0 {
return nil, fmt.Errorf("no elements to aggregate")
}

g1 := bls12381.NewG1()
result := g1.Zero()

for _, element := range elements {
point, err := g1.FromCompressed(element)
if err != nil {
return nil, fmt.Errorf("failed to decompress G1 point: %w", err)
}
g1.Add(result, result, point)
}

return g1.ToCompressed(result), nil
}

// BLS12381AggregateG2 aggregates multiple G2 points into a single compressed G2 point.
func (v *Verifier) BLS12381AggregateG2(elements [][]byte) ([]byte, error) {
if len(elements) == 0 {
return nil, fmt.Errorf("no elements to aggregate")
}

g2 := bls12381.NewG2()
result := g2.Zero()

for _, element := range elements {
point, err := g2.FromCompressed(element)
if err != nil {
return nil, fmt.Errorf("failed to decompress G2 point: %w", err)
}
g2.Add(result, result, point)
}

return g2.ToCompressed(result), nil
}

// BLS12381HashToG1 hashes a message to a G1 point.
func (v *Verifier) BLS12381HashToG1(message []byte) ([]byte, error) {
g1 := bls12381.NewG1()
domain := []byte("BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_")
point, err := g1.HashToCurve(message, domain)
if err != nil {
return nil, fmt.Errorf("failed to hash to G1: %w", err)
}
return g1.ToCompressed(point), nil
}

// BLS12381HashToG2 hashes a message to a G2 point.
func (v *Verifier) BLS12381HashToG2(message []byte) ([]byte, error) {
g2 := bls12381.NewG2()
domain := []byte("BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_")
point, err := g2.HashToCurve(message, domain)
if err != nil {
return nil, fmt.Errorf("failed to hash to G2: %w", err)
}
return g2.ToCompressed(point), nil
}

// BLS12381PairingEquality checks if e(a1, a2) = e(b1, b2).
func (v *Verifier) BLS12381PairingEquality(a1Compressed, a2Compressed, b1Compressed, b2Compressed []byte) (bool, error) {
engine := bls12381.NewEngine()
g1 := bls12381.NewG1()
g2 := bls12381.NewG2()

a1, err := g1.FromCompressed(a1Compressed)
if err != nil {
return false, fmt.Errorf("failed to decompress a1: %w", err)
}

a2, err := g2.FromCompressed(a2Compressed)
if err != nil {
return false, fmt.Errorf("failed to decompress a2: %w", err)
}

b1, err := g1.FromCompressed(b1Compressed)
if err != nil {
return false, fmt.Errorf("failed to decompress b1: %w", err)
}

b2, err := g2.FromCompressed(b2Compressed)
if err != nil {
return false, fmt.Errorf("failed to decompress b2: %w", err)
}

engine.AddPair(a1, a2)
engine.AddPairInv(b1, b2)
return engine.Check(), nil
}
Loading

0 comments on commit fafed9d

Please sign in to comment.