File: ensemble.go

package info (click to toggle)
addchain 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,396 kB
  • sloc: sh: 428; makefile: 8
file content (71 lines) | stat: -rw-r--r-- 1,937 bytes parent folder | download
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
// Package ensemble provides a collection of addition chain algorithms intended
// for target integers of cryptographic interest.
package ensemble

import (
	"github.com/mmcloughlin/addchain/alg"
	"github.com/mmcloughlin/addchain/alg/contfrac"
	"github.com/mmcloughlin/addchain/alg/dict"
	"github.com/mmcloughlin/addchain/alg/heuristic"
	"github.com/mmcloughlin/addchain/alg/opt"
)

// Ensemble is a convenience for building an ensemble of chain algorithms intended for large integers.
func Ensemble() []alg.ChainAlgorithm {
	// Choose sequence algorithms.
	seqalgs := []alg.SequenceAlgorithm{
		heuristic.NewAlgorithm(heuristic.UseFirst(
			heuristic.Halving{},
			heuristic.DeltaLargest{},
		)),
		heuristic.NewAlgorithm(heuristic.UseFirst(
			heuristic.Halving{},
			heuristic.Approximation{},
		)),
	}

	for _, strategy := range contfrac.Strategies {
		if strategy.Singleton() {
			seqalgs = append(seqalgs, contfrac.NewAlgorithm(strategy))
		}
	}

	// Build decomposers.
	decomposers := []dict.Decomposer{}
	for k := uint(4); k <= 128; k *= 2 {
		decomposers = append(decomposers, dict.SlidingWindow{K: k})
	}

	decomposers = append(decomposers, dict.RunLength{T: 0})
	for t := uint(16); t <= 128; t *= 2 {
		decomposers = append(decomposers, dict.RunLength{T: t})
	}

	for k := uint(2); k <= 8; k++ {
		decomposers = append(decomposers, dict.Hybrid{K: k, T: 0})
		for t := uint(16); t <= 64; t *= 2 {
			decomposers = append(decomposers, dict.Hybrid{K: k, T: t})
		}
	}

	// Build dictionary algorithms for every combination.
	as := []alg.ChainAlgorithm{}
	for _, decomp := range decomposers {
		for _, seqalg := range seqalgs {
			a := dict.NewAlgorithm(decomp, seqalg)
			as = append(as, a)
		}
	}

	// Add the runs algorithms.
	for _, seqalg := range seqalgs {
		as = append(as, dict.NewRunsAlgorithm(seqalg))
	}

	// Wrap in an optimization layer.
	for i, a := range as {
		as[i] = opt.Algorithm{Algorithm: a}
	}

	return as
}