-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmontgomery.mpcl
62 lines (50 loc) · 1.12 KB
/
montgomery.mpcl
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
// -*- go -*-
//
// Copyright (c) 2020-2021 Markku Rossi
//
// All rights reserved.
//
package math
func makeMontgomery(m uint) uint {
n := size(m)
tmpType := make(uint, size(m)*2+1)
rrmType := make(uint, size(m))
var tmp tmpType = 1
tmp = (tmp << (n * 2)) % tmpType(m)
return rrmType(tmp)
}
func reduce(m, rrm, t uint) uint {
n := size(m)
a := t
aType := make(uint, size(a))
for i := 0; i < n; i++ {
if a&1 != 0 {
a += aType(m)
}
a = a >> 1
}
if a >= aType(m) {
a = a - aType(m)
}
return a
}
// ExpMontgomery computes modular exponentiation b**e mod |m|
// (i.e. the sign of m is ignored). The m must be bigger than 0 and
// not even number.
func ExpMontgomery(b, e, m uint) uint {
cType := make(uint, size(m)*2)
rrm := makeMontgomery(m)
t1 := cType(b) * cType(rrm)
t2 := cType(e) * cType(rrm)
r1 := math.reduce(m, rrm, t1)
r2 := math.reduce(m, rrm, t2)
prod := math.reduce(m, rrm, rrm)
base := math.reduce(m, rrm, b*rrm)
for i := 0; i < size(e); i++ {
if e>>i&1 != 0 {
prod = math.reduce(m, rrm, prod*base)
}
base = math.reduce(m, rrm, base*base)
}
return math.reduce(m, rrm, prod)
}