Skip to content

Commit

Permalink
refactor(eddsa-poseidon)!: restrict message types
Browse files Browse the repository at this point in the history
BREAKING CHANGE: message type

re privacy-scaling-explorations#230
  • Loading branch information
f3r10 committed Sep 7, 2024
1 parent 62cb5d5 commit ff0d241
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
10 changes: 5 additions & 5 deletions packages/eddsa-poseidon/src/eddsa-poseidon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
subOrder,
unpackPoint
} from "@zk-kit/baby-jubjub"
import type { BigNumberish } from "@zk-kit/utils"
import type { BigNumber, BigNumberish } from "@zk-kit/utils"
import { crypto, requireBuffer } from "@zk-kit/utils"
import { bigNumberishToBigInt, leBigIntToBuffer, leBufferToBigInt } from "@zk-kit/utils/conversions"
import { requireBigNumberish } from "@zk-kit/utils/error-handlers"
Expand Down Expand Up @@ -87,7 +87,7 @@ export function derivePublicKey(privateKey: Buffer | Uint8Array | string): Point
* @param message The message to be signed.
* @returns The signature object, containing properties relevant to EdDSA signatures, such as 'R8' and 'S' values.
*/
export function signMessage(privateKey: Buffer | Uint8Array | string, message: BigNumberish): Signature<bigint> {
export function signMessage(privateKey: Buffer | Uint8Array | string, message: BigNumber): Signature<bigint> {
// Convert the private key to buffer.
privateKey = checkPrivateKey(privateKey)

Expand Down Expand Up @@ -121,7 +121,7 @@ export function signMessage(privateKey: Buffer | Uint8Array | string, message: B
* @param publicKey The public key associated with the private key used to sign the message.
* @returns Returns true if the signature is valid and corresponds to the message and public key, false otherwise.
*/
export function verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean {
export function verifySignature(message: BigNumber, signature: Signature, publicKey: Point): boolean {
if (
!isPoint(publicKey) ||
!isSignature(signature) ||
Expand Down Expand Up @@ -281,7 +281,7 @@ export class EdDSAPoseidon {
* @param message The message to be signed.
* @returns The signature of the message.
*/
signMessage(message: BigNumberish): Signature<bigint> {
signMessage(message: BigNumber): Signature<bigint> {
return signMessage(this.privateKey, message)
}

Expand All @@ -291,7 +291,7 @@ export class EdDSAPoseidon {
* @param signature The signature to be verified.
* @returns True if the signature is valid for the message and public key, false otherwise.
*/
verifySignature(message: BigNumberish, signature: Signature): boolean {
verifySignature(message: BigNumber, signature: Signature): boolean {
return verifySignature(message, signature, this.publicKey)
}
}
2 changes: 1 addition & 1 deletion packages/eddsa-poseidon/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Point } from "@zk-kit/baby-jubjub"
import type { BigNumberish } from "@zk-kit/utils"
import { type BigNumberish } from "@zk-kit/utils"
import { bigNumberishToBigInt, bufferToBigInt } from "@zk-kit/utils/conversions"
import { requireTypes } from "@zk-kit/utils/error-handlers"
import { isArray, isBigNumber, isBigNumberish, isObject } from "@zk-kit/utils/type-checks"
Expand Down
17 changes: 12 additions & 5 deletions packages/eddsa-poseidon/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { babyjub, eddsa } from "circomlibjs"
import { Buffer } from "buffer"
import { crypto } from "@zk-kit/utils"
import { bufferToBigInt, crypto } from "@zk-kit/utils"
import { utils } from "ffjavascript"
import { r, packPoint, Point } from "@zk-kit/baby-jubjub"
import {
Expand Down Expand Up @@ -84,7 +84,7 @@ describe("EdDSAPoseidon", () => {
it("Should sign a message (number)", async () => {
const message = 22

const signature = signMessage(privateKey, message)
const signature = signMessage(privateKey, BigInt(message))

const circomlibSignature = eddsa.signPoseidon(privateKey, BigInt(message))

Expand All @@ -96,7 +96,7 @@ describe("EdDSAPoseidon", () => {
it("Should sign a message (hexadecimal)", async () => {
const message = "0x12"

const signature = signMessage(privateKey, message)
const signature = signMessage(privateKey, BigInt(message))

const circomlibSignature = eddsa.signPoseidon(privateKey, BigInt(message))

Expand All @@ -108,7 +108,7 @@ describe("EdDSAPoseidon", () => {
it("Should sign a message (buffer)", async () => {
const message = Buffer.from("message")

const signature = signMessage(privateKey, message)
const signature = signMessage(privateKey, bufferToBigInt(message))

const circomlibSignature = eddsa.signPoseidon(privateKey, BigInt(`0x${message.toString("hex")}`))

Expand All @@ -117,7 +117,7 @@ describe("EdDSAPoseidon", () => {
expect(signature.S).toBe(circomlibSignature.S)
})

it("Should sign a message (string)", async () => {
it("Should sign a message if less than 32 bytes (string)", async () => {
const message = "message"

const signature = signMessage(privateKey, message)
Expand All @@ -129,6 +129,13 @@ describe("EdDSAPoseidon", () => {
expect(signature.S).toBe(circomlibSignature.S)
})

it("Should fail if message is larger than 32 bytes (string)", async () => {
const message = bufferToBigInt(Buffer.from(crypto.getRandomValues(34)))

const fun = () => signMessage(privateKey, message)
expect(fun).toThrow("Size 32 is too small, need at least 33 bytes")
})

it("Should throw an error if the message type is not supported", async () => {
const message = true

Expand Down

0 comments on commit ff0d241

Please sign in to comment.