-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eddsa-poseidon): adds Blake2s hashing for eddsa and conditional …
…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
1 parent
9178996
commit 25f30b1
Showing
15 changed files
with
645 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Oops, something went wrong.