Skip to content

Instantly share code, notes, and snippets.

@pcaversaccio
Created February 8, 2024 11:17
Show Gist options
  • Save pcaversaccio/f638372faf1297e2ccdb40d664afe889 to your computer and use it in GitHub Desktop.
Save pcaversaccio/f638372faf1297e2ccdb40d664afe889 to your computer and use it in GitHub Desktop.
Native P256 signature verification function. Deployed on https://mumbai.polygonscan.com/address/0x7d0959292E46a3B659239Afce53F66bB7a62A8Ec.
# pragma version ^0.3.10
"""
@title P256 Signature Verification Function
@custom:contract-name P256Verifier
@license GNU Affero General Public License v3.0 only
@author pcaversaccio
@notice The `verify` function can be used to natively (currently
only supported on Polygon Mumbai test network) verify a
P256 (a.k.a. secp256r1 elliptic curve) signature. For more
technical details, please refer to EIP-7212:
https://eips.ethereum.org/EIPS/eip-7212,
and RIP-7212:
https://github.com/ethereum/RIPs/blob/master/RIPS/rip-7212.md.
"""
# The fixed address for the precompiled contract `P256VERIFY`.
# See: https://github.com/ethereum/RIPs/blob/master/RIPS/rip-7212.md#specification.
_P256VERIFY: constant(address) = 0x0000000000000000000000000000000000000100
@external
@payable
def __init__():
"""
@dev To omit the opcodes for checking the `msg.value`
in the creation-time EVM bytecode, the constructor
is declared as `payable`.
"""
pass
@external
@view
def verify(hash: bytes32, signature: Bytes[64], public_key: Bytes[64]) -> bytes32:
"""
@dev Verifies natively (i.e. via `STATICCALL` to the precompiled address
`0x0000000000000000000000000000000000000100`) a P256 (a.k.a. secp256r1
elliptic curve) signature.
@param hash The 32-byte message digest that was signed.
@param signature The secp256r1 64-byte signature of `hash`.
@param public_key The `x` and `y` coordinates of the public key.
@return bytes32 The 32-byte return value, which can be either `0x00...00`
(invalid) or `0x00..01` (valid).
"""
payload: Bytes[160] = concat(hash, signature, public_key)
return convert(0, bytes32) if (len(payload) != 160) else convert(raw_call(_P256VERIFY, payload, max_outsize=32, is_static_call=True), bytes32)
@pcaversaccio
Copy link
Author

pcaversaccio commented Feb 8, 2024

Example Payload

Copied from here.

  • hash: 0x4cee90eb86eaa050036147a12d49004b6b9c72bd725d39d4785011fe190f0b4d
  • signature: 0xa73bd4903f0ce3b639bbbf6e8e80d16931ff4bcf5993d58468e8fb19086e8cac36dbcd03009df8c59286b162af3bd7fcc0450c9aa81be5d10d312af6c66b1d60
  • public_key: 0x4aebd3099c618202fcfe16ae7770b0c49ab5eadf74b754204a3bb6060e44eff37618b065f9832de4ca6ca971a7a1adc826d0f7c00181a5fb2ddf79ae00b4e10e

Invoke via cast:

 cast call 0x7d0959292E46a3B659239Afce53F66bB7a62A8Ec "verify(bytes32,bytes,bytes)" --rpc-url "https://polygon-mumbai.blockpi.network/v1/rpc/public" 0x4cee90eb86eaa050036147a12d49004b6b9c72bd725d39d4785011fe190f0b4d 0xa73bd4903f0ce3b639bbbf6e8e80d16931ff4bcf5993d58468e8fb19086e8cac36dbcd03009df8c59286b162af3bd7fcc0450c9aa81be5d10d312af6c66b1d60 0x4aebd3099c618202fcfe16ae7770b0c49ab5eadf74b754204a3bb6060e44eff37618b065f9832de4ca6ca971a7a1adc826d0f7c00181a5fb2ddf79ae00b4e10e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment