-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbinary.go
More file actions
43 lines (37 loc) · 1.05 KB
/
binary.go
File metadata and controls
43 lines (37 loc) · 1.05 KB
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
// Package binary implements generic binary addition chain algorithms.
package binary
import (
"math/big"
"github.com/mmcloughlin/addchain"
"github.com/mmcloughlin/addchain/internal/bigint"
)
// References:
//
// [hehcc:exp] Christophe Doche. Exponentiation. Handbook of Elliptic and Hyperelliptic Curve
// Cryptography, chapter 9. 2006.
// http://koclab.cs.ucsb.edu/teaching/ecc/eccPapers/Doche-ch09.pdf
// RightToLeft builds a chain algorithm for the right-to-left binary method,
// akin to [hehcc:exp] Algorithm 9.2.
type RightToLeft struct{}
func (RightToLeft) String() string { return "binary_right_to_left" }
// FindChain applies the right-to-left binary method to n.
func (RightToLeft) FindChain(n *big.Int) (addchain.Chain, error) {
c := addchain.Chain{}
b := new(big.Int).Set(n)
d := bigint.One()
var x *big.Int
for bigint.IsNonZero(b) {
c.AppendClone(d)
if b.Bit(0) == 1 {
if x == nil {
x = bigint.Clone(d)
} else {
x.Add(x, d)
c.AppendClone(x)
}
}
b.Rsh(b, 1)
d.Lsh(d, 1)
}
return c, nil
}