summaryrefslogtreecommitdiff
path: root/sys/src/libmp/port/mpcmp.c
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@felloff.net>2015-11-21 09:39:59 +0100
committercinap_lenrek <cinap_lenrek@felloff.net>2015-11-21 09:39:59 +0100
commit38e1e5272fc9c66a00d702246813135452819ffe (patch)
treeb2d56b8f5e66a17daeb63693fc4dbd15c7308275 /sys/src/libmp/port/mpcmp.c
parentb677ab0c5909942bf8946e9e9bd148dea7dae718 (diff)
libmp: initial attempt at constant time code, faster reductions for special primes (for ecc)
introduce MPtimesafe flag to request time invariant computation disables normalization so significant digits are not leaked.
Diffstat (limited to 'sys/src/libmp/port/mpcmp.c')
-rw-r--r--sys/src/libmp/port/mpcmp.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/sys/src/libmp/port/mpcmp.c b/sys/src/libmp/port/mpcmp.c
index a2e3cf724..7ab5a16b6 100644
--- a/sys/src/libmp/port/mpcmp.c
+++ b/sys/src/libmp/port/mpcmp.c
@@ -8,10 +8,14 @@ mpmagcmp(mpint *b1, mpint *b2)
{
int i;
- i = b1->top - b2->top;
- if(i)
- return i;
-
+ i = b1->flags | b2->flags;
+ if(i & MPtimesafe)
+ return mpvectscmp(b1->p, b1->top, b2->p, b2->top);
+ if(i & MPnorm){
+ i = b1->top - b2->top;
+ if(i)
+ return i;
+ }
return mpveccmp(b1->p, b1->top, b2->p, b2->top);
}
@@ -19,10 +23,8 @@ mpmagcmp(mpint *b1, mpint *b2)
int
mpcmp(mpint *b1, mpint *b2)
{
- if(b1->sign != b2->sign)
- return b1->sign - b2->sign;
- if(b1->sign < 0)
- return mpmagcmp(b2, b1);
- else
- return mpmagcmp(b1, b2);
+ int sign;
+
+ sign = (b1->sign - b2->sign) >> 1; // -1, 0, 1
+ return sign | (sign&1)-1 & mpmagcmp(b1, b2)*b1->sign;
}