# How to use elliptic - 10 common examples

## To help you get started, we’ve selected a few elliptic examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

FabricLabs / fabric / tests / peer.js View on Github
``````'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);
});``````
AztecProtocol / AZTEC / packages / weierstrudel / huff_tests / main_loop.spec.js View on Github
``````// 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 &lt; 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 () =&gt; {
main = new Runtime('main_loop.huff', pathToTestData, true);
});

it('macro MAIN__WEIERSTRUDEL calculates scalar multiplication of ONE point', async () =&gt; {``````
AztecProtocol / AZTEC / packages / weierstrudel / huff_tests / endomorphism.spec.js View on Github
``````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]);``````
AztecProtocol / AZTEC / packages / weierstrudel / huff_tests / main_loop.spec.js View on Github
``````// 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 &lt; 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(() =&gt; {
main = new Runtime('main_loop.huff', pathToTestData);
});
it('macro GET_P2_LOCATION correctly calculates point location from a wnaf', async () =&gt; {``````
AztecProtocol / AZTEC / packages / weierstrudel / huff_tests / precompute_table.spec.js View on Github
``````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');

// 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,``````
hyperledger / fabric-sdk-node / test / unit / cryptosuite-ecdsa-aes.js View on Github
``````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()');
}
};``````
hyperledger / fabric-sdk-node / test / unit / headless-tests.js View on Github
``````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()');
}
};``````
GridPlus / gridplus-sdk / test / btc-bcy.js View on Github
``````client.addresses(req, (err, addresses) => {
assert(err === null, err);
// // Get the baseline balance for the addresses
assert(err === null, err);
done()
});
});
});``````
GridPlus / gridplus-sdk / test / btc-bcy.js View on Github
``````client.addresses(req, (err, addresses) => {
assert(err === null, err);
// // Get the baseline balance for the addresses
assert(err === null, err);
done()
});
});
});``````
GridPlus / gridplus-sdk / test / btc-bcy.js View on Github
``````client.addresses(req, (err, addresses) => {
assert(err === null, err);
// // Get the baseline balance for the addresses
assert(err === null, err);
done()
});
});
});``````

EC cryptography

MIT