@aws-amplify/react-native 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/AmplifyRTNCore.podspec +35 -0
  2. package/LICENSE +201 -0
  3. package/android/build.gradle +103 -0
  4. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  5. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  6. package/android/gradle.properties +5 -0
  7. package/android/gradlew +234 -0
  8. package/android/gradlew.bat +89 -0
  9. package/android/src/hasPackageName/AndroidManifest.xml +4 -0
  10. package/android/src/main/AndroidManifest.xml +3 -0
  11. package/android/src/main/kotlin/com/amazonaws/amplify/rtncore/AmplifyRTNCoreModule.kt +29 -0
  12. package/android/src/main/kotlin/com/amazonaws/amplify/rtncore/AmplifyRTNCorePackage.kt +17 -0
  13. package/android/src/main/kotlin/com/amazonaws/amplify/rtncore/BigInteger.kt +65 -0
  14. package/ios/AmplifyRTNCore-Bridging-Header.h +5 -0
  15. package/ios/AmplifyRTNCore.mm +21 -0
  16. package/ios/AmplifyRTNCore.swift +29 -0
  17. package/ios/AmplifyRTNCore.xcodeproj/project.pbxproj +283 -0
  18. package/ios/AmplifyRTNCore.xcodeproj/project.xcworkspace/contents.xcworkspacedata +4 -0
  19. package/ios/BigInteger.swift +78 -0
  20. package/ios/JKBigInteger/JKBigDecimal.h +40 -0
  21. package/ios/JKBigInteger/JKBigDecimal.m +224 -0
  22. package/ios/JKBigInteger/JKBigInteger.h +58 -0
  23. package/ios/JKBigInteger/JKBigInteger.m +419 -0
  24. package/ios/JKBigInteger/LICENSE.txt +18 -0
  25. package/ios/JKBigInteger/LibTomMath/tommath.c +6925 -0
  26. package/ios/JKBigInteger/LibTomMath/tommath.h +584 -0
  27. package/ios/JKBigInteger/LibTomMath/tommath_class.h +999 -0
  28. package/ios/JKBigInteger/LibTomMath/tommath_superclass.h +76 -0
  29. package/lib/apis/computeModPow.d.ts +2 -0
  30. package/lib/apis/computeModPow.js +9 -0
  31. package/lib/apis/computeModPow.js.map +1 -0
  32. package/lib/apis/computeS.d.ts +2 -0
  33. package/lib/apis/computeS.js +9 -0
  34. package/lib/apis/computeS.js.map +1 -0
  35. package/lib/apis/getOperatingSystem.d.ts +1 -0
  36. package/lib/apis/getOperatingSystem.js +9 -0
  37. package/lib/apis/getOperatingSystem.js.map +1 -0
  38. package/lib/apis/index.d.ts +3 -0
  39. package/lib/apis/index.js +12 -0
  40. package/lib/apis/index.js.map +1 -0
  41. package/lib/index.d.ts +2 -0
  42. package/lib/index.js +19 -0
  43. package/lib/index.js.map +1 -0
  44. package/lib/moduleLoaders/index.d.ts +8 -0
  45. package/lib/moduleLoaders/index.js +22 -0
  46. package/lib/moduleLoaders/index.js.map +1 -0
  47. package/lib/moduleLoaders/loadAmplifyPushNotification.d.ts +21 -0
  48. package/lib/moduleLoaders/loadAmplifyPushNotification.js +26 -0
  49. package/lib/moduleLoaders/loadAmplifyPushNotification.js.map +1 -0
  50. package/lib/moduleLoaders/loadAppState.d.ts +1 -0
  51. package/lib/moduleLoaders/loadAppState.js +9 -0
  52. package/lib/moduleLoaders/loadAppState.js.map +1 -0
  53. package/lib/moduleLoaders/loadAsyncStorage.d.ts +2 -0
  54. package/lib/moduleLoaders/loadAsyncStorage.js +26 -0
  55. package/lib/moduleLoaders/loadAsyncStorage.js.map +1 -0
  56. package/lib/moduleLoaders/loadBase64.d.ts +5 -0
  57. package/lib/moduleLoaders/loadBase64.js +9 -0
  58. package/lib/moduleLoaders/loadBase64.js.map +1 -0
  59. package/lib/moduleLoaders/loadBuffer.d.ts +22 -0
  60. package/lib/moduleLoaders/loadBuffer.js +9 -0
  61. package/lib/moduleLoaders/loadBuffer.js.map +1 -0
  62. package/lib/moduleLoaders/loadGetRandomValues.d.ts +1 -0
  63. package/lib/moduleLoaders/loadGetRandomValues.js +21 -0
  64. package/lib/moduleLoaders/loadGetRandomValues.js.map +1 -0
  65. package/lib/moduleLoaders/loadNetInfo.d.ts +4 -0
  66. package/lib/moduleLoaders/loadNetInfo.js +26 -0
  67. package/lib/moduleLoaders/loadNetInfo.js.map +1 -0
  68. package/lib/moduleLoaders/loadUrlPolyfill.d.ts +1 -0
  69. package/lib/moduleLoaders/loadUrlPolyfill.js +21 -0
  70. package/lib/moduleLoaders/loadUrlPolyfill.js.map +1 -0
  71. package/lib/nativeModule.d.ts +2 -0
  72. package/lib/nativeModule.js +18 -0
  73. package/lib/nativeModule.js.map +1 -0
  74. package/lib/types.d.ts +15 -0
  75. package/lib/types.js +5 -0
  76. package/lib/types.js.map +1 -0
  77. package/lib-esm/apis/computeModPow.d.ts +2 -0
  78. package/lib-esm/apis/computeModPow.js +5 -0
  79. package/lib-esm/apis/computeModPow.js.map +1 -0
  80. package/lib-esm/apis/computeS.d.ts +2 -0
  81. package/lib-esm/apis/computeS.js +5 -0
  82. package/lib-esm/apis/computeS.js.map +1 -0
  83. package/lib-esm/apis/getOperatingSystem.d.ts +1 -0
  84. package/lib-esm/apis/getOperatingSystem.js +5 -0
  85. package/lib-esm/apis/getOperatingSystem.js.map +1 -0
  86. package/lib-esm/apis/index.d.ts +3 -0
  87. package/lib-esm/apis/index.js +6 -0
  88. package/lib-esm/apis/index.js.map +1 -0
  89. package/lib-esm/index.d.ts +2 -0
  90. package/lib-esm/index.js +5 -0
  91. package/lib-esm/index.js.map +1 -0
  92. package/lib-esm/moduleLoaders/index.d.ts +8 -0
  93. package/lib-esm/moduleLoaders/index.js +11 -0
  94. package/lib-esm/moduleLoaders/index.js.map +1 -0
  95. package/lib-esm/moduleLoaders/loadAmplifyPushNotification.d.ts +21 -0
  96. package/lib-esm/moduleLoaders/loadAmplifyPushNotification.js +22 -0
  97. package/lib-esm/moduleLoaders/loadAmplifyPushNotification.js.map +1 -0
  98. package/lib-esm/moduleLoaders/loadAppState.d.ts +1 -0
  99. package/lib-esm/moduleLoaders/loadAppState.js +5 -0
  100. package/lib-esm/moduleLoaders/loadAppState.js.map +1 -0
  101. package/lib-esm/moduleLoaders/loadAsyncStorage.d.ts +2 -0
  102. package/lib-esm/moduleLoaders/loadAsyncStorage.js +22 -0
  103. package/lib-esm/moduleLoaders/loadAsyncStorage.js.map +1 -0
  104. package/lib-esm/moduleLoaders/loadBase64.d.ts +5 -0
  105. package/lib-esm/moduleLoaders/loadBase64.js +5 -0
  106. package/lib-esm/moduleLoaders/loadBase64.js.map +1 -0
  107. package/lib-esm/moduleLoaders/loadBuffer.d.ts +22 -0
  108. package/lib-esm/moduleLoaders/loadBuffer.js +5 -0
  109. package/lib-esm/moduleLoaders/loadBuffer.js.map +1 -0
  110. package/lib-esm/moduleLoaders/loadGetRandomValues.d.ts +1 -0
  111. package/lib-esm/moduleLoaders/loadGetRandomValues.js +17 -0
  112. package/lib-esm/moduleLoaders/loadGetRandomValues.js.map +1 -0
  113. package/lib-esm/moduleLoaders/loadNetInfo.d.ts +4 -0
  114. package/lib-esm/moduleLoaders/loadNetInfo.js +22 -0
  115. package/lib-esm/moduleLoaders/loadNetInfo.js.map +1 -0
  116. package/lib-esm/moduleLoaders/loadUrlPolyfill.d.ts +1 -0
  117. package/lib-esm/moduleLoaders/loadUrlPolyfill.js +17 -0
  118. package/lib-esm/moduleLoaders/loadUrlPolyfill.js.map +1 -0
  119. package/lib-esm/nativeModule.d.ts +2 -0
  120. package/lib-esm/nativeModule.js +15 -0
  121. package/lib-esm/nativeModule.js.map +1 -0
  122. package/lib-esm/types.d.ts +15 -0
  123. package/lib-esm/types.js +4 -0
  124. package/lib-esm/types.js.map +1 -0
  125. package/package.json +69 -0
  126. package/src/apis/computeModPow.ts +8 -0
  127. package/src/apis/computeS.ts +8 -0
  128. package/src/apis/getOperatingSystem.ts +6 -0
  129. package/src/apis/index.ts +6 -0
  130. package/src/index.ts +14 -0
  131. package/src/moduleLoaders/index.ts +11 -0
  132. package/src/moduleLoaders/loadAmplifyPushNotification.ts +29 -0
  133. package/src/moduleLoaders/loadAppState.ts +6 -0
  134. package/src/moduleLoaders/loadAsyncStorage.ts +29 -0
  135. package/src/moduleLoaders/loadBase64.ts +6 -0
  136. package/src/moduleLoaders/loadBuffer.ts +6 -0
  137. package/src/moduleLoaders/loadGetRandomValues.ts +19 -0
  138. package/src/moduleLoaders/loadNetInfo.ts +31 -0
  139. package/src/moduleLoaders/loadUrlPolyfill.ts +19 -0
  140. package/src/nativeModule.ts +22 -0
  141. package/src/types.ts +19 -0
@@ -0,0 +1,224 @@
1
+ //
2
+ // JKBigDecimal.m
3
+ // JKBigInteger
4
+ //
5
+ // Created by Midfar Sun on 5/4/15.
6
+ // Copyright (c) 2015 Midfar Sun. All rights reserved.
7
+ //
8
+
9
+ // Licensed under the MIT License
10
+
11
+ #import "JKBigDecimal.h"
12
+
13
+ @implementation JKBigDecimal
14
+ @synthesize bigInteger, figure;
15
+
16
+ + (BOOL)supportsSecureCoding {
17
+ return YES;
18
+ }
19
+
20
+ - (id)init
21
+ {
22
+ return [self initWithString:@"0"];
23
+ }
24
+
25
+ - (id)initWithString:(NSString *)string
26
+ {
27
+ self = [super init];
28
+ if (self) {
29
+ figure = 0;
30
+ if ([string containsString:@"."]) {
31
+ NSRange range = [string rangeOfString:@"."];
32
+ figure = string.length-range.location-range.length;
33
+ string = [string stringByReplacingCharactersInRange:range withString:@""];
34
+ }
35
+ bigInteger = [[JKBigInteger alloc] initWithString:string];
36
+ }
37
+ return self;
38
+ }
39
+
40
+ + (id)decimalWithString:(NSString *)string
41
+ {
42
+ return [[JKBigDecimal alloc] initWithString:string];
43
+ }
44
+
45
+ -(id)initWithBigInteger:(JKBigInteger *)i figure:(NSInteger)f
46
+ {
47
+ self = [super init];
48
+ if (self) {
49
+ bigInteger = i;
50
+ figure = f;
51
+ }
52
+ return self;
53
+ }
54
+
55
+ - (instancetype)initWithCoder:(NSCoder *)decoder
56
+ {
57
+ self = [super init];
58
+ if (self) {
59
+ bigInteger = [[JKBigInteger alloc] initWithCoder:decoder];
60
+ figure = [decoder decodeInt32ForKey:@"JKBigDecimalFigure"];
61
+ }
62
+ return self;
63
+ }
64
+ -(void)encodeWithCoder:(NSCoder *)encoder
65
+ {
66
+ [bigInteger encodeWithCoder:encoder];
67
+ [encoder encodeInteger:figure forKey:@"JKBigDecimalFigure"];
68
+ }
69
+
70
+ - (id)add:(JKBigDecimal *)bigDecimal
71
+ {
72
+ NSInteger maxFigure = 0;
73
+ if (figure>=bigDecimal.figure) {
74
+ maxFigure = figure;
75
+ NSInteger exponent = maxFigure-bigDecimal.figure;
76
+ JKBigInteger *mInteger = [[JKBigInteger alloc] initWithString:@"10"];
77
+ JKBigInteger *newInteger = [mInteger pow:(unsigned int)exponent];
78
+ bigDecimal.bigInteger = [bigDecimal.bigInteger multiply:newInteger];
79
+ bigDecimal.figure = maxFigure;
80
+
81
+ }else{
82
+ maxFigure = bigDecimal.figure;
83
+ NSInteger exponent = maxFigure-figure;
84
+ JKBigInteger *mInteger = [[JKBigInteger alloc] initWithString:@"10"];
85
+ JKBigInteger *newInteger = [mInteger pow:(unsigned int)exponent];
86
+ bigInteger = [bigInteger multiply:newInteger];
87
+ figure = maxFigure;
88
+
89
+ }
90
+ JKBigInteger *newBigInteger = [bigInteger add:bigDecimal.bigInteger];
91
+ JKBigDecimal *newBigDecimal = [[JKBigDecimal alloc] initWithBigInteger:newBigInteger figure:maxFigure];
92
+ return newBigDecimal;
93
+ }
94
+
95
+ - (id)subtract:(JKBigDecimal *)bigDecimal
96
+ {
97
+ NSInteger maxFigure = 0;
98
+ if (figure>=bigDecimal.figure) {
99
+ maxFigure = figure;
100
+ NSInteger exponent = maxFigure-bigDecimal.figure;
101
+ JKBigInteger *mInteger = [[JKBigInteger alloc] initWithString:@"10"];
102
+ JKBigInteger *newInteger = [mInteger pow:(unsigned int)exponent];
103
+ bigDecimal.bigInteger = [bigDecimal.bigInteger multiply:newInteger];
104
+ bigDecimal.figure = maxFigure;
105
+
106
+ }else{
107
+ maxFigure = bigDecimal.figure;
108
+ NSInteger exponent = maxFigure-figure;
109
+ JKBigInteger *mInteger = [[JKBigInteger alloc] initWithString:@"10"];
110
+ JKBigInteger *newInteger = [mInteger pow:(unsigned int)exponent];
111
+ bigInteger = [bigDecimal.bigInteger multiply:newInteger];
112
+ figure = maxFigure;
113
+
114
+ }
115
+ JKBigInteger *newBigInteger = [bigInteger subtract:bigDecimal.bigInteger];
116
+ JKBigDecimal *newBigDecimal = [[JKBigDecimal alloc] initWithBigInteger:newBigInteger figure:maxFigure];
117
+ return newBigDecimal;
118
+ }
119
+
120
+ - (id)multiply:(JKBigDecimal *)bigDecimal
121
+ {
122
+ NSInteger totalFigure = figure+bigDecimal.figure;
123
+ JKBigInteger *newBigInteger = [bigInteger multiply:bigDecimal.bigInteger];
124
+ JKBigDecimal *newBigDecimal = [[JKBigDecimal alloc] initWithBigInteger:newBigInteger figure:totalFigure];
125
+ return newBigDecimal;
126
+ }
127
+
128
+ - (id)divide:(JKBigDecimal *)bigDecimal
129
+ {
130
+ NSInteger totalFigure = figure-bigDecimal.figure;
131
+ if (totalFigure<0) {
132
+ NSInteger exponent = -totalFigure;
133
+ totalFigure=0;
134
+ JKBigInteger *mInteger = [[JKBigInteger alloc] initWithString:@"10"];
135
+ JKBigInteger *newInteger = [mInteger pow:(unsigned int)exponent];
136
+ bigInteger = [bigInteger multiply:newInteger];
137
+ }
138
+ JKBigInteger *newBigInteger = [bigInteger divide:bigDecimal.bigInteger];
139
+ JKBigDecimal *newBigDecimal = [[JKBigDecimal alloc] initWithBigInteger:newBigInteger figure:totalFigure];
140
+ return newBigDecimal;
141
+ }
142
+
143
+ - (id)remainder:(JKBigDecimal *)bigDecimal
144
+ {
145
+ NSInteger totalFigure = figure-bigDecimal.figure;
146
+ if (totalFigure<0) {
147
+ NSInteger exponent = -totalFigure;
148
+ totalFigure=0;
149
+ JKBigInteger *mInteger = [[JKBigInteger alloc] initWithString:@"10"];
150
+ JKBigInteger *newInteger = [mInteger pow:(unsigned int)exponent];
151
+ bigInteger = [bigInteger multiply:newInteger];
152
+ }
153
+ JKBigInteger *newBigInteger = [bigInteger remainder:bigDecimal.bigInteger];
154
+ JKBigDecimal *newBigDecimal = [[JKBigDecimal alloc] initWithBigInteger:newBigInteger figure:bigDecimal.figure];
155
+ return newBigDecimal;
156
+ }
157
+
158
+ //- (NSArray *)divideAndRemainder:(JKBigDecimal *)bigInteger
159
+ //{
160
+ //
161
+ //}
162
+
163
+ -(NSComparisonResult) compare:(JKBigDecimal *)other {
164
+ JKBigDecimal *tens = [[JKBigDecimal alloc] initWithString:@"10"];
165
+ JKBigInteger *scaledNum;
166
+ JKBigInteger *scaledCompareTo;
167
+
168
+ if (figure > other.figure){
169
+ tens = [tens pow:(int)figure];
170
+ } else {
171
+ tens = [tens pow:(int)other.figure];
172
+ }
173
+ //scale my value to integer value
174
+ scaledNum = [[JKBigInteger alloc] initWithString:[[self multiply:tens] stringValue]];
175
+ //scale other value to integer
176
+ scaledCompareTo = [[JKBigInteger alloc] initWithString:[[other multiply:tens] stringValue]];
177
+ NSComparisonResult compareBigInteger = [scaledNum compare:scaledCompareTo];
178
+ return compareBigInteger;
179
+ }
180
+
181
+ - (id)pow:(unsigned int)exponent
182
+ {
183
+ NSInteger totalFigure = figure*exponent;
184
+ JKBigInteger *newBigInteger = [bigInteger pow:exponent];
185
+ JKBigDecimal *newBigDecimal = [[JKBigDecimal alloc] initWithBigInteger:newBigInteger figure:totalFigure];
186
+ return newBigDecimal;
187
+ }
188
+
189
+ - (id)negate
190
+ {
191
+ JKBigInteger *newBigInteger = [bigInteger negate];
192
+ JKBigDecimal *newBigDecimal = [[JKBigDecimal alloc] initWithBigInteger:newBigInteger figure:figure];
193
+ return newBigDecimal;
194
+ }
195
+
196
+ - (id)abs
197
+ {
198
+ JKBigInteger *newBigInteger = [bigInteger abs];
199
+ JKBigDecimal *newBigDecimal = [[JKBigDecimal alloc] initWithBigInteger:newBigInteger figure:figure];
200
+ return newBigDecimal;
201
+ }
202
+
203
+ - (NSString *)stringValue
204
+ {
205
+ NSString *string = [bigInteger stringValue];
206
+ if (figure==0) {
207
+ return string;
208
+ }
209
+ NSMutableString *mString = [NSMutableString stringWithString:string];
210
+ NSInteger newFigure = string.length-figure;
211
+ while (newFigure<=0) {
212
+ [mString insertString:@"0" atIndex:0];
213
+ newFigure++;
214
+ }
215
+ [mString insertString:@"." atIndex:newFigure];
216
+ return mString;
217
+ }
218
+
219
+ - (NSString *)description
220
+ {
221
+ return [self stringValue];
222
+ }
223
+
224
+ @end
@@ -0,0 +1,58 @@
1
+ //
2
+ // JKBigInteger.h
3
+ // JKBigInteger
4
+ //
5
+ // Created by Jānis Kiršteins on 5/21/13.
6
+ // Copyright (c) 2013 Jānis Kiršteins. All rights reserved.
7
+ //
8
+
9
+ // Licensed under the MIT License
10
+
11
+ #import <Foundation/Foundation.h>
12
+ #include "tommath.h"
13
+
14
+ @interface JKBigInteger : NSObject <NSSecureCoding>
15
+
16
+ - (id)initWithValue:(mp_int *)value;
17
+ - (mp_int *)value;
18
+
19
+ - (id)initWithUnsignedLong:(unsigned long)ul;
20
+ - (id)initWithString:(NSString *)string;
21
+ - (id)initWithString:(NSString *)string andRadix:(int)radix;
22
+ - (id)initWithCString:(char *)cString;
23
+ - (id)initWithCString:(char *)cString andRadix:(int)radix;
24
+
25
+ - (id)add:(JKBigInteger *)bigInteger;
26
+ - (id)subtract:(JKBigInteger *)bigInteger;
27
+ - (id)multiply:(JKBigInteger *)bigInteger;
28
+ - (id)divide:(JKBigInteger *)bigInteger;
29
+
30
+ - (id)remainder:(JKBigInteger *)bigInteger;
31
+ - (NSArray *)divideAndRemainder:(JKBigInteger *)bigInteger;
32
+
33
+ - (id)pow:(unsigned int)exponent;
34
+ - (id)pow:(JKBigInteger*)exponent andMod:(JKBigInteger*)modulus;
35
+ - (id)negate;
36
+ - (id)abs;
37
+
38
+ - (id)bitwiseXor:(JKBigInteger *)bigInteger;
39
+ - (id)bitwiseOr:(JKBigInteger *)bigInteger;
40
+ - (id)bitwiseAnd:(JKBigInteger *)bigInteger;
41
+ - (id)shiftLeft:(unsigned int)n;
42
+ - (id)shiftRight:(unsigned int)n;
43
+
44
+ - (id)gcd:(JKBigInteger *)bigInteger;
45
+
46
+ - (NSComparisonResult) compare:(JKBigInteger *)bigInteger;
47
+
48
+ - (unsigned long)unsignedIntValue;
49
+ - (NSString *)stringValue;
50
+ - (NSString *)stringValueWithRadix:(int)radix;
51
+
52
+ - (NSString *)description;
53
+
54
+ - (unsigned int)countBytes;
55
+ - (void)toByteArraySigned: (unsigned char*) byteArray;
56
+ - (void)toByteArrayUnsigned: (unsigned char*) byteArray;
57
+
58
+ @end
@@ -0,0 +1,419 @@
1
+ //
2
+ // JKBigInteger.m
3
+ // JKBigInteger
4
+ //
5
+ // Created by Jānis Kiršteins on 5/21/13.
6
+ // Copyright (c) 2013 Jānis Kiršteins. All rights reserved.
7
+ //
8
+
9
+ // Licensed under the MIT License
10
+
11
+ #import "JKBigInteger.h"
12
+
13
+ @implementation JKBigInteger {
14
+ @private
15
+ mp_int m_value;
16
+ }
17
+
18
+ + (BOOL)supportsSecureCoding {
19
+ return YES;
20
+ }
21
+
22
+ - (id)initWithValue:(mp_int *)value {
23
+
24
+ self = [super init];
25
+
26
+ if (self) {
27
+ mp_init_copy(&m_value, value);
28
+ }
29
+
30
+ return self;
31
+ }
32
+
33
+ - (mp_int *)value {
34
+ return &m_value;
35
+ }
36
+
37
+ - (id)initWithUnsignedLong:(unsigned long)unsignedLong {
38
+
39
+ self = [super init];
40
+
41
+ if (self) {
42
+ mp_set_int(&m_value, unsignedLong);
43
+ }
44
+
45
+ return self;
46
+ }
47
+
48
+ - (id)init {
49
+ return [self initWithUnsignedLong:0];
50
+ }
51
+
52
+ - (id)initWithCString:(char *)cString andRadix:(int)radix {
53
+
54
+ if (radix < 2 || radix > 64) {
55
+ return nil;
56
+ }
57
+
58
+ self = [super init];
59
+
60
+ if (self) {
61
+ mp_init(&m_value);
62
+ int result;
63
+ result = mp_read_radix(&m_value, cString, radix);
64
+
65
+ if (result != MP_OKAY) {
66
+ mp_clear(&m_value);
67
+ return nil;
68
+ }
69
+ }
70
+
71
+ return self;
72
+ }
73
+ - (id)initWithCString:(char *)cString {
74
+
75
+ int radix = 10;
76
+ return [self initWithCString:cString andRadix:radix];
77
+ }
78
+
79
+ - (id)initWithString:(NSString *)string andRadix:(int)radix {
80
+ return [self initWithCString:(char *)[string UTF8String] andRadix:radix];
81
+ }
82
+
83
+ - (id)initWithString:(NSString *)string {
84
+
85
+ int radix = 10;
86
+ return [self initWithCString:(char *)[string UTF8String] andRadix:radix];
87
+ }
88
+
89
+ - (id)initWithCoder:(NSCoder *)decoder {
90
+
91
+ self = [super init];
92
+
93
+ if (self) {
94
+ int sign = [decoder decodeInt32ForKey:@"JKBigIntegerSign"];
95
+ int alloc = [decoder decodeInt32ForKey:@"JKBigIntegerAlloc"];
96
+
97
+ mp_init_size(&m_value, alloc);
98
+
99
+ NSData *data = (NSData *)[decoder decodeObjectOfClass:[NSData class] forKey:@"JKBigIntegerDP"];
100
+ mp_digit *temp = (mp_digit *)[data bytes];
101
+
102
+ for (unsigned int i = 0; i < alloc; ++i) {
103
+ m_value.dp[i] = temp[i];
104
+ }
105
+
106
+ m_value.used = alloc;
107
+ m_value.sign = sign;
108
+ }
109
+
110
+ return self;
111
+ }
112
+ - (void)encodeWithCoder:(NSCoder *)encoder {
113
+
114
+ mp_clamp(&m_value);
115
+
116
+ NSData *data = [NSData dataWithBytes:(const void *)m_value.dp
117
+ length:m_value.alloc * sizeof(mp_digit)];
118
+
119
+ [encoder encodeObject:data forKey:@"JKBigIntegerDP"];
120
+ [encoder encodeInteger:m_value.alloc forKey:@"JKBigIntegerAlloc"];
121
+ [encoder encodeInteger:m_value.sign forKey:@"JKBigIntegerSign"];
122
+ }
123
+
124
+ - (id)add:(JKBigInteger *)bigInteger {
125
+
126
+ mp_int sum;
127
+ mp_init(&sum);
128
+
129
+ mp_add(&m_value, [bigInteger value], &sum);
130
+
131
+ id newBigInteger = [[JKBigInteger alloc] initWithValue:&sum];
132
+ mp_clear(&sum);
133
+
134
+ return newBigInteger;
135
+ }
136
+
137
+ - (id)subtract:(JKBigInteger *)bigInteger {
138
+
139
+ mp_int difference;
140
+ mp_init(&difference);
141
+
142
+ mp_sub(&m_value, [bigInteger value], &difference);
143
+
144
+ id newBigInteger = [[JKBigInteger alloc] initWithValue:&difference];
145
+ mp_clear(&difference);
146
+
147
+ return newBigInteger;
148
+ }
149
+
150
+ - (id)multiply:(JKBigInteger *)bigInteger {
151
+
152
+ mp_int product;
153
+ mp_init(&product);
154
+
155
+ mp_mul(&m_value, [bigInteger value], &product);
156
+
157
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&product];
158
+ mp_clear(&product);
159
+
160
+ return newBigInteger;
161
+ }
162
+
163
+ - (id)divide:(JKBigInteger *)bigInteger {
164
+
165
+ int result;
166
+ mp_int quotient;
167
+ mp_init(&quotient);
168
+
169
+ result = mp_div(&m_value, [bigInteger value], &quotient, NULL);
170
+ if (result == MP_VAL) {
171
+ mp_clear(&quotient);
172
+ return nil;
173
+ }
174
+
175
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&quotient];
176
+ mp_clear(&quotient);
177
+
178
+ return newBigInteger;
179
+ }
180
+
181
+ - (id)remainder:(JKBigInteger *)bigInteger {
182
+
183
+ int result;
184
+ mp_int remainder;
185
+ mp_init(&remainder);
186
+
187
+ result = mp_div(&m_value, [bigInteger value], NULL, &remainder);
188
+ if (result == MP_VAL) {
189
+ mp_clear(&remainder);
190
+ return nil;
191
+ }
192
+
193
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&remainder];
194
+ mp_clear(&remainder);
195
+
196
+ return newBigInteger;
197
+ }
198
+
199
+ - (NSArray *)divideAndRemainder:(JKBigInteger *)bigInteger {
200
+
201
+ int result;
202
+ mp_int quotient, remainder;
203
+ mp_init_multi(&quotient, &remainder, NULL);
204
+
205
+ result = mp_div(&m_value, [bigInteger value], &quotient, &remainder);
206
+ if (result == MP_VAL) {
207
+ mp_clear_multi(&quotient, &remainder, NULL);
208
+ return nil;
209
+ }
210
+
211
+ JKBigInteger *quotientBigInteger = [[JKBigInteger alloc] initWithValue:&quotient];
212
+ JKBigInteger *remainderBigInteger = [[JKBigInteger alloc] initWithValue:&remainder];
213
+ mp_clear_multi(&quotient, &remainder, NULL);
214
+
215
+ return @[quotientBigInteger, remainderBigInteger];
216
+ }
217
+
218
+ - (id)pow:(unsigned int)exponent {
219
+
220
+ int result;
221
+ mp_int power;
222
+ mp_init(&power);
223
+
224
+ result = mp_expt_d(&m_value, (mp_digit)exponent, &power);
225
+ if (result == MP_VAL) {
226
+ mp_clear(&power);
227
+ return nil;
228
+ }
229
+
230
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&power];
231
+ mp_clear(&power);
232
+
233
+ return newBigInteger;
234
+ }
235
+
236
+ - (id)pow:(JKBigInteger*)exponent andMod: (JKBigInteger*)modulus {
237
+
238
+ int result;
239
+ mp_int output;
240
+ mp_init(&output);
241
+
242
+ result = mp_exptmod(&m_value, &exponent->m_value, &modulus->m_value, &output);
243
+ if (result == MP_VAL) {
244
+ mp_clear(&output);
245
+ return nil;
246
+ }
247
+
248
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&output];
249
+ mp_clear(&output);
250
+
251
+ return newBigInteger;
252
+ }
253
+
254
+ - (id)negate {
255
+
256
+ mp_int negate;
257
+ mp_init(&negate);
258
+ mp_neg(&m_value, &negate);
259
+
260
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&negate];
261
+ mp_clear(&negate);
262
+
263
+ return newBigInteger;
264
+ }
265
+
266
+ - (id)abs {
267
+
268
+ mp_int absolute;
269
+ mp_init(&absolute);
270
+ mp_abs(&m_value, &absolute);
271
+
272
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&absolute];
273
+ mp_clear(&absolute);
274
+
275
+ return newBigInteger;
276
+ }
277
+
278
+ - (id)bitwiseXor:(JKBigInteger *)bigInteger {
279
+
280
+ mp_int xor;
281
+ mp_init(&xor);
282
+ mp_xor(&m_value, [bigInteger value], &xor);
283
+
284
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&xor];
285
+ mp_clear(&xor);
286
+
287
+ return newBigInteger;
288
+ }
289
+
290
+ - (id)bitwiseOr:(JKBigInteger *)bigInteger {
291
+
292
+ mp_int or;
293
+ mp_init(&or);
294
+ mp_or(&m_value, [bigInteger value], &or);
295
+
296
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&or];
297
+ mp_clear(&or);
298
+
299
+ return newBigInteger;
300
+ }
301
+
302
+ - (id)bitwiseAnd:(JKBigInteger *)bigInteger {
303
+
304
+ mp_int and;
305
+ mp_init(&and);
306
+ mp_and(&m_value, [bigInteger value], &and);
307
+
308
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&and];
309
+ mp_clear(&and);
310
+
311
+ return newBigInteger;
312
+ }
313
+
314
+ - (id)shiftLeft:(unsigned int)n {
315
+
316
+ mp_int lShift;
317
+ mp_init(&lShift);
318
+ mp_mul_2d(&m_value, n, &lShift);
319
+
320
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&lShift];
321
+ mp_clear(&lShift);
322
+
323
+ return newBigInteger;
324
+ }
325
+
326
+ - (id)shiftRight:(unsigned int)n {
327
+
328
+ mp_int rShift;
329
+ mp_init(&rShift);
330
+ mp_div_2d(&m_value, n, &rShift, NULL);
331
+
332
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&rShift];
333
+ mp_clear(&rShift);
334
+
335
+ return newBigInteger;
336
+ }
337
+ - (id)gcd:(JKBigInteger *)bigInteger {
338
+
339
+ int result;
340
+ mp_int gcd;
341
+ mp_init(&gcd);
342
+
343
+ result = mp_gcd(&m_value, [bigInteger value], &gcd);
344
+ if (result == MP_VAL) {
345
+ mp_clear(&gcd);
346
+ return nil;
347
+ }
348
+
349
+ JKBigInteger *newBigInteger = [[JKBigInteger alloc] initWithValue:&gcd];
350
+ mp_clear(&gcd);
351
+
352
+ return newBigInteger;
353
+ }
354
+
355
+ - (NSComparisonResult) compare:(JKBigInteger *)bigInteger {
356
+
357
+ NSComparisonResult comparisonResult;
358
+ comparisonResult = mp_cmp([bigInteger value], &m_value);
359
+
360
+ switch (comparisonResult) {
361
+ case MP_GT:
362
+ return NSOrderedAscending;
363
+ case MP_EQ:
364
+ return NSOrderedSame;
365
+ case MP_LT:
366
+ return NSOrderedDescending;
367
+ default:
368
+ return 0;
369
+ }
370
+ }
371
+
372
+ - (unsigned long)unsignedIntValue {
373
+ return mp_get_int(&m_value);
374
+ }
375
+
376
+ - (NSString *)stringValue {
377
+
378
+ int radix = 10;
379
+ return [self stringValueWithRadix:radix];
380
+ }
381
+
382
+ - (NSString *)stringValueWithRadix:(int)radix {
383
+
384
+ int stringSize;
385
+ mp_radix_size(&m_value, radix, &stringSize);
386
+ char cString[stringSize];
387
+ mp_toradix(&m_value, cString, radix);
388
+
389
+ for (int i = 0; i < stringSize; ++i) {
390
+ cString[i] = (char)tolower(cString[i]);
391
+ }
392
+
393
+ return [NSString stringWithUTF8String:cString];
394
+ }
395
+
396
+ - (NSString *)description {
397
+ return [self stringValue];
398
+ }
399
+
400
+ - (void)dealloc {
401
+ mp_clear(&m_value);
402
+ }
403
+
404
+ /* Returns the number of bytes required to store this JKBigInteger as binary */
405
+ - (unsigned int)countBytes {
406
+ return (unsigned int) mp_unsigned_bin_size(&m_value);
407
+ }
408
+
409
+ /* Retrieves the signed [big endian] format of this JKBigInteger */
410
+ - (void)toByteArraySigned: (unsigned char*) byteArray {
411
+ mp_to_signed_bin(&m_value, byteArray);
412
+ }
413
+
414
+ /* Retrieves the unsigned [big endian] format of this JKBigInteger */
415
+ - (void)toByteArrayUnsigned: (unsigned char*) byteArray {
416
+ mp_to_unsigned_bin(&m_value, byteArray);
417
+ }
418
+
419
+ @end
@@ -0,0 +1,18 @@
1
+ Copyright (C) 2013 Jānis Kiršteins
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.