Skip to content

Commit

Permalink
WIP: unit tests for BLS
Browse files Browse the repository at this point in the history
  • Loading branch information
fanhousanbu committed Oct 5, 2024
1 parent 9b45ec4 commit db888b1
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 2 deletions.
37 changes: 37 additions & 0 deletions index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ethers } from 'ethers';

function base64ToBytes(base64: string): Buffer {
return Buffer.from(base64, 'base64');
}

function bytesToUint256Array(bytes: Buffer): string[] {
const uint256Array: string[] = [];
for (let i = 0; i < bytes.length; i += 32) {
const chunk = bytes.subarray(i, i + 32);
uint256Array.push(ethers.toBigInt(chunk).toString());
}
return uint256Array;
}

// 立即执行的异步函数
(async () => {

// 示例数据
const pubkeyBase64 = 'shxvsYSiVXG/HoCOSO1RFeYzeMBvCxVV/bEPzTk4CH9cZz73ejD5LhrzHIw40UST';
const msgBase64 = 'YWJj';
const sigBase64 = 'sKSxcK9kYEpZ0KJbbCbyMSHOjcf6ffauBjVD4TIYYiZaDh69pPugobhxMrj358fRAXt+NdXUkP9HfmqRXPSub8xfVi6alfZ8bGbUvWOK2KN/4S/OLPQaO+BYWZKY2/SF';


// 解码并转换为 uint256 数组
const pubkeyBytes = base64ToBytes(pubkeyBase64);
const msgBytes = base64ToBytes(msgBase64);
const sigBytes = base64ToBytes(sigBase64);

const pubkeyUint256Array = bytesToUint256Array(pubkeyBytes);
const msgUint256Array = bytesToUint256Array(msgBytes);
const sigUint256Array = bytesToUint256Array(sigBytes);

console.log('Public Key:', pubkeyUint256Array);
console.log('Message:', msgUint256Array);
console.log('Signature:', sigUint256Array);
})();
308 changes: 308 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "account-contracts",
"version": "1.0.0",
"description": "**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**",
"main": "index.js",
"type": "module",
"directories": {
"lib": "lib",
"test": "test"
},
"scripts": {
"start": "npx tsx index.tsx",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ethers": "^6.13.3"
},
"devDependencies": {
"@types/node": "^22.7.4",
"ts-node": "^10.9.2",
"typescript": "^5.6.2"
}
}
10 changes: 8 additions & 2 deletions test/BLS.Verifier.Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ contract BLSVerifierTest is Test {
}

function testValidateUserOpSignature() public {
uint256[2] memory message = [uint256(1), uint256(2)];
uint256[2] memory signature = [uint256(3), uint256(4)];
uint256[2] memory message = [
uint256(6382179),
uint256(0)
];
uint256[2] memory signature = [
uint256(79898048916366891423109317880192719895075503885619475743155465631070124794406),
uint256(0)
];
uint256[4] memory publicKey = [uint256(5), uint256(6), uint256(7), uint256(8)];
blsVerifier.setBlsPublicKey(publicKey);
bool isValid = blsVerifier.validateUserOpSignature(message, signature);
Expand Down
Loading

0 comments on commit db888b1

Please sign in to comment.