-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathAmplifyBigInt+Operations.swift
More file actions
147 lines (115 loc) · 4.09 KB
/
Copy pathAmplifyBigInt+Operations.swift
File metadata and controls
147 lines (115 loc) · 4.09 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
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
140
141
142
143
144
145
146
147
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import Foundation
import libtommathAmplify
public extension AmplifyBigInt {
// MARK: - Addition
static func + (lhs: AmplifyBigInt, rhs: AmplifyBigInt) -> AmplifyBigInt {
let sum = AmplifyBigInt()
let result = amplify_mp_add(&lhs.value, &rhs.value, &sum.value)
if result != AMPLIFY_MP_OKAY {
fatalError("Error occurred during + operation: \(result)")
}
return sum
}
static func + (lhs: AmplifyBigInt, rhs: Int) -> AmplifyBigInt {
return lhs + AmplifyBigInt(rhs)
}
static func + (lhs: Int, rhs: AmplifyBigInt) -> AmplifyBigInt {
return AmplifyBigInt(lhs) + rhs
}
static func += ( lhs: inout AmplifyBigInt, rhs: AmplifyBigInt) {
lhs = lhs + rhs
}
static func += ( lhs: inout AmplifyBigInt, rhs: Int) {
lhs = lhs + rhs
}
// MARK: - Subtraction
static func - (lhs: AmplifyBigInt, rhs: AmplifyBigInt) -> AmplifyBigInt {
let difference = AmplifyBigInt()
let result = amplify_mp_sub(&lhs.value, &rhs.value, &difference.value)
if result != AMPLIFY_MP_OKAY {
fatalError("Error occurred during - operation: \(result)")
}
return difference
}
static func - (lhs: AmplifyBigInt, rhs: Int) -> AmplifyBigInt {
return lhs - AmplifyBigInt(rhs)
}
static func -= ( lhs: inout AmplifyBigInt, rhs: AmplifyBigInt) {
lhs = lhs - rhs
}
static func -= ( lhs: inout AmplifyBigInt, rhs: Int) {
lhs = lhs - rhs
}
// MARK: - Multiplication
static func * (lhs: AmplifyBigInt, rhs: AmplifyBigInt) -> AmplifyBigInt {
let product = AmplifyBigInt()
let result = amplify_mp_mul(&lhs.value, &rhs.value, &product.value)
if result != AMPLIFY_MP_OKAY {
fatalError("Error occurred during * operation: \(result)")
}
return product
}
static func * (lhs: AmplifyBigInt, rhs: Int) -> AmplifyBigInt {
return lhs * AmplifyBigInt(rhs)
}
static func * (lhs: Int, rhs: AmplifyBigInt) -> AmplifyBigInt {
return rhs * lhs
}
static func *= ( lhs: inout AmplifyBigInt, rhs: AmplifyBigInt) {
lhs = lhs * rhs
}
static func *= ( lhs: inout AmplifyBigInt, rhs: Int) {
lhs = lhs * rhs
}
// MARK: - Division
static func / (lhs: AmplifyBigInt, rhs: AmplifyBigInt) -> Self {
fatalError()
}
static func /= (lhs: inout AmplifyBigInt, rhs: AmplifyBigInt) {
fatalError()
}
static func % (lhs: AmplifyBigInt, rhs: AmplifyBigInt) -> AmplifyBigInt {
let quotient = AmplifyBigInt()
let remainder = AmplifyBigInt()
let result = amplify_mp_div(&lhs.value, &rhs.value, "ient.value, &remainder.value)
if result != AMPLIFY_MP_OKAY {
fatalError("Error occurred during % operation: \(result)")
}
return remainder
}
static func % (lhs: AmplifyBigInt, rhs: Int) -> AmplifyBigInt {
return lhs % AmplifyBigInt(rhs)
}
static func %= (lhs: inout AmplifyBigInt, rhs: AmplifyBigInt) {
lhs = lhs % rhs
}
// MARK: - Exponentional
func pow(
_ power: AmplifyBigInt,
modulus: AmplifyBigInt
) -> AmplifyBigInt {
let exponentialModulus = AmplifyBigInt()
let result = amplify_mp_exptmod(&value, &power.value, &modulus.value, &exponentialModulus.value)
guard result == AMPLIFY_MP_OKAY else {
fatalError("Error occurred during pow(:modulus:) operation: \(result)")
}
return exponentialModulus
}
// MARK: - Binary operations
static func &= (lhs: inout AmplifyBigInt, rhs: AmplifyBigInt) {
fatalError()
}
static func |= (lhs: inout AmplifyBigInt, rhs: AmplifyBigInt) {
fatalError()
}
/// Stores the result of performing a bitwise XOR operation on the two given values in the left-hand-side variable.
static func ^= (lhs: inout AmplifyBigInt, rhs: AmplifyBigInt) {
fatalError()
}
}