@bnb-chain/reed-solomon
Version:
lighting implement for reed solomon
4 lines (3 loc) • 12 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).RSUtils={})}(this,(function(t){"use strict";let e=!1,r=!1;const n={debug:1,default:2,info:2,warning:3,error:4,off:5};let o=n.default,s=null;const i=function(){try{const t=[];if(["NFD","NFC","NFKD","NFKC"].forEach((e=>{try{if("test"!=="test".normalize(e))throw new Error("bad normalize")}catch(r){t.push(e)}})),t.length)throw new Error("missing "+t.join(", "));if(String.fromCharCode(233).normalize("NFD")!==String.fromCharCode(101,769))throw new Error("broken implementation")}catch(t){return t.message}return null}();var h,u;!function(t){t.DEBUG="DEBUG",t.INFO="INFO",t.WARNING="WARNING",t.ERROR="ERROR",t.OFF="OFF"}(h||(h={})),function(t){t.UNKNOWN_ERROR="UNKNOWN_ERROR",t.NOT_IMPLEMENTED="NOT_IMPLEMENTED",t.UNSUPPORTED_OPERATION="UNSUPPORTED_OPERATION",t.NETWORK_ERROR="NETWORK_ERROR",t.SERVER_ERROR="SERVER_ERROR",t.TIMEOUT="TIMEOUT",t.BUFFER_OVERRUN="BUFFER_OVERRUN",t.NUMERIC_FAULT="NUMERIC_FAULT",t.MISSING_NEW="MISSING_NEW",t.INVALID_ARGUMENT="INVALID_ARGUMENT",t.MISSING_ARGUMENT="MISSING_ARGUMENT",t.UNEXPECTED_ARGUMENT="UNEXPECTED_ARGUMENT",t.CALL_EXCEPTION="CALL_EXCEPTION",t.INSUFFICIENT_FUNDS="INSUFFICIENT_FUNDS",t.NONCE_EXPIRED="NONCE_EXPIRED",t.REPLACEMENT_UNDERPRICED="REPLACEMENT_UNDERPRICED",t.UNPREDICTABLE_GAS_LIMIT="UNPREDICTABLE_GAS_LIMIT",t.TRANSACTION_REPLACED="TRANSACTION_REPLACED",t.ACTION_REJECTED="ACTION_REJECTED"}(u||(u={}));const a="0123456789abcdef";class c{constructor(t){Object.defineProperty(this,"version",{enumerable:!0,value:t,writable:!1})}_log(t,e){const r=t.toLowerCase();null==n[r]&&this.throwArgumentError("invalid log level name","logLevel",t),o>n[r]||console.log.apply(console,e)}debug(...t){this._log(c.levels.DEBUG,t)}info(...t){this._log(c.levels.INFO,t)}warn(...t){this._log(c.levels.WARNING,t)}makeError(t,e,n){if(r)return this.makeError("censored error",e,{});e||(e=c.errors.UNKNOWN_ERROR),n||(n={});const o=[];Object.keys(n).forEach((t=>{const e=n[t];try{if(e instanceof Uint8Array){let r="";for(let t=0;t<e.length;t++)r+=a[e[t]>>4],r+=a[15&e[t]];o.push(t+"=Uint8Array(0x"+r+")")}else o.push(t+"="+JSON.stringify(e))}catch(e){o.push(t+"="+JSON.stringify(n[t].toString()))}})),o.push(`code=${e}`),o.push(`version=${this.version}`);const s=t;let i="";switch(e){case u.NUMERIC_FAULT:{i="NUMERIC_FAULT";const e=t;switch(e){case"overflow":case"underflow":case"division-by-zero":i+="-"+e;break;case"negative-power":case"negative-width":i+="-unsupported";break;case"unbound-bitwise-result":i+="-unbound-result"}break}case u.CALL_EXCEPTION:case u.INSUFFICIENT_FUNDS:case u.MISSING_NEW:case u.NONCE_EXPIRED:case u.REPLACEMENT_UNDERPRICED:case u.TRANSACTION_REPLACED:case u.UNPREDICTABLE_GAS_LIMIT:i=e}i&&(t+=" [ See: https://links.ethers.org/v5-errors-"+i+" ]"),o.length&&(t+=" ("+o.join(", ")+")");const h=new Error(t);return h.reason=s,h.code=e,Object.keys(n).forEach((function(t){h[t]=n[t]})),h}throwError(t,e,r){throw this.makeError(t,e,r)}throwArgumentError(t,e,r){return this.throwError(t,c.errors.INVALID_ARGUMENT,{argument:e,value:r})}assert(t,e,r,n){t||this.throwError(e,r,n)}assertArgument(t,e,r,n){t||this.throwArgumentError(e,r,n)}checkNormalize(t){i&&this.throwError("platform missing String.prototype.normalize",c.errors.UNSUPPORTED_OPERATION,{operation:"String.prototype.normalize",form:i})}checkSafeUint53(t,e){"number"==typeof t&&(null==e&&(e="value not safe"),(t<0||t>=9007199254740991)&&this.throwError(e,c.errors.NUMERIC_FAULT,{operation:"checkSafeInteger",fault:"out-of-safe-range",value:t}),t%1&&this.throwError(e,c.errors.NUMERIC_FAULT,{operation:"checkSafeInteger",fault:"non-integer",value:t}))}checkArgumentCount(t,e,r){r=r?": "+r:"",t<e&&this.throwError("missing argument"+r,c.errors.MISSING_ARGUMENT,{count:t,expectedCount:e}),t>e&&this.throwError("too many arguments"+r,c.errors.UNEXPECTED_ARGUMENT,{count:t,expectedCount:e})}checkNew(t,e){t!==Object&&null!=t||this.throwError("missing new",c.errors.MISSING_NEW,{name:e.name})}checkAbstract(t,e){t===e?this.throwError("cannot instantiate abstract class "+JSON.stringify(e.name)+" directly; use a sub-class",c.errors.UNSUPPORTED_OPERATION,{name:t.name,operation:"new"}):t!==Object&&null!=t||this.throwError("missing new",c.errors.MISSING_NEW,{name:e.name})}static globalLogger(){return s||(s=new c("logger/5.8.0")),s}static setCensorship(t,n){if(!t&&n&&this.globalLogger().throwError("cannot permanently disable censorship",c.errors.UNSUPPORTED_OPERATION,{operation:"setCensorship"}),e){if(!t)return;this.globalLogger().throwError("error censorship permanent",c.errors.UNSUPPORTED_OPERATION,{operation:"setCensorship"})}r=!!t,e=!!n}static setLogLevel(t){const e=n[t.toLowerCase()];null!=e?o=e:c.globalLogger().warn("invalid log level - "+t)}static from(t){return new c(t)}}c.errors=u,c.levels=h;const l=new c("bytes/5.8.0");function f(t){return t.slice||(t.slice=function(){const e=Array.prototype.slice.call(arguments);return f(new Uint8Array(Array.prototype.slice.apply(t,e)))}),t}function E(t){return"number"==typeof t&&t==t&&t%1==0}function g(t,e){if(e||(e={}),"number"==typeof t){l.checkSafeUint53(t,"invalid arrayify value");const e=[];for(;t;)e.unshift(255&t),t=parseInt(String(t/256));return 0===e.length&&e.push(0),f(new Uint8Array(e))}if(e.allowMissingPrefix&&"string"==typeof t&&"0x"!==t.substring(0,2)&&(t="0x"+t),function(t){return!!t.toHexString}(t)&&(t=t.toHexString()),function(t,e){if("string"!=typeof t||!t.match(/^0x[0-9A-Fa-f]*$/))return!1;if(e&&t.length!==2+2*e)return!1;return!0}(t)){let r=t.substring(2);r.length%2&&("left"===e.hexPad?r="0"+r:"right"===e.hexPad?r+="0":l.throwArgumentError("hex data is odd-length","value",t));const n=[];for(let t=0;t<r.length;t+=2)n.push(parseInt(r.substring(t,t+2),16));return f(new Uint8Array(n))}return function(t){if(null==t)return!1;if(t.constructor===Uint8Array)return!0;if("string"==typeof t)return!1;if(!E(t.length)||t.length<0)return!1;for(let e=0;e<t.length;e++){const r=t[e];if(!E(r)||r<0||r>=256)return!1}return!0}(t)?f(new Uint8Array(t)):l.throwArgumentError("invalid arrayify value","value",t)}function N(t){t=g(t);let e="";for(let r=0;r<t.length;r++)e+=String.fromCharCode(t[r]);return btoa(e)}function p(t){if(!Number.isSafeInteger(t)||t<0)throw new Error(`positive integer expected, not ${t}`)}function d(t,...e){if(!((r=t)instanceof Uint8Array||null!=r&&"object"==typeof r&&"Uint8Array"===r.constructor.name))throw new Error("Uint8Array expected");var r;if(e.length>0&&!e.includes(t.length))throw new Error(`Uint8Array expected of length ${e}, not of length=${t.length}`)}function R(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}function w(t,e){d(t);const r=e.outputLen;if(t.length<r)throw new Error(`digestInto() expects output buffer of length at least ${r}`)}const I={number:p,bool:function(t){if("boolean"!=typeof t)throw new Error(`boolean expected, not ${t}`)},bytes:d,hash:function(t){if("function"!=typeof t||"function"!=typeof t.create)throw new Error("Hash should be wrapped by utils.wrapConstructor");p(t.outputLen),p(t.blockLen)},exists:R,output:w},U=t=>new DataView(t.buffer,t.byteOffset,t.byteLength),y=(t,e)=>t<<32-e|t>>>e;
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */function A(t){return"string"==typeof t&&(t=function(t){if("string"!=typeof t)throw new Error("utf8ToBytes expected string, got "+typeof t);return new Uint8Array((new TextEncoder).encode(t))}(t)),d(t),t}new Uint8Array(new Uint32Array([287454020]).buffer)[0];class b{clone(){return this._cloneInto()}}function T(t){const e=e=>t().update(A(e)).digest(),r=t();return e.outputLen=r.outputLen,e.blockLen=r.blockLen,e.create=()=>t(),e}const _=(t,e,r)=>t&e^t&r^e&r;class O extends b{constructor(t,e,r,n){super(),this.blockLen=t,this.outputLen=e,this.padOffset=r,this.isLE=n,this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.buffer=new Uint8Array(t),this.view=U(this.buffer)}update(t){R(this);const{view:e,buffer:r,blockLen:n}=this,o=(t=A(t)).length;for(let s=0;s<o;){const i=Math.min(n-this.pos,o-s);if(i!==n)r.set(t.subarray(s,s+i),this.pos),this.pos+=i,s+=i,this.pos===n&&(this.process(e,0),this.pos=0);else{const e=U(t);for(;n<=o-s;s+=n)this.process(e,s)}}return this.length+=t.length,this.roundClean(),this}digestInto(t){R(this),w(t,this),this.finished=!0;const{buffer:e,view:r,blockLen:n,isLE:o}=this;let{pos:s}=this;e[s++]=128,this.buffer.subarray(s).fill(0),this.padOffset>n-s&&(this.process(r,0),s=0);for(let t=s;t<n;t++)e[t]=0;!function(t,e,r,n){if("function"==typeof t.setBigUint64)return t.setBigUint64(e,r,n);const o=BigInt(32),s=BigInt(4294967295),i=Number(r>>o&s),h=Number(r&s),u=n?4:0,a=n?0:4;t.setUint32(e+u,i,n),t.setUint32(e+a,h,n)}(r,n-8,BigInt(8*this.length),o),this.process(r,0);const i=U(t),h=this.outputLen;if(h%4)throw new Error("_sha2: outputLen should be aligned to 32bit");const u=h/4,a=this.get();if(u>a.length)throw new Error("_sha2: outputLen bigger than state");for(let t=0;t<u;t++)i.setUint32(4*t,a[t],o)}digest(){const{buffer:t,outputLen:e}=this;this.digestInto(t);const r=t.slice(0,e);return this.destroy(),r}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());const{blockLen:e,buffer:r,length:n,finished:o,destroyed:s,pos:i}=this;return t.length=n,t.pos=i,t.finished=o,t.destroyed=s,n%e&&t.buffer.set(r),t}}const C=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),m=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),L=new Uint32Array(64);class S extends O{constructor(){super(64,32,8,!1),this.A=0|m[0],this.B=0|m[1],this.C=0|m[2],this.D=0|m[3],this.E=0|m[4],this.F=0|m[5],this.G=0|m[6],this.H=0|m[7]}get(){const{A:t,B:e,C:r,D:n,E:o,F:s,G:i,H:h}=this;return[t,e,r,n,o,s,i,h]}set(t,e,r,n,o,s,i,h){this.A=0|t,this.B=0|e,this.C=0|r,this.D=0|n,this.E=0|o,this.F=0|s,this.G=0|i,this.H=0|h}process(t,e){for(let r=0;r<16;r++,e+=4)L[r]=t.getUint32(e,!1);for(let t=16;t<64;t++){const e=L[t-15],r=L[t-2],n=y(e,7)^y(e,18)^e>>>3,o=y(r,17)^y(r,19)^r>>>10;L[t]=o+L[t-7]+n+L[t-16]|0}let{A:r,B:n,C:o,D:s,E:i,F:h,G:u,H:a}=this;for(let t=0;t<64;t++){const e=a+(y(i,6)^y(i,11)^y(i,25))+((c=i)&h^~c&u)+C[t]+L[t]|0,l=(y(r,2)^y(r,13)^y(r,22))+_(r,n,o)|0;a=u,u=h,h=i,i=s+e|0,s=o,o=n,n=r,r=e+l|0}var c;r=r+this.A|0,n=n+this.B|0,o=o+this.C|0,s=s+this.D|0,i=i+this.E|0,h=h+this.F|0,u=u+this.G|0,a=a+this.H|0,this.set(r,n,o,s,i,h,u,a)}roundClean(){L.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}}const P=T((()=>new S));(()=>{const t="object"==typeof globalThis&&"crypto"in globalThis?globalThis.crypto:void 0,e="undefined"!=typeof module&&"function"==typeof module.require&&module.require.bind(module);e&&!t&&e("crypto")})();const D=function(t){return e=>(I.bytes(e),t(e))}(P);function v(t,e){let r=[];return t.forEach((t=>{r.push(t)})),e.forEach((t=>{r.push(t)})),r}t.concat=v,t.getIntegrityUint8Array=function(t){const e=t.reduce(((t,e)=>v(t,e)),[]);return Uint8Array.from(e)},t.sha256=D,t.splitPrice=function(t,e){let r=[],n=0;for(;n<t.length;)r.push(t.slice(n,n+e)),n+=e;return r},t.toBase64=function(t){const e=[];for(let r=0;r<t.length;r++)e.push(N(t[r]));return e},Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=utils.aio.js.map