Commit graph

39 commits

Author SHA1 Message Date
Dag-Erling Smørgrav
06c2f1280a Add a missing break.
This was harmless as it would either fall through to an empty default statement (if CRYB_COVERAGE was defined) or out of the switch, but it's still bad style.
2017-09-04 21:00:01 +02:00
Dag-Erling Smørgrav
a09cdc6318 Fix equality predicates.
The count we passed to memcmp() in mpi_eq() and mpi_eq_abs() was actually the number of significant words in the MPI, rather than the number of bytes we wanted to compare.  Multiply by 4 to get the correct value.

To make the intent of the code more apparent, introduce a private MPI_MSW() macro which evaluates to the number of significant words (or 1-based index of the most significant word).  This also comes in handy in mpi_{add,sub,mul}_abs().

Add a couple of test cases which not only demonstrate the bug we fixed here but also demonstrate why we must compare whole words: on a big-endian machine, we would be comparing the unused upper bytes of the first and only word instead of the lower bytes which actually hold a value...
2017-04-22 05:32:25 +02:00
Dag-Erling Smørgrav
c556bc0aa8 Implement MPI division. 2017-04-06 19:52:23 +02:00
Dag-Erling Smørgrav
3c2b134fe4 Implement fast increment / decrement functions. 2017-04-06 19:52:23 +02:00
Dag-Erling Smørgrav
111efcb949 Correct function descriptions. 2017-04-06 19:52:23 +02:00
Dag-Erling Smørgrav
2a66f62078 Implement MPI multiplication. 2017-04-06 19:52:23 +02:00
Dag-Erling Smørgrav
206c6ffc85 Implement ffs() / fls() and use the latter to compute the MSB. 2017-04-06 19:52:23 +02:00
Dag-Erling Smørgrav
8477c4ae0b Slightly improve function descriptions. 2017-04-06 19:51:47 +02:00
Dag-Erling Smørgrav
094635f394 Remove debugging code. 2017-04-06 19:51:47 +02:00
Dag-Erling Smørgrav
b4eb918dad Implement GCD using Stein's binary algorithm. 2017-04-06 19:51:47 +02:00
Dag-Erling Smørgrav
a11c52e896 Refactor the comparison predicates, check for identity first. 2017-04-06 19:51:47 +02:00
Dag-Erling Smørgrav
47a0bf838f Add functions for comparing an MPI to an integer. 2017-04-06 19:51:47 +02:00
Dag-Erling Smørgrav
26e434d64b Add functions that return the LSB and MSB of an MPI. 2017-04-06 19:51:47 +02:00
Dag-Erling Smørgrav
7e05fe419a Remove unused headers. 2017-04-06 19:51:47 +02:00
Dag-Erling Smørgrav
895d1eb539 Constify where possible. 2017-04-06 19:51:47 +02:00
Dag-Erling Smørgrav
9ee45d4e34 Fix sign bug in special cases in mpi_{add,sub}_abs().
We failed to clear the negative flag when handling trivial cases, so if one of the terms was 0 and the other was negative, the result would be an exact copy of the non-zero term instead of its absolute value.
2017-04-06 19:51:47 +02:00
Dag-Erling Smørgrav
fbf69f31cb Start using pkg-config / pkgconf. 2017-03-07 00:54:46 +01:00
Dag-Erling Smørgrav
f70dac496f Mechanically bump copyright dates to the date of the latest commit. 2017-02-19 20:07:43 +01:00
Dag-Erling Smørgrav
f6905c8edb Fix bugs in cryb_mpi_{add,sub}_abs() caused by assuming that the target is initally positive zero.
If its operands were identical, cryb_mpi_add_abs() would leave the target untouched.  Explicitly call mpi_zero() before returning.  While there, extend the “identical operands” shortcut to also cover equality.

Both cryb_mpi_add_abs() and cryb_mpi_sub_abs() would leave the target's negative flag untouched.  Explicitly clear it before returning.
2017-02-19 17:20:40 +01:00
Dag-Erling Smørgrav
f612ea1b14 Add dependencies on libcryb-core to all other libraries, as already enforced in the configure script. 2016-11-14 12:59:13 +01:00
Dag-Erling Smørgrav
dc43296a47 Centralize most of .gitignore. 2016-09-17 21:38:54 +02:00
Dag-Erling Smørgrav
06a757e878 Big reorganization and cleanup 2016-09-04 14:56:39 +02:00
Dag-Erling Smørgrav
4d7893004c Prepare for moving to Github 2015-12-14 15:55:38 +01:00
Dag-Erling Smørgrav
94471e9923 Always clear the negative flag in mpi_zero() 2015-10-03 14:23:32 +00:00
Dag-Erling Smørgrav
a33f58cad7 Fix equality predicate. 2015-01-07 19:23:38 +00:00
Dag-Erling Smørgrav
d0fb2359f1 Implement mpi_{add,sub}() in terms of mpi_{add,sub}_abs(). 2015-01-07 18:59:44 +00:00
Dag-Erling Smørgrav
7a90ca7b42 Since X may point to the same MPI as either G or L (or even both), we
can't set the carry flag based on the contents of G or L after having
modified X.  Instead, compute and stash the new carry value first.
2014-12-30 19:14:59 +00:00
Dag-Erling Smørgrav
0d483f20ab Fix msb calculation. We may have to backtrack quite a bit, for
instance when subtracting two nearly equal large numbers.
2014-12-30 16:45:18 +00:00
Dag-Erling Smørgrav
1c024928f9 Fix off-by-one bug in mpi_cmp() which could cause it to read past the
end of the buffer.
2014-12-30 16:35:13 +00:00
Dag-Erling Smørgrav
43b870dc83 WIP: implement subtraction correctly. The actual result is now correct
most of the time, but the MSB calculation is not.
2014-12-30 11:23:30 +00:00
Dag-Erling Smørgrav
7524a14386 comment nit 2014-12-30 11:15:28 +00:00
Dag-Erling Smørgrav
c5b962387c Add mpi_eq_abs() and mpi_eq() predicates. 2014-12-30 11:12:11 +00:00
Dag-Erling Smørgrav
bc8e9d67ba In both mpi_add_abs() and mpi_sub_abs(), make sure that the target is
untouched if the operation fails.  Also ensure that A = A +/- A works.
2014-12-29 23:44:34 +00:00
Dag-Erling Smørgrav
98c6349d4a In mpi_zero(), save a memset() if the target is already zero. 2014-12-29 23:22:54 +00:00
Dag-Erling Smørgrav
1412dac680 Shifting any number by zero places is a no-op, but so is shifting zero by
any number of places.
2014-12-29 23:20:26 +00:00
Dag-Erling Smørgrav
8228f19d5a In mpi_copy(), any failure should leave the target untouched. 2014-12-29 23:18:57 +00:00
Dag-Erling Smørgrav
82f5c5cf77 In mpi_load(), use be32dec() when possible.
In mpi_set(), it is impossible for the value being loaded to exceed
the minimum size of an mpi; thus, there is no need for mpi_grow().
2014-12-29 15:08:09 +00:00
Dag-Erling Smørgrav
d4fd9a1958 rm unused variable 2014-08-12 07:33:30 +00:00
Dag-Erling Smørgrav
be31515f5e Start of a multiple-precision integer arithmetic library. 2014-08-12 07:30:31 +00:00