-
Notifications
You must be signed in to change notification settings - Fork 5
/
marionette.go
74 lines (60 loc) · 1.78 KB
/
marionette.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package marionette
import (
"context"
"math/big"
"math/rand"
"time"
"go.uber.org/zap"
)
const (
PartyClient = "client"
PartyServer = "server"
)
func init() {
config := zap.NewDevelopmentConfig()
config.EncoderConfig.TimeKey = ""
config.EncoderConfig.CallerKey = ""
Logger, _ = config.Build()
}
// Logger is the global marionette logger.
var Logger = zap.NewNop()
// Rand returns a new PRNG seeded from the current time.
// This function can be overridden by the tests to provide a repeatable PRNG.
var Rand = func() *rand.Rand { return rand.New(rand.NewSource(time.Now().UnixNano())) }
// PluginFunc represents a plugin in the MAR language.
type PluginFunc func(ctx context.Context, fsm FSM, args ...interface{}) error
// FindPlugin returns a plugin function by module & name.
func FindPlugin(module, method string) PluginFunc {
return plugins[pluginKey{module, method}]
}
// RegisterPlugin adds a plugin to the plugin registry.
// Panic on duplicate registration.
func RegisterPlugin(module, method string, fn PluginFunc) {
if v := FindPlugin(module, method); v != nil {
panic("plugin already registered")
}
plugins[pluginKey{module, method}] = fn
}
type pluginKey struct {
module string
method string
}
var plugins = make(map[pluginKey]PluginFunc)
// Cipher represents the interface to the FTE Cipher.
type Cipher interface {
Capacity() int
Encrypt(plaintext []byte) (ciphertext []byte, err error)
Decrypt(ciphertext []byte) (plaintext, remainder []byte, err error)
}
// DFA represents the interface to the DFA ranker.
type DFA interface {
Capacity() int
Rank(s string) (rank *big.Int, err error)
Unrank(rank *big.Int) (ret string, err error)
NumWordsInSlice(n int) (numWords *big.Int, err error)
}
func assert(condition bool) {
if !condition {
panic("assertion failed")
}
}