Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict';
const Fabric = require('../');
const assert = require('assert');
const expect = require('chai').expect;
const crypto = require('crypto');
const net = require('net');
const EC = require('elliptic').ec;
const ec = new EC('secp256k1');
const Peer = require('../lib/peer');
const Message = require('../lib/message');
// const key = '/test';
const data = require('../data/message');
const TEST_PORT = 7775;
const GENESIS_DATA = 'Hello, world!';
const BULK_COUNT = 1000;
describe('Peer', function () {
it('should expose a constructor', function () {
assert.equal(Peer instanceof Function, true);
});
// NOTE: potential areas to improve
// 1: main loop is garbage, too many special case tests
// 2: P macro used too often, huge bytecode bload
// 3: not sure we need to use 'mod' in PRECOMPUTE_TABLE__RESCALE_15, just subtract from 4p when we need to negate?
function sliceMemory(memArray) {
const numWords = Math.ceil(memArray.length / 32);
const result = [];
for (let i = 0; i < numWords * 32; i += 32) {
result.push(new BN(memArray.slice(i, i + 32), 16));
}
return result;
}
// eslint-disable-next-line new-cap
const referenceCurve = new EC.curve.short({
a: '0',
b: '3',
p: p.toString(16),
n: n.toString(16),
gRed: false,
g: ['1', '2'],
});
describe('bn128 main loop', function describe() {
this.timeout(10000);
let main;
before(async () => {
main = new Runtime('main_loop.huff', pathToTestData, true);
});
it('macro MAIN__WEIERSTRUDEL calculates scalar multiplication of ONE point', async () => {
const chai = require('chai');
const BN = require('bn.js');
const crypto = require('crypto');
const EC = require('elliptic');
const path = require('path');
const { Runtime } = require('../../huff');
const { n, lambda, p, beta, randomPoint } = require('../js_snippets/bn128_reference');
const { expect } = chai;
const pathToTestData = path.posix.resolve(__dirname, '../huff_modules');
// eslint-disable-next-line new-cap
const referenceCurve = new EC.curve.short({
a: '0',
b: '3',
p: p.toString(16),
n: n.toString(16),
gRed: false,
g: ['1', '2'],
});
describe('endomorphism split', () => {
let endomorphism;
before(() => {
endomorphism = new Runtime('endomorphism.huff', pathToTestData);
});
it('macro ENDOMORPHISM correctly splits scalar k into half-length scalars k1, k2', async () => {
const k = new BN(crypto.randomBytes(32), 16).umod(n);
const { stack } = await endomorphism('ENDOMORPHISM', [k]);
// NOTE: potential areas to improve
// 1: main loop is garbage, too many special case tests
// 2: P macro used too often, huge bytecode bload
// 3: not sure we need to use 'mod' in RESCALE_15, just subtract from 4p when we need to negate?
function sliceMemory(memArray) {
const numWords = Math.ceil(memArray.length / 32);
const result = [];
for (let i = 0; i < numWords * 32; i += 32) {
result.push(new BN(memArray.slice(i, i + 32), 16));
}
return result;
}
// eslint-disable-next-line new-cap
const referenceCurve = new EC.curve.short({
a: '0',
b: '3',
p: p.toString(16),
n: n.toString(16),
gRed: false,
g: ['1', '2'],
});
describe('bn128 main loop', function describe() {
this.timeout(10000);
let main;
before(() => {
main = new Runtime('main_loop.huff', pathToTestData);
});
it('macro GET_P2_LOCATION correctly calculates point location from a wnaf', async () => {
const BN = require('bn.js');
const EC = require('elliptic');
const path = require('path');
const { Runtime } = require('../../huff');
const bn128Reference = require('../js_snippets/bn128_reference');
const { beta, p, n } = bn128Reference;
const { expect } = chai;
const pathToTestData = path.posix.resolve(__dirname, '../huff_modules');
const p2 = p.add(p);
const p3 = p.add(p).add(p);
// eslint-disable-next-line new-cap
const referenceCurve = new EC.curve.short({
a: '0',
b: '3',
p: p.toString(16),
n: n.toString(16),
gRed: false,
g: ['1', '2'],
});
function splitPoint(x, y) {
return {
x: p.sub(x),
xEndo: p
.sub(x)
.mul(beta)
.umod(p),
yNeg: y,
const testSignature = function (msg) {
const sig = cryptoUtils.sign(key, cryptoUtils.hash(msg));
if (sig) {
// test that signatures have low-S
const halfOrder = halfOrdersForCurve[key._key.ecparams.name];
const sigObject = new Signature(sig);
if (sigObject.s.cmp(halfOrder) === 1) {
t.fail('Invalid signature object: S value larger than N/2');
} else {
t.pass('Valid signature object generated from sign()');
}
// using internal calls to verify the signature
const pubKey = cryptoUtils._ecdsa.keyFromPublic(key.getPublicKey()._key.pubKeyHex, 'hex');
// note that the signature is generated on the hash of the message, not the message itself
t.equal(pubKey.verify(cryptoUtils.hash(msg), Buffer.from(sig)), true,
'CryptoSuite_ECDSA_AES function tests: sign() method produced proper signature that was successfully verified');
} else {
t.fail('Invalid signature generated by sign()');
}
};
var testSignature = function (msg) {
var sig = cryptoUtils.sign(key, cryptoUtils.hash(msg));
if (sig) {
// test that signatures have low-S
var halfOrder = halfOrdersForCurve[key._key.ecparams.name];
var sigObject = new Signature(sig);
if (sigObject.s.cmp(halfOrder) == 1) {
t.fail('Invalid signature object: S value larger than N/2');
} else {
t.pass('Valid signature object generated from sign()');
}
// using internal calls to verify the signature
var pubKey = cryptoUtils._ecdsa.keyFromPublic(key.getPublicKey()._key.pubKeyHex, 'hex');
// note that the signature is generated on the hash of the message, not the message itself
t.equal(pubKey.verify(cryptoUtils.hash(msg), new Buffer(sig)), true,
'CryptoSuite_ECDSA_AES function tests: sign() method produced proper signature that was successfully verified');
} else {
t.fail('Invalid signature generated by sign()');
}
};
client.addresses(req, (err, addresses) => {
assert(err === null, err);
assert(addresses.length === 2);
assert(addresses[0].slice(0, 1) === 'B' || addresses[0].slice(0, 1) === 'C', 'Address 1 is not a BCY address');
assert(addresses[1].slice(0, 1) === 'B' || addresses[1].slice(0, 1) === 'C', 'Address 2 is not a BCY address')
deviceAddresses = addresses;
// // Get the baseline balance for the addresses
client.getBalance('BTC', { address: deviceAddresses }, (err) => {
assert(err === null, err);
done()
});
});
});
client.addresses(req, (err, addresses) => {
assert(err === null, err);
assert(addresses.length === 2);
assert(addresses[0].slice(0, 1) === 'B' || addresses[0].slice(0, 1) === 'C', 'Address 1 is not a BCY address');
assert(addresses[1].slice(0, 1) === 'B' || addresses[1].slice(0, 1) === 'C', 'Address 2 is not a BCY address')
deviceAddresses = addresses;
// // Get the baseline balance for the addresses
client.getBalance('BTC', { address: deviceAddresses }, (err) => {
assert(err === null, err);
done()
});
});
});
client.addresses(req, (err, addresses) => {
assert(err === null, err);
assert(addresses.length === 2);
assert(addresses[0].slice(0, 1) === 'B' || addresses[0].slice(0, 1) === 'C', 'Address 1 is not a BCY address');
assert(addresses[1].slice(0, 1) === 'B' || addresses[1].slice(0, 1) === 'C', 'Address 2 is not a BCY address')
deviceAddresses = addresses;
// // Get the baseline balance for the addresses
client.getBalance('BTC', { address: deviceAddresses }, (err) => {
assert(err === null, err);
done()
});
});
});