Development Artifact Cleanup: ✅ BROTHER_NODE REORGANIZATION: Moved development test node to appropriate location - dev/test-nodes/brother_node/: Moved from root directory for better organization - Contains development configuration, test logs, and test chain data - No impact on production systems - purely development/testing artifact ✅ DEVELOPMENT ARTIFACTS IDENTIFIED: - Chain ID: aitbc-brother-chain (test/development chain) - Ports: 8010 (P2P) and 8011 (RPC) - different from production - Environment: .env file with test configuration - Logs: rpc.log and node.log from development testing session (March 15, 2026) ✅ ROOT DIRECTORY CLEANUP: Removed development clutter from production directory - brother_node/ moved to dev/test-nodes/brother_node/ - Root directory now contains only production-ready components - Development artifacts properly organized in dev/ subdirectory DIRECTORY STRUCTURE IMPROVEMENT: 📁 dev/test-nodes/: Development and testing node configurations 🏗️ Root Directory: Clean production structure with only essential components 🧪 Development Isolation: Test environments separated from production BENEFITS: ✅ Clean Production Directory: No development artifacts in root ✅ Better Organization: Development nodes grouped in dev/ subdirectory ✅ Clear Separation: Production vs development environments clearly distinguished ✅ Maintainability: Easier to identify and manage development components RESULT: Successfully moved brother_node development artifact to dev/test-nodes/ subdirectory, cleaning up the root directory while preserving development testing environment for future use.
108 lines
4.6 KiB
JavaScript
Executable File
108 lines
4.6 KiB
JavaScript
Executable File
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
// Poseidon Hash: https://eprint.iacr.org/2019/458.pdf, https://www.poseidon-hash.info
|
|
import { FpPow, validateField } from './modular.js';
|
|
export function validateOpts(opts) {
|
|
const { Fp, mds, reversePartialPowIdx: rev, roundConstants: rc } = opts;
|
|
const { roundsFull, roundsPartial, sboxPower, t } = opts;
|
|
validateField(Fp);
|
|
for (const i of ['t', 'roundsFull', 'roundsPartial']) {
|
|
if (typeof opts[i] !== 'number' || !Number.isSafeInteger(opts[i]))
|
|
throw new Error(`Poseidon: invalid param ${i}=${opts[i]} (${typeof opts[i]})`);
|
|
}
|
|
// MDS is TxT matrix
|
|
if (!Array.isArray(mds) || mds.length !== t)
|
|
throw new Error('Poseidon: wrong MDS matrix');
|
|
const _mds = mds.map((mdsRow) => {
|
|
if (!Array.isArray(mdsRow) || mdsRow.length !== t)
|
|
throw new Error(`Poseidon MDS matrix row: ${mdsRow}`);
|
|
return mdsRow.map((i) => {
|
|
if (typeof i !== 'bigint')
|
|
throw new Error(`Poseidon MDS matrix value=${i}`);
|
|
return Fp.create(i);
|
|
});
|
|
});
|
|
if (rev !== undefined && typeof rev !== 'boolean')
|
|
throw new Error(`Poseidon: invalid param reversePartialPowIdx=${rev}`);
|
|
if (roundsFull % 2 !== 0)
|
|
throw new Error(`Poseidon roundsFull is not even: ${roundsFull}`);
|
|
const rounds = roundsFull + roundsPartial;
|
|
if (!Array.isArray(rc) || rc.length !== rounds)
|
|
throw new Error('Poseidon: wrong round constants');
|
|
const roundConstants = rc.map((rc) => {
|
|
if (!Array.isArray(rc) || rc.length !== t)
|
|
throw new Error(`Poseidon wrong round constants: ${rc}`);
|
|
return rc.map((i) => {
|
|
if (typeof i !== 'bigint' || !Fp.isValid(i))
|
|
throw new Error(`Poseidon wrong round constant=${i}`);
|
|
return Fp.create(i);
|
|
});
|
|
});
|
|
if (!sboxPower || ![3, 5, 7].includes(sboxPower))
|
|
throw new Error(`Poseidon wrong sboxPower=${sboxPower}`);
|
|
const _sboxPower = BigInt(sboxPower);
|
|
let sboxFn = (n) => FpPow(Fp, n, _sboxPower);
|
|
// Unwrapped sbox power for common cases (195->142μs)
|
|
if (sboxPower === 3)
|
|
sboxFn = (n) => Fp.mul(Fp.sqrN(n), n);
|
|
else if (sboxPower === 5)
|
|
sboxFn = (n) => Fp.mul(Fp.sqrN(Fp.sqrN(n)), n);
|
|
return Object.freeze({ ...opts, rounds, sboxFn, roundConstants, mds: _mds });
|
|
}
|
|
export function splitConstants(rc, t) {
|
|
if (typeof t !== 'number')
|
|
throw new Error('poseidonSplitConstants: wrong t');
|
|
if (!Array.isArray(rc) || rc.length % t)
|
|
throw new Error('poseidonSplitConstants: wrong rc');
|
|
const res = [];
|
|
let tmp = [];
|
|
for (let i = 0; i < rc.length; i++) {
|
|
tmp.push(rc[i]);
|
|
if (tmp.length === t) {
|
|
res.push(tmp);
|
|
tmp = [];
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
export function poseidon(opts) {
|
|
const _opts = validateOpts(opts);
|
|
const { Fp, mds, roundConstants, rounds, roundsPartial, sboxFn, t } = _opts;
|
|
const halfRoundsFull = _opts.roundsFull / 2;
|
|
const partialIdx = _opts.reversePartialPowIdx ? t - 1 : 0;
|
|
const poseidonRound = (values, isFull, idx) => {
|
|
values = values.map((i, j) => Fp.add(i, roundConstants[idx][j]));
|
|
if (isFull)
|
|
values = values.map((i) => sboxFn(i));
|
|
else
|
|
values[partialIdx] = sboxFn(values[partialIdx]);
|
|
// Matrix multiplication
|
|
values = mds.map((i) => i.reduce((acc, i, j) => Fp.add(acc, Fp.mulN(i, values[j])), Fp.ZERO));
|
|
return values;
|
|
};
|
|
const poseidonHash = function poseidonHash(values) {
|
|
if (!Array.isArray(values) || values.length !== t)
|
|
throw new Error(`Poseidon: wrong values (expected array of bigints with length ${t})`);
|
|
values = values.map((i) => {
|
|
if (typeof i !== 'bigint')
|
|
throw new Error(`Poseidon: wrong value=${i} (${typeof i})`);
|
|
return Fp.create(i);
|
|
});
|
|
let round = 0;
|
|
// Apply r_f/2 full rounds.
|
|
for (let i = 0; i < halfRoundsFull; i++)
|
|
values = poseidonRound(values, true, round++);
|
|
// Apply r_p partial rounds.
|
|
for (let i = 0; i < roundsPartial; i++)
|
|
values = poseidonRound(values, false, round++);
|
|
// Apply r_f/2 full rounds.
|
|
for (let i = 0; i < halfRoundsFull; i++)
|
|
values = poseidonRound(values, true, round++);
|
|
if (round !== rounds)
|
|
throw new Error(`Poseidon: wrong number of rounds: last round=${round}, total=${rounds}`);
|
|
return values;
|
|
};
|
|
// For verification in tests
|
|
poseidonHash.roundConstants = roundConstants;
|
|
return poseidonHash;
|
|
}
|
|
//# sourceMappingURL=poseidon.js.map
|