no more NaN -> throw Exception

This commit is contained in:
Juanra Dikal 2021-03-24 18:30:45 +01:00
parent 806cc27a6f
commit 0bd13b078e
23 changed files with 229 additions and 127 deletions

View File

@ -1 +1 @@
function n(n){return n>=0?n:-n}function t(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let t=1;do{t++}while((n>>=1n)>1n);return t}function r(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),n<=0n||t<=0n)throw new RangeError("a and b MUST be > 0");let r=0n,e=1n,u=1n,f=0n;for(;0n!==n;){const o=t/n,i=t%n,g=r-u*o,c=e-f*o;t=n,n=i,r=u,e=f,u=g,f=c}return{g:t,x:r,y:e}}function e(t,r){let e="number"==typeof t?BigInt(n(t)):n(t),u="number"==typeof r?BigInt(n(r)):n(r);if(0n===e)return u;if(0n===u)return e;let f=0n;for(;0n===(1n&(e|u));)e>>=1n,u>>=1n,f++;for(;0n===(1n&e);)e>>=1n;do{for(;0n===(1n&u);)u>>=1n;if(e>u){const n=e;e=u,u=n}u-=e}while(0n!==u);return e<<f}function u(t,r){return"number"==typeof t&&(t=BigInt(t)),"number"==typeof r&&(r=BigInt(r)),0n===t&&0n===r?BigInt(0):n(t*r)/e(t,r)}function f(n,t){return n>=t?n:t}function o(n,t){return n>=t?t:n}function i(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),t<=0n)return NaN;const r=n%t;return r<0n?r+t:r}function g(n,t){try{const e=r(i(n,t),t);return 1n!==e.g?NaN:i(e.x,t)}catch(n){return NaN}}function c(t,r,e){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof r&&(r=BigInt(r)),"number"==typeof e&&(e=BigInt(e)),e<=0n)return NaN;if(1n===e)return BigInt(0);if(t=i(t,e),r<0n)return g(c(t,n(r),e),e);let u=1n;for(;r>0;)r%2n===1n&&(u=u*t%e),r/=2n,t=t**2n%e;return u}export{n as abs,t as bitLength,r as eGcd,e as gcd,u as lcm,f as max,o as min,g as modInv,c as modPow,i as toZn}; function n(n){return n>=0?n:-n}function t(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let t=1;do{t++}while((n>>=1n)>1n);return t}function r(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),n<=0n||t<=0n)throw new RangeError("a and b MUST be > 0");let r=0n,e=1n,o=1n,u=0n;for(;0n!==n;){const i=t/n,f=t%n,g=r-o*i,b=e-u*i;t=n,n=f,r=o,e=u,o=g,u=b}return{g:t,x:r,y:e}}function e(t,r){let e="number"==typeof t?BigInt(n(t)):n(t),o="number"==typeof r?BigInt(n(r)):n(r);if(0n===e)return o;if(0n===o)return e;let u=0n;for(;0n===(1n&(e|o));)e>>=1n,o>>=1n,u++;for(;0n===(1n&e);)e>>=1n;do{for(;0n===(1n&o);)o>>=1n;if(e>o){const n=e;e=o,o=n}o-=e}while(0n!==o);return e<<u}function o(t,r){return"number"==typeof t&&(t=BigInt(t)),"number"==typeof r&&(r=BigInt(r)),0n===t&&0n===r?BigInt(0):n(t*r)/e(t,r)}function u(n,t){return n>=t?n:t}function i(n,t){return n>=t?t:n}function f(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),t<=0n)throw new RangeError("n must be > 0");const r=n%t;return r<0n?r+t:r}function g(n,t){const e=r(f(n,t),t);if(1n!==e.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return f(e.x,t)}function b(t,r,e){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof r&&(r=BigInt(r)),"number"==typeof e&&(e=BigInt(e)),e<=0n)throw new RangeError("n must be > 0");if(1n===e)return 0n;if(t=f(t,e),r<0n)return g(b(t,n(r),e),e);let o=1n;for(;r>0;)r%2n===1n&&(o=o*t%e),r/=2n,t=t**2n%e;return o}export{n as abs,t as bitLength,r as eGcd,e as gcd,o as lcm,u as max,i as min,g as modInv,b as modPow,f as toZn};

View File

@ -1 +1 @@
var bigintModArith=function(n){"use strict";function t(n){return n>=0?n:-n}function r(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),n<=0n||t<=0n)throw new RangeError("a and b MUST be > 0");let r=0n,e=1n,u=1n,i=0n;for(;0n!==n;){const o=t/n,f=t%n,c=r-u*o,g=e-i*o;t=n,n=f,r=u,e=i,u=c,i=g}return{g:t,x:r,y:e}}function e(n,r){let e="number"==typeof n?BigInt(t(n)):t(n),u="number"==typeof r?BigInt(t(r)):t(r);if(0n===e)return u;if(0n===u)return e;let i=0n;for(;0n===(1n&(e|u));)e>>=1n,u>>=1n,i++;for(;0n===(1n&e);)e>>=1n;do{for(;0n===(1n&u);)u>>=1n;if(e>u){const n=e;e=u,u=n}u-=e}while(0n!==u);return e<<i}function u(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),t<=0n)return NaN;const r=n%t;return r<0n?r+t:r}function i(n,t){try{const e=r(u(n,t),t);return 1n!==e.g?NaN:u(e.x,t)}catch(n){return NaN}}return n.abs=t,n.bitLength=function(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let t=1;do{t++}while((n>>=1n)>1n);return t},n.eGcd=r,n.gcd=e,n.lcm=function(n,r){return"number"==typeof n&&(n=BigInt(n)),"number"==typeof r&&(r=BigInt(r)),0n===n&&0n===r?BigInt(0):t(n*r)/e(n,r)},n.max=function(n,t){return n>=t?n:t},n.min=function(n,t){return n>=t?t:n},n.modInv=i,n.modPow=function n(r,e,o){if("number"==typeof r&&(r=BigInt(r)),"number"==typeof e&&(e=BigInt(e)),"number"==typeof o&&(o=BigInt(o)),o<=0n)return NaN;if(1n===o)return BigInt(0);if(r=u(r,o),e<0n)return i(n(r,t(e),o),o);let f=1n;for(;e>0;)e%2n===1n&&(f=f*r%o),e/=2n,r=r**2n%o;return f},n.toZn=u,Object.defineProperty(n,"__esModule",{value:!0}),n}({}); var bigintModArith=function(n){"use strict";function t(n){return n>=0?n:-n}function r(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),n<=0n||t<=0n)throw new RangeError("a and b MUST be > 0");let r=0n,e=1n,o=1n,i=0n;for(;0n!==n;){const u=t/n,f=t%n,g=r-o*u,b=e-i*u;t=n,n=f,r=o,e=i,o=g,i=b}return{g:t,x:r,y:e}}function e(n,r){let e="number"==typeof n?BigInt(t(n)):t(n),o="number"==typeof r?BigInt(t(r)):t(r);if(0n===e)return o;if(0n===o)return e;let i=0n;for(;0n===(1n&(e|o));)e>>=1n,o>>=1n,i++;for(;0n===(1n&e);)e>>=1n;do{for(;0n===(1n&o);)o>>=1n;if(e>o){const n=e;e=o,o=n}o-=e}while(0n!==o);return e<<i}function o(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),t<=0n)throw new RangeError("n must be > 0");const r=n%t;return r<0n?r+t:r}function i(n,t){const e=r(o(n,t),t);if(1n!==e.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${t.toString()}`);return o(e.x,t)}return n.abs=t,n.bitLength=function(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let t=1;do{t++}while((n>>=1n)>1n);return t},n.eGcd=r,n.gcd=e,n.lcm=function(n,r){return"number"==typeof n&&(n=BigInt(n)),"number"==typeof r&&(r=BigInt(r)),0n===n&&0n===r?BigInt(0):t(n*r)/e(n,r)},n.max=function(n,t){return n>=t?n:t},n.min=function(n,t){return n>=t?t:n},n.modInv=i,n.modPow=function n(r,e,u){if("number"==typeof r&&(r=BigInt(r)),"number"==typeof e&&(e=BigInt(e)),"number"==typeof u&&(u=BigInt(u)),u<=0n)throw new RangeError("n must be > 0");if(1n===u)return 0n;if(r=o(r,u),e<0n)return i(n(r,t(e),u),u);let f=1n;for(;e>0;)e%2n===1n&&(f=f*r%u),e/=2n,r=r**2n%u;return f},n.toZn=o,Object.defineProperty(n,"__esModule",{value:!0}),n}({});

View File

@ -1 +1 @@
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).bigintModArith={})}(this,(function(n){"use strict";function t(n){return n>=0?n:-n}function e(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),n<=0n||t<=0n)throw new RangeError("a and b MUST be > 0");let e=0n,r=1n,o=1n,i=0n;for(;0n!==n;){const f=t/n,u=t%n,c=e-o*f,g=r-i*f;t=n,n=u,e=o,r=i,o=c,i=g}return{g:t,x:e,y:r}}function r(n,e){let r="number"==typeof n?BigInt(t(n)):t(n),o="number"==typeof e?BigInt(t(e)):t(e);if(0n===r)return o;if(0n===o)return r;let i=0n;for(;0n===(1n&(r|o));)r>>=1n,o>>=1n,i++;for(;0n===(1n&r);)r>>=1n;do{for(;0n===(1n&o);)o>>=1n;if(r>o){const n=r;r=o,o=n}o-=r}while(0n!==o);return r<<i}function o(n,t){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),t<=0n)return NaN;const e=n%t;return e<0n?e+t:e}function i(n,t){try{const r=e(o(n,t),t);return 1n!==r.g?NaN:o(r.x,t)}catch(n){return NaN}}n.abs=t,n.bitLength=function(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let t=1;do{t++}while((n>>=1n)>1n);return t},n.eGcd=e,n.gcd=r,n.lcm=function(n,e){return"number"==typeof n&&(n=BigInt(n)),"number"==typeof e&&(e=BigInt(e)),0n===n&&0n===e?BigInt(0):t(n*e)/r(n,e)},n.max=function(n,t){return n>=t?n:t},n.min=function(n,t){return n>=t?t:n},n.modInv=i,n.modPow=function n(e,r,f){if("number"==typeof e&&(e=BigInt(e)),"number"==typeof r&&(r=BigInt(r)),"number"==typeof f&&(f=BigInt(f)),f<=0n)return NaN;if(1n===f)return BigInt(0);if(e=o(e,f),r<0n)return i(n(e,t(r),f),f);let u=1n;for(;r>0;)r%2n===1n&&(u=u*e%f),r/=2n,e=e**2n%f;return u},n.toZn=o,Object.defineProperty(n,"__esModule",{value:!0})})); !function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).bigintModArith={})}(this,(function(n){"use strict";function e(n){return n>=0?n:-n}function t(n,e){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof e&&(e=BigInt(e)),n<=0n||e<=0n)throw new RangeError("a and b MUST be > 0");let t=0n,r=1n,o=1n,i=0n;for(;0n!==n;){const f=e/n,u=e%n,g=t-o*f,b=r-i*f;e=n,n=u,t=o,r=i,o=g,i=b}return{g:e,x:t,y:r}}function r(n,t){let r="number"==typeof n?BigInt(e(n)):e(n),o="number"==typeof t?BigInt(e(t)):e(t);if(0n===r)return o;if(0n===o)return r;let i=0n;for(;0n===(1n&(r|o));)r>>=1n,o>>=1n,i++;for(;0n===(1n&r);)r>>=1n;do{for(;0n===(1n&o);)o>>=1n;if(r>o){const n=r;r=o,o=n}o-=r}while(0n!==o);return r<<i}function o(n,e){if("number"==typeof n&&(n=BigInt(n)),"number"==typeof e&&(e=BigInt(e)),e<=0n)throw new RangeError("n must be > 0");const t=n%e;return t<0n?t+e:t}function i(n,e){const r=t(o(n,e),e);if(1n!==r.g)throw new RangeError(`${n.toString()} does not have inverse modulo ${e.toString()}`);return o(r.x,e)}n.abs=e,n.bitLength=function(n){if("number"==typeof n&&(n=BigInt(n)),1n===n)return 1;let e=1;do{e++}while((n>>=1n)>1n);return e},n.eGcd=t,n.gcd=r,n.lcm=function(n,t){return"number"==typeof n&&(n=BigInt(n)),"number"==typeof t&&(t=BigInt(t)),0n===n&&0n===t?BigInt(0):e(n*t)/r(n,t)},n.max=function(n,e){return n>=e?n:e},n.min=function(n,e){return n>=e?e:n},n.modInv=i,n.modPow=function n(t,r,f){if("number"==typeof t&&(t=BigInt(t)),"number"==typeof r&&(r=BigInt(r)),"number"==typeof f&&(f=BigInt(f)),f<=0n)throw new RangeError("n must be > 0");if(1n===f)return 0n;if(t=o(t,f),r<0n)return i(n(t,e(r),f),f);let u=1n;for(;r>0;)r%2n===1n&&(u=u*t%f),r/=2n,t=t**2n%f;return u},n.toZn=o,Object.defineProperty(n,"__esModule",{value:!0})}));

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -10,6 +10,9 @@ export interface Egcd {
* @param a * @param a
* @param b * @param b
* *
* @throws {RangeError}
* This excepction is thrown if a or b are less than 0
*
* @returns A triple (g, x, y), such that ax + by = g = gcd(a, b). * @returns A triple (g, x, y), such that ax + by = g = gcd(a, b).
*/ */
export declare function eGcd(a: number | bigint, b: number | bigint): Egcd; export declare function eGcd(a: number | bigint, b: number | bigint): Egcd;

View File

@ -1 +1 @@
{"version":3,"file":"egcd.d.ts","sourceRoot":"","sources":["../../../../src/ts/egcd.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,IAAI;IACnB,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;CACV;AACD;;;;;;;;GAQG;AACH,wBAAgB,IAAI,CAAE,CAAC,EAAE,MAAM,GAAC,MAAM,EAAE,CAAC,EAAE,MAAM,GAAC,MAAM,GAAG,IAAI,CA4B9D"} {"version":3,"file":"egcd.d.ts","sourceRoot":"","sources":["../../../../src/ts/egcd.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,IAAI;IACnB,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;CACV;AACD;;;;;;;;;;;GAWG;AACH,wBAAgB,IAAI,CAAE,CAAC,EAAE,MAAM,GAAC,MAAM,EAAE,CAAC,EAAE,MAAM,GAAC,MAAM,GAAG,IAAI,CA4B9D"}

View File

@ -4,7 +4,10 @@
* @param a The number to find an inverse for * @param a The number to find an inverse for
* @param n The modulo * @param n The modulo
* *
* @returns The inverse modulo n or number NaN if it does not exist * @throws {RangeError}
* Excpeption thorwn when a does not have inverse modulo n
*
* @returns The inverse modulo n
*/ */
export declare function modInv(a: number | bigint, n: number | bigint): bigint | number; export declare function modInv(a: number | bigint, n: number | bigint): bigint;
//# sourceMappingURL=modInv.d.ts.map //# sourceMappingURL=modInv.d.ts.map

View File

@ -1 +1 @@
{"version":3,"file":"modInv.d.ts","sourceRoot":"","sources":["../../../../src/ts/modInv.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAE,CAAC,EAAE,MAAM,GAAC,MAAM,EAAE,CAAC,EAAE,MAAM,GAAC,MAAM,GAAG,MAAM,GAAC,MAAM,CAWzE"} {"version":3,"file":"modInv.d.ts","sourceRoot":"","sources":["../../../../src/ts/modInv.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,wBAAgB,MAAM,CAAE,CAAC,EAAE,MAAM,GAAC,MAAM,EAAE,CAAC,EAAE,MAAM,GAAC,MAAM,GAAG,MAAM,CAOlE"}

View File

@ -5,7 +5,10 @@
* @param e exponent * @param e exponent
* @param n modulo * @param n modulo
* *
* @returns b**e mod n or number NaN if n <= 0 * @throws {RangeError}
* Excpeption thrown when n is not > 0
*
* @returns b**e mod n
*/ */
export declare function modPow(b: number | bigint, e: number | bigint, n: number | bigint): bigint | number; export declare function modPow(b: number | bigint, e: number | bigint, n: number | bigint): bigint;
//# sourceMappingURL=modPow.d.ts.map //# sourceMappingURL=modPow.d.ts.map

View File

@ -1 +1 @@
{"version":3,"file":"modPow.d.ts","sourceRoot":"","sources":["../../../../src/ts/modPow.ts"],"names":[],"mappings":"AAGA;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAE,CAAC,EAAE,MAAM,GAAC,MAAM,EAAE,CAAC,EAAE,MAAM,GAAC,MAAM,EAAE,CAAC,EAAE,MAAM,GAAC,MAAM,GAAG,MAAM,GAAC,MAAM,CAsB3F"} {"version":3,"file":"modPow.d.ts","sourceRoot":"","sources":["../../../../src/ts/modPow.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CAAE,CAAC,EAAE,MAAM,GAAC,MAAM,EAAE,CAAC,EAAE,MAAM,GAAC,MAAM,EAAE,CAAC,EAAE,MAAM,GAAC,MAAM,GAAG,MAAM,CA0BpF"}

View File

@ -4,10 +4,13 @@
* @remarks * @remarks
* a and b must be the same type, either number or bigint * a and b must be the same type, either number or bigint
* *
* @param {number|bigint} a An integer * @param a - An integer
* @param {number|bigint} n The modulo * @param n - The modulo
* *
* @returns A bigint with the smallest positive representation of a modulo n or number NaN if n < 0 * @throws {RangeError}
* Excpeption thrown when n is not > 0
*
* @returns A bigint with the smallest positive representation of a modulo n
*/ */
export declare function toZn(a: number | bigint, n: number | bigint): bigint | number; export declare function toZn(a: number | bigint, n: number | bigint): bigint;
//# sourceMappingURL=toZn.d.ts.map //# sourceMappingURL=toZn.d.ts.map

View File

@ -1 +1 @@
{"version":3,"file":"toZn.d.ts","sourceRoot":"","sources":["../../../../src/ts/toZn.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,wBAAgB,IAAI,CAAE,CAAC,EAAE,MAAM,GAAC,MAAM,EAAE,CAAC,EAAE,MAAM,GAAC,MAAM,GAAG,MAAM,GAAC,MAAM,CAQvE"} {"version":3,"file":"toZn.d.ts","sourceRoot":"","sources":["../../../../src/ts/toZn.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,IAAI,CAAE,CAAC,EAAE,MAAM,GAAC,MAAM,EAAE,CAAC,EAAE,MAAM,GAAC,MAAM,GAAG,MAAM,CAUhE"}

View File

@ -41,7 +41,7 @@ Name | Type |
The absolute value of a The absolute value of a
Defined in: [ts/abs.ts:8](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/abs.ts#L8) Defined in: [ts/abs.ts:8](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/abs.ts#L8)
___ ___
@ -61,7 +61,7 @@ Name | Type |
The bit length The bit length
Defined in: [ts/bitLength.ts:7](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/bitLength.ts#L7) Defined in: [ts/bitLength.ts:7](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/bitLength.ts#L7)
___ ___
@ -72,6 +72,9 @@ ___
An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm. An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm.
Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b). Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b).
**`throws`** {RangeError}
This excepction is thrown if a or b are less than 0
#### Parameters: #### Parameters:
Name | Type | Name | Type |
@ -83,7 +86,7 @@ Name | Type |
A triple (g, x, y), such that ax + by = g = gcd(a, b). A triple (g, x, y), such that ax + by = g = gcd(a, b).
Defined in: [ts/egcd.ts:15](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/egcd.ts#L15) Defined in: [ts/egcd.ts:18](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/egcd.ts#L18)
___ ___
@ -104,7 +107,7 @@ Name | Type |
The greatest common divisor of a and b The greatest common divisor of a and b
Defined in: [ts/gcd.ts:10](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/gcd.ts#L10) Defined in: [ts/gcd.ts:10](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/gcd.ts#L10)
___ ___
@ -125,7 +128,7 @@ Name | Type |
The least common multiple of a and b The least common multiple of a and b
Defined in: [ts/lcm.ts:10](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/lcm.ts#L10) Defined in: [ts/lcm.ts:10](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/lcm.ts#L10)
___ ___
@ -146,7 +149,7 @@ Name | Type |
Maximum of numbers a and b Maximum of numbers a and b
Defined in: [ts/max.ts:9](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/max.ts#L9) Defined in: [ts/max.ts:9](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/max.ts#L9)
___ ___
@ -167,16 +170,19 @@ Name | Type |
Minimum of numbers a and b Minimum of numbers a and b
Defined in: [ts/min.ts:9](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/min.ts#L9) Defined in: [ts/min.ts:9](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/min.ts#L9)
___ ___
### modInv ### modInv
**modInv**(`a`: *number* \| *bigint*, `n`: *number* \| *bigint*): *bigint* \| *number* **modInv**(`a`: *number* \| *bigint*, `n`: *number* \| *bigint*): *bigint*
Modular inverse. Modular inverse.
**`throws`** {RangeError}
Excpeption thorwn when a does not have inverse modulo n
#### Parameters: #### Parameters:
Name | Type | Description | Name | Type | Description |
@ -184,20 +190,23 @@ Name | Type | Description |
`a` | *number* \| *bigint* | The number to find an inverse for | `a` | *number* \| *bigint* | The number to find an inverse for |
`n` | *number* \| *bigint* | The modulo | `n` | *number* \| *bigint* | The modulo |
**Returns:** *bigint* \| *number* **Returns:** *bigint*
The inverse modulo n or number NaN if it does not exist The inverse modulo n
Defined in: [ts/modInv.ts:11](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/modInv.ts#L11) Defined in: [ts/modInv.ts:14](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/modInv.ts#L14)
___ ___
### modPow ### modPow
**modPow**(`b`: *number* \| *bigint*, `e`: *number* \| *bigint*, `n`: *number* \| *bigint*): *bigint* \| *number* **modPow**(`b`: *number* \| *bigint*, `e`: *number* \| *bigint*, `n`: *number* \| *bigint*): *bigint*
Modular exponentiation b**e mod n. Currently using the right-to-left binary method Modular exponentiation b**e mod n. Currently using the right-to-left binary method
**`throws`** {RangeError}
Excpeption thrown when n is not > 0
#### Parameters: #### Parameters:
Name | Type | Description | Name | Type | Description |
@ -206,23 +215,26 @@ Name | Type | Description |
`e` | *number* \| *bigint* | exponent | `e` | *number* \| *bigint* | exponent |
`n` | *number* \| *bigint* | modulo | `n` | *number* \| *bigint* | modulo |
**Returns:** *bigint* \| *number* **Returns:** *bigint*
b**e mod n or number NaN if n <= 0 b**e mod n
Defined in: [ts/modPow.ts:13](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/modPow.ts#L13) Defined in: [ts/modPow.ts:16](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/modPow.ts#L16)
___ ___
### toZn ### toZn
**toZn**(`a`: *number* \| *bigint*, `n`: *number* \| *bigint*): *bigint* \| *number* **toZn**(`a`: *number* \| *bigint*, `n`: *number* \| *bigint*): *bigint*
Finds the smallest positive element that is congruent to a in modulo n Finds the smallest positive element that is congruent to a in modulo n
**`remarks`** **`remarks`**
a and b must be the same type, either number or bigint a and b must be the same type, either number or bigint
**`throws`** {RangeError}
Excpeption thrown when n is not > 0
#### Parameters: #### Parameters:
Name | Type | Description | Name | Type | Description |
@ -230,8 +242,8 @@ Name | Type | Description |
`a` | *number* \| *bigint* | An integer | `a` | *number* \| *bigint* | An integer |
`n` | *number* \| *bigint* | The modulo | `n` | *number* \| *bigint* | The modulo |
**Returns:** *bigint* \| *number* **Returns:** *bigint*
A bigint with the smallest positive representation of a modulo n or number NaN if n < 0 A bigint with the smallest positive representation of a modulo n
Defined in: [ts/toZn.ts:12](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/toZn.ts#L12) Defined in: [ts/toZn.ts:15](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/toZn.ts#L15)

View File

@ -16,7 +16,7 @@
**g**: *bigint* **g**: *bigint*
Defined in: [ts/egcd.ts:2](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/egcd.ts#L2) Defined in: [ts/egcd.ts:2](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/egcd.ts#L2)
___ ___
@ -24,7 +24,7 @@ ___
**x**: *bigint* **x**: *bigint*
Defined in: [ts/egcd.ts:3](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/egcd.ts#L3) Defined in: [ts/egcd.ts:3](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/egcd.ts#L3)
___ ___
@ -32,4 +32,4 @@ ___
**y**: *bigint* **y**: *bigint*
Defined in: [ts/egcd.ts:4](https://github.com/juanelas/bigint-mod-arith/blob/6131edd/src/ts/egcd.ts#L4) Defined in: [ts/egcd.ts:4](https://github.com/juanelas/bigint-mod-arith/blob/806cc27/src/ts/egcd.ts#L4)

View File

@ -10,6 +10,9 @@ export interface Egcd {
* @param a * @param a
* @param b * @param b
* *
* @throws {RangeError}
* This excepction is thrown if a or b are less than 0
*
* @returns A triple (g, x, y), such that ax + by = g = gcd(a, b). * @returns A triple (g, x, y), such that ax + by = g = gcd(a, b).
*/ */
export function eGcd (a: number|bigint, b: number|bigint): Egcd { export function eGcd (a: number|bigint, b: number|bigint): Egcd {

View File

@ -6,17 +6,16 @@ import { toZn } from './toZn'
* @param a The number to find an inverse for * @param a The number to find an inverse for
* @param n The modulo * @param n The modulo
* *
* @returns The inverse modulo n or number NaN if it does not exist * @throws {RangeError}
* Excpeption thorwn when a does not have inverse modulo n
*
* @returns The inverse modulo n
*/ */
export function modInv (a: number|bigint, n: number|bigint): bigint|number { export function modInv (a: number|bigint, n: number|bigint): bigint {
try { const egcd = eGcd(toZn(a, n), n)
const egcd = eGcd(toZn(a, n), n) if (egcd.g !== 1n) {
if (egcd.g !== 1n) { throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`) // modular inverse does not exist
return NaN // modular inverse does not exist } else {
} else { return toZn(egcd.x, n)
return toZn(egcd.x, n)
}
} catch (error) {
return NaN
} }
} }

View File

@ -8,16 +8,23 @@ import { toZn } from './toZn'
* @param e exponent * @param e exponent
* @param n modulo * @param n modulo
* *
* @returns b**e mod n or number NaN if n <= 0 * @throws {RangeError}
* Excpeption thrown when n is not > 0
*
* @returns b**e mod n
*/ */
export function modPow (b: number|bigint, e: number|bigint, n: number|bigint): bigint|number { export function modPow (b: number|bigint, e: number|bigint, n: number|bigint): bigint {
if (typeof b === 'number') b = BigInt(b) if (typeof b === 'number') b = BigInt(b)
if (typeof e === 'number') e = BigInt(e) if (typeof e === 'number') e = BigInt(e)
if (typeof n === 'number') n = BigInt(n) if (typeof n === 'number') n = BigInt(n)
if (n <= 0n) { return NaN } else if (n === 1n) { return BigInt(0) } if (n <= 0n) {
throw new RangeError('n must be > 0')
} else if (n === 1n) {
return 0n
}
b = toZn(b, n) as bigint b = toZn(b, n)
if (e < 0n) { if (e < 0n) {
return modInv(modPow(b, abs(e), n), n) return modInv(modPow(b, abs(e), n), n)

View File

@ -4,16 +4,21 @@
* @remarks * @remarks
* a and b must be the same type, either number or bigint * a and b must be the same type, either number or bigint
* *
* @param {number|bigint} a An integer * @param a - An integer
* @param {number|bigint} n The modulo * @param n - The modulo
* *
* @returns A bigint with the smallest positive representation of a modulo n or number NaN if n < 0 * @throws {RangeError}
* Excpeption thrown when n is not > 0
*
* @returns A bigint with the smallest positive representation of a modulo n
*/ */
export function toZn (a: number|bigint, n: number|bigint): bigint|number { export function toZn (a: number|bigint, n: number|bigint): bigint {
if (typeof a === 'number') a = BigInt(a) if (typeof a === 'number') a = BigInt(a)
if (typeof n === 'number') n = BigInt(n) if (typeof n === 'number') n = BigInt(n)
if (n <= 0n) { return NaN } if (n <= 0n) {
throw new RangeError('n must be > 0')
}
const aZn = a % n const aZn = a % n
return (aZn < 0n) ? aZn + n : aZn return (aZn < 0n) ? aZn + n : aZn

View File

@ -14,21 +14,20 @@ describe('modInv', function () {
a: BigInt(-2), a: BigInt(-2),
n: BigInt(5), n: BigInt(5),
modInv: BigInt(2) modInv: BigInt(2)
}, }]
const invalidInputs = [
{ {
a: BigInt(2), a: BigInt(2),
n: BigInt(4), n: BigInt(4)
modInv: NaN
}, },
{ {
a: BigInt(0), a: BigInt(0),
n: BigInt(0), n: BigInt(0)
modInv: NaN
}, },
{ {
a: BigInt(0), a: BigInt(0),
n: BigInt(37), n: BigInt(37)
modInv: NaN
} }
] ]
for (const input of inputs) { for (const input of inputs) {
@ -40,4 +39,16 @@ describe('modInv', function () {
}) })
}) })
} }
for (const input of invalidInputs) {
describe(`modInv(${input.a}, ${input.n})`, function () {
it('should throw RangeError', function () {
try {
_pkg.modInv(input.a, input.n)
throw new Error('should have failed')
} catch (err) {
chai.expect(err).to.be.instanceOf(RangeError)
}
})
})
}
}) })

View File

@ -1,11 +1,5 @@
describe('modPow', function () { describe('modPow', function () {
const inputs = [ const inputs = [
{
a: BigInt(4),
b: BigInt(-1),
n: BigInt(0),
modPow: NaN
},
{ {
a: BigInt(4), a: BigInt(4),
b: BigInt(-1), b: BigInt(-1),
@ -37,6 +31,13 @@ describe('modPow', function () {
modPow: BigInt(2) modPow: BigInt(2)
} }
] ]
const invalidInputs = [
{
a: BigInt(4),
b: BigInt(-1),
n: BigInt(0)
}
]
this.timeout(90000) this.timeout(90000)
for (const input of inputs) { for (const input of inputs) {
@ -47,6 +48,18 @@ describe('modPow', function () {
}) })
}) })
} }
for (const input of invalidInputs) {
describe(`modPow(${input.a}, ${input.b}, ${input.n})`, function () {
it('should throw RangeError', function () {
try {
_pkg.modPow(input.a, input.b, input.n)
throw new Error('should have failed')
} catch (err) {
chai.expect(err).to.be.instanceOf(RangeError)
}
})
})
}
describe('Time profiling', function () { describe('Time profiling', function () {
let iterations = 500 let iterations = 500
it(`just testing ${iterations} iterations of a big modular exponentiation (1024 bits)`, function () { it(`just testing ${iterations} iterations of a big modular exponentiation (1024 bits)`, function () {

View File

@ -16,6 +16,13 @@ describe('toZn', function () {
toZn: BigInt(4) toZn: BigInt(4)
} }
] ]
const invalidInputs = [
{
a: BigInt(4),
n: BigInt(-1),
toZn: BigInt(0)
}
]
for (const input of inputs) { for (const input of inputs) {
describe(`toZn(${input.a}, ${input.n})`, function () { describe(`toZn(${input.a}, ${input.n})`, function () {
it(`should return ${input.toZn}`, function () { it(`should return ${input.toZn}`, function () {
@ -24,4 +31,16 @@ describe('toZn', function () {
}) })
}) })
} }
for (const input of invalidInputs) {
describe(`toZn(${input.a}, ${input.n})`, function () {
it('should throw RangeError', function () {
try {
_pkg.toZn(input.a, input.n)
throw new Error('should have failed')
} catch (err) {
chai.expect(err).to.be.instanceOf(RangeError)
}
})
})
}
}) })