Skip to content

Commit

Permalink
fix(eddsa-poseidon): set default private key as buffer (#298)
Browse files Browse the repository at this point in the history
* fix(eddsa-poseidon): set default private key as buffer

The private key must be 1 of the following supported types: buffer, uint8array or text. Since it was
a hexadecimal string, it has been changed to buffer.

re #297

* test(eddsa-poseidon): add test to check private key type

re #297

* test(eddsa-poseidon): add test to check if priv key is expected value

re #297
  • Loading branch information
cedoor authored Jun 8, 2024
1 parent 18fadde commit c6f7bf0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
9 changes: 2 additions & 7 deletions packages/eddsa-poseidon/src/eddsa-poseidon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ import {
} from "@zk-kit/baby-jubjub"
import type { BigNumberish } from "@zk-kit/utils"
import { crypto, requireBuffer } from "@zk-kit/utils"
import {
bigNumberishToBigInt,
leBigIntToBuffer,
leBufferToBigInt,
bufferToHexadecimal
} from "@zk-kit/utils/conversions"
import { bigNumberishToBigInt, leBigIntToBuffer, leBufferToBigInt } from "@zk-kit/utils/conversions"
import { requireBigNumberish } from "@zk-kit/utils/error-handlers"
import F1Field from "@zk-kit/utils/f1-field"
import * as scalar from "@zk-kit/utils/scalar"
Expand Down Expand Up @@ -274,7 +269,7 @@ export class EdDSAPoseidon {
*
* @param privateKey The private key used for signing and public key derivation.
*/
constructor(privateKey: Buffer | Uint8Array | string = bufferToHexadecimal(crypto.getRandomValues(32))) {
constructor(privateKey: Buffer | Uint8Array | string = crypto.getRandomValues(32)) {
this.privateKey = privateKey
this.secretScalar = deriveSecretScalar(privateKey)
this.publicKey = derivePublicKey(privateKey)
Expand Down
6 changes: 4 additions & 2 deletions packages/eddsa-poseidon/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ describe("EdDSAPoseidon", () => {

const signature = eddsa.signMessage(message)

expect(typeof eddsa.privateKey).toBe("string")
expect(eddsa.privateKey).toBe(privateKey)
expect(eddsa.secretScalar).toBe(deriveSecretScalar(privateKey))
expect(eddsa.packedPublicKey).toBe(packPublicKey(eddsa.publicKey))
expect(eddsa.verifySignature(message, signature)).toBeTruthy()
Expand All @@ -385,8 +387,8 @@ describe("EdDSAPoseidon", () => {

const signature = eddsa.signMessage(message)

expect(typeof eddsa.privateKey).toBe("string")
expect(eddsa.privateKey).toHaveLength(64)
expect(typeof eddsa.privateKey).toBe("object")
expect(eddsa.privateKey).toHaveLength(32)
expect(eddsa.secretScalar).toBe(deriveSecretScalar(eddsa.privateKey))
expect(eddsa.packedPublicKey).toBe(packPublicKey(eddsa.publicKey))
expect(eddsa.verifySignature(message, signature)).toBeTruthy()
Expand Down

0 comments on commit c6f7bf0

Please sign in to comment.