diff --git a/packages/eddsa-poseidon/src/utils.ts b/packages/eddsa-poseidon/src/utils.ts index c1a019b2..b20df6bc 100644 --- a/packages/eddsa-poseidon/src/utils.ts +++ b/packages/eddsa-poseidon/src/utils.ts @@ -70,8 +70,9 @@ export function checkMessage(message: BigNumberish): bigint { ? bigNumberishToBigInt(message) : bufferToBigInt(Buffer.from(message as string)) - const maxLength = 2n ** 256n / 2n - 1n - if (bigIntMessage > maxLength) throw new Error(`Message length is larger than 32 bytes`) + const maxLength = 2n ** 256n / 2n + if (bigIntMessage < maxLength * -1n || bigIntMessage >= maxLength) + throw new Error(`Message length is larger than 32 bytes`) return bigIntMessage } diff --git a/packages/eddsa-poseidon/tests/eddsa-poseidon-blake1.test.ts b/packages/eddsa-poseidon/tests/eddsa-poseidon-blake1.test.ts index 0ce5eecf..b42d87cf 100644 --- a/packages/eddsa-poseidon/tests/eddsa-poseidon-blake1.test.ts +++ b/packages/eddsa-poseidon/tests/eddsa-poseidon-blake1.test.ts @@ -147,7 +147,15 @@ describe("EdDSAPoseidon", () => { }) it("Should throw an error if the message is larger than 32 Bytes [number]", async () => { - const message = 2 ** 256 / 2 + const message = 2 ** 255 + + const fun = () => signMessage(privateKey, message) + + expect(fun).toThrow(`Message length is larger than 32 bytes`) + }) + + it("Should throw an error if the message is larger than 32 Bytes [-number]", async () => { + const message = -(2n ** 255n + 1n) const fun = () => signMessage(privateKey, message)