From e956b8b2d26c745e5f1b6b5f8ee4072f0b036cb4 Mon Sep 17 00:00:00 2001 From: Denis Date: Thu, 21 Mar 2024 16:48:58 +0400 Subject: [PATCH] Added test for secp256r1 precompile (RIP-7212) --- tests/solidity/contracts/RIP7212.sol | 26 ++++++++++++++++++++++++++ tests/solidity/test/RIP7212.js | 27 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/solidity/contracts/RIP7212.sol create mode 100644 tests/solidity/test/RIP7212.js diff --git a/tests/solidity/contracts/RIP7212.sol b/tests/solidity/contracts/RIP7212.sol new file mode 100644 index 00000000..2d9b1feb --- /dev/null +++ b/tests/solidity/contracts/RIP7212.sol @@ -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); + } +} \ No newline at end of file diff --git a/tests/solidity/test/RIP7212.js b/tests/solidity/test/RIP7212.js new file mode 100644 index 00000000..ead45957 --- /dev/null +++ b/tests/solidity/test/RIP7212.js @@ -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 + }) +}) \ No newline at end of file