it('allows priming the cache', async () => { const [ identityLoader, loadCalls ] = idLoader<string>(); identityLoader.prime('A', 'A'); const [ a, b ] = await Promise.all([ identityLoader.load('A'), identityLoader.load('B') ]); expect(a).toBe('A'); expect(b).toBe('B'); expect(loadCalls).toEqual([ [ 'B' ] ]); });
it('Does not interact with a cache when cache is disabled', () => { const promiseX = Promise.resolve('X'); const cacheMap = new Map([ [ 'X', promiseX ] ]); const [ identityLoader ] = idLoader<string>({ cache: false, cacheMap }); identityLoader.prime('A', 'A'); expect(cacheMap.get('A')).toBe(undefined); identityLoader.clear('X'); expect(cacheMap.get('X')).toBe(promiseX); identityLoader.clearAll(); expect(cacheMap.get('X')).toBe(promiseX); });
describe('Unhandled rejections', () => { it('Not catching a primed error is an unhandled rejection', async () => { let hadUnhandledRejection = false; // Override Jest's unhandled detection global.jasmine.process.removeAllListeners('unhandledRejection'); global.jasmine.process.on('unhandledRejection', () => { hadUnhandledRejection = true; }); const identityLoader = new DataLoader<number, number>(async keys => keys); identityLoader.prime(1, new Error('Error: 1')); // Ignore result. identityLoader.load(1); await new Promise(resolve => setTimeout(resolve, 10)); expect(hadUnhandledRejection).toBe(true); }); });
}); identityLoader.prime(1, 1);
it('allows forcefully priming the cache', async () => { const [ identityLoader, loadCalls ] = idLoader<string>(); identityLoader.prime('A', 'X'); const a1 = await identityLoader.load('A'); const b1 = await identityLoader.load('B'); expect(a1).toBe('X'); expect(b1).toBe('B'); identityLoader.clear('A').prime('A', 'Y'); identityLoader.clear('B').prime('B', 'Y'); const a2 = await identityLoader.load('A'); const b2 = await identityLoader.load('B'); expect(a2).toBe('Y'); expect(b2).toBe('Y'); expect(loadCalls).toEqual([ [ 'B' ] ]); });
const key2 = { id: 123 }; identityLoader.prime(key1, key1);
const [ identityLoader, loadCalls ] = idLoader<number>(); identityLoader.prime(1, new Error('Error: 1'));
it('does not prime keys that already exist', async () => { const [ identityLoader, loadCalls ] = idLoader<string>(); identityLoader.prime('A', 'X'); const a1 = await identityLoader.load('A'); const b1 = await identityLoader.load('B'); expect(a1).toBe('X'); expect(b1).toBe('B'); identityLoader.prime('A', 'Y'); identityLoader.prime('B', 'Y'); const a2 = await identityLoader.load('A'); const b2 = await identityLoader.load('B'); expect(a2).toBe('X'); expect(b2).toBe('B'); expect(loadCalls).toEqual([ [ 'B' ] ]); });
function BaseCurve(type,conf){this.type=type;this.p=new BN(conf.p,16);// Use Montgomery, when there is no fast reduction for the prime this.red=conf.prime?BN.red(conf.prime):BN.mont(this.p);// Useful for many curves this.zero=new BN(0).toRed(this.red);this.one=new BN(1).toRed(this.red);this.two=new BN(2).toRed(this.red);// Curve configuration, optional this.n=conf.n&&new BN(conf.n,16);this.g=conf.g&&this.pointFromJSON(conf.g,conf.gRed);// Temporary arrays this._wnafT1=new Array(4);this._wnafT2=new Array(4);this._wnafT3=new Array(4);this._wnafT4=new Array(4);// Generalized Greg Maxwell's trick var adjustCount=this.n&&this.p.div(this.n);if(!adjustCount||adjustCount.cmpn(100)>0){this.redN=null;}else{this._maxwellTrick=true;this.redN=this.n.toRed(this.red);}}
(function (Buffer){ var generatePrime=require("./lib/generatePrime"),primes=require("./lib/primes.json"),DH=require("./lib/dh");function getDiffieHellman(e){var r=new Buffer(primes[e].prime,"hex"),f=new Buffer(primes[e].gen,"hex");return new DH(r,f)}var ENCODINGS={binary:!0,hex:!0,base64:!0};function createDiffieHellman(e,r,f,i){return Buffer.isBuffer(r)||void 0===ENCODINGS[r]?createDiffieHellman(e,"binary",r,f):(r=r||"binary",i=i||"binary",f=f||new Buffer([2]),Buffer.isBuffer(f)||(f=new Buffer(f,i)),"number"==typeof e?new DH(generatePrime(e,f),f,!0):(Buffer.isBuffer(e)||(e=new Buffer(e,r)),new DH(e,f,!0)))}exports.DiffieHellmanGroup=exports.createDiffieHellmanGroup=exports.getDiffieHellman=getDiffieHellman,exports.createDiffieHellman=exports.DiffieHellman=createDiffieHellman; }).call(this,require("buffer").Buffer)
function isCarmichael(n){ let c = 0; for(let i = 1; i <= n; i++){ if(n % i == 0){ if(prime(i) && ((n-1) % (i-1) == 0)){ c++; if(c >= 3 && i % 2 != 0 && squarefree(i)){ return true; } } } } return false; }
/** * Creates a snapshot (copy) of the associated {@link CypherPokerContract#game} instance's * current data in the format of a contract data object for use as a condition in subsequent * contract actions. */ gameSnapshot() { var snapshot = new Object(); snapshot.players = this.getPlayers(false, false); snapshot.table = this.table; try { snapshot.prime = this.getPlayer(this.game.ownPID).keychain[0].prime; } catch (err) { snapshot.prime = null; } snapshot.cardDecks = this.game.getCardDecks(); return (snapshot); }
it('responds to KEY-EXT:PARAMS and creates a shared secret', function(done) { ipc.connectToNet('auth_socket', '127.0.0.1', 7777, function() { var dhObj = cs.diffieHellman(); ipc.of.auth_socket.emit('KEY-EXT:PARAMS', { prime: dhObj.prime, key: dhObj.publicKey }); ipc.of.auth_socket.on('KEY-EXT:PUBLIC', function(data) { ipc.of.auth_socket.klyng_secret = dhObj.computeSecret(data.key); var welcomeMsg = cs.verify(data.cipherWelcome, ipc.of.auth_socket.klyng_secret); expect(welcomeMsg).to.equal("Hello from Beacon's TCP Server!"); done(); }) }); });
function BaseCurve(type, conf) { this.type = type; this.p = new bn(conf.p, 16); // Use Montgomery, when there is no fast reduction for the prime this.red = conf.prime ? bn.red(conf.prime) : bn.mont(this.p); // Useful for many curves this.zero = new bn(0).toRed(this.red); this.one = new bn(1).toRed(this.red); this.two = new bn(2).toRed(this.red); // Curve configuration, optional this.n = conf.n && new bn(conf.n, 16); this.g = conf.g && this.pointFromJSON(conf.g, conf.gRed); // Temporary arrays this._wnafT1 = new Array(4); this._wnafT2 = new Array(4); this._wnafT3 = new Array(4); this._wnafT4 = new Array(4); }
}, { maxBatchSize: 1 }); identityLoader.prime(1, 1);