OpenZeppelin / cairo-contracts

OpenZeppelin Contracts written in Cairo for Starknet, a decentralized ZK Rollup
https://docs.openzeppelin.com/contracts-cairo
MIT License
796 stars 320 forks source link

secp256r1 utils and is_valid_p256_signature #988

Open 0xknwn opened 1 month ago

0xknwn commented 1 month ago

Fixes #987

PR Checklist

0xknwn commented 1 month ago

We've deployed an account on sepolia that can validate P256 signature. If you check the Scarb.toml from 0xknwn/starknet-modular-account, you can see it is based on this branch for now and that is has both the secp256k1 and secp256r1 utils/store from you project

We have run a transaction on sepolia with that account:

[
    "0xa1913d80b4b735ea849919d5636cb5e9",
    "0x98fd57ced24ecea3e19b87540589788d",
    "0x6a05a126fc09205b0a3a509ba0db9243",
    "0x3e75ae4643235e4dad268f0a480ef3f5",
    "0x0"
  ]

Now if you check this typescript program, you can see that,:

[
  "0xa1913d80b4b735ea849919d5636cb5e9",
  "0x98fd57ced24ecea3e19b87540589788d",
  "0x6a05a126fc09205b0a3a509ba0db9243",
  "0x3e75ae4643235e4dad268f0a480ef3f5"
]

So with OpenZeppelin/cairo-contracts with the store/utils for secp256r1 have allowed us to develop an account module that can validate a P256 signature. For the implementation, you can check the p256validator.cairo file in the project.

andrew-fleming commented 1 month ago

Thanks for opening this PR, @0xknwn! I think this will be an awesome addition to the lib. We'll take a look as soon as we can

0xknwn commented 1 month ago

I will work on it tomorrow @andrew-fleming ! Thank you for the feedback and sorry if I did not include the tests.

FYI, this is the transaction that uses the code on Sepolia https://sepolia.starkscan.co/tx/0x419d921d19fbb356ebe8f57be613b8d4d1880c5ad18768ba4e12baa566ec06

andrew-fleming commented 1 month ago

@0xknwn it's my pleasure and thank you for opening the PR!

sorry if I did not include the tests.

No worries, this was easy to miss

0xknwn commented 1 month ago

@andrew-fleming I have done the following to follow up with your feedback:

  1. rebase on main as it is right now (i.e. 0.13.0)
  2. nitpick: remove the empty line
  3. added test_secp256r1 in src/tests/account.cairo - obviously tests are failing, now! I am working on this...
  4. remove the traits that are useless from src/tests/account/test_p256_account.cairo
  5. removed the _u256 from src/tests/account/test_p256_account.cairo as you are right, it is not needed
  6. I am working on the tests now: you are right, I have 5 failing and they are all failing with get_curve_size (as you've reproduced) on the following error:
Panicked with 0x4f7074696f6e3a3a756e77726170206661696c65642e ('Option::unwrap failed.').

I've also figured out that SIGNED_TX_DATA() is actually not referenced in the tests so is just here to document. I will let you know if I can get deeper into the get_curve_size issue.

0xknwn commented 1 month ago

@andrew-fleming ,

I did some additional investigations and I've now fixed the remaining issues, afaik:

Hopefully I did not miss anything else. Thank you for your patience.

0xknwn commented 1 month ago

Hey @0xknwn! The PR is looking great, thanks again for taking the time. I left some small suggestions. Also, we are in the middle of a migration to the latest edition, which would conflict with this PR if we merge it now. It would be great if you wait for that one to be merged, and then update this PR. If not we will be happy to update it and merge it later.

@ericnordelo, I guess it was a rhetorical question 🤣. jk! I am not in a rush: I have forked your repository and our project is using the fork. So let's proceed that way, I will rebase on 0.14 once out and will rework this PR.

Can you please review my answers to your comments and let me know what you think?