Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 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 A = new BN(
'5472060717959818805561601436314318772174077789324455915672259473661306552146',
10
)
const G = [new BN(1, 10), new BN(2, 10)]
// Convinience Numbers
const bnOne = new BN('1', 10)
const bnTwo = new BN('2', 10)
const bnThree = new BN('3', 10)
// AltBn128 Object
const bn128 = {}
// ECC Curve
bn128.curve = new EC.curve.short({
a: '0',
b: '3',
p: P,
n: N,
gRed: false,
g: G
})
/**
* BN.js reduction context for bn128 curve group's prime modulus.
*/
bn128.groupReduction = BN.red(bn128.curve.n)
/**
* Gets a random Scalar.
*/
const BN = require('bn.js')
const EC = require('elliptic')
const crypto = require('crypto')
const FIELD_MODULUS = new BN("30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47", 16);
const GROUP_MODULUS = new BN("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001", 16);
const B_MAX = 4294967295;
const bn128 = {};
// The elliptic.js curve object
bn128.curve = new EC.curve.short({
a: '0',
b: '3',
p: FIELD_MODULUS,
n: GROUP_MODULUS,
gRed: false,
g: ['77da99d806abd13c9f15ece5398525119d11e11e9836b2ee7d23f6159ad87d4', '1485efa927f2ad41bff567eec88f32fb0a0f706588b4e41a8d587d008b7f875'],
});
bn128.zero = bn128.curve.g.mul(0);
bn128.p = BN.red(bn128.curve.p);
bn128.q = BN.red(bn128.curve.n);
bn128.randomScalar = () => {
return new BN(crypto.randomBytes(32), 16).toRed(bn128.q);
};
public static get curve() {
return new EC.curve.short({
a: '0',
b: '3',
p: P,
n: N,
gRed: false,
g: G
})
}
/** modulus of bn128's elliptic curve group (n)
* @type {BN}
* @default
* 21888242871839275222246405745257275088548364400416034343698204186575808495617
*/
bn128.groupModulus = new BN('21888242871839275222246405745257275088548364400416034343698204186575808495617', 10);
bn128.compressionMask = new BN('8000000000000000000000000000000000000000000000000000000000000000', 16);
bn128.groupReduction = BN.red(bn128.groupModulus);
bn128.zeroBnRed = new BN(0).toRed(bn128.groupReduction);
/**
* The elliptic.js curve object
*/
bn128.curve = new EC.curve.short({
a: '0',
b: '3',
p: bn128.fieldModulus.toString(16),
n: bn128.groupModulus.toString(16),
gRed: false,
g: ['1', '2'],
});
const hXHex = '0x10f7463e3bdb09c66bcc67cbd978bb8a2fd8883782d177aefc6d155391b1d1b8';
const hYHex = '0x12c4f960e11ba5bf0184d3433a98127e90a6fdb2d1f12cdb369a5d3870866627';
/**
* X-Coordinate of AZTEC's second generator point 'h'. Created by taking the keccak256 hash of the ascii string
* 'just read the instructions', right-padded to 32 bytes. i.e:
* 0x6A75737420726561642074686520696E737472756374696F6E73000000000000. H_X is the result of this hash, modulo
* the elliptic curve group modulus n.
function SuperEC() {
this.curve = new elliptic.curve.short(
{
p: new BN(617665577873),
a: new BN(22541923),
b: new BN(27623856),
g: [
new BN(228638339943),
new BN(433622854135)
],
n: new BN(617666383997)
});
this.k = new BN(361792168050);
}
module.exports = SuperEC;
const { constants } = require('@aztec/dev-utils');
const BN = require('bn.js');
const crypto = require('crypto');
const EC = require('elliptic');
const decodePoint = require('./decodePoint');
const { FIELD_MODULUS, GROUP_MODULUS, H_X, H_Y, K_MAX } = constants;
const compressionMask = new BN('8000000000000000000000000000000000000000000000000000000000000000', 16);
const bn128 = {};
/**
* The elliptic.js curve object
*/
bn128.curve = new EC.curve.short({
a: '0',
b: '3',
p: FIELD_MODULUS.toString(16),
n: GROUP_MODULUS.toString(16),
gRed: false,
g: ['1', '2'],
});
/**
* BN.js reduction context for bn128 curve group's prime modulus
*/
bn128.groupReduction = BN.red(bn128.curve.n);
/**
* Get a random BN in the bn128 curve group's reduction context
* @method randomGroupScalar