Skip to content

Commit

Permalink
Renaming. Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolezhniuk committed Nov 18, 2024
1 parent d93f119 commit ed6abd0
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 289 deletions.
3 changes: 0 additions & 3 deletions .eslintrc

This file was deleted.

4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/node_modules
coverage
/coverage
dist
.DS_Store
.idea
.vscode
.vscode
3 changes: 3 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import config from '@iden3/eslint-config';

export default config;
522 changes: 289 additions & 233 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@
"@iden3/eslint-config": "https://github.com/iden3/eslint-config#feat/update-versions",
"@types/jest": "^29.5.14",
"@typescript-eslint/eslint-plugin": "8.14.0",
"@vitest/coverage-v8": "^2.1.4",
"@vitest/coverage-v8": "^2.1.5",
"esbuild": "^0.24.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.2.1",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"ts-node": "^10.9.2",
"typescript": "5.6.3",
"vitest": "^2.1.4"
"vitest": "^2.1.5"
}
}
10 changes: 5 additions & 5 deletions src/babyjub/babyjub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ export class BabyJub {
const gamma = F.mul(a[1], b[0]);
const delta = F.mul(F.sub(a[1], F.mul(this.A, a[0])), F.add(b[0], b[1]));
const tau = F.mul(beta, gamma);
const dtau = F.mul(this.D, tau);
const dTau = F.mul(this.D, tau);

res[0] = F.div(F.add(beta, gamma), F.add(F.one, dtau));
res[0] = F.div(F.add(beta, gamma), F.add(F.one, dTau));

res[1] = F.div(F.add(delta, F.sub(F.mul(this.A, beta), gamma)), F.sub(F.one, dtau));
res[1] = F.div(F.add(delta, F.sub(F.mul(this.A, beta), gamma)), F.sub(F.one, dTau));

return res as [bigint, bigint];
}

mulPointEscalar(base: bigint[], e: bigint): [bigint, bigint] {
mulPointEScalar(base: bigint[], e: bigint): [bigint, bigint] {
const F = this.F;
let res: [bigint, bigint] = [F.e('0'), F.e('1')];
let rem = e;
Expand All @@ -78,7 +78,7 @@ export class BabyJub {
inSubgroup(P: bigint[]): boolean {
const F = this.F;
if (!this.inCurve(P)) return false;
const res = this.mulPointEscalar(P, this.subOrder);
const res = this.mulPointEScalar(P, this.subOrder);
return F.isZero(res[0]) && F.eq(res[1], F.one);
}

Expand Down
16 changes: 8 additions & 8 deletions src/babyjub/eddsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export class Eddsa {
static prv2pub(prv: Uint8Array): [bigint, bigint] {
const sBuff = this.pruneBuffer(new Blake512().update(prv).digest());
const s = Scalar.fromRprLE(sBuff, 0, 32);
const A = babyJub.mulPointEscalar(babyJub.Base8, Scalar.shr(s, 3n));
const A = babyJub.mulPointEScalar(babyJub.Base8, Scalar.shr(s, 3n));
return A;
}

static signPoseidon(prv: Uint8Array, msg: bigint) {
const h1 = new Blake512().update(prv).digest();
const sBuff = Eddsa.pruneBuffer(h1.slice(0, 32));
const s = utils.leBuff2int(sBuff);
const A = babyJub.mulPointEscalar(babyJub.Base8, Scalar.shr(s, 3n));
const A = babyJub.mulPointEScalar(babyJub.Base8, Scalar.shr(s, 3n));

const msgBuff = utils.leInt2Buff(msg, 32);

Expand All @@ -37,7 +37,7 @@ export class Eddsa {
let r = utils.leBuff2int(rBuff);
const Fr = new F1Field(babyJub.subOrder);
r = Fr.e(r) as bigint;
const R8 = babyJub.mulPointEscalar(babyJub.Base8, r);
const R8 = babyJub.mulPointEScalar(babyJub.Base8, r);
const hm = poseidon.hash([R8[0], R8[1], A[0], A[1], msg]);
const S = Fr.add(r, Fr.mul(hm, s));
return {
Expand All @@ -60,12 +60,12 @@ export class Eddsa {
const hm = poseidon.hash([sig.R8[0], sig.R8[1], A[0], A[1], msg]);
const hms = hm;

const Pleft = babyJub.mulPointEscalar(babyJub.Base8, sig.S);
let Pright = babyJub.mulPointEscalar(A, Scalar.mul(hms, 8n));
Pright = babyJub.addPoint(sig.R8, Pright);
const pLeft = babyJub.mulPointEScalar(babyJub.Base8, sig.S);
let pRight = babyJub.mulPointEScalar(A, Scalar.mul(hms, 8n));
pRight = babyJub.addPoint(sig.R8, pRight);

if (!babyJub.F.eq(Pleft[0], Pright[0])) return false;
if (!babyJub.F.eq(Pleft[1], Pright[1])) return false;
if (!babyJub.F.eq(pLeft[0], pRight[0])) return false;
if (!babyJub.F.eq(pLeft[1], pRight[1])) return false;
return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/base58.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const base58ToBytes = (
if (c < 0)
//see if the base58 digit lookup is invalid (-1)
throw new Error(`Can't convert base58 string ${str} to bytes`);
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
c || b.length ^ i ? i : b.push(0); //prepend the result array with a zero if the base58 digit is zero and non-zero characters haven't been seen yet (to ensure correct decode length)
while (j in d || c) {
//start looping through the bytes until there are no more bytes and no carry amount
Expand Down
14 changes: 7 additions & 7 deletions src/blake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class Blake512 {
private _length: number[];
private _zo: Uint8Array;
private _oo: Uint8Array;
private _nullt: boolean;
private _nullT: boolean;
constructor() {
this._h = [
0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a,
Expand All @@ -109,7 +109,7 @@ export class Blake512 {
this._blockOffset = 0;
this._length = [0, 0, 0, 0];

this._nullt = false;
this._nullT = false;

this._zo = zo;
this._oo = oo;
Expand All @@ -136,7 +136,7 @@ export class Blake512 {
for (i = 16; i < 24; ++i) v[i] = (this._s[i - 16] ^ u512[i - 16]) >>> 0;
for (i = 24; i < 32; ++i) v[i] = u512[i - 16];

if (!this._nullt) {
if (!this._nullT) {
v[24] = (v[24] ^ this._length[1]) >>> 0;
v[25] = (v[25] ^ this._length[0]) >>> 0;
v[26] = (v[26] ^ this._length[1]) >>> 0;
Expand Down Expand Up @@ -176,24 +176,24 @@ export class Blake512 {
len[0] += this._blockOffset * 8;
this._lengthCarry(len);

const msglen = new Uint8Array(16);
const dataView = new DataView(msglen.buffer);
const msgLen = new Uint8Array(16);
const dataView = new DataView(msgLen.buffer);
for (let i = 0; i < 4; ++i) dataView.setUint32(i * 4, len[3 - i]);

if (this._blockOffset === 111) {
this._length[0] -= 8;
this.update(this._oo);
} else {
if (this._blockOffset < 111) {
if (this._blockOffset === 0) this._nullt = true;
if (this._blockOffset === 0) this._nullT = true;
this._length[0] -= (111 - this._blockOffset) * 8;
this.update(Blake512.padding.slice(0, 111 - this._blockOffset));
} else {
this._length[0] -= (128 - this._blockOffset) * 8;
this.update(Blake512.padding.slice(0, 128 - this._blockOffset));
this._length[0] -= 111 * 8;
this.update(Blake512.padding.slice(1, 1 + 111));
this._nullt = true;
this._nullT = true;
}

this.update(this._zo);
Expand Down
42 changes: 22 additions & 20 deletions src/ff/f1field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class F1Field {
zero: bigint;
p: bigint;
m: bigint;
negone: bigint;
negOne: bigint;
two: bigint;
half: bigint;
bitLength: number;
Expand All @@ -30,7 +30,7 @@ export class F1Field {
this.zero = BigInt(0);
this.p = BigInt(p);
this.m = Scalar.one;
this.negone = this.p - this.one;
this.negOne = this.p - this.one;
this.two = BigInt(2);
this.half = this.p >> this.one;
this.bitLength = Scalar.bitLength(this.p);
Expand All @@ -42,16 +42,16 @@ export class F1Field {
this.R = this.e(this.one << BigInt(this.n64 * 64));
this.Ri = this.inv(this.R);

const e = this.negone >> this.one;
const e = this.negOne >> this.one;
this.nqr = this.two;
let r = this.pow(this.nqr, e);
while (!this.eq(r, this.negone)) {
while (!this.eq(r, this.negOne)) {
this.nqr = this.nqr + this.one;
r = this.pow(this.nqr, e);
}

this.s = 0;
this.t = this.negone;
this.t = this.negOne;

while ((this.t & this.one) == this.zero) {
this.s = this.s + 1;
Expand All @@ -60,6 +60,7 @@ export class F1Field {

this.nqr_to_t = this.pow(this.nqr, this.t);

// eslint-disable-next-line @cspell/spellchecker
tonelliShanks(this);

this.shift = this.square(this.nqr);
Expand All @@ -74,9 +75,9 @@ export class F1Field {
res = BigInt('0x' + a);
}
if (res < 0) {
let nres = -res;
if (nres >= this.p) nres = nres % this.p;
return this.p - nres;
let nRes = -res;
if (nRes >= this.p) nRes = nRes % this.p;
return this.p - nRes;
} else {
return res >= this.p ? res % this.p : res;
}
Expand Down Expand Up @@ -147,7 +148,7 @@ export class F1Field {
return this.mul(a, this.inv(b));
}

idiv(a: bigint, b: bigint): bigint {
iDiv(a: bigint, b: bigint): bigint {
if (!b) throw new Error('Division by zero');
return a / b;
}
Expand All @@ -158,11 +159,11 @@ export class F1Field {
let t = this.zero;
let r = this.p;
let newt = this.one;
let newr = a % this.p;
while (newr) {
const q = r / newr;
let newR = a % this.p;
while (newR) {
const q = r / newR;
[t, newt] = [newt, t - q * newt];
[r, newr] = [newr, r - q * newr];
[r, newR] = [newR, r - q * newR];
}
if (t < this.zero) t += this.p;
return t;
Expand Down Expand Up @@ -190,12 +191,12 @@ export class F1Field {
return res >= this.p ? res - this.p : res;
}

bxor(a: bigint, b: bigint): bigint {
bXor(a: bigint, b: bigint): bigint {
const res = (a ^ b) & this.mask;
return res >= this.p ? res - this.p : res;
}

bnot(a: bigint): bigint {
bNot(a: bigint): bigint {
const res = a ^ this.mask;
return res >= this.p ? res - this.p : res;
}
Expand Down Expand Up @@ -240,7 +241,7 @@ export class F1Field {
if (n == this.zero) return this.zero;

// Test that have solution
const res = this.pow(n, this.negone >> this.one);
const res = this.pow(n, this.negOne >> this.one);
if (res != this.one) return null;

let m = this.s;
Expand Down Expand Up @@ -327,12 +328,12 @@ export class F1Field {
return this.toRprLE(buff, o, this.mul(this.R, e));
}

// Pases a buffer with Little Endian Representation
// Passes a buffer with Little Endian Representation
fromRprLE(buff: Uint8Array, o: number) {
return Scalar.fromRprLE(buff, o, this.n8);
}

// Pases a buffer with Big Endian Representation
// Passes a buffer with Big Endian Representation
fromRprBE(buff: Uint8Array, o: number) {
return Scalar.fromRprBE(buff, o, this.n8);
}
Expand Down Expand Up @@ -361,6 +362,7 @@ export class F1Field {
sqrt_tm1d2!: bigint;
}

// eslint-disable-next-line @cspell/spellchecker
function tonelliShanks(F: F1Field) {
F.sqrt_q = Scalar.pow(F.p, F.m);

Expand All @@ -386,7 +388,7 @@ function tonelliShanks(F: F1Field) {
if (F.isZero(a)) return F.zero;
let w = F.pow(a, F.sqrt_tm1d2);
const a0 = F.pow(F.mul(F.square(w), a), 2n ** (F.sqrt_s - Scalar.one));
if (F.eq(a0, F.negone)) return null;
if (F.eq(a0, F.negOne)) return null;

let v = F.sqrt_s;
let x = F.mul(a, w);
Expand Down Expand Up @@ -425,7 +427,7 @@ export function mulScalar(F: F1Field, base: bigint, e: bigint): bigint {
} else if (n[n.length - 1] == -1) {
res = F.neg(base);
} else {
throw new Error('invlaud NAF');
throw new Error('invalid NAF');
}

for (let i = n.length - 2; i >= 0; i--) {
Expand Down
2 changes: 1 addition & 1 deletion src/ff/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function getRandomBytes(length: number): Uint8Array {
}
// eslint-disable-next-line no-unused-labels
NODE: {
// eslint-disable-next-line @typescript-eslint/no-var-requires
// eslint-disable-next-line @typescript-eslint/no-require-imports
const crypto = require('crypto');
return crypto.randomBytes(length);
}
Expand Down
6 changes: 3 additions & 3 deletions src/ff/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const bor = (a: bigint, b: bigint): bigint => {
return a | b;
};

export const bxor = (a: bigint, b: bigint): bigint => {
export const bXor = (a: bigint, b: bigint): bigint => {
return a ^ b;
};

Expand Down Expand Up @@ -213,7 +213,7 @@ export const toRprBE = (buff: Uint8Array, o: number, e: bigint, n8: number): voi
for (let i = 0; i < n8 / 4 - l; i++) v.setInt32(0, 0, false);
};

// Pases a buffer with Little Endian Representation
// Passes a buffer with Little Endian Representation
export const fromRprLE = (buff: Uint8Array, o: number, n8?: number): bigint => {
n8 = n8 || buff.byteLength;
o = o || 0;
Expand All @@ -223,7 +223,7 @@ export const fromRprLE = (buff: Uint8Array, o: number, n8?: number): bigint => {
return fromString(a.join(''), 16);
};

// Pases a buffer with Big Endian Representation
// Passes a buffer with Big Endian Representation
export const fromRprBE = (buff: Uint8Array, o: number, n8: number): bigint => {
n8 = n8 || buff.byteLength;
o = o || 0;
Expand Down
6 changes: 3 additions & 3 deletions src/ff/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as Scalar from './scalar';

export function unstringifyBigInts(o: unknown): unknown {
export function unStringifyBigInts(o: unknown): unknown {
if (Array.isArray(o)) {
return o.map(unstringifyBigInts);
return o.map(unStringifyBigInts);
} else if (typeof o == 'object') {
const res: { [k: string]: unknown } = {};
for (const [key, val] of Object.entries(o as unknown as { [k: string]: unknown })) {
res[key] = unstringifyBigInts(val);
res[key] = unStringifyBigInts(val);
}
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion src/poseidon/poseidon-opt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { F1Field, Scalar, utils } from '../ff';
import op from './poseidon-constants-opt.json';

export const OPT = utils.unstringifyBigInts(op) as {
export const OPT = utils.unStringifyBigInts(op) as {
C: bigint[][];
S: bigint[][];
M: bigint[][][];
Expand Down
2 changes: 1 addition & 1 deletion tests/sha256.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('SHA-256 Hashing', () => {

it('should hash different inputs to different values', () => {
const input1 = 'Hello, World!';
const input2 = 'Hello, Goodbay!';
const input2 = 'Hello, Good bay!';
const hash1 = sha256(encoder.encode(input1));
const hash2 = sha256(encoder.encode(input2));

Expand Down

0 comments on commit ed6abd0

Please sign in to comment.