ref: cc706364d052347dcf45ca6dabe9a92978ad6b11
parent: 90baca8302fd8b6ccad7d06b61da9ecf451ccf83
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Aug 28 22:12:42 EDT 2016
libmp: allow passing nil to v,x,y results of mpextendedgcd(), simplify mpinvert()
--- a/libmp/mpextendedgcd.c
+++ b/libmp/mpextendedgcd.c
@@ -15,8 +15,16 @@
mpint *u, *A, *B, *C, *D;
int g;
+ if(v == nil){
+ v = mpnew(0);
+ mpextendedgcd(a, b, v, x, y);
+ mpfree(v);
+ return;
+ }
+ assert(x == nil || (x->flags & MPtimesafe) == 0);
+ assert(y == nil || (y->flags & MPtimesafe) == 0);
assert((a->flags&b->flags) & MPnorm);
- assert(((a->flags|b->flags|v->flags|x->flags|y->flags) & MPtimesafe) == 0);
+ assert(((a->flags|b->flags|v->flags) & MPtimesafe) == 0);
if(a->sign < 0 || b->sign < 0){
mpassign(mpzero, v);
@@ -104,6 +112,4 @@
mpfree(u);
mpfree(a);
mpfree(b);
-
- return;
}
--- a/libmp/mpinvert.c
+++ b/libmp/mpinvert.c
@@ -1,21 +1,17 @@
#include "os.h"
#include <mp.h>
-#define iseven(a) (((a)->p[0] & 1) == 0)
-
// use extended gcd to find the multiplicative inverse
// res = b**-1 mod m
void
mpinvert(mpint *b, mpint *m, mpint *res)
{
- mpint *dc1, *dc2; // don't care
+ mpint *v;
- dc1 = mpnew(0);
- dc2 = mpnew(0);
- mpextendedgcd(b, m, dc1, res, dc2);
- if(mpcmp(dc1, mpone) != 0)
+ v = mpnew(0);
+ mpextendedgcd(b, m, v, res, nil);
+ if(mpcmp(v, mpone) != 0)
abort();
+ mpfree(v);
mpmod(res, m, res);
- mpfree(dc1);
- mpfree(dc2);
}