Skip to content

Commit

Permalink
Added test for secp256r1 precompile (RIP-7212)
Browse files Browse the repository at this point in the history
  • Loading branch information
delneg committed Mar 21, 2024
1 parent d09833f commit e956b8b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/solidity/contracts/RIP7212.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract RIP7212 {
/// @dev The address of the pre-compiled p256 verifier contract (following RIP-7212)
address internal constant PRECOMPILED_P256_VERIFIER = address(1032);

/// @dev Check if the pre-compiled p256 verifier is available on this chain
function isPreCompiledP256Available() public view returns (bool) {
// Test signature data, from https://gist.github.com/ulerdogan/8f1714895e23a54147fc529ea30517eb
bytes memory testSignatureData =
hex"4cee90eb86eaa050036147a12d49004b6b9c72bd725d39d4785011fe190f0b4da73bd4903f0ce3b639bbbf6e8e80d16931ff4bcf5993d58468e8fb19086e8cac36dbcd03009df8c59286b162af3bd7fcc0450c9aa81be5d10d312af6c66b1d604aebd3099c618202fcfe16ae7770b0c49ab5eadf74b754204a3bb6060e44eff37618b065f9832de4ca6ca971a7a1adc826d0f7c00181a5fb2ddf79ae00b4e10e";

// Perform the static call
(bool success, bytes memory data) = PRECOMPILED_P256_VERIFIER.staticcall(testSignatureData);
if (!success || data.length == 0) {
return false;
}

// Decode the result
uint256 result = abi.decode(data, (uint256));

// Check it's 1 (valid signature)
return result == uint256(1);
}
}
27 changes: 27 additions & 0 deletions tests/solidity/test/RIP7212.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { expect } = require("chai");
const { ethers } = require("hardhat")
const { sendShieldedTransaction, sendShieldedQuery } = require("./testUtils")

describe('RIP7212', () => {
let contract

before(async () => {
const RIP7212 = await ethers.getContractFactory('RIP7212')
contract = await RIP7212.deploy()
await contract.deployed()
})

it('Should be able to execute RIP7212 precompile', async () => {
const [signer] = await ethers.getSigners()

const isAvailableResponse = await sendShieldedQuery(
signer.provider,
contract.address,
contract.interface.encodeFunctionData("isPreCompiledP256Available", [])
);
console.log(isAvailableResponse);
const result = contract.interface.decodeFunctionResult("isPreCompiledP256Available", isAvailableResponse)
console.log(result);
expect(result).to.be.true
})
})

0 comments on commit e956b8b

Please sign in to comment.