aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2021-07-16 01:01:48 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2021-08-11 20:59:50 +0200
commitd57e1be89f05ef4d898043d13edeabb4c90da458 (patch)
treea067793b2d479b1c1b4ff97c5dc4952b12dc8e3f /core
parent18dda6ff9d8f67f259bafbcdfe5fd5286f5a45fa (diff)
bigint: Improve `add`.
Diffstat (limited to 'core')
-rw-r--r--core/math/bigint/basic.odin65
-rw-r--r--core/math/bigint/example.odin6
2 files changed, 57 insertions, 14 deletions
diff --git a/core/math/bigint/basic.odin b/core/math/bigint/basic.odin
index 56ac692ff..c6a36f60f 100644
--- a/core/math/bigint/basic.odin
+++ b/core/math/bigint/basic.odin
@@ -13,6 +13,12 @@ import "core:mem"
import "core:intrinsics"
/*
+ ===========================
+ User-level routines
+ ===========================
+*/
+
+/*
High-level addition. Handles sign.
*/
add :: proc(dest, a, b: ^Int) -> (err: Error) {
@@ -41,6 +47,49 @@ add :: proc(dest, a, b: ^Int) -> (err: Error) {
}
/*
+ High-level subtraction, dest = number - decrease. Handles signs.
+*/
+sub :: proc(dest, number, decrease: ^Int) -> (err: Error) {
+ dest := dest; x := number; y := decrease;
+ _panic_if_uninitialized(number); _panic_if_uninitialized(decrease); _panic_if_uninitialized(dest);
+
+ if x.sign != y.sign {
+ /*
+ Subtract a negative from a positive, OR subtract a positive from a negative.
+ In either case, ADD their magnitudes and use the sign of the first number.
+ */
+ dest.sign = x.sign;
+ return _add(dest, x, y);
+ }
+
+ /*
+ Subtract a positive from a positive, OR negative from a negative.
+ First, take the difference between their magnitudes, then...
+ */
+ if cmp_mag(x, y) == .Less_Than {
+ /*
+ The second has a larger magnitude.
+ The result has the *opposite* sign from the first number.
+ */
+ dest.sign = .Negative if is_pos(x) else .Zero_or_Positive;
+ x, y = y, x;
+ } else {
+ /*
+ The first has a larger or equal magnitude.
+ Copy the sign from the first.
+ */
+ dest.sign = x.sign;
+ }
+ return _sub(dest, x, y);
+}
+
+/*
+ ==========================
+ Low-level routines
+ ==========================
+*/
+
+/*
Low-level addition, unsigned.
Handbook of Applied Cryptography, algorithm 14.7.
*/
@@ -121,23 +170,17 @@ _add :: proc(dest, a, b: ^Int) -> (err: Error) {
return .OK;
}
-
/*
- Low-level subtraction. Assumes |a| > |b|.
+ Low-level subtraction, dest = number - decrease. Assumes |number| > |decrease|.
Handbook of Applied Cryptography, algorithm 14.9.
*/
-_sub :: proc(dest, a, b: ^Int) -> (err: Error) {
- dest := dest; x := a; y := b;
- _panic_if_uninitialized(a); _panic_if_uninitialized(b); _panic_if_uninitialized(dest);
-
- for n in 0..=12 {
- dest.digit[n] = DIGIT(n);
- dest.used = n+1;
- }
+_sub :: proc(dest, number, decrease: ^Int) -> (err: Error) {
+ dest := dest; x := number; y := decrease;
+ _panic_if_uninitialized(number); _panic_if_uninitialized(decrease); _panic_if_uninitialized(dest);
old_used := dest.used;
min_used := y.used;
- max_used := a.used;
+ max_used := x.used;
i: int;
err = grow(dest, max(max_used, _DEFAULT_DIGIT_COUNT));
diff --git a/core/math/bigint/example.odin b/core/math/bigint/example.odin
index 4b89b193d..0b57d7a8d 100644
--- a/core/math/bigint/example.odin
+++ b/core/math/bigint/example.odin
@@ -17,11 +17,11 @@ demo :: proc() {
a, b, c: ^Int;
err: Error;
- a, err = init(-21);
+ a, err = init(21);
defer destroy(a);
fmt.printf("a: %v, err: %v\n\n", a, err);
- b, err = init(42);
+ b, err = init(-21);
defer destroy(b);
fmt.printf("b: %v, err: %v\n\n", b, err);
@@ -29,7 +29,7 @@ demo :: proc() {
c, err = init();
defer destroy(c);
- err = add(c, a, b);
+ err = sub(c, a, b);
fmt.printf("c: %v, err: %v\n\n", c, err);
}