-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
break packages down into smaller pieces
- Loading branch information
Showing
26 changed files
with
1,906 additions
and
448 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
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,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 | ||
} |
This file was deleted.
Oops, something went wrong.
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,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 | ||
} |
Oops, something went wrong.