Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from blake-hash and blake2b to audited noble-hashes #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
"mocha": "^9.1.3"
},
"dependencies": {
"blake-hash": "^2.0.0",
"blake2b": "^2.1.3",
"@noble/hashes": "^1.7.1",
"ethers": "^5.5.1",
"ffjavascript": "^0.3.0"
}
Expand Down
24 changes: 12 additions & 12 deletions src/eddsa.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { blake512 } from "@noble/hashes/blake1";
import { Scalar } from "ffjavascript";
import buildBabyJub from "./babyjub.js";
import buildPedersenHash from "./pedersen_hash.js";
import buildMimc7 from "./mimc7.js";
import { buildPoseidon } from "./poseidon_wasm.js";
import buildMimcSponge from "./mimcsponge.js";
import createBlakeHash from "blake-hash";
import buildPedersenHash from "./pedersen_hash.js";
import { buildPoseidon } from "./poseidon_wasm.js";

export default async function buildEddsa() {
const babyJub = await buildBabyJub("bn128");
Expand Down Expand Up @@ -35,22 +35,22 @@ class Eddsa {

prv2pub(prv) {
const F = this.babyJub.F;
const sBuff = this.pruneBuffer(createBlakeHash("blake512").update(Buffer.from(prv)).digest());
const sBuff = this.pruneBuffer(blake512(Buffer.from(prv)));
let s = Scalar.fromRprLE(sBuff, 0, 32);
const A = this.babyJub.mulPointEscalar(this.babyJub.Base8, Scalar.shr(s,3));
return A;
}

signPedersen(prv, msg) {
const F = this.babyJub.F;
const sBuff = this.pruneBuffer(createBlakeHash("blake512").update(Buffer.from(prv)).digest());
const sBuff = this.pruneBuffer(blake512(Buffer.from(prv)));
const s = Scalar.fromRprLE(sBuff, 0, 32);
const A = this.babyJub.mulPointEscalar(this.babyJub.Base8, Scalar.shr(s, 3));

const composeBuff = new Uint8Array(32 + msg.length);
composeBuff.set(sBuff.slice(32), 0);
composeBuff.set(msg, 32);
const rBuff = createBlakeHash("blake512").update(Buffer.from(composeBuff)).digest();
const rBuff = blake512(Buffer.from(composeBuff));
let r = Scalar.mod(Scalar.fromRprLE(rBuff, 0, 64), this.babyJub.subOrder);
const R8 = this.babyJub.mulPointEscalar(this.babyJub.Base8, r);
const R8p = this.babyJub.packPoint(R8);
Expand Down Expand Up @@ -79,15 +79,15 @@ class Eddsa {

signMiMC(prv, msg) {
const F = this.babyJub.F;
const sBuff = this.pruneBuffer(createBlakeHash("blake512").update(Buffer.from(prv)).digest());
const sBuff = this.pruneBuffer(blake512(Buffer.from(prv)));
const s = Scalar.fromRprLE(sBuff, 0, 32);
const A = this.babyJub.mulPointEscalar(this.babyJub.Base8, Scalar.shr(s, 3));


const composeBuff = new Uint8Array(32 + msg.length);
composeBuff.set(sBuff.slice(32), 0);
F.toRprLE(composeBuff, 32, msg);
const rBuff = createBlakeHash("blake512").update(Buffer.from(composeBuff)).digest();
const rBuff = blake512(Buffer.from(composeBuff));
let r = Scalar.mod(Scalar.fromRprLE(rBuff, 0, 64), this.babyJub.subOrder);
const R8 = this.babyJub.mulPointEscalar(this.babyJub.Base8, r);

Expand All @@ -108,14 +108,14 @@ class Eddsa {

signMiMCSponge(prv, msg) {
const F = this.babyJub.F;
const sBuff = this.pruneBuffer(createBlakeHash("blake512").update(Buffer.from(prv)).digest());
const sBuff = this.pruneBuffer(blake512(Buffer.from(prv)));
const s = Scalar.fromRprLE(sBuff, 0, 32);
const A = this.babyJub.mulPointEscalar(this.babyJub.Base8, Scalar.shr(s, 3));

const composeBuff = new Uint8Array(32 + msg.length);
composeBuff.set(sBuff.slice(32), 0);
F.toRprLE(composeBuff, 32, msg);
const rBuff = createBlakeHash("blake512").update(Buffer.from(composeBuff)).digest();
const rBuff = blake512(Buffer.from(composeBuff));
let r = Scalar.mod(Scalar.fromRprLE(rBuff, 0, 64), this.babyJub.subOrder);
const R8 = this.babyJub.mulPointEscalar(this.babyJub.Base8, r);

Expand All @@ -136,14 +136,14 @@ class Eddsa {

signPoseidon(prv, msg) {
const F = this.babyJub.F;
const sBuff = this.pruneBuffer(createBlakeHash("blake512").update(Buffer.from(prv)).digest());
const sBuff = this.pruneBuffer(blake512(Buffer.from(prv)));
const s = Scalar.fromRprLE(sBuff, 0, 32);
const A = this.babyJub.mulPointEscalar(this.babyJub.Base8, Scalar.shr(s, 3));

const composeBuff = new Uint8Array(32 + msg.length);
composeBuff.set(sBuff.slice(32), 0);
F.toRprLE(composeBuff, 32, msg);
const rBuff = createBlakeHash("blake512").update(Buffer.from(composeBuff)).digest();
const rBuff = blake512(Buffer.from(composeBuff));
let r = Scalar.mod(Scalar.fromRprLE(rBuff, 0, 64), this.babyJub.subOrder);
const R8 = this.babyJub.mulPointEscalar(this.babyJub.Base8, r);

Expand Down
10 changes: 5 additions & 5 deletions src/pedersen_hash.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import buildBabyJub from "./babyjub.js";
import blake2b from "blake2b";
import createBlakeHash from "blake-hash";
import { blake256 } from "@noble/hashes/blake1";
import { blake2b } from "@noble/hashes/blake2b";
import { Scalar } from "ffjavascript";
import buildBabyJub from "./babyjub.js";

const GENPOINT_PREFIX = "PedersenGenerator";
const windowSize = 4;
Expand All @@ -21,9 +21,9 @@ class PedersenHash {

baseHash(type, S) {
if (type == "blake") {
return createBlakeHash("blake256").update(S).digest();
return Buffer.from(blake256(S));
} else if (type == "blake2b") {
return Buffer.from(blake2b(32).update(Buffer.from(S)).digest());
return Buffer.from(blake2b(Buffer.from(S)));
}
}

Expand Down
17 changes: 4 additions & 13 deletions src/testblake.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import createBlakeHash from 'blake-hash';
import blake2b from "blake2b";

const msg = (new TextEncoder()).encode("blake256");
const msgB = Buffer.from(msg)

import { blake256 } from '@noble/hashes/blake1';
import { blake2b } from '@noble/hashes/blake2b';

const toHexString = bytes =>
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');

const h1 = createBlakeHash('blake256').digest();

const h2 = blake2b(64).digest();


console.log(toHexString(h1));
console.log(toHexString(h2));
console.log(toHexString(blake256('')));
console.log(toHexString(blake2b('')));