```  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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
```
```/// The module,addition, is where scalar addition takes place.
/// The module, difference, is where the difference between two scalar values is calculated.

pub mod difference;

/// The module, largeint, is where the largeint struct is implemented along with a variety
/// of useful methods and functions. Update 0.2.0 will introduce the capability to multiply and
/// divide LargeInts. Currently only addition and subtraction are supported for the type LargeInt.

pub mod largeint {

use difference::*;

/// Arithmetic is carried out on an instance of LargeInt. The scalar value is stored in a
/// in a String and its sign in the enum Sign.

#[derive(PartialEq, Debug)]
pub struct LargeInt {
pub digits: String,
pub sign: Sign,
}

#[derive(PartialEq, Debug)]
pub enum Sign {
Positive,
Negative,
Unsigned,
}

// An enum with 3 states to be used in the compare function.
#[derive(PartialEq, Debug)]
pub enum Compare {
Larger,
Equal,
Smaller,
}

impl LargeInt {

/// The get_int() and get_sign() methods are in place to make it simple to retrieve
/// information about an instance of LargeInt in a simple way.

// get_int() returns a copy of the scalar LargeInt value as a String.
pub fn get_int(&self) -> String {
let mut int =  String::new();
int.push_str(&self.digits);
int
}

// get_sign() returns the Sign of the LargeInt value as a String.
pub fn get_sign(&self) -> String {
let sign: String;
if &self.sign == &Sign::Positive {
sign = String::from("Positive");
} else if &self.sign == &Sign::Negative {
sign = String::from("Negative");
} else {
sign = String::from("Unsigned");
}
sign
}

// Compares two LargeInts scalar value. Output is Compare::Larger if large2 contains the larger
// scalar value, Compare::Smaller if large2 is smaller, and Compare::Equal if both are equal.
pub fn compare(&self, large2: &LargeInt) -> Compare {
let mut flag = Compare::Larger;
let large1 = to_vec(&self.digits);
let large2 = to_vec(&large2.digits);

if large1.len() < large2.len() {
flag = Compare::Smaller;
} else if large1 == large2 {
flag = Compare::Equal;
} else if large1.len() == large2.len() {
for i in 0..large2.len() {
if &large2[i] > &large1[i] {
flag = Compare::Smaller;
break
}
else if &large2[i] < &large1[i] {
break
}
else {
continue
}
}
}
flag
}

/// Depending on the Sign of the parameters, add() and subtract() will either call addition()
///or difference() and then assign the appropiate Sign to the result.

pub fn add(&self, large2: &LargeInt) -> LargeInt {
let digits: String;
let sign: Sign;
if &self.sign == &Sign::Positive && large2.sign == Sign::Positive {
digits = addition(&self.digits, &large2.digits);
sign = Sign::Positive;
} else if &self.sign == &Sign::Positive && large2.sign == Sign::Negative {
if &self.digits.len() > &large2.digits.len() {
digits = difference(&self.digits, &large2.digits);
sign = Sign::Positive;
} else if &self.digits.len() < &large2.digits.len(){
digits = difference(&large2.digits, &self.digits);
sign = Sign::Negative;
} else if &self.compare(&large2) == &Compare::Larger {
digits = difference(&self.digits, &large2.digits);
sign = Sign::Positive;
} else if &self.compare(&large2) == &Compare::Smaller {
digits = difference(&large2.digits, &self.digits);
sign = Sign::Negative;
} else {
digits = String::from("0");
sign = Sign::Unsigned;
}
} else if &self.sign == &Sign::Negative && large2.sign == Sign::Positive {
if &self.digits.len() > &large2.digits.len() {
digits = difference(&self.digits, &large2.digits);
sign = Sign::Negative;
} else if &self.digits.len() < &large2.digits.len(){
digits = difference(&large2.digits, &self.digits);
sign = Sign::Positive;
} else if &self.compare(&large2) == &Compare::Larger {
digits = difference(&self.digits, &large2.digits);
sign = Sign::Negative;
} else if &self.compare(&large2) == &Compare::Smaller {
digits = difference(&large2.digits, &self.digits);
sign = Sign::Positive;
} else {
digits = String::from("0");
sign = Sign::Unsigned;
}
} else if &self.sign == &Sign::Unsigned || large2.sign == Sign::Unsigned {
match &self.sign {
&Sign::Unsigned => {
if large2.sign == Sign::Positive {
digits = large2.get_int();
sign = Sign::Positive;
} else if large2.sign == Sign::Negative {
digits = large2.get_int();
sign = Sign::Negative;
} else {
digits = String::from("0");
sign = Sign::Unsigned;
}
}
&Sign::Positive => {
digits = self.get_int();
sign = Sign::Positive;
}
&Sign::Negative => {
digits = self.get_int();
sign = Sign::Negative;
}
}
} else {
digits = addition(&self.digits, &large2.digits);
sign = Sign::Negative;
}

LargeInt {
digits,
sign,
}
}

pub fn subtract(&self, large2: &LargeInt) -> LargeInt{
let digits: String;
let sign: Sign;
if &self.sign == &Sign::Positive && large2.sign == Sign::Positive {
if &self.digits.len() > &large2.digits.len() {
digits = difference(&self.digits, &large2.digits);
sign = Sign::Positive;
} else if &self.digits.len() < &large2.digits.len(){
digits = difference(&large2.digits, &self.digits);
sign = Sign::Negative;
} else if &self.compare(&large2) == &Compare::Larger {
digits = difference(&self.digits, &large2.digits);
sign = Sign::Positive;
} else if &self.compare(&large2) == &Compare::Smaller {
digits = difference(&large2.digits, &self.digits);
sign = Sign::Negative;
} else {
digits = String::from("0");
sign = Sign::Unsigned;
}
} else if &self.sign == &Sign::Positive && large2.sign == Sign::Negative {
digits = addition(&self.digits, &large2.digits);
sign = Sign::Positive;
} else if &self.sign == &Sign::Negative && large2.sign == Sign::Positive {
digits = addition(&self.digits, &large2.digits);
sign = Sign::Negative;
} else if &self.sign == &Sign::Negative && large2.sign == Sign::Negative {
if &self.digits.len() > &large2.digits.len() {
digits = difference(&self.digits, &large2.digits);
sign = Sign::Negative;
} else if &self.digits.len() < &large2.digits.len() {
digits = difference(&large2.digits, &self.digits);
sign = Sign::Positive;
} else if &self.compare(&large2) == &Compare::Larger {
digits = difference(&self.digits, &large2.digits);
sign = Sign::Negative;
} else if &self.compare(&large2) == &Compare::Smaller {
digits = difference(&large2.digits, &self.digits);
sign = Sign::Positive;
} else {
digits = String::from("0");
sign = Sign::Unsigned;
}
} else {
match &self.sign {
&Sign::Unsigned => {
if large2.sign == Sign::Positive {
digits = large2.get_int();
sign = Sign::Negative;
} else if large2.sign == Sign::Negative {
digits = large2.get_int();
sign = Sign::Positive;
} else {
digits = String::from("0");
sign = Sign::Unsigned;
}
}
&Sign::Positive => {
digits = self.get_int();
sign = Sign::Positive;
}
&Sign::Negative => {
digits = self.get_int();
sign = Sign::Negative;
}
}
}
LargeInt {
digits,
sign,
}
}
}

/// While Largeint and its fields have been made public, it is strongly recommended that
/// LargeInt creation is done through the new() function which will instantiate an instance of
/// LargeInt safely, making the necessary corrections to the fields if necessary, or panicking
/// if an invalid input was submitted.

pub fn new(digits: String, mut sign: Sign) -> LargeInt {
let mut digits_clone = digits.clone();
let digit_0 = &digits[0..1];
if digits == String::from("") {
panic!("The large integer value is undefined!")
} else if digit_0 == "0" && digits.len() > 1 {
panic!("Invalid LargeInt creation. Please remove the leading 0")
} else if digit_0 == "0" && digits.len() == 1 {
digits_clone = String::from("0");
sign = Sign::Unsigned;
} else if digit_0 != "0" && sign == Sign::Unsigned {
panic!("Invalid LargeInt creation. Nonzero integers must be assigned
either Sign::Positive or Sign::Negative")
}
let new_int = LargeInt {
digits: digits_clone,
sign: sign,
};
new_int
}

pub fn to_vec(input_str: &str) -> Vec<u8> {
let mut vec: Vec<u8> = Vec::new();
for i in input_str.chars() {
let vec_input = i.to_digit(10).unwrap() as u8;
vec.push(vec_input);
}
vec
}

pub fn vec_to_str(vec: &Vec<u8>) -> String {
let mut output = String::from("");
for i in vec {
output.push_str(&i.to_string());
}
output
}
}
```