Before we can add signed numbers we need to know which of two magnitudes is bigger - subtraction always takes the smaller from the larger. Today you compare two limb arrays.
Compare two magnitudes and return -1, 0, or 1 by length then by limbs from the top.
Because magnitudes are normalized - no leading zero limbs - the one with more limbs is unconditionally larger. That makes comparison a two-step affair: first compare the number of limbs, and only when they match do you look at the limbs themselves. When lengths are equal you scan from the most significant limb down and the first difference decides it; if you never find one, the magnitudes are equal.
This little function is load-bearing. Signed subtraction needs to subtract the
smaller magnitude from the larger and then pick the right sign, and division will
lean on it too. Returning the familiar -1 / 0 / 1 three-way result keeps it easy
to reuse everywhere an ordering is needed.
func cmpMag(a, b mag) int {if len(a) != len(b) {if len(a) < len(b) { return -1 }return 1}for i := len(a) - 1; i >= 0; i-- { // top limb first// compare a[i] and b[i]; return -1 or 1 on the first difference}return 0}