Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
motemotech committed Oct 25, 2024
2 parents 5062530 + 291501f commit 7617cd7
Show file tree
Hide file tree
Showing 18 changed files with 758 additions and 342 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
- Rust: [privacy-scaling-explorations/zk-kit.rust](https://github.com/privacy-scaling-explorations/zk-kit.rust)
- Solidity: [privacy-scaling-explorations/zk-kit.solidity](https://github.com/privacy-scaling-explorations/zk-kit.solidity)

## 📄 Papers

- LeanIMT ([Download PDF](https://github.com/privacy-scaling-explorations/zk-kit/raw/main/papers/leanimt/paper/leanimt-paper.pdf))

## 📦 Packages

<table>
Expand Down
2 changes: 2 additions & 0 deletions packages/eddsa-poseidon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ or [JSDelivr](https://www.jsdelivr.com/):

## 📜 Usage

The public key is generated using [BLAKE](<https://en.wikipedia.org/wiki/BLAKE_(hash_function)>) by default and BLAKE2 if specified in the import as follows: `import { ... } from "@zk-kit/eddsa-poseidon/blake-2b"`.

```typescript
import {
derivePublicKey,
Expand Down
17 changes: 14 additions & 3 deletions packages/eddsa-poseidon/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zk-kit/eddsa-poseidon",
"version": "1.0.3",
"version": "1.1.0",
"description": "A JavaScript EdDSA library for secure signing and verification using Poseidon the Baby Jubjub elliptic curve.",
"type": "module",
"license": "MIT",
Expand All @@ -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 @@ -41,7 +51,6 @@
"@rollup/plugin-typescript": "^11.1.6",
"circomlibjs": "0.0.8",
"ffjavascript": "0.2.38",
"poseidon-lite": "0.2.0",
"rimraf": "^5.0.5",
"rollup": "^4.12.0",
"rollup-plugin-cleanup": "^3.2.1",
Expand All @@ -51,6 +60,8 @@
"dependencies": {
"@zk-kit/baby-jubjub": "1.0.3",
"@zk-kit/utils": "1.2.1",
"buffer": "6.0.3"
"blakejs": "^1.2.1",
"buffer": "6.0.3",
"poseidon-lite": "0.3.0"
}
}
52 changes: 45 additions & 7 deletions packages/eddsa-poseidon/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@ export default [
"@zk-kit/utils/f1-field",
"@zk-kit/utils/scalar",
"@zk-kit/utils/error-handlers",
"@zk-kit/utils/type-checks"
"@zk-kit/utils/type-checks",
"poseidon-lite/poseidon5"
],
plugins: [
typescript({ tsconfig: "./build.tsconfig.json" }),
nodeResolve(),
commonjs(),
cleanup({ comments: "jsdoc" })
]
plugins: [typescript({ tsconfig: "./build.tsconfig.json" }), cleanup({ comments: "jsdoc" })]
},
{
input: "src/index.ts",
Expand Down Expand Up @@ -70,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 7617cd7

Please sign in to comment.