forked from dapplink-labs/dapplink-secret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secrets.min.js
2 lines (2 loc) · 9.3 KB
/
secrets.min.js
1
2
/*! savour-secret 2022-12-30 */
!function(r,t){"use strict";"function"==typeof define&&define.amd?define([],function(){return r.secrets=t()}):"object"==typeof exports?module.exports=t(require("crypto")):r.secrets=t(r.crypto)}(this,function(n){"use strict";var u,b,i,a,s;function h(){u={bits:8,radix:16,minBits:3,maxBits:20,bytesPerChar:2,maxBytesPerChar:6,primitivePolynomials:[null,null,1,3,3,5,3,3,29,17,9,5,83,27,43,3,45,9,39,39,9,5,3,33,27,9,71,39,9,5,83]},b={},i=new Array(1024).join("0"),a=!0,s=["nodeCryptoRandomBytes","browserCryptoGetRandomValues","testRandom"]}function f(){return!(!b||!b.rng||"function"!=typeof b.rng)}function g(r,t){var e;if(0===t||1===t)return r;if(t&&1024<t)throw new Error("Padding must be multiples of no larger than 1024 bits.");return t=t||b.bits,(e=r?r.length%t:e)?(i+r).slice(-(t-e+r.length)):r}function m(r){for(var t,e="",n=r.length-1;0<=n;n--){if(t=parseInt(r[n],16),isNaN(t))throw new Error("Invalid hex character.");e=g(t.toString(2),4)+e}return e}function c(r){for(var t,e="",n=(r=g(r,4)).length;4<=n;n-=4){if(t=parseInt(r.slice(n-4,n),2),isNaN(t))throw new Error("Invalid binary character.");e=t.toString(16)+e}return e}function o(){return!(!n||"object"!=typeof n||"function"!=typeof n.getRandomValues&&"object"!=typeof n.getRandomValues||"function"!=typeof Uint32Array&&"object"!=typeof Uint32Array)}function l(){return"object"==typeof n&&"function"==typeof n.randomBytes}function p(r){function a(r,t,e,n){var i,a=0,o="";for(t&&(i=t.length-1);a<i||o.length<r;)o+=g(Math.abs(parseInt(t[a],e)).toString(2),n),a++;return((o=o.substr(-r)).match(/0/g)||[]).length===o.length?null:o}function t(r){for(var t=null,e=Math.ceil(r/8);null===t;)t=a(r,n.randomBytes(e).toString("hex"),16,4);return t}function e(r){for(var t=null,e=Math.ceil(r/32);null===t;)t=a(r,n.getRandomValues(new Uint32Array(e)),10,32);return t}return r&&"testRandom"===r?(b.typeCSPRNG=r,function(r){for(var t=null,e=Math.ceil(r/32),n=new Uint32Array(e),i=0;i<n.length;i++)n[i]=123456789;for(;null===t;)t=a(r,n,10,32);return t}):r&&"nodeCryptoRandomBytes"===r?(b.typeCSPRNG=r,t):r&&"browserCryptoGetRandomValues"===r?(b.typeCSPRNG=r,e):l()?(b.typeCSPRNG="nodeCryptoRandomBytes",t):o()?(b.typeCSPRNG="browserCryptoGetRandomValues",e):void 0}function d(r,t){for(var e=[],n=(r=t?g(r,t):r).length;n>b.bits;n-=b.bits)e.push(parseInt(r.slice(n-b.bits,n),2));return e.push(parseInt(r.slice(0,n),2)),e}function w(r,t){for(var e=b.logs[r],n=0,i=t.length-1;0<=i;i--)n=0!==n?b.exps[(e+b.logs[n])%b.maxShares]^t[i]:t[i];return n}function y(r,t,e){for(var n,i,a=0,o=0,s=t.length;o<s;o++)if(e[o]){for(n=b.logs[e[o]],i=0;i<s;i++)if(o!==i){if(r===t[i]){n=-1;break}n=(n+b.logs[r^t[i]]-b.logs[t[o]^t[i]]+b.maxShares)%b.maxShares}a=-1===n?a:a^b.exps[n]}return a}function x(r,t,e){for(var n,i=[],a=[r],o=1;o<e;o++)a[o]=parseInt(b.rng(b.bits),2);for(n=t+(o=1);o<n;o++)i[o-1]={x:o,y:w(o,a)};return i}function v(r,t,e){var n,i;if(t=parseInt(t,b.radix),n=(r=parseInt(r,10)||b.bits).toString(36).toUpperCase(),i=(r=Math.pow(2,r)-1).toString(b.radix).length,i=g(t.toString(b.radix),i),"number"!=typeof t||t%1!=0||t<1||r<t)throw new Error("Share id must be an integer between 1 and "+r+", inclusive.");return n+i+e}var r={init:function(r,t){var e,n,i=[],a=[],o=1;if(h(),r&&("number"!=typeof r||r%1!=0||r<u.minBits||r>u.maxBits))throw new Error("Number of bits must be an integer between "+u.minBits+" and "+u.maxBits+", inclusive.");if(t&&-1===s.indexOf(t))throw new Error("Invalid RNG type argument : '"+t+"'");for(b.radix=u.radix,b.bits=r||u.bits,b.size=Math.pow(2,b.bits),b.maxShares=b.size-1,e=u.primitivePolynomials[b.bits],n=0;n<b.size;n++)i[a[n]=o]=n,(o<<=1)>=b.size&&(o=(o^=e)&b.maxShares);if(b.logs=i,b.exps=a,t&&this.setRNG(t),f()||this.setRNG(),!(f()&&b.bits&&b.size&&b.maxShares&&b.logs&&b.exps&&b.logs.length===b.size&&b.exps.length===b.size))throw new Error("Initialization failed.")},combine:function(r,t){var e,n,i,a,o,s,h,u="",f=[],l=[];for(t=t||0,e=0,i=r.length;e<i;e++){if(s=this.extractShareComponents(r[e]),void 0===o)o=s.bits;else if(s.bits!==o)throw new Error("Mismatched shares: Different bit settings.");if(b.bits!==o&&this.init(o),-1===f.indexOf(s.id))for(f.push(s.id),n=0,a=(h=d(m(s.data))).length;n<a;n++)l[n]=l[n]||[],l[n][f.length-1]=h[n]}for(e=0,i=l.length;e<i;e++)u=g(y(t,f,l[e]).toString(2))+u;return c(1<=t?u:u.slice(u.indexOf("1")+1))},getConfig:function(){var r={};return r.radix=b.radix,r.bits=b.bits,r.maxShares=b.maxShares,r.hasCSPRNG=f(),r.typeCSPRNG=b.typeCSPRNG,r},extractShareComponents:function(r){var t,e,n,i={},a=parseInt(r.substr(0,1),36);if(a&&("number"!=typeof a||a%1!=0||a<u.minBits||a>u.maxBits))throw new Error("Invalid share : Number of bits must be an integer between "+u.minBits+" and "+u.maxBits+", inclusive.");if(e=Math.pow(2,a)-1,n=(Math.pow(2,a)-1).toString(b.radix).length,"number"!=typeof(t=(n=new RegExp("^([a-kA-K3-9]{1})([a-fA-F0-9]{"+n+"})([a-fA-F0-9]+)$").exec(r))?parseInt(n[2],b.radix):t)||t%1!=0||t<1||e<t)throw new Error("Invalid share : Share id must be an integer between 1 and "+b.maxShares+", inclusive.");if(n&&n[3])return i.bits=a,i.id=t,i.data=n[3],i;throw new Error("The share data provided is invalid : "+r)},setRNG:function(r){var t="Random number generator is invalid ",e=" Supply an CSPRNG of the form function(bits){} that returns a string containing 'bits' number of random 1's and 0's.";if(r&&"string"==typeof r&&-1===s.indexOf(r))throw new Error("Invalid RNG type argument : '"+r+"'");if((r=r||p())&&"string"==typeof r&&(r=p(r)),a){if(r&&"function"!=typeof r)throw new Error(t+"(Not a function)."+e);if(r&&"string"!=typeof r(b.bits))throw new Error(t+"(Output is not a string)."+e);if(r&&!parseInt(r(b.bits),2))throw new Error(t+"(Binary string output not parseable to an Integer)."+e);if(r&&r(b.bits).length>b.bits)throw new Error(t+"(Output length is greater than config.bits)."+e);if(r&&r(b.bits).length<b.bits)throw new Error(t+"(Output length is less than config.bits)."+e)}return b.rng=r,!0},str2hex:function(r,t){var e,n,i,a,o,s,h="";if("string"!=typeof r)throw new Error("Input must be a character string.");if("number"!=typeof(t=t||u.bytesPerChar)||t<1||t>u.maxBytesPerChar||t%1!=0)throw new Error("Bytes per character must be an integer between 1 and "+u.maxBytesPerChar+", inclusive.");for(e=2*t,n=Math.pow(16,e)-1,o=0,s=r.length;o<s;o++){if(a=r[o].charCodeAt(),isNaN(a))throw new Error("Invalid character: "+r[o]);if(n<a)throw i=Math.ceil(Math.log(a+1)/Math.log(256)),new Error("Invalid character code ("+a+"). Maximum allowable is 256^bytes-1 ("+n+"). To convert this character, use at least "+i+" bytes.");h=g(a.toString(16),e)+h}return h},hex2str:function(r,t){var e,n,i,a="";if("string"!=typeof r)throw new Error("Input must be a hexadecimal string.");if("number"!=typeof(t=t||u.bytesPerChar)||t%1!=0||t<1||t>u.maxBytesPerChar)throw new Error("Bytes per character must be an integer between 1 and "+u.maxBytesPerChar+", inclusive.");for(n=0,i=(r=g(r,e=2*t)).length;n<i;n+=e)a=String.fromCharCode(parseInt(r.slice(n,n+e),16))+a;return a},random:function(r){if("number"!=typeof r||r%1!=0||r<2||65536<r)throw new Error("Number of bits must be an Integer between 1 and 65536.");return c(b.rng(r))},share:function(r,t,e,n){var i,a,o,s,h,u=new Array(t),f=new Array(t);if(n=n||128,"string"!=typeof r)throw new Error("Secret must be a string.");if("number"!=typeof t||t%1!=0||t<2)throw new Error("Number of shares must be an integer between 2 and 2^bits-1 ("+b.maxShares+"), inclusive.");if(t>b.maxShares)throw i=Math.ceil(Math.log(t+1)/Math.LN2),new Error("Number of shares must be an integer between 2 and 2^bits-1 ("+b.maxShares+"), inclusive. To create "+t+" shares, use at least "+i+" bits.");if("number"!=typeof e||e%1!=0||e<2)throw new Error("Threshold number of shares must be an integer between 2 and 2^bits-1 ("+b.maxShares+"), inclusive.");if(e>b.maxShares)throw i=Math.ceil(Math.log(e+1)/Math.LN2),new Error("Threshold number of shares must be an integer between 2 and 2^bits-1 ("+b.maxShares+"), inclusive. To use a threshold of "+e+", use at least "+i+" bits.");if(t<e)throw new Error("Threshold number of shares was "+e+" but must be less than or equal to the "+t+" shares specified as the total to generate.");if("number"!=typeof n||n%1!=0||n<0||1024<n)throw new Error("Zero-pad length must be an integer between 0 and 1024 inclusive.");for(o=0,h=(r=d(r="1"+m(r),n)).length;o<h;o++)for(a=x(r[o],t,e),s=0;s<t;s++)u[s]=u[s]||a[s].x.toString(b.radix),f[s]=g(a[s].y.toString(2))+(f[s]||"");for(o=0;o<t;o++)u[o]=v(b.bits,u[o],c(f[o]));return u},xorShare:function(t,e,n){if("string"==typeof t&&"number"==typeof e&&"number"==typeof n){var i=[];n=n<=25?n:n%25;for(let r=0;r<t.length;r++){var a=t.charCodeAt(r);a=(a=+a^e).toString(n),i.push(a)}var r=String.fromCharCode(n+97);return i.join(r)}},inverseXor:function(r,t,e){if("string"==typeof r&&"number"==typeof t&&"number"==typeof e){var n=[],i=(e=e<=25?e:e%25,String.fromCharCode(e+97)),a=r.split(i);for(let r=0;r<a.length;r++){var o=+parseInt(a[r],e)^t,o=String.fromCharCode(o);n.push(o)}return n.join("")}},newShare:function(r,t){var e=(r=r&&"string"==typeof r?parseInt(r,b.radix):r).toString(b.radix);if(r&&e&&t&&t[0])return v(this.extractShareComponents(t[0]).bits,e,this.combine(t,r));throw new Error("Invalid 'id' or 'shares' Array argument to newShare().")},_reset:h,_padLeft:g,_hex2bin:m,_bin2hex:c,_hasCryptoGetRandomValues:o,_hasCryptoRandomBytes:l,_getRNG:p,_isSetRNG:f,_splitNumStringToIntArray:d,_horner:w,_lagrange:y,_getShares:x,_constructPublicShareString:v};return r.init(),r});