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

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

Merged
Show file tree
Hide file tree
Changes from 12 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
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 @@ -51,6 +61,7 @@
"dependencies": {
"@zk-kit/baby-jubjub": "1.0.3",
"@zk-kit/utils": "1.2.1",
"blakejs": "^1.2.1",
"buffer": "6.0.3"
}
}
42 changes: 42 additions & 0 deletions packages/eddsa-poseidon/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,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 {
hannahredler marked this conversation as resolved.
Show resolved Hide resolved
update(data: Buffer): HashFunction
digest(): Buffer
hannahredler marked this conversation as resolved.
Show resolved Hide resolved
}
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
hannahredler marked this conversation as resolved.
Show resolved Hide resolved
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
Loading