Skip to content

Commit

Permalink
tests: add failure cases
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong committed Jan 10, 2025
1 parent 56a9ab7 commit 440028f
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 72 deletions.
58 changes: 40 additions & 18 deletions tests/module/test_misc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as misc from 'misc';
import * as ckb from 'ckb';

function test_ckb_smt_verify1() {
function test_ckb_smt_verify1(failure) {
// Test vectors from the Rust example
const keyHex =
'381dc5391dab099da5e28acd1ad859a051cf18ace804d037f12819c6fbc0e18b';
Expand All @@ -27,17 +27,28 @@ function test_ckb_smt_verify1() {
const endInsert = ckb.current_cycles();
console.log(`SMT insert cycles: ${endInsert - startInsert}`);

// Verify proof and measure cycles
const startVerify = ckb.current_cycles();
const isValid = smt.verify(rootHash, proof);
const endVerify = ckb.current_cycles();
console.log(`SMT verify cycles: ${endVerify - startVerify}`);
if (failure) {
// Verify proof and measure cycles
const startVerify = ckb.current_cycles();
const wrongRootHash = misc.hex.decode(
'0000000000000000000000000000000000000000000000000000000000000000');
const isValid = smt.verify(wrongRootHash, proof);
const endVerify = ckb.current_cycles();
console.log(`SMT verify cycles: ${endVerify - startVerify}`);
console.assert(isValid === false, 'SMT verification should fail');
} else {
// Verify proof and measure cycles
const startVerify = ckb.current_cycles();
const isValid = smt.verify(rootHash, proof);
const endVerify = ckb.current_cycles();
console.log(`SMT verify cycles: ${endVerify - startVerify}`);
console.assert(isValid === true, 'SMT verification failed');
}

console.assert(isValid === true, 'SMT verification failed');
console.log('test_ckb_smt_verify1 ok');
}

function test_ckb_smt_verify2() {
function test_ckb_smt_verify2(failure) {
const keyHex =
'a9bb945be71f0bd2757d33d2465b6387383da42f321072e47472f0c9c7428a8a';
const valueHex =
Expand All @@ -59,12 +70,21 @@ function test_ckb_smt_verify2() {
const endInsert = ckb.current_cycles();
console.log(`SMT verify2 insert cycles: ${endInsert - startInsert}`);

const startVerify = ckb.current_cycles();
const isValid = smt.verify(rootHash, proof);
const endVerify = ckb.current_cycles();
console.log(`SMT verify2 verify cycles: ${endVerify - startVerify}`);

console.assert(isValid === true, 'SMT verification2 failed');
if (failure) {
const startVerify = ckb.current_cycles();
const wrongProof = misc.hex.decode(
'0000000000000000000000000000000000000000000000000000000000000000');
const isValid = smt.verify(rootHash, wrongProof);
const endVerify = ckb.current_cycles();
console.log(`SMT verify2 verify cycles: ${endVerify - startVerify}`);
console.assert(isValid === false, 'SMT verification2 should fail');
} else {
const startVerify = ckb.current_cycles();
const isValid = smt.verify(rootHash, proof);
const endVerify = ckb.current_cycles();
console.log(`SMT verify2 verify cycles: ${endVerify - startVerify}`);
console.assert(isValid === true, 'SMT verification2 failed');
}
console.log('test_ckb_smt_verify2 ok');
}

Expand Down Expand Up @@ -131,7 +151,7 @@ function test_ckb_smt_verify_invalid() {
}

function test_base64_encode() {
const inputHex = '48656c6c6f20576f726c6421'; // "Hello World!" in hex
const inputHex = '48656c6c6f20576f726c6421'; // "Hello World!" in hex
const expectedBase64 = 'SGVsbG8gV29ybGQh';

const input = misc.hex.decode(inputHex);
Expand All @@ -142,7 +162,7 @@ function test_base64_encode() {
}

function test_base64_decode() {
const base64Input = 'SGVsbG8gV29ybGQh'; // "Hello World!" in base64
const base64Input = 'SGVsbG8gV29ybGQh'; // "Hello World!" in base64
const expectedHex = '48656c6c6f20576f726c6421';

const decoded = misc.base64.decode(base64Input);
Expand All @@ -154,8 +174,10 @@ function test_base64_decode() {

// Add the new test cases to the main execution
console.log('test_misc.js ...');
test_ckb_smt_verify1();
test_ckb_smt_verify2();
test_ckb_smt_verify1(true);
test_ckb_smt_verify1(false);
test_ckb_smt_verify2(true);
test_ckb_smt_verify2(false);
test_ckb_smt_verify3();
test_ckb_smt_verify_invalid();
test_base64_encode();
Expand Down
132 changes: 78 additions & 54 deletions tests/module/test_secp256k1.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,91 @@
import * as secp256k1 from 'secp256k1';
import * as ckb from 'ckb';
import * as misc from 'misc';

function hexStringToUint8Array(hexString) {
// Remove any non-hex characters (like spaces and commas)
hexString = hexString.replace(/[^0-9A-Fa-f]/g, '');
const bytes = new Uint8Array(hexString.length / 2);
for (let i = 0; i < hexString.length; i += 2) {
bytes[i / 2] = parseInt(hexString.substr(i, 2), 16);
}
return bytes;
}

function arrayBufferToHexString(buffer) {
return Array.from(new Uint8Array(buffer))
.map(b => b.toString(16).padStart(2, '0'))
.join('');
}

function test_recovery() {
function test_recovery(failure) {
const recid = 1;
const msg = hexStringToUint8Array(
const msg = misc.hex.decode(
'6a0024347e28905e2587c4c7598332a39' +
'ba6684bb6b74653511656a02bd20edb');
const sig = hexStringToUint8Array(
const sig = misc.hex.decode(
'76e6d0e5ea61b46fe10443fe5b4d1bc6' +
'ce2d0d49d55e810312f7c22702e0548a' +
'3969ce72940a34632f93ebd1b8d591c3' +
'775428f035c6577e4adf8068b04819f0');
const s = ckb.current_cycles();
const expected_pubkey = hexStringToUint8Array(
const expected_pubkey = misc.hex.decode(
'aca98c5822b997c15f8c974386a11b14' +
'a0d009a4d5156e145644573e82ef7e7b' +
'226b9eb6173d6b4504606eb8d9558bde' +
'98d12100836e92d306a40f337ed8a0f3');
const e = ckb.current_cycles();
console.log(`hexStringToUint8Array: ${e - s}`);
console.log(`misc.hex.decode: ${e - s}`);

// Verify the signature
const start = ckb.current_cycles();
const pubkey = secp256k1.recover(sig.buffer, recid, msg.buffer);
const end = ckb.current_cycles();
console.log(`recover cycles: ${end - start}`);
console.assert(
arrayBufferToHexString(pubkey) ===
arrayBufferToHexString(expected_pubkey),
'Signature recovery failed');
if (failure) {
let success = false;
const wrong_sig = misc.hex.decode(
'00000000000000000000000000000000' +
'00000000000000000000000000000000' +
'00000000000000000000000000000000' +
'00000000000000000000000000000000');
try {
success = secp256k1.recover(wrong_sig, recid, msg);
} catch (e) {
success = true;
}
console.assert(success, 'Signature recovery should fail');
} else {
const start = ckb.current_cycles();
const pubkey = secp256k1.recover(sig, recid, msg);
const end = ckb.current_cycles();
console.log(`recover cycles: ${end - start}`);
console.assert(
misc.hex.encode(pubkey) === misc.hex.encode(expected_pubkey),
'Signature recovery failed');
}
console.log('test_recovery ok');
}

function test_recovery_failure() {
const recid = 1;
const wrong_msg = misc.hex.decode(
'00000000000000000000000000000000' +
'00000000000000000000000000000000');
const sig = misc.hex.decode(
'76e6d0e5ea61b46fe10443fe5b4d1bc6' +
'ce2d0d49d55e810312f7c22702e0548a' +
'3969ce72940a34632f93ebd1b8d591c3' +
'775428f035c6577e4adf8068b04819f0');
const expected_pubkey = misc.hex.decode(
'aca98c5822b997c15f8c974386a11b14' +
'a0d009a4d5156e145644573e82ef7e7b' +
'226b9eb6173d6b4504606eb8d9558bde' +
'98d12100836e92d306a40f337ed8a0f3');

const pubkey = secp256k1.recover(sig, recid, wrong_msg);
console.assert(
misc.hex.encode(pubkey) !== misc.hex.encode(expected_pubkey),
'Signature recovery should fail');
console.log('test_recovery_failure ok');
}

function test_verify() {
const sig = hexStringToUint8Array(
const sig = misc.hex.decode(
'76e6d0e5ea61b46fe10443fe5b4d1bc6' +
'ce2d0d49d55e810312f7c22702e0548a' +
'3969ce72940a34632f93ebd1b8d591c3' +
'775428f035c6577e4adf8068b04819f0');
const msg = hexStringToUint8Array(
const msg = misc.hex.decode(
'6a0024347e28905e2587c4c7598332a39' +
'ba6684bb6b74653511656a02bd20edb');
const pubkey = hexStringToUint8Array(
const pubkey = misc.hex.decode(
'aca98c5822b997c15f8c974386a11b14' +
'a0d009a4d5156e145644573e82ef7e7b' +
'226b9eb6173d6b4504606eb8d9558bde' +
'98d12100836e92d306a40f337ed8a0f3');
const start = ckb.current_cycles();
const success = secp256k1.verify(sig.buffer, msg.buffer, pubkey.buffer);
const success = secp256k1.verify(sig, msg, pubkey);
const end = ckb.current_cycles();
console.log(`verify cycles: ${end - start}`);
console.assert(success, 'test_verify failed');
Expand All @@ -72,51 +94,51 @@ function test_verify() {
}

function test_parse_pubkey() {
const pubkey = hexStringToUint8Array(
const pubkey = misc.hex.decode(
'0375fbccbf29be9408ed96ca232fb941' +
'b358e6158ace9fbfe8214c994d38bd9ff9');
const start = ckb.current_cycles();
const out_pubkey = secp256k1.parsePubkey(pubkey.buffer);
const out_pubkey = secp256k1.parsePubkey(pubkey);
const end = ckb.current_cycles();
console.log(`parsePubkey cycles: ${end - start}`);
console.assert(
arrayBufferToHexString(out_pubkey) ===
"f99fbd384d994c21e8bf9fce8a15e658" +
"b341b92f23ca96ed0894be29bfccfb75" +
"3d797a1b2ce723964030b3ef1e31656b" +
"04a9c3fadcf100a613b385fec85620d1",
misc.hex.encode(out_pubkey) ===
'f99fbd384d994c21e8bf9fce8a15e658' +
'b341b92f23ca96ed0894be29bfccfb75' +
'3d797a1b2ce723964030b3ef1e31656b' +
'04a9c3fadcf100a613b385fec85620d1',
'parsePubkey failed');
console.log("test_parse_pubkey ok");
console.log('test_parse_pubkey ok');
}

function test_serialize_pubkey() {
const pubkey = hexStringToUint8Array(
const pubkey = misc.hex.decode(
'f99fbd384d994c21e8bf9fce8a15e658' +
'b341b92f23ca96ed0894be29bfccfb75' +
'3d797a1b2ce723964030b3ef1e31656b' +
'04a9c3fadcf100a613b385fec85620d1');
const start = ckb.current_cycles();
const out_pubkey = secp256k1.serializePubkey(pubkey.buffer, true);
const out_pubkey = secp256k1.serializePubkey(pubkey, true);
const end = ckb.current_cycles();
console.log(`serializePubkey: ${end - start}`);
console.assert(
arrayBufferToHexString(out_pubkey) ===
'0375fbccbf29be9408ed96ca232fb941' +
'b358e6158ace9fbfe8214c994d38bd9ff9',
misc.hex.encode(out_pubkey) ===
'0375fbccbf29be9408ed96ca232fb941' +
'b358e6158ace9fbfe8214c994d38bd9ff9',
'serializePubkey failed');

const pubkey2 = hexStringToUint8Array(
const pubkey2 = misc.hex.decode(
'11dae3a18c58627d4564aff118f9b49f' +
'c1dc48992e0f615cef19732b7d8842d4' +
'95a0eb41da0b71a3e18dd063e9097d40' +
'944936ee93f498b26188faaa02276bde');
const out_pubkey2 = secp256k1.serializePubkey(pubkey2.buffer, false);
const out_pubkey2 = secp256k1.serializePubkey(pubkey2, false);
console.assert(
arrayBufferToHexString(out_pubkey2) ===
'04d442887d2b7319ef5c610f2e9948dc' +
'c19fb4f918f1af64457d62588ca1e3da' +
'11de6b2702aafa8861b298f493ee3649' +
'94407d09e963d08de1a3710bda41eba095',
misc.hex.encode(out_pubkey2) ===
'04d442887d2b7319ef5c610f2e9948dc' +
'c19fb4f918f1af64457d62588ca1e3da' +
'11de6b2702aafa8861b298f493ee3649' +
'94407d09e963d08de1a3710bda41eba095',
'serializePubkey failed');

console.log('test_serialize_pubkey ok');
Expand All @@ -134,7 +156,9 @@ function test_func_not_found() {
}

console.log('test_secp256k1.js ...');
test_recovery();
test_recovery(true);
test_recovery(false);
test_recovery_failure();
test_verify();
test_parse_pubkey();
test_serialize_pubkey();
Expand Down

0 comments on commit 440028f

Please sign in to comment.