Skip to content

Commit

Permalink
feat(eddsa-poseidon): adds Blake2s hashing for eddsa and conditional …
Browse files Browse the repository at this point in the history
…imports (#329)

* feat(eddsa-poseidon): adds Blake2s hashing for eddsa and conditional imports

Currently EdDSA Poseidon hashes using Blake 1, which is now outdated. This commit swaps the default
hashing algorithm for Blake2, whilst also introducing conditional imports so the user can specify
the underlying hashing algorithm if required.

BREAKING CHANGE: n

* feat(eddsa-poseidon): clean up code

re 152

* feat(eddsa-poseidon): fix exports

* feat(eddsa-poseidon): update comments

* refactor(eddsa-poseidon): makes the blake algorithms implement the hash function interface

* feat(eddsa-poseidon): removes implementation of blake2b in favour of the blakejs library

BREAKING CHANGE: n

* refactor(eddsa-poseidon): simplifies the input types for the hash functions

re #152

* refactor(eddsa-poseidon): removes to hex function in favour of zk-kit bufferToHexadecimal

re #152

* fix(eddsa-poseidon): change the default import back to blake1 and fix the export files

re 152

* docs(eddsa-poseidon): update comments

re 152

* refactor(eddsa-poseidon): unifies both algorithms to use default exports

re 152

* refactor(eddsa-poseidon): use imported buffer type instead of the default

re #152

* fix(eddsa-poseidon): throw an error if unsupported algorithm is chosen"

BREAKING CHANGE: n

re #51

* refactor(eddsa-poseidon): rename test file

re #152

---------

Co-authored-by: Cedoor <[email protected]>
  • Loading branch information
hannahredler and cedoor authored Oct 22, 2024
1 parent 9178996 commit 25f30b1
Show file tree
Hide file tree
Showing 15 changed files with 645 additions and 313 deletions.
11 changes: 11 additions & 0 deletions packages/eddsa-poseidon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
"types": "./dist/index.d.ts",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./blake-1": {
"types": "./dist/index.d.ts",
"require": "./dist/lib.commonjs/eddsa-poseidon-blake-1.cjs",
"default": "./dist/esm/eddsa-poseidon-blake-1.js"
},
"./blake-2b": {
"types": "./dist/index.d.ts",
"require": "./dist/lib.commonjs/eddsa-poseidon-blake-2b.cjs",
"default": "./dist/esm/eddsa-poseidon-blake-2b.js"
}
},
"files": [
Expand Down Expand Up @@ -50,6 +60,7 @@
"dependencies": {
"@zk-kit/baby-jubjub": "1.0.3",
"@zk-kit/utils": "1.2.1",
"blakejs": "^1.2.1",
"buffer": "6.0.3",
"poseidon-lite": "0.3.0"
}
Expand Down
42 changes: 42 additions & 0 deletions packages/eddsa-poseidon/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,47 @@ export default [
input: "src/index.ts",
output: [{ file: "dist/index.d.ts", format: "es" }],
plugins: [dts()]
},
{
input: "src/eddsa-poseidon-blake-1.ts",
output: [
{
dir: "./dist/lib.commonjs",
format: "cjs",
banner,
entryFileNames: "[name].cjs"
},
{ dir: "./dist/lib.esm", format: "es", banner }
],
external: [
...Object.keys(pkg.dependencies),
"@zk-kit/utils/conversions",
"@zk-kit/utils/f1-field",
"@zk-kit/utils/scalar",
"@zk-kit/utils/error-handlers",
"@zk-kit/utils/type-checks"
],
plugins: [typescript({ tsconfig: "./build.tsconfig.json", declaration: false, declarationDir: undefined })]
},
{
input: "src/eddsa-poseidon-blake-2b.ts",
output: [
{
dir: "./dist/lib.commonjs",
format: "cjs",
banner,
entryFileNames: "[name].cjs"
},
{ dir: "./dist/lib.esm", format: "es", banner }
],
external: [
...Object.keys(pkg.dependencies),
"@zk-kit/utils/conversions",
"@zk-kit/utils/f1-field",
"@zk-kit/utils/scalar",
"@zk-kit/utils/error-handlers",
"@zk-kit/utils/type-checks"
],
plugins: [typescript({ tsconfig: "./build.tsconfig.json", declaration: false, declarationDir: undefined })]
}
]
6 changes: 6 additions & 0 deletions packages/eddsa-poseidon/src/HashFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Buffer } from "buffer"

export interface HashFunction {
update(data: Buffer): HashFunction
digest(): Buffer
}
3 changes: 2 additions & 1 deletion packages/eddsa-poseidon/src/blake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import { Buffer } from "buffer"
import { HashFunction } from "./HashFunction"

const zo = Buffer.from([0x01])
const oo = Buffer.from([0x81])
Expand Down Expand Up @@ -156,7 +157,7 @@ function lengthCarry(arr: number[]) {
* hashing, allowing data to be added in chunks.
*/
/* eslint-disable import/prefer-default-export */
export class Blake512 {
export default class Blake512 implements HashFunction {
private _h: number[]
private _s: number[]
private _block: Buffer
Expand Down
51 changes: 51 additions & 0 deletions packages/eddsa-poseidon/src/blake2b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { blake2bInit, blake2bUpdate, blake2bFinal, Blake2bCTX } from "blakejs"
import { HashFunction } from "./HashFunction"

/**
* @module Blake2b
* Implements the Blake2b cryptographic hash function.
* Blake2b is a second iteration of the blake algorithm
*
* This code is a wrapper around the "blakeJS" JavaScript library.
* It supports hashing with optional keys, or output length for enhanced security in certain contexts.
*/

export default class Blake2b implements HashFunction {
key: Uint8Array | null = null
outlen: number = 64
context: Blake2bCTX
/**
* Constructor of the Blake2b engine
* @param outlen The fixed output length of the generated hash
* @param key Optional key parameter if keyed hashes are required
* @returns This instance, to allow method chaining.
*/
constructor(outlen: number = 64, key?: Uint8Array) {
if (key) this.key = key
if (outlen <= 0 || outlen > 64) throw new Error("Illegal output length, expected 0 < length <= 64")
else this.outlen = outlen

this.context = blake2bInit(this.outlen, key)
}

/**
* Updates the hash with new data. This method can be called multiple
* times to incrementally add data to the hash computation.
* @param input The data to add to the hash.
* @returns The instance, to allow method chaining.
*/
update(input: Buffer) {
blake2bUpdate(this.context, input)
return this
}

/**
* Completes the hash computation and returns the final hash value.
* This method applies the necessary padding, performs the final compression,
* and returns the output.
* @returns The Blake2b hash of the input data.
*/
digest() {
return Buffer.from(blake2bFinal(this.context))
}
}
15 changes: 15 additions & 0 deletions packages/eddsa-poseidon/src/eddsa-poseidon-blake-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { EdDSAPoseidonFactory, SupportedHashingAlgorithms } from "./eddsa-poseidon-factory"

export const {
EdDSAPoseidon,
derivePublicKey,
deriveSecretScalar,
packPublicKey,
packSignature,
signMessage,
unpackPublicKey,
unpackSignature,
verifySignature
} = EdDSAPoseidonFactory(SupportedHashingAlgorithms.BLAKE1)

export * from "./types"
15 changes: 15 additions & 0 deletions packages/eddsa-poseidon/src/eddsa-poseidon-blake-2b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { EdDSAPoseidonFactory, SupportedHashingAlgorithms } from "./eddsa-poseidon-factory"

export const {
EdDSAPoseidon,
derivePublicKey,
deriveSecretScalar,
packPublicKey,
packSignature,
signMessage,
unpackPublicKey,
unpackSignature,
verifySignature
} = EdDSAPoseidonFactory(SupportedHashingAlgorithms.BLAKE2b)

export * from "./types"
Loading

0 comments on commit 25f30b1

Please sign in to comment.