--- /n/sources/plan9/sys/include/mp.h Mon Sep 7 20:29:15 2009 +++ /sys/include/mp.h Wed Jun 26 23:40:07 2024 @@ -45,8 +45,10 @@ char* mptoa(mpint*, int, char*, int); mpint* letomp(uchar*, uint, mpint*); /* byte array, little-endian */ int mptole(mpint*, uchar*, uint, uchar**); +void mptolel(mpint *b, uchar *p, int n); mpint* betomp(uchar*, uint, mpint*); /* byte array, big-endian */ int mptobe(mpint*, uchar*, uint, uchar**); +void mptober(mpint *b, uchar *p, int n); uint mptoui(mpint*); /* unsigned int */ mpint* uitomp(uint, mpint*); int mptoi(mpint*); /* int */ --- /n/sources/plan9/sys/include/ape/mp.h Mon Feb 4 14:12:44 2013 +++ /sys/include/ape/mp.h Wed Jun 26 23:40:09 2024 @@ -54,8 +54,10 @@ char* mptoa(mpint*, int, char*, int); mpint* letomp(uchar*, uint, mpint*); /* byte array, little-endian */ int mptole(mpint*, uchar*, uint, uchar**); +void mptolel(mpint *b, uchar *p, int n); mpint* betomp(uchar*, uint, mpint*); /* byte array, big-endian */ int mptobe(mpint*, uchar*, uint, uchar**); +void mptober(mpint *b, uchar *p, int n); uint mptoui(mpint*); /* unsigned int */ mpint* uitomp(uint, mpint*); int mptoi(mpint*); /* int */ --- /n/sources/plan9/sys/man/2/mp Mon May 24 14:37:16 2010 +++ /sys/man/2/mp Wed Jun 26 23:40:09 2024 @@ -1,6 +1,6 @@ .TH MP 2 .SH NAME -mpsetminbits, mpnew, mpfree, mpbits, mpnorm, mpcopy, mpassign, mprand, strtomp, mpfmt,mptoa, betomp, mptobe, letomp, mptole, mptoui, uitomp, mptoi, itomp, uvtomp, mptouv, vtomp, mptov, mpdigdiv, mpadd, mpsub, mpleft, mpright, mpmul, mpexp, mpmod, mpdiv, mpcmp, mpextendedgcd, mpinvert, mpsignif, mplowbits0, mpvecdigmuladd, mpvecdigmulsub, mpvecadd, mpvecsub, mpveccmp, mpvecmul, mpmagcmp, mpmagadd, mpmagsub, crtpre, crtin, crtout, crtprefree, crtresfree \- extended precision arithmetic +mpsetminbits, mpnew, mpfree, mpbits, mpnorm, mpcopy, mpassign, mprand, strtomp, mpfmt,mptoa, betomp, mptobe, mptober, letomp, mptole, mptolel, mptoui, uitomp, mptoi, itomp, uvtomp, mptouv, vtomp, mptov, mpdigdiv, mpadd, mpsub, mpleft, mpright, mpmul, mpexp, mpmod, mpdiv, mpcmp, mpextendedgcd, mpinvert, mpsignif, mplowbits0, mpvecdigmuladd, mpvecdigmulsub, mpvecadd, mpvecsub, mpveccmp, mpvecmul, mpmagcmp, mpmagadd, mpmagsub, crtpre, crtin, crtout, crtprefree, crtresfree \- extended precision arithmetic .SH SYNOPSIS .B #include .br @@ -49,12 +49,18 @@ int mptobe(mpint *b, uchar *buf, uint blen, uchar **bufp) .PP .B +void mptober(mpint *b, uchar *buf, int blen) +.PP +.B mpint* letomp(uchar *buf, uint blen, mpint *b) .PP .B int mptole(mpint *b, uchar *buf, uint blen, uchar **bufp) .PP .B +void mptolel(mpint *b, uchar *buf, int blen) +.PP +.B uint mptoui(mpint*) .PP .B @@ -371,6 +377,24 @@ .IR bufp . Sign is ignored in these conversions, i.e., the byte array version is always positive. +.PP +.I Mptober +and +.I mptolel +fill +.I blen +lower bytes of an +.I mpint +into a fixed length byte array. +.I Mptober +fills the bytes right adjusted in big endian order so that the least +significant byte is at +.I buf[blen-1] +while +.I mptolel +fills in little endian order; left adjusted; so that the least +significant byte is filled into +.IR buf[0] . .PP .IR Betomp , and diff -Nru /n/sources/plan9/sys/src/libmp/port/mkfile /sys/src/libmp/port/mkfile --- /n/sources/plan9/sys/src/libmp/port/mkfile Thu May 27 10:20:04 2004 +++ /sys/src/libmp/port/mkfile Wed Jun 26 23:40:10 2024 @@ -6,7 +6,9 @@ mpfmt\ strtomp\ mptobe\ + mptober\ mptole\ + mptolel\ betomp\ letomp\ mpadd\ diff -Nru /n/sources/plan9/sys/src/libmp/port/mptobe.c /sys/src/libmp/port/mptobe.c --- /n/sources/plan9/sys/src/libmp/port/mptobe.c Wed Jun 26 23:28:58 2024 +++ /sys/src/libmp/port/mptobe.c Wed Jun 26 23:40:11 2024 @@ -2,57 +2,31 @@ #include #include "dat.h" -// convert an mpint into a big endian byte array (most significant byte first) +// convert an mpint into a big endian byte array (most significant byte first; left adjusted) // return number of bytes converted // if p == nil, allocate and result array int mptobe(mpint *b, uchar *p, uint n, uchar **pp) { - int i, j, suppress; - mpdigit x; - uchar *e, *s, c; + int m; + m = (mpsignif(b)+7)/8; + if(m == 0) + m++; if(p == nil){ - n = (b->top+1)*Dbytes; + n = m; p = malloc(n); if(p == nil) sysfatal("mptobe: %r"); setmalloctag(p, getcallerpc(&b)); + } else { + if(n < m) + return -1; + if(n > m) + memset(p+m, 0, n-m); } if(pp != nil) *pp = p; - memset(p, 0, n); - - // special case 0 - if(b->top == 0){ - if(n < 1) - return -1; - else - return 1; - } - - s = p; - e = s+n; - suppress = 1; - for(i = b->top-1; i >= 0; i--){ - x = b->p[i]; - for(j = Dbits-8; j >= 0; j -= 8){ - c = x>>j; - if(c == 0 && suppress) - continue; - if(p >= e) - return -1; - *p++ = c; - suppress = 0; - } - } - - // guarantee at least one byte - if(s == p){ - if(p >= e) - return -1; - *p++ = 0; - } - - return p - s; + mptober(b, p, m); + return m; } diff -Nru /n/sources/plan9/sys/src/libmp/port/mptober.c /sys/src/libmp/port/mptober.c --- /n/sources/plan9/sys/src/libmp/port/mptober.c Wed Dec 31 19:00:00 1969 +++ /sys/src/libmp/port/mptober.c Wed Jun 26 23:40:13 2024 @@ -0,0 +1,34 @@ +#include "os.h" +#include +#include "dat.h" + +void +mptober(mpint *b, uchar *p, int n) +{ + int i, j, m; + mpdigit x; + + memset(p, 0, n); + + p += n; + m = b->top*Dbytes; + if(m < n) + n = m; + + i = 0; + while(n >= Dbytes){ + n -= Dbytes; + x = b->p[i++]; + for(j = 0; j < Dbytes; j++){ + *--p = x; + x >>= 8; + } + } + if(n > 0){ + x = b->p[i]; + for(j = 0; j < n; j++){ + *--p = x; + x >>= 8; + } + } +} diff -Nru /n/sources/plan9/sys/src/libmp/port/mptole.c /sys/src/libmp/port/mptole.c --- /n/sources/plan9/sys/src/libmp/port/mptole.c Wed Jun 26 23:29:08 2024 +++ /sys/src/libmp/port/mptole.c Wed Jun 26 23:40:13 2024 @@ -9,47 +9,21 @@ int mptole(mpint *b, uchar *p, uint n, uchar **pp) { - int i, j; - mpdigit x; - uchar *e, *s; + int m; + m = (mpsignif(b)+7)/8; + if(m == 0) + m++; if(p == nil){ - n = (b->top+1)*Dbytes; + n = m; p = malloc(n); if(p == nil) sysfatal("mptole: %r"); setmalloctag(p, getcallerpc(&b)); - } + } else if(n < m) + return -1; if(pp != nil) *pp = p; - memset(p, 0, n); - - // special case 0 - if(b->top == 0){ - if(n < 1) - return -1; - else - return 0; - } - - s = p; - e = s+n; - for(i = 0; i < b->top-1; i++){ - x = b->p[i]; - for(j = 0; j < Dbytes; j++){ - if(p >= e) - return -1; - *p++ = x; - x >>= 8; - } - } - x = b->p[i]; - while(x > 0){ - if(p >= e) - return -1; - *p++ = x; - x >>= 8; - } - - return p - s; + mptolel(b, p, n); + return m; } diff -Nru /n/sources/plan9/sys/src/libmp/port/mptolel.c /sys/src/libmp/port/mptolel.c --- /n/sources/plan9/sys/src/libmp/port/mptolel.c Wed Dec 31 19:00:00 1969 +++ /sys/src/libmp/port/mptolel.c Wed Jun 26 23:40:14 2024 @@ -0,0 +1,33 @@ +#include "os.h" +#include +#include "dat.h" + +void +mptolel(mpint *b, uchar *p, int n) +{ + int i, j, m; + mpdigit x; + + memset(p, 0, n); + + m = b->top*Dbytes; + if(m < n) + n = m; + + i = 0; + while(n >= Dbytes){ + n -= Dbytes; + x = b->p[i++]; + for(j = 0; j < Dbytes; j++){ + *p++ = x; + x >>= 8; + } + } + if(n > 0){ + x = b->p[i]; + for(j = 0; j < n; j++){ + *p++ = x; + x >>= 8; + } + } +} diff -Nru /n/sources/plan9/sys/src/ape/lib/mp/port/mkfile /sys/src/ape/lib/mp/port/mkfile --- /n/sources/plan9/sys/src/ape/lib/mp/port/mkfile Tue May 28 21:27:28 2013 +++ /sys/src/ape/lib/mp/port/mkfile Wed Jun 26 23:42:10 2024 @@ -8,7 +8,9 @@ mpfmt\ strtomp\ mptobe\ + mptober\ mptole\ + mptolel\ betomp\ letomp\ mpadd\