bn_mp_clamp.c 673 B

123456789101112131415161718192021222324252627
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_CLAMP_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* trim unused digits
  6. *
  7. * This is used to ensure that leading zero digits are
  8. * trimed and the leading "used" digit will be non-zero
  9. * Typically very fast. Also fixes the sign if there
  10. * are no more leading digits
  11. */
  12. void mp_clamp(mp_int *a)
  13. {
  14. /* decrease used while the most significant digit is
  15. * zero.
  16. */
  17. while ((a->used > 0) && (a->dp[a->used - 1] == 0u)) {
  18. --(a->used);
  19. }
  20. /* reset the sign flag if used == 0 */
  21. if (a->used == 0) {
  22. a->sign = MP_ZPOS;
  23. }
  24. }
  25. #endif