// https://tools.ietf.org/html/rfc7693
import {MASK,int32s2bytes,bytes2int32s,zeroPad} from "./util.js";
const BLOCK_LEN=64;
const IV=[0x6A09E667,0xBB67AE85,0x3C6EF372,0xA54FF53A,0x510E527F,0x9B05688C,0x1F83D9AB,0x5BE0CD19];
function rrot(x,shift){
return ((x>>>shift)|(x<<(32-shift)))&MASK;
}
/**
* @param {number} outputLen output length in bytes, 1<=x<=32
* @param {Array} key byte array, 0<=key.length<=32
* @returns {BLAKE2S}
*/
export function BLAKE2S(outputLen=32,key=[]){
this._buffer=[];
this._dataLen=[0,0]; // low, high
this._outputLen=outputLen;
this._state=IV.slice();
this._state[0]^=0x01010000^(key.length<<8)^this._outputLen;
if(key.length>0){this.update(zeroPad(key,BLOCK_LEN));}
}
BLAKE2S.prototype.update=function(data){
for(let i=0;ix^v[i]^v[i+8]);
};
BLAKE2S.prototype._mix=function(arr,ia,ib,ic,id,x,y){
let a=arr[ia], b=arr[ib], c=arr[ic], d=arr[id];
a=(a+b+x)&MASK; d=rrot(d^a,16);
c=(c+d)&MASK; b=rrot(b^c,12);
a=(a+b+y)&MASK; d=rrot(d^a,8);
c=(c+d)&MASK; b=rrot(b^c,7);
arr[ia]=a; arr[ib]=b; arr[ic]=c; arr[id]=d;
};
export function blake2s(data,outputLen=32,key=[]){
let h=new BLAKE2S(outputLen,key);
for(let i=0;i