From 3aefaf62e4de2181582277bb60fe2a4ee5c506a2 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Thu, 17 Nov 2016 16:12:14 +0000 Subject: Fix libtommath's mp_radix_size() function such that it returns 2 for single-digit numbers. Add testcases for mp_radix_size() and mp_iseven(). Undo useless change in bn_mp_add_d.c (bring back libtommath's version). --- generic/tclObj.c | 8 +++---- generic/tclTestObj.c | 49 +++++++++++++++++++++++++++++++++++++++++-- libtommath/bn_mp_add_d.c | 11 ++++++---- libtommath/bn_mp_radix_size.c | 19 +++++++---------- tests/obj.test | 13 ++++++++++++ 5 files changed, 77 insertions(+), 23 deletions(-) diff --git a/generic/tclObj.c b/generic/tclObj.c index 230842a..5b11071 100644 --- a/generic/tclObj.c +++ b/generic/tclObj.c @@ -3230,13 +3230,11 @@ UpdateStringOfBignum( if (status != MP_OKAY) { Tcl_Panic("radix size failure in UpdateStringOfBignum"); } - if (size == 3) { + if (size < 2) { /* - * mp_radix_size() returns 3 when more than INT_MAX bytes would be + * mp_radix_size() returns < 2 when more than INT_MAX bytes would be * needed to hold the string rep (because mp_radix_size ignores - * integer overflow issues). When we know the string rep will be more - * than 3, we can conclude the string rep would overflow our string - * length limits. + * integer overflow issues). * * Note that so long as we enforce our bignums to the size that fits * in a packed bignum, this branch will never be taken. diff --git a/generic/tclTestObj.c b/generic/tclTestObj.c index f113cfe..4226d51 100644 --- a/generic/tclTestObj.c +++ b/generic/tclTestObj.c @@ -132,10 +132,11 @@ TestbignumobjCmd( Tcl_Obj *const objv[]) /* Argument vector */ { const char * subcmds[] = { - "set", "get", "mult10", "div10", NULL + "set", "get", "mult10", "div10", "iseven", "radixsize", NULL }; enum options { - BIGNUM_SET, BIGNUM_GET, BIGNUM_MULT10, BIGNUM_DIV10 + BIGNUM_SET, BIGNUM_GET, BIGNUM_MULT10, BIGNUM_DIV10, BIGNUM_ISEVEN, + BIGNUM_RADIXSIZE }; int index, varIndex; @@ -253,6 +254,50 @@ TestbignumobjCmd( } else { SetVarToObj(varIndex, Tcl_NewBignumObj(&newValue)); } + break; + + case BIGNUM_ISEVEN: + if (objc != 3) { + Tcl_WrongNumArgs(interp, 2, objv, "varIndex"); + return TCL_ERROR; + } + if (CheckIfVarUnset(interp, varIndex)) { + return TCL_ERROR; + } + if (Tcl_GetBignumFromObj(interp, varPtr[varIndex], + &bignumValue) != TCL_OK) { + return TCL_ERROR; + } + if (!Tcl_IsShared(varPtr[varIndex])) { + Tcl_SetIntObj(varPtr[varIndex], mp_iseven(&bignumValue)); + } else { + SetVarToObj(varIndex, Tcl_NewIntObj(mp_iseven(&bignumValue))); + } + mp_clear(&bignumValue); + break; + + case BIGNUM_RADIXSIZE: + if (objc != 3) { + Tcl_WrongNumArgs(interp, 2, objv, "varIndex"); + return TCL_ERROR; + } + if (CheckIfVarUnset(interp, varIndex)) { + return TCL_ERROR; + } + if (Tcl_GetBignumFromObj(interp, varPtr[varIndex], + &bignumValue) != TCL_OK) { + return TCL_ERROR; + } + if (mp_radix_size(&bignumValue, 10, &index) != MP_OKAY) { + return TCL_ERROR; + } + if (!Tcl_IsShared(varPtr[varIndex])) { + Tcl_SetIntObj(varPtr[varIndex], index); + } else { + SetVarToObj(varIndex, Tcl_NewIntObj(index)); + } + mp_clear(&bignumValue); + break; } Tcl_SetObjResult(interp, varPtr[varIndex]); diff --git a/libtommath/bn_mp_add_d.c b/libtommath/bn_mp_add_d.c index 5281ad4..aec8fc8 100644 --- a/libtommath/bn_mp_add_d.c +++ b/libtommath/bn_mp_add_d.c @@ -12,7 +12,7 @@ * The library is free for all purposes without any express * guarantee it works. * - * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ /* single digit addition */ @@ -37,9 +37,8 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c) /* c = |a| - b */ res = mp_sub_d(a, b, c); - /* fix signs */ - a->sign = MP_NEG; - c->sign = (c->used) ? MP_NEG : MP_ZPOS; + /* fix sign */ + a->sign = c->sign = MP_NEG; /* clamp */ mp_clamp(c); @@ -107,3 +106,7 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c) } #endif + +/* $Source$ */ +/* $Revision: 0.41 $ */ +/* $Date: 2007-04-18 09:58:18 +0000 $ */ diff --git a/libtommath/bn_mp_radix_size.c b/libtommath/bn_mp_radix_size.c index 40c4d04..9d95c48 100644 --- a/libtommath/bn_mp_radix_size.c +++ b/libtommath/bn_mp_radix_size.c @@ -12,7 +12,7 @@ * The library is free for all purposes without any express * guarantee it works. * - * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ /* returns size of ASCII reprensentation */ @@ -66,18 +66,13 @@ int mp_radix_size (mp_int * a, int radix, int *size) } mp_clear (&t); - /* - * return digs + 1, the 1 is for the NULL byte that would be required. - * mp_toradix_n requires a minimum of 3 bytes, so never report less than - * that. - */ - - if ( digs >= 2 ) { - *size = digs + 1; - } else { - *size = 3; - } + /* return digs + 1, the 1 is for the NULL byte that would be required. */ + *size = digs + 1; return MP_OKAY; } #endif + +/* $Source$ */ +/* $Revision: 0.41 $ */ +/* $Date: 2007-04-18 09:58:18 +0000 $ */ diff --git a/tests/obj.test b/tests/obj.test index 126d5ca..f8aae59 100644 --- a/tests/obj.test +++ b/tests/obj.test @@ -623,6 +623,19 @@ test obj-33.7 {integer overflow on input} {longIs32bit wideBiggerThanInt} { list [string is integer $x] [expr { wide($x) }] } {0 -4294967296} +test obj-34.1 {mp_iseven} testobj { + set result "" + lappend result [testbignumobj set 1 0] + lappend result [testbignumobj iseven 1] ; + lappend result [testobj type 1] +} {0 1 int} +test obj-34.2 {mp_radix_size} testobj { + set result "" + lappend result [testbignumobj set 1 9] + lappend result [testbignumobj radixsize 1] ; + lappend result [testobj type 1] +} {9 2 int} + if {[testConstraint testobj]} { testobj freeallvars } -- cgit v0.12