UNPKG

@arithmetic-operations-for/integers-modulo-n-big-endian

Version:
3 lines (2 loc) 3.95 kB
!function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports,require("@arithmetic-operations-for/naturals-big-endian")):"function"==typeof define&&define.amd?define(["exports","@arithmetic-operations-for/naturals-big-endian"],s):s((t||self).integersModuloNBigEndian={},t.naturalsBigEndian)}(this,function(t,s){function i(t,i,n,h,o,r,e,c,u,f,d){const a=d-i,l=i<<1,p=s._zeros(l),w=l;s._mul(t,u,a,d,r,e,c,p,0,w);const m=i,R=s._zeros(l),b=l;s._mul(t,p,m,w,n,h,o,R,0,b),s._iadd(t,u,f,d,R,0,b),s._copy(u,f+1,a,u,a);const g=a;return s._reset(u,f,g),s._cmp_n(u,g,d,n,h)>=0&&(s._isub(t,u,g,d,n,h,o),!0)}function n(t,n,h,o,r,e){const c=n.length,u=2*c+1;return s._mul(t,o,0,o.length,r,0,r.length,e,u-o.length-r.length,u),i(t,c,n,0,c,h,0,c,e,0,u)}function h(t,i,n,h){const o=i.length;return s._iadd(t,n,0,o,h,0,h.length),(s._cmp(n,0,o,h,0,h.length)<0||s._cmp_n(n,0,o,i,0)>=0)&&(s._isub(t,n,0,o,i,0,o),!0)}function o(t,i,n,h){const o=i.length,r=s._cmp(n,0,o,h,0,h.length)<0;return s._isub(t,n,0,o,h,0,h.length),!!r&&(s._iadd(t,n,0,o,i,0,o),!0)}function r(t,n){const h=n.length,o=2*h+1,r=s._zeros(o);r[h]=1;const[e,c,u,f,d,a,l,p,w,m,R]=s._extended_euclidean_algorithm(t,r,h,o,n,0,h);if(e.length-c!=1||1!==e[c])throw new Error("Montgomery: GCD(R,N) is not 1.");const b=s._alloc(h);R%2==0?s._sub(t,r,0,h,d,d.length-h,d.length,b,0,h):s._copy(d,d.length-h,d.length,b,0);const g=s._zeros(o);g[0]=1,s._idivmod(t,g,0,o,n,0,h,r,0,o);const y=s._alloc(h);s._copy(g,h+1,o,y,0),i(t,h,n,0,h,b,0,h,g,0,o);const v=s._alloc(h);s._copy(g,h+1,o,v,0);const x=s._zeros(o);s._mul(t,y,0,h,y,0,h,x,1,o),i(t,h,n,0,h,b,0,h,x,0,o);const M=s._alloc(h);return s._copy(x,h+1,o,M,0),{k:h,M:b,R:v,R2:y,R3:M}}function e(t,i){const n=s._alloc(t);return s._copy(i,i.length-t,i.length,n,0),n}function c(t,i,n){const h=i.length,o=n.length,r=s._trim_positive(n,0,o),c=o-r;if(c>h||c===h&&s._cmp_n(n,r,o,i,0)>=0){const u=s._alloc(c);s._copy(n,r,o,u,0);const f=s._zeros(c);return s._idivmod(t,u,0,c,i,0,h,f,0,c),e(h,u)}const u=s._zeros(h);return s._copy(n,r,o,u,h-c),u}t.Montgomery=class{constructor(t,s){const{k:i,M:n,R:h,R2:o,R3:e}=r(t,s);this.b=t,this.N=s,this.k=i,this.M=n,this.R=h,this.R2=o,this.R3=e}one(){return this.R}zero(){return s._zeros(this.k)}from(t){const i=s._zeros(2*this.k+1),h=c(this.b,this.N,t);return n(this.b,this.N,this.M,this.R2,h,i),e(this.k,i)}out(t){const n=2*this.k+1,h=s._zeros(n);s._copy(t,0,this.k,h,n-this.k),i(this.b,this.k,this.N,0,this.k,this.M,0,this.k,h,0,n);const o=s._trim_positive(h,this.k+1,n),r=s._alloc(n-o);return s._copy(h,o,n,r,0),r}mul(t,i){const h=s._zeros(2*this.k+1);return n(this.b,this.N,this.M,t,i,h),e(this.k,h)}add(t,i){const n=s._alloc(this.k);return s._copy(t,0,this.k,n,0),h(this.b,this.N,n,i),n}sub(t,i){const n=s._alloc(this.k);return s._copy(t,0,this.k,n,0),o(this.b,this.N,n,i),n}inv(t){const i=s._trim_positive(t,0,this.k);let[h,o,r,c,u,f,d,a,l,p,w]=s._extended_euclidean_algorithm(this.b,this.N,0,this.k,t,i,this.k);if(h.length-o!=1||1!==h[o])throw new Error("aRmodN has no inverse modulo N");const m=s._zeros(2*this.k+1);if(w%2==1){const t=s._zeros(this.k);s._sub(this.b,this.N,0,this.k,u,0,this.k,t,0,this.k),u=t}return n(this.b,this.N,this.M,this.R3,u,m),e(this.k,m)}pown(t,s){const i=s>=0;if(i||(s=-s),0===s)return this.R;if(1===s)return i?t:this.inv(t);const n=[];do{n.push(1&s),s>>=1}while(1!==s);return this.p(t,n,i)}p(t,i,h){const o=s._alloc(this.k);s._copy(t,0,this.k,o,0);const r=2*this.k+1,e=s._alloc(r);do{s._reset(e,0,r),n(this.b,this.N,this.M,o,o,e),s._copy(e,r-this.k,r,o,0),1===i.pop()&&(s._reset(e,0,r),n(this.b,this.N,this.M,o,t,e),s._copy(e,r-this.k,r,o,0))}while(i.length>0);return h?o:this.inv(o)}pow(t,i,n){if(void 0===n&&(n=!0),s.jz(i,0,i.length-1))return this.pown(t,n?i[i.length-1]:-i[i.length-1]);const h=s.convert(this.b,2,i,0,i.length);return h.reverse(),h.pop(),this.p(t,h,n)}},t._iadd=h,t._isub=o,t._montgomery=r,t._mul=n,t._redc=i,t.modN=c,t.modR=e}); //# sourceMappingURL=index.umd.js.map