Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(eddsa-poseidon)!: restrict message types #318

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

f3r10
Copy link

@f3r10 f3r10 commented Sep 7, 2024

Description

The type supported for the EdDSA message has been reduced to BigNumber (either bigint or string which must still be a stringified bigint))

Related Issue(s)

Closes #230

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have run yarn style without getting any errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Member

@cedoor cedoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @f3r10 for this PR.

Looks like you need to fix the error message in your test.

Copy link
Collaborator

@artwyman artwyman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change needs to include functional code not just types and tests. In particular the checkMessage function is where the input needs to be validated and converted appropriately.

Also welcome to the team and thanks for picking up this PR. I'm a thorough and detail-oriented reviewer. Don't take that as being unwelcoming. :)

@@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly don't know what this type keyword is for. The build works fine for me locally if I simply remove it entirely, so I suggest doing that if it passes the GitHub checks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports could be values or types. TS can infer what kind of import that is but the type keyword here makes it more explicit.

@@ -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> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the intent here is to change not only the types but the handling and checking of the inputs here. A BigNumber is limited to bigint or a stringified bigint (not an arbitrary string, not a buffer). The checkMessage function is the place where you should be checking that and converting to bigint appropriately.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also per @cedoor's comment on #230 there should be some documentation above about the fact that the message is a single BabyJub field element, represented as a bigint. Best-practice for users with other types of input is probably to hash it (e.g. with a poseidon hash), or encode their inputs bytes directly if small enough (e.g. using bufferToBigInt()).

@@ -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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a stringified bigint, so should be accepted as a BigNumber without explicit conversion.

I also suggest adding another test case with a decimal string like "123" which should also work. Anything parsable by BigInt(s) should work.

@@ -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 () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this case shouldn't be supported without an explicit conversion to bigint

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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is failing on GitHub

@f3r10
Copy link
Author

f3r10 commented Sep 12, 2024

awesome!! thanks @artwyman for your feedback. I am going to work through your comments right away.

@cedoor
Copy link
Member

cedoor commented Oct 24, 2024

Hi @f3r10, are you still going to work on this?

@f3r10
Copy link
Author

f3r10 commented Oct 26, 2024

My apologies, @cedoor. I started a new job and have been really busy. I hope to contribute once I settle into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Restrict types of message in eddsa-poseidon's signMessage
3 participants