-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeccak256.js
2 lines (2 loc) · 3.71 KB
/
keccak256.js
1
2
function b(t){if(!Number.isSafeInteger(t)||t<0)throw new Error()}function M(t){return t instanceof Uint8Array||t!=null&&typeof t=="object"&&t.constructor.name==="Uint8Array"}function p(t,...e){if(!M(t))throw new Error();if(e.length>0&&!e.includes(t.length))throw new Error()}function k(t,e=!0){if(t.d)throw new Error();if(e&&t.f)throw new Error()}function m(t,e){p(t);let n=e.o;if(t.length<n)throw new Error()}var x=4294967295n,A=32n;function D(t,e=!1){return e?{h:Number(t&x),l:Number(t>>A&x)}:{h:Number(t>>A&x)|0,l:Number(t&x)|0}}function B(t,e=!1){let n=new Uint32Array(t.length),r=new Uint32Array(t.length);for(let s=0;s<t.length;s++){let{h:i,l:o}=D(t[s],e);[n[s],r[s]]=[i,o]}return[n,r]}var O=(t,e,n)=>t<<n|e>>>32-n,I=(t,e,n)=>e<<n|t>>>32-n,S=(t,e,n)=>e<<n-32|t>>>64-n,H=(t,e,n)=>t<<n-32|e>>>64-n;var E=t=>new Uint32Array(t.buffer,t.byteOffset,Math.floor(t.byteLength/4));var L=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68,P=t=>t<<24&4278190080|t<<8&16711680|t>>>8&65280|t>>>24&255;function _(t){for(let e=0;e<t.length;e++)t[e]=P(t[e])}function W(t){if(typeof t!="string")throw new Error();return new Uint8Array(new TextEncoder().encode(t))}function w(t){return typeof t=="string"&&(t=W(t)),p(t),t}var ot={}.toString;function U(t){let e=r=>t().u(w(r)).g(),n=t();return e.o=n.o,e.b=n.b,e.c=()=>t(),e}function T(t){let e=(r,s)=>t(s).u(w(r)).g(),n=t({});return e.o=n.o,e.b=n.b,e.c=r=>t(r),e}var X=[],$=[],C=[],v=0n,h=1n,q=2n,z=7n,G=256n,J=113n;for(let t=0,e=h,n=1,r=0;t<24;t++){[n,r]=[r,(2*n+3*r)%5],X.push(2*(5*r+n)),$.push((t+1)*(t+2)/2%64);let s=v;for(let i=0;i<7;i++)e=(e<<h^(e>>z)*J)%G,e&q&&(s^=h<<(h<<BigInt(i))-h);C.push(s)}var[K,Q]=B(C,!0),F=(t,e,n)=>n>32?S(t,e,n):O(t,e,n),j=(t,e,n)=>n>32?H(t,e,n):I(t,e,n);function Y(t,e=24){let n=new Uint32Array(10);for(let r=24-e;r<24;r++){for(let o=0;o<10;o++)n[o]=t[o]^t[o+10]^t[o+20]^t[o+30]^t[o+40];for(let o=0;o<10;o+=2){let c=(o+8)%10,l=(o+2)%10,a=n[l],u=n[l+1],R=F(a,u,1)^n[c],V=j(a,u,1)^n[c+1];for(let d=0;d<50;d+=10)t[o+d]^=R,t[o+d+1]^=V}let s=t[2],i=t[3];for(let o=0;o<24;o++){let c=$[o],l=F(s,i,c),a=j(s,i,c),u=X[o];s=t[u],i=t[u+1],t[u]=l,t[u+1]=a}for(let o=0;o<50;o+=10){for(let c=0;c<10;c++)n[c]=t[o+c];for(let c=0;c<10;c++)t[o+c]^=~n[(c+2)%10]&n[(c+4)%10]}t[0]^=K[r],t[1]^=Q[r]}n.fill(0)}var g=class t{constructor(e,n,r,s=!1,i=24){if(this.b=e,this.s=n,this.o=r,this.e=s,this.r=i,this.po=0,this.p=0,this.f=!1,this.d=!1,b(r),0>=this.b||this.b>=200)throw new Error();this.q=new Uint8Array(200),this.s3=E(this.q)}k(){L||_(this.s3),Y(this.s3,this.r),L||_(this.s3),this.p=0,this.po=0}u(e){k(this);let{b:n,q:r}=this;e=w(e);let s=e.length;for(let i=0;i<s;){let o=Math.min(n-this.po,s-i);for(let c=0;c<o;c++)r[this.po++]^=e[i++];this.po===n&&this.k()}return this}fi(){if(this.f)return;this.f=!0;let{q:e,s:n,po:r,b:s}=this;e[r]^=n,n&128&&r===s-1&&this.k(),e[s-1]^=128,this.k()}w(e){k(this,!1),p(e),this.fi();let n=this.q,{b:r}=this;for(let s=0,i=e.length;s<i;){this.p>=r&&this.k();let o=Math.min(r-this.p,i-s);e.set(n.subarray(this.p,this.p+o),s),this.p+=o,s+=o}return e}x(e){if(!this.e)throw new Error();return this.w(e)}xof(e){return b(e),this.x(new Uint8Array(e))}di(e){if(m(e,this),this.f)throw new Error();return this.w(e),this.de(),e}g(){return this.di(new Uint8Array(this.o))}de(){this.d=!0,this.q.fill(0)}c(e){let{b:n,s:r,o:s,r:i,e:o}=this;return e||(e=new t(n,r,s,o,i)),e.s3.set(this.s3),e.po=this.po,e.p=this.p,e.f=this.f,e.r=i,e.s=r,e.o=s,e.e=o,e.d=this.d,e}},f=(t,e,n)=>U(()=>new g(e,t,n)),ft=f(6,144,224/8),ut=f(6,136,256/8),pt=f(6,104,384/8),ht=f(6,72,512/8),lt=f(1,144,224/8),Z=f(1,136,256/8),at=f(1,104,384/8),dt=f(1,72,512/8),N=(t,e,n)=>T((r={})=>new g(e,t,r.D===void 0?n:r.D,!0)),xt=N(31,168,128/8),yt=N(31,136,256/8);export{Z as keccak256};
// noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com)