gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 1.23 kB
JavaScript
/*! Copyright 2025 the gnablib contributors MPL-1.1 */
import{asLE as t}from"../endian/platform.js";import{U64 as s,U64MutArray as e}from"../primitive/number/U64.js";import{sLen as a}from"../safe/safe.js";import{APrng64 as r}from"./APrng64.js";export class Sfc64 extends r{constructor(){super(...arguments),this.bitGen=64,this.safeBits=64}rawNext(){const t=this._state.at(0).mut().addEq(this._state.at(1)).addEq(this._state.at(3));return this._state.at(3).addEq(s.fromI32s(1,0)),this._state.at(0).set(this._state.at(1).xorEq(this._state.at(1).rShiftEq(11))),this._state.at(1).set(this._state.at(2).add(this._state.at(2).lShift(3))),this._state.at(2).lRotEq(24).addEq(t),t}get[Symbol.toStringTag](){return"sfc64"}static new(t=!1){const s=Uint32Array.of(2065550767,3793791033,2713282036,1853398634,2148091215,113532184,1,0),a=e.mount(s);return new Sfc64(a,t)}static seed(t,a,r,i=!1){const o=e.fromLen(8);let n;o.at(0).set(t),a&&r?(o.at(1).set(a),o.at(2).set(r),n=18):(o.at(1).set(t),o.at(2).set(t),n=12),o.at(3).set(s.fromI32s(1,0));const f=new Sfc64(o,i);for(let t=0;t<n;t++)f.rawNext();return f}static restore(s,r=!1){a("state",s).exactly(32).throwNot();const i=s.slice();return t.i32(i,0,8),new Sfc64(e.fromBytes(i.buffer),r)}}