-
Notifications
You must be signed in to change notification settings - Fork 1
/
montgomery.go
139 lines (101 loc) · 2.95 KB
/
montgomery.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package schnorrq
import (
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/pkg/errors"
"github.com/qubic/go-schnorrq/order"
"math/big"
)
type Endianness bool
const (
LittleEndian Endianness = false
BigEndian Endianness = true
)
type MontgomeryNumber struct {
orderElement order.Element
endianness Endianness
}
func (number *MontgomeryNumber) FromStandard(array []byte, endian Endianness, doModOrder bool) error {
if len(array) != 32 {
return errors.New("cannot create Montgomery number, input array is not 32 bytes long")
}
var data [32]byte
copy(data[:], array[:])
//If we have to mod order the number first
if doModOrder {
data = modOrder(data, endian)
}
number.endianness = endian
number.orderElement = elementFromStandard(data, endian)
return nil
}
func (number *MontgomeryNumber) ToStandard() [32]byte {
return elementToStandard(number.orderElement, number.endianness)
}
func (number *MontgomeryNumber) Mult(ma, mb MontgomeryNumber) {
var element order.Element
element.Mul(&ma.orderElement, &mb.orderElement)
number.orderElement = element
number.endianness = ma.endianness
}
func (number *MontgomeryNumber) Sub(ma, mb MontgomeryNumber) {
var element order.Element
element.Sub(&ma.orderElement, &mb.orderElement)
number.orderElement = element
number.endianness = ma.endianness
}
// Print prints the contents of the MontgomeryNumber
func (number *MontgomeryNumber) Print(standardRepresentation bool) {
printElement(number.orderElement, standardRepresentation)
}
//Utility functions
func reverseEndianness(array []byte) []byte {
length := len(array)
reverse := make([]byte, length)
for i := 0; i < length; i++ {
reverse[i] = array[length-i-1]
}
return reverse
}
func modOrder(array [32]byte, endian Endianness) [32]byte {
return elementToStandard(elementFromStandard(array, endian), endian)
}
//Element functions
func elementFromStandard(array [32]byte, endian Endianness) order.Element {
var element order.Element
switch endian {
case BigEndian:
element.SetBigInt(new(big.Int).SetBytes(array[:]))
break
case LittleEndian:
element.SetBigInt(new(big.Int).SetBytes(reverseEndianness(array[:])))
break
}
return element
}
func elementToStandard(element order.Element, endian Endianness) [32]byte {
var array [32]byte
switch endian {
case BigEndian:
order.BigEndian.PutElement(&array, element)
break
case LittleEndian:
order.LittleEndian.PutElement(&array, element)
break
}
return array
}
func printElement(element order.Element, standardRepresentation bool) {
var array [32]byte
if !standardRepresentation {
binary.LittleEndian.PutUint64((array)[0:8], element[0])
binary.LittleEndian.PutUint64((array)[8:16], element[1])
binary.LittleEndian.PutUint64((array)[16:24], element[2])
binary.LittleEndian.PutUint64((array)[24:32], element[3])
fmt.Printf("%s\n", hex.EncodeToString(array[:]))
return
}
order.LittleEndian.PutElement(&array, element)
fmt.Printf("%s\n", hex.EncodeToString(array[:]))
}