+1
-1
@@ -42,7 +42,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.20.12",
|
||||
"@polkadot/dev": "^0.68.36",
|
||||
"@polkadot/dev": "^0.68.37",
|
||||
"@polkadot/util": "^10.4.1",
|
||||
"@polkadot/x-randomvalues": "^10.4.1",
|
||||
"@types/jest": "^29.4.0",
|
||||
|
||||
@@ -4,51 +4,63 @@
|
||||
import { assert, u8aToHex } from '@polkadot/util';
|
||||
|
||||
export function bip39Generate (wasm) {
|
||||
const res = wasm.bip39Generate(21);
|
||||
it('generates a bip39 phrase', () => {
|
||||
const res = wasm.bip39Generate(21);
|
||||
|
||||
console.log('\tPHR', res);
|
||||
// console.log('\tPHR', res);
|
||||
|
||||
assert(res.split(' ').length === 21, 'ERROR: Invalid bip39 Phase length');
|
||||
assert(res.split(' ').length === 21, 'ERROR: Invalid bip39 Phase length');
|
||||
});
|
||||
}
|
||||
|
||||
export function bip39GenerateSubsequent (wasm) {
|
||||
const val1 = wasm.bip39Generate(24);
|
||||
const val2 = wasm.bip39Generate(24);
|
||||
it('generates different subsequent mnemonics', () => {
|
||||
const val1 = wasm.bip39Generate(24);
|
||||
const val2 = wasm.bip39Generate(24);
|
||||
|
||||
console.log('\tVL1', val1);
|
||||
console.log('\tVL2', val2);
|
||||
// console.log('\tVL1', val1);
|
||||
// console.log('\tVL2', val2);
|
||||
|
||||
assert(val1 !== val2, 'ERROR: Subsequent mnemonics are the same');
|
||||
assert(val1 !== val2, 'ERROR: Subsequent mnemonics are the same');
|
||||
});
|
||||
}
|
||||
|
||||
export function bip39Validate (wasm) {
|
||||
const res = wasm.bip39Validate(wasm.bip39Generate(12));
|
||||
it('validates a created mnemonic', () => {
|
||||
const res = wasm.bip39Validate(wasm.bip39Generate(12));
|
||||
|
||||
console.log('\tVAL', res);
|
||||
// console.log('\tVAL', res);
|
||||
|
||||
assert(res, 'ERROR: Invalid bip39 validation');
|
||||
assert(res, 'ERROR: Invalid bip39 validation');
|
||||
});
|
||||
}
|
||||
|
||||
export function bip39ToEntropy (wasm) {
|
||||
const res = u8aToHex(wasm.bip39ToEntropy('legal winner thank year wave sausage worth useful legal winner thank yellow'));
|
||||
it('creates correct entropy for a known mnemonic', () => {
|
||||
const res = u8aToHex(wasm.bip39ToEntropy('legal winner thank year wave sausage worth useful legal winner thank yellow'));
|
||||
|
||||
console.log('\tENT', res);
|
||||
// console.log('\tENT', res);
|
||||
|
||||
assert(res === '0x7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f', 'ERROR: Invalid bip39 entropy');
|
||||
assert(res === '0x7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f', 'ERROR: Invalid bip39 entropy');
|
||||
});
|
||||
}
|
||||
|
||||
export function bip39ToMiniSecret (wasm) {
|
||||
const res = u8aToHex(wasm.bip39ToMiniSecret('legal winner thank year wave sausage worth useful legal winner thank yellow', 'Substrate'));
|
||||
it('creates the correct minisecret from a mnemonic', () => {
|
||||
const res = u8aToHex(wasm.bip39ToMiniSecret('legal winner thank year wave sausage worth useful legal winner thank yellow', 'Substrate'));
|
||||
|
||||
console.log('\tMIN', res);
|
||||
// console.log('\tMIN', res);
|
||||
|
||||
assert(res === '0x4313249608fe8ac10fd5886c92c4579007272cb77c21551ee5b8d60b78041685', 'ERROR: Invalid bip39 mini secret');
|
||||
assert(res === '0x4313249608fe8ac10fd5886c92c4579007272cb77c21551ee5b8d60b78041685', 'ERROR: Invalid bip39 mini secret');
|
||||
});
|
||||
}
|
||||
|
||||
export function bip39ToSeed (wasm) {
|
||||
const res = u8aToHex(wasm.bip39ToSeed('seed sock milk update focus rotate barely fade car face mechanic mercy', ''));
|
||||
it('creates the correct seed for a mnemonic', () => {
|
||||
const res = u8aToHex(wasm.bip39ToSeed('seed sock milk update focus rotate barely fade car face mechanic mercy', ''));
|
||||
|
||||
console.log('\tSEE', res);
|
||||
// console.log('\tSEE', res);
|
||||
|
||||
assert(res === '0x3c121e20de068083b49c2315697fb59a2d9e8643c24e5ea7628132c58969a027', 'ERROR: Invalid bip39 mini secret');
|
||||
assert(res === '0x3c121e20de068083b49c2315697fb59a2d9e8643c24e5ea7628132c58969a027', 'ERROR: Invalid bip39 mini secret');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -16,39 +16,49 @@ function randomPair (wasm) {
|
||||
}
|
||||
|
||||
export function ed25519PairFromSeed (wasm) {
|
||||
const [pair, pk, sk] = extractKeys(wasm.ed25519KeypairFromSeed(stringToU8a('12345678901234567890123456789012')));
|
||||
it('creates a known pair from a known seed', () => {
|
||||
const [pair, pk, sk] = extractKeys(wasm.ed25519KeypairFromSeed(stringToU8a('12345678901234567890123456789012')));
|
||||
|
||||
console.log('\tSEC', u8aToHex(sk));
|
||||
console.log('\tPUB', u8aToHex(pk));
|
||||
// console.log('\tSEC', u8aToHex(sk));
|
||||
// console.log('\tPUB', u8aToHex(pk));
|
||||
|
||||
assert(u8aToHex(pair) === '0x31323334353637383930313233343536373839303132333435363738393031322f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee', 'ERROR: pairFromSeed() does not match');
|
||||
assert(u8aToHex(sk) === '0x3132333435363738393031323334353637383930313233343536373839303132');
|
||||
assert(u8aToHex(pk) === '0x2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee');
|
||||
assert(u8aToHex(pair) === '0x31323334353637383930313233343536373839303132333435363738393031322f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee', 'ERROR: pairFromSeed() does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function ed25519SignAndVerify (wasm) {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
const signature = wasm.ed25519Sign(pk, sk, stringToU8a('this is a message'));
|
||||
const isValid = wasm.ed25519Verify(signature, stringToU8a('this is a message'), pk);
|
||||
it('signs and verifies', () => {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
const signature = wasm.ed25519Sign(pk, sk, stringToU8a('this is a message'));
|
||||
const isValid = wasm.ed25519Verify(signature, stringToU8a('this is a message'), pk);
|
||||
|
||||
console.log('\tSIG', u8aToHex(signature));
|
||||
console.log('\tRES', isValid);
|
||||
// console.log('\tSIG', u8aToHex(signature));
|
||||
// console.log('\tRES', isValid);
|
||||
|
||||
assert(isValid, 'ERROR: Unable to verify signature');
|
||||
assert(isValid, 'ERROR: Unable to verify signature');
|
||||
});
|
||||
}
|
||||
|
||||
export function ed25519VerifyExisting (wasm) {
|
||||
const isValid = wasm.ed25519Verify(hexToU8a('0x90588f3f512496f2dd40571d162e8182860081c74e2085316e7c4396918f07da412ee029978e4dd714057fe973bd9e7d645148bf7b66680d67c93227cde95202'), stringToU8a('this is a message'), hexToU8a('0x2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee'));
|
||||
it('verifies a known signature', () => {
|
||||
const isValid = wasm.ed25519Verify(hexToU8a('0x90588f3f512496f2dd40571d162e8182860081c74e2085316e7c4396918f07da412ee029978e4dd714057fe973bd9e7d645148bf7b66680d67c93227cde95202'), stringToU8a('this is a message'), hexToU8a('0x2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee'));
|
||||
|
||||
console.log('\tRES', isValid);
|
||||
// console.log('\tRES', isValid);
|
||||
|
||||
assert(isValid, 'ERROR: Unable to verify signature');
|
||||
assert(isValid, 'ERROR: Unable to verify signature');
|
||||
});
|
||||
}
|
||||
|
||||
export function ed25519Benchmark (wasm) {
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
it('runs a verification benchamark', () => {
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
|
||||
for (let i = 0; i < 256; i++) {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
for (let i = 0; i < 256; i++) {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
|
||||
assert(wasm.ed25519Verify(wasm.ed25519Sign(pk, sk, MESSAGE), MESSAGE, pk), 'ERROR: Unable to verify signature');
|
||||
}
|
||||
assert(wasm.ed25519Verify(wasm.ed25519Sign(pk, sk, MESSAGE), MESSAGE, pk), 'ERROR: Unable to verify signature');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4,83 +4,103 @@
|
||||
import { assert, stringToU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
export function blake2bHash (wasm) {
|
||||
const hash = u8aToHex(wasm.blake2b(stringToU8a('abc'), new Uint8Array(), 64));
|
||||
it('creates a known blake2b hash', () => {
|
||||
const hash = u8aToHex(wasm.blake2b(stringToU8a('abc'), new Uint8Array(), 64));
|
||||
|
||||
console.log('\tRES', hash);
|
||||
// console.log('\tRES', hash);
|
||||
|
||||
assert(hash === '0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923', 'ERROR: blake2bHash does not match');
|
||||
assert(hash === '0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923', 'ERROR: blake2bHash does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function hmacSha256 (wasm) {
|
||||
const hash = u8aToHex(wasm.hmacSha256(stringToU8a('secret'), stringToU8a('some message')));
|
||||
it('creates a known hmacSha256', () => {
|
||||
const hash = u8aToHex(wasm.hmacSha256(stringToU8a('secret'), stringToU8a('some message')));
|
||||
|
||||
console.log('\tRES', hash);
|
||||
// console.log('\tRES', hash);
|
||||
|
||||
assert(hash === '0xf28a70b41263840e5c059a0a733336e0957efba87902aa8cca11441d4b0c96d7', 'ERROR: hmacSha256 does not match');
|
||||
assert(hash === '0xf28a70b41263840e5c059a0a733336e0957efba87902aa8cca11441d4b0c96d7', 'ERROR: hmacSha256 does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function hmacSha512 (wasm) {
|
||||
const hash = u8aToHex(wasm.hmacSha512(stringToU8a('secret'), stringToU8a('some message')));
|
||||
it('creates a known hmacSha512', () => {
|
||||
const hash = u8aToHex(wasm.hmacSha512(stringToU8a('secret'), stringToU8a('some message')));
|
||||
|
||||
console.log('\tRES', hash);
|
||||
// console.log('\tRES', hash);
|
||||
|
||||
assert(hash === '0x295832e97ed77be75a9fa98029497e4a722c4b9a2f21b39d34f1befa931a39ec520fd24711d6f5c03501384ea66b83066a01a82c57a0460f8cd1f471fcce5841', 'ERROR: hmacSha512 does not match');
|
||||
assert(hash === '0x295832e97ed77be75a9fa98029497e4a722c4b9a2f21b39d34f1befa931a39ec520fd24711d6f5c03501384ea66b83066a01a82c57a0460f8cd1f471fcce5841', 'ERROR: hmacSha512 does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function keccak256 (wasm) {
|
||||
const hash = u8aToHex(wasm.keccak256(stringToU8a('test value')));
|
||||
it('creates a known keccak256 hash', () => {
|
||||
const hash = u8aToHex(wasm.keccak256(stringToU8a('test value')));
|
||||
|
||||
console.log('\tRES', hash);
|
||||
// console.log('\tRES', hash);
|
||||
|
||||
assert(hash === '0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e', 'ERROR: keccak256 does not match');
|
||||
assert(hash === '0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e', 'ERROR: keccak256 does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function keccak512 (wasm) {
|
||||
const hash = u8aToHex(wasm.keccak512(stringToU8a('test')));
|
||||
it('creates a known keccak512 hash', () => {
|
||||
const hash = u8aToHex(wasm.keccak512(stringToU8a('test')));
|
||||
|
||||
console.log('\tRES', hash);
|
||||
// console.log('\tRES', hash);
|
||||
|
||||
assert(hash === '0x1e2e9fc2002b002d75198b7503210c05a1baac4560916a3c6d93bcce3a50d7f00fd395bf1647b9abb8d1afcc9c76c289b0c9383ba386a956da4b38934417789e', 'ERROR: keccak512 does not match');
|
||||
assert(hash === '0x1e2e9fc2002b002d75198b7503210c05a1baac4560916a3c6d93bcce3a50d7f00fd395bf1647b9abb8d1afcc9c76c289b0c9383ba386a956da4b38934417789e', 'ERROR: keccak512 does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function pbkdf2Hash (wasm) {
|
||||
const hash = u8aToHex(wasm.pbkdf2(stringToU8a('hello world'), stringToU8a('this is a salt'), 2048));
|
||||
it('creates a known pbkdf2 hash', () => {
|
||||
const hash = u8aToHex(wasm.pbkdf2(stringToU8a('hello world'), stringToU8a('this is a salt'), 2048));
|
||||
|
||||
console.log('\tRES', hash);
|
||||
// console.log('\tRES', hash);
|
||||
|
||||
assert(hash === '0x5fcbe04f05300a3ecc5c35d18ea0b78f3f6853d2ae5f3fca374f69a7d1f78b5def5c60dae1a568026c7492511e0c53521e8bb6e03a650e1263265fee92722270', 'ERROR: pbkdf2Hash does not match');
|
||||
assert(hash === '0x5fcbe04f05300a3ecc5c35d18ea0b78f3f6853d2ae5f3fca374f69a7d1f78b5def5c60dae1a568026c7492511e0c53521e8bb6e03a650e1263265fee92722270', 'ERROR: pbkdf2Hash does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function scryptHash (wasm) {
|
||||
const hash = u8aToHex(wasm.scrypt(stringToU8a('password'), stringToU8a('salt'), 14, 8, 1));
|
||||
it('creates a known scrypt hash', () => {
|
||||
const hash = u8aToHex(wasm.scrypt(stringToU8a('password'), stringToU8a('salt'), 14, 8, 1));
|
||||
|
||||
console.log('\tRES', hash);
|
||||
// console.log('\tRES', hash);
|
||||
|
||||
assert(hash === '0x745731af4484f323968969eda289aeee005b5903ac561e64a5aca121797bf7734ef9fd58422e2e22183bcacba9ec87ba0c83b7a2e788f03ce0da06463433cda6', 'ERROR: scryptHash does not match');
|
||||
assert(hash === '0x745731af4484f323968969eda289aeee005b5903ac561e64a5aca121797bf7734ef9fd58422e2e22183bcacba9ec87ba0c83b7a2e788f03ce0da06463433cda6', 'ERROR: scryptHash does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function sha256Hash (wasm) {
|
||||
const hash = u8aToHex(wasm.sha256(stringToU8a('hello world')));
|
||||
it('creates a known sha256 hash', () => {
|
||||
const hash = u8aToHex(wasm.sha256(stringToU8a('hello world')));
|
||||
|
||||
console.log('\tRES', hash);
|
||||
// console.log('\tRES', hash);
|
||||
|
||||
assert(hash === '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9', 'ERROR: sha256Hash does not match');
|
||||
assert(hash === '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9', 'ERROR: sha256Hash does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function sha512Hash (wasm) {
|
||||
const hash = u8aToHex(wasm.sha512(stringToU8a('hello world')));
|
||||
it('creates a known sha512 hash', () => {
|
||||
const hash = u8aToHex(wasm.sha512(stringToU8a('hello world')));
|
||||
|
||||
console.log('\tRES', hash);
|
||||
// console.log('\tRES', hash);
|
||||
|
||||
assert(hash === '0x309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f', 'ERROR: sha512Hash does not match');
|
||||
assert(hash === '0x309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f', 'ERROR: sha512Hash does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function twoxHash (wasm) {
|
||||
const hash64 = u8aToHex(wasm.twox(stringToU8a('abc'), 1));
|
||||
const hash256 = u8aToHex(wasm.twox(stringToU8a('abc'), 4));
|
||||
it('creates a known twox hash', () => {
|
||||
const hash64 = u8aToHex(wasm.twox(stringToU8a('abc'), 1));
|
||||
const hash256 = u8aToHex(wasm.twox(stringToU8a('abc'), 4));
|
||||
|
||||
console.log('\t64 ', hash64);
|
||||
console.log('\t256', hash256);
|
||||
// console.log('\t64 ', hash64);
|
||||
// console.log('\t256', hash256);
|
||||
|
||||
assert(hash64 === '0x990977adf52cbc44' && hash256 === '0x990977adf52cbc440889329981caa9bef7da5770b2b8a05303b75d95360dd62b', 'ERROR: twoxHash does not match');
|
||||
assert(hash64 === '0x990977adf52cbc44' && hash256 === '0x990977adf52cbc440889329981caa9bef7da5770b2b8a05303b75d95360dd62b', 'ERROR: twoxHash does not match');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -14,15 +14,16 @@ export const tests = {
|
||||
// We place secp256k1 first, this allows the interaction with it in the
|
||||
// hashing (specifically scrypt) test not be an issue (ASM.js only)
|
||||
// https://github.com/polkadot-js/wasm/issues/307
|
||||
...secp256k1,
|
||||
...ed25519,
|
||||
...sr25519,
|
||||
...vrf,
|
||||
...bip39,
|
||||
...hashing
|
||||
secp256k1,
|
||||
// eslint-disable-next-line sort-keys
|
||||
bip39,
|
||||
ed25519,
|
||||
hashing,
|
||||
sr25519,
|
||||
vrf
|
||||
};
|
||||
|
||||
export async function beforeAll (name, wasm) {
|
||||
export async function initRun (name, wasm) {
|
||||
const result = await wasm.waitReady();
|
||||
|
||||
console.log(`*** waitReady()=${result} for ${wasm.bridge.type}`);
|
||||
@@ -38,25 +39,31 @@ export function runAll (name, wasm) {
|
||||
|
||||
Object
|
||||
.entries(tests)
|
||||
.forEach(([name, test]) => {
|
||||
const timerId = `\t${name}`;
|
||||
.forEach(([describeName, tests]) => {
|
||||
describe(describeName, () => {
|
||||
Object
|
||||
.entries(tests)
|
||||
.forEach(([name, test]) => {
|
||||
const timerId = `\t${name}`;
|
||||
|
||||
count++;
|
||||
count++;
|
||||
|
||||
try {
|
||||
console.time(timerId);
|
||||
console.log();
|
||||
console.log(timerId);
|
||||
try {
|
||||
console.time(timerId);
|
||||
console.log();
|
||||
// console.log(timerId);
|
||||
|
||||
test(wasm);
|
||||
test(wasm);
|
||||
|
||||
console.timeEnd(timerId);
|
||||
} catch (error) {
|
||||
console.error();
|
||||
console.error(error);
|
||||
console.timeEnd(timerId);
|
||||
} catch (error) {
|
||||
console.error();
|
||||
console.error(error);
|
||||
|
||||
failed.push(name);
|
||||
}
|
||||
failed.push(name);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
if (failed.length) {
|
||||
@@ -64,17 +71,38 @@ export function runAll (name, wasm) {
|
||||
}
|
||||
}
|
||||
|
||||
export function runUnassisted (name, wasm) {
|
||||
console.log(`\n*** ${name}: Running tests`);
|
||||
export function runUnassisted (type, wasm) {
|
||||
console.log(`\n*** ${type}: Running tests`);
|
||||
|
||||
beforeAll(name, wasm)
|
||||
.then(() => runAll(name, wasm))
|
||||
// for these we are pass-through describe and it handlers
|
||||
globalThis.describe = (name, fn) => {
|
||||
console.log('\n', name);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
fn();
|
||||
};
|
||||
|
||||
globalThis.it = (name, fn) => {
|
||||
console.log(`\t${name}`);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
fn();
|
||||
};
|
||||
|
||||
console.time(type);
|
||||
|
||||
initRun(type, wasm)
|
||||
.then(() => {
|
||||
console.log(`\n*** ${name}: All passed`);
|
||||
runAll(type, wasm);
|
||||
console.log(`\n*** ${type}: All passed`);
|
||||
console.timeEnd(type);
|
||||
console.log();
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error.message, '\n');
|
||||
console.error(`\n*** ${type}: FAILED:`, error.message, '\n');
|
||||
console.timeEnd(type);
|
||||
console.log();
|
||||
process.exit(-1);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4,70 +4,82 @@
|
||||
import { assert, hexToU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
export function secp256k1Compress (wasm) {
|
||||
const pubkey = '0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af';
|
||||
const result = u8aToHex(wasm.secp256k1Compress(hexToU8a(pubkey)));
|
||||
it('compresses a known publicKey', () => {
|
||||
const pubkey = '0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af';
|
||||
const result = u8aToHex(wasm.secp256k1Compress(hexToU8a(pubkey)));
|
||||
|
||||
console.log('\tRES', result);
|
||||
// console.log('\tRES', result);
|
||||
|
||||
assert(result === '0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077', 'ERROR: secp256k1Compress does not match');
|
||||
assert(result === '0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077', 'ERROR: secp256k1Compress does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function secp256k1Expand (wasm) {
|
||||
const pubkey = '0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077';
|
||||
const result = u8aToHex(wasm.secp256k1Expand(hexToU8a(pubkey)));
|
||||
it('expands a known compressed key', () => {
|
||||
const pubkey = '0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077';
|
||||
const result = u8aToHex(wasm.secp256k1Expand(hexToU8a(pubkey)));
|
||||
|
||||
console.log('\tRES', result);
|
||||
// console.log('\tRES', result);
|
||||
|
||||
assert(result === '0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af', 'ERROR: secp256k1Expand does not match');
|
||||
assert(result === '0x04b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb1307763fe926c273235fd979a134076d00fd1683cbd35868cb485d4a3a640e52184af', 'ERROR: secp256k1Expand does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function secp256k1FromSeed1 (wasm) {
|
||||
const seckey = '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae';
|
||||
const result = u8aToHex(wasm.secp256k1FromSeed(hexToU8a(seckey)));
|
||||
it('creates a known pair from seed (1)', () => {
|
||||
const seckey = '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae';
|
||||
const result = u8aToHex(wasm.secp256k1FromSeed(hexToU8a(seckey)));
|
||||
|
||||
console.log('\tRES', result);
|
||||
// console.log('\tRES', result);
|
||||
|
||||
assert(result === '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae03fd8c74f795ced92064b86191cb2772b1e3a0947740aa0a5a6e379592471fd85b', 'ERROR: secp256k1FromSeed does not match');
|
||||
assert(result === '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae03fd8c74f795ced92064b86191cb2772b1e3a0947740aa0a5a6e379592471fd85b', 'ERROR: secp256k1FromSeed does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function secp256k1FromSeed2 (wasm) {
|
||||
// https://github.com/polkadot-js/wasm/issues/307
|
||||
const seckey = new Uint8Array([
|
||||
203, 109, 249, 222, 30, 252, 167, 163, 153, 138, 142,
|
||||
173, 78, 2, 21, 157, 95, 169, 156, 62, 13, 79,
|
||||
214, 67, 38, 103, 57, 11, 180, 114, 104, 84
|
||||
]);
|
||||
const expected = u8aToHex(new Uint8Array([
|
||||
203, 109, 249, 222, 30, 252, 167, 163, 153, 138, 142,
|
||||
173, 78, 2, 21, 157, 95, 169, 156, 62, 13, 79,
|
||||
214, 67, 38, 103, 57, 11, 180, 114, 104, 84, 2,
|
||||
10, 16, 145, 52, 31, 229, 102, 75, 250, 23, 130,
|
||||
213, 224, 71, 121, 104, 144, 104, 201, 22, 176, 76,
|
||||
179, 101, 236, 49, 83, 117, 86, 132, 217, 161
|
||||
]));
|
||||
const result = u8aToHex(wasm.secp256k1FromSeed(seckey));
|
||||
it('creates a known pair from seed (2)', () => {
|
||||
// https://github.com/polkadot-js/wasm/issues/307
|
||||
const seckey = new Uint8Array([
|
||||
203, 109, 249, 222, 30, 252, 167, 163, 153, 138, 142,
|
||||
173, 78, 2, 21, 157, 95, 169, 156, 62, 13, 79,
|
||||
214, 67, 38, 103, 57, 11, 180, 114, 104, 84
|
||||
]);
|
||||
const expected = u8aToHex(new Uint8Array([
|
||||
203, 109, 249, 222, 30, 252, 167, 163, 153, 138, 142,
|
||||
173, 78, 2, 21, 157, 95, 169, 156, 62, 13, 79,
|
||||
214, 67, 38, 103, 57, 11, 180, 114, 104, 84, 2,
|
||||
10, 16, 145, 52, 31, 229, 102, 75, 250, 23, 130,
|
||||
213, 224, 71, 121, 104, 144, 104, 201, 22, 176, 76,
|
||||
179, 101, 236, 49, 83, 117, 86, 132, 217, 161
|
||||
]));
|
||||
const result = u8aToHex(wasm.secp256k1FromSeed(seckey));
|
||||
|
||||
console.log('\tRES', result);
|
||||
// console.log('\tRES', result);
|
||||
|
||||
assert(result === expected, 'ERROR: secp256k1FromSeed does not match');
|
||||
assert(result === expected, 'ERROR: secp256k1FromSeed does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function secp256k1Recover (wasm) {
|
||||
const sig = '0x7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff';
|
||||
const msg = '0xa30b64ce1eedf409c8afb801d72c05234e64849ea538c15dd3c8cf4ffcf166c9';
|
||||
const result = u8aToHex(wasm.secp256k1Recover(hexToU8a(msg), hexToU8a(sig), 0));
|
||||
it('recovers a publicKey from signature', () => {
|
||||
const sig = '0x7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff';
|
||||
const msg = '0xa30b64ce1eedf409c8afb801d72c05234e64849ea538c15dd3c8cf4ffcf166c9';
|
||||
const result = u8aToHex(wasm.secp256k1Recover(hexToU8a(msg), hexToU8a(sig), 0));
|
||||
|
||||
console.log('\tRES', result);
|
||||
// console.log('\tRES', result);
|
||||
|
||||
assert(result === '0x028d13da15a02f3a70677339d51b14177ee9b49657662b35e56a9d9dee17db1d30', 'ERROR: secp256k1Recover does not match');
|
||||
assert(result === '0x028d13da15a02f3a70677339d51b14177ee9b49657662b35e56a9d9dee17db1d30', 'ERROR: secp256k1Recover does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function secp256k1Sign (wasm) {
|
||||
const sec = '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae';
|
||||
const msg = '0x68c731589a583d08b70861683b59ce3dd56284cb2f0da5b6cd83e6641dac3aab';
|
||||
const result = u8aToHex(wasm.secp256k1Sign(hexToU8a(msg), hexToU8a(sec)));
|
||||
it('creates a known signature', () => {
|
||||
const sec = '0x4380de832af797688026ce24f85204d508243f201650c1a134929e5458b7fbae';
|
||||
const msg = '0x68c731589a583d08b70861683b59ce3dd56284cb2f0da5b6cd83e6641dac3aab';
|
||||
const result = u8aToHex(wasm.secp256k1Sign(hexToU8a(msg), hexToU8a(sec)));
|
||||
|
||||
console.log('\tRES', result);
|
||||
// console.log('\tRES', result);
|
||||
|
||||
assert(result === '0xdf92f73d9f060cefacf187b5414491cb992998ace017fa48839b5cda3e264ba83b105adec9e9872647a7d8bb28855b45e22805aea3d097953cbb1391f671d13e01', 'ERROR: secp256k1Sign does not match');
|
||||
assert(result === '0xdf92f73d9f060cefacf187b5414491cb992998ace017fa48839b5cda3e264ba83b105adec9e9872647a7d8bb28855b45e22805aea3d097953cbb1391f671d13e01', 'ERROR: secp256k1Sign does not match');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -16,124 +16,152 @@ function randomPair (wasm) {
|
||||
}
|
||||
|
||||
export function sr25519PairFromSeed (wasm) {
|
||||
const pair = wasm.sr25519KeypairFromSeed(stringToU8a('12345678901234567890123456789012'));
|
||||
it('creates a known pair from a known seed', () => {
|
||||
const pair = wasm.sr25519KeypairFromSeed(stringToU8a('12345678901234567890123456789012'));
|
||||
|
||||
console.log('\tSEC', u8aToHex(pair.slice(0, 64)));
|
||||
console.log('\tPUB', u8aToHex(pair.slice(64)));
|
||||
// console.log('\tSEC', u8aToHex(pair.slice(0, 64)));
|
||||
// console.log('\tPUB', u8aToHex(pair.slice(64)));
|
||||
|
||||
assert(u8aToHex(pair) === '0xf0106660c3dda23f16daa9ac5b811b963077f5bc0af89f85804f0de8e424f050f98d66f39442506ff947fd911f18c7a7a5da639a63e8d3b4e233f74143d951c1741c08a06f41c596608f6774259bd9043304adfa5d3eea62760bd9be97634d63', 'ERROR: pairFromSeed() does not match');
|
||||
assert(u8aToHex(pair) === '0xf0106660c3dda23f16daa9ac5b811b963077f5bc0af89f85804f0de8e424f050f98d66f39442506ff947fd911f18c7a7a5da639a63e8d3b4e233f74143d951c1741c08a06f41c596608f6774259bd9043304adfa5d3eea62760bd9be97634d63', 'ERROR: pairFromSeed() does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function sr25519DevFromSeed (wasm) {
|
||||
const pair = wasm.sr25519KeypairFromSeed(hexToU8a('0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e'));
|
||||
it('creates a known development pair', () => {
|
||||
const pair = wasm.sr25519KeypairFromSeed(hexToU8a('0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e'));
|
||||
|
||||
console.log('\tSEC', u8aToHex(pair.slice(0, 64)));
|
||||
console.log('\tPUB', u8aToHex(pair.slice(64)));
|
||||
// console.log('\tSEC', u8aToHex(pair.slice(0, 64)));
|
||||
// console.log('\tPUB', u8aToHex(pair.slice(64)));
|
||||
|
||||
assert(u8aToHex(pair) === '0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a', 'ERROR: devFromSeed() does not match');
|
||||
assert(u8aToHex(pair) === '0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a', 'ERROR: devFromSeed() does not match');
|
||||
});
|
||||
}
|
||||
|
||||
export function sr25519VerifyExisting (wasm) {
|
||||
const PK = hexToU8a('0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d');
|
||||
const MESSAGE = stringToU8a('I hereby verify that I control 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');
|
||||
const SIGNATURE = hexToU8a('0xb83881d3bd7302981ee1c504fe5b7b394682927131fc0846fd616bb40fa14d02640fd2aca785b4cb57904765c2e4e75f59a7dd30154c209964369912091f6981');
|
||||
it('verifies a known signature', () => {
|
||||
const PK = hexToU8a('0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d');
|
||||
const MESSAGE = stringToU8a('I hereby verify that I control 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');
|
||||
const SIGNATURE = hexToU8a('0xb83881d3bd7302981ee1c504fe5b7b394682927131fc0846fd616bb40fa14d02640fd2aca785b4cb57904765c2e4e75f59a7dd30154c209964369912091f6981');
|
||||
|
||||
const isValid = wasm.sr25519Verify(SIGNATURE, MESSAGE, PK);
|
||||
const isValid = wasm.sr25519Verify(SIGNATURE, MESSAGE, PK);
|
||||
|
||||
console.log('\tRES', isValid);
|
||||
// console.log('\tRES', isValid);
|
||||
|
||||
assert(isValid, 'ERROR: Unable to verify signature');
|
||||
assert(isValid, 'ERROR: Unable to verify signature');
|
||||
});
|
||||
}
|
||||
|
||||
export function sr25519SignDeterministic (wasm) {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
const sig1 = u8aToHex(wasm.sr25519Sign(pk, sk, stringToU8a('this is a message')));
|
||||
const sig2 = u8aToHex(wasm.sr25519Sign(pk, sk, stringToU8a('this is a message')));
|
||||
it('creates non-deterministic signatures', () => {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
const sig1 = u8aToHex(wasm.sr25519Sign(pk, sk, stringToU8a('this is a message')));
|
||||
const sig2 = u8aToHex(wasm.sr25519Sign(pk, sk, stringToU8a('this is a message')));
|
||||
|
||||
console.log('\tSG1', sig1);
|
||||
console.log('\tSG2', sig2);
|
||||
// console.log('\tSG1', sig1);
|
||||
// console.log('\tSG2', sig2);
|
||||
|
||||
assert(sig1 !== sig2, 'ERROR: Signatures are deterministic');
|
||||
assert(sig1 !== sig2, 'ERROR: Signatures are deterministic');
|
||||
});
|
||||
}
|
||||
|
||||
export function sr25519SignAndVerify (wasm) {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
const signature = wasm.sr25519Sign(pk, sk, stringToU8a('this is a message'));
|
||||
const isValid = wasm.sr25519Verify(signature, stringToU8a('this is a message'), pk);
|
||||
it('verifies a created signature', () => {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
const signature = wasm.sr25519Sign(pk, sk, stringToU8a('this is a message'));
|
||||
const isValid = wasm.sr25519Verify(signature, stringToU8a('this is a message'), pk);
|
||||
|
||||
console.log('\tSIG', u8aToHex(signature));
|
||||
console.log('\tRES', isValid);
|
||||
// console.log('\tSIG', u8aToHex(signature));
|
||||
// console.log('\tRES', isValid);
|
||||
|
||||
assert(isValid, 'ERROR: Unable to verify signature');
|
||||
assert(isValid, 'ERROR: Unable to verify signature');
|
||||
});
|
||||
}
|
||||
|
||||
export function sr25519DeriveHard (wasm) {
|
||||
const [pair] = randomPair(wasm);
|
||||
const derived = wasm.sr25519DeriveKeypairHard(pair, hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'));
|
||||
it('derives using a hard path', () => {
|
||||
const [pair] = randomPair(wasm);
|
||||
const derived = wasm.sr25519DeriveKeypairHard(pair, hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'));
|
||||
|
||||
console.log('\tSEC', u8aToHex(derived.slice(0, 64)));
|
||||
console.log('\tPUB', u8aToHex(derived.slice(64)));
|
||||
// console.log('\tSEC', u8aToHex(derived.slice(0, 64)));
|
||||
// console.log('\tPUB', u8aToHex(derived.slice(64)));
|
||||
|
||||
assert(derived.length === 96);
|
||||
});
|
||||
}
|
||||
|
||||
export function sr25519DeriveHardKnown (wasm) {
|
||||
const derived = wasm.sr25519DeriveKeypairHard(hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x14416c6963650000000000000000000000000000000000000000000000000000'));
|
||||
const publicKey = u8aToHex(derived.slice(64));
|
||||
it('derives a known hard key', () => {
|
||||
const derived = wasm.sr25519DeriveKeypairHard(hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x14416c6963650000000000000000000000000000000000000000000000000000'));
|
||||
const publicKey = u8aToHex(derived.slice(64));
|
||||
|
||||
console.log('\tSEC', u8aToHex(derived.slice(0, 64)));
|
||||
console.log('\tPUB', publicKey);
|
||||
// console.log('\tSEC', u8aToHex(derived.slice(0, 64)));
|
||||
// console.log('\tPUB', publicKey);
|
||||
|
||||
assert(publicKey === '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', 'Unmatched resulting public keys');
|
||||
assert(publicKey === '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', 'Unmatched resulting public keys');
|
||||
});
|
||||
}
|
||||
|
||||
export function sr25519DeriveSoft (wasm) {
|
||||
const [pair] = randomPair(wasm);
|
||||
const derived = wasm.sr25519DeriveKeypairSoft(pair, hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'));
|
||||
it('derives using a soft path', () => {
|
||||
const [pair] = randomPair(wasm);
|
||||
const derived = wasm.sr25519DeriveKeypairSoft(pair, hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'));
|
||||
|
||||
console.log('\tSEC', u8aToHex(derived.slice(0, 64)));
|
||||
console.log('\tPUB', u8aToHex(derived.slice(64)));
|
||||
// console.log('\tSEC', u8aToHex(derived.slice(0, 64)));
|
||||
// console.log('\tPUB', u8aToHex(derived.slice(64)));
|
||||
|
||||
assert(derived.length === 96);
|
||||
});
|
||||
}
|
||||
|
||||
export function sr25519DeriveSoftKnown (wasm) {
|
||||
const derived = wasm.sr25519DeriveKeypairSoft(hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'));
|
||||
const publicKey = u8aToHex(derived.slice(64));
|
||||
it('derives a known soft key', () => {
|
||||
const derived = wasm.sr25519DeriveKeypairSoft(hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'));
|
||||
const publicKey = u8aToHex(derived.slice(64));
|
||||
|
||||
console.log('\tSEC', u8aToHex(derived.slice(0, 64)));
|
||||
console.log('\tPUB', publicKey);
|
||||
// console.log('\tSEC', u8aToHex(derived.slice(0, 64)));
|
||||
// console.log('\tPUB', publicKey);
|
||||
|
||||
assert(publicKey === '0x40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a', 'Unmatched resulting public keys');
|
||||
assert(publicKey === '0x40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a', 'Unmatched resulting public keys');
|
||||
});
|
||||
}
|
||||
|
||||
export function sr25519DeriveSoftPubkey (wasm) {
|
||||
const derived = u8aToHex(wasm.sr25519DerivePublicSoft(hexToU8a('0x46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000')));
|
||||
it('derives a known soft publicKey', () => {
|
||||
const derived = u8aToHex(wasm.sr25519DerivePublicSoft(hexToU8a('0x46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000')));
|
||||
|
||||
console.log('\tPUB', derived);
|
||||
// console.log('\tPUB', derived);
|
||||
|
||||
assert(derived === '0x40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a', 'Unmatched resulting public keys');
|
||||
assert(derived === '0x40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a', 'Unmatched resulting public keys');
|
||||
});
|
||||
}
|
||||
|
||||
export function sr25519KeyAgreement (wasm) {
|
||||
const pair1 = wasm.sr25519KeypairFromSeed(hexToU8a('0x3b44c558f9a8f3dc9690d53088558c1ba2529b677e316c6054d1852595b004af'));
|
||||
const pair2 = wasm.sr25519KeypairFromSeed(hexToU8a('0x923b80f79c6981fe756272128ec236eb510ae016dd20bdccbd77a9416b7ab94e'));
|
||||
const [, pk1, sk1] = extractKeys(pair1);
|
||||
const [, pk2, sk2] = extractKeys(pair2);
|
||||
it('allows for agreements', () => {
|
||||
const pair1 = wasm.sr25519KeypairFromSeed(hexToU8a('0x3b44c558f9a8f3dc9690d53088558c1ba2529b677e316c6054d1852595b004af'));
|
||||
const pair2 = wasm.sr25519KeypairFromSeed(hexToU8a('0x923b80f79c6981fe756272128ec236eb510ae016dd20bdccbd77a9416b7ab94e'));
|
||||
const [, pk1, sk1] = extractKeys(pair1);
|
||||
const [, pk2, sk2] = extractKeys(pair2);
|
||||
|
||||
assert(u8aToHex(wasm.sr25519Agree(pk1, sk2)) === '0xfa7b90001b790fe42ff78b8cd86f6cf7a7c0a70b72f6b4c771b5d67536450222', 'Unmatched agreement keys');
|
||||
assert(u8aToHex(wasm.sr25519Agree(pk2, sk1)) === '0xfa7b90001b790fe42ff78b8cd86f6cf7a7c0a70b72f6b4c771b5d67536450222', 'Unmatched agreement keys');
|
||||
assert(u8aToHex(wasm.sr25519Agree(pk1, sk2)) === '0xfa7b90001b790fe42ff78b8cd86f6cf7a7c0a70b72f6b4c771b5d67536450222', 'Unmatched agreement keys');
|
||||
assert(u8aToHex(wasm.sr25519Agree(pk2, sk1)) === '0xfa7b90001b790fe42ff78b8cd86f6cf7a7c0a70b72f6b4c771b5d67536450222', 'Unmatched agreement keys');
|
||||
|
||||
for (let i = 0; i < 256; i++) {
|
||||
const [, pk1, sk1] = randomPair(wasm);
|
||||
const [, pk2, sk2] = randomPair(wasm);
|
||||
for (let i = 0; i < 256; i++) {
|
||||
const [, pk1, sk1] = randomPair(wasm);
|
||||
const [, pk2, sk2] = randomPair(wasm);
|
||||
|
||||
assert(u8aToHex(wasm.sr25519Agree(pk1, sk2)) === u8aToHex(wasm.sr25519Agree(pk2, sk1)), 'Unmatched agreement keys');
|
||||
}
|
||||
assert(u8aToHex(wasm.sr25519Agree(pk1, sk2)) === u8aToHex(wasm.sr25519Agree(pk2, sk1)), 'Unmatched agreement keys');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function sr25519Benchmark (wasm) {
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
it('runs a verification benchmark', () => {
|
||||
const MESSAGE = stringToU8a('this is a message');
|
||||
|
||||
for (let i = 0; i < 256; i++) {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
for (let i = 0; i < 256; i++) {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
|
||||
assert(wasm.sr25519Verify(wasm.sr25519Sign(pk, sk, MESSAGE), MESSAGE, pk), 'ERROR: Unable to verify signature');
|
||||
}
|
||||
assert(wasm.sr25519Verify(wasm.sr25519Sign(pk, sk, MESSAGE), MESSAGE, pk), 'ERROR: Unable to verify signature');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -14,36 +14,42 @@ function randomPair (wasm) {
|
||||
}
|
||||
|
||||
export function vrfSignAndVerifyCompat (wasm) {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
const outAndProof = wasm.vrfSign(sk, stringToU8a('my VRF context'), stringToU8a('this is a message'), new Uint8Array());
|
||||
const isValid = wasm.vrfVerify(pk, stringToU8a('my VRF context'), stringToU8a('this is a message'), new Uint8Array(), outAndProof);
|
||||
it('can sign and verify (1)', () => {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
const outAndProof = wasm.vrfSign(sk, stringToU8a('my VRF context'), stringToU8a('this is a message'), new Uint8Array());
|
||||
const isValid = wasm.vrfVerify(pk, stringToU8a('my VRF context'), stringToU8a('this is a message'), new Uint8Array(), outAndProof);
|
||||
|
||||
console.log('\tVRF', u8aToHex(outAndProof));
|
||||
console.log('\tRES', isValid);
|
||||
// console.log('\tVRF', u8aToHex(outAndProof));
|
||||
// console.log('\tRES', isValid);
|
||||
|
||||
assert(isValid, 'ERROR: Unable to verify VRF output & proof');
|
||||
assert(isValid, 'ERROR: Unable to verify VRF output & proof');
|
||||
});
|
||||
}
|
||||
|
||||
export function vrfSignAndVerify (wasm) {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
const outAndProof = wasm.vrfSign(sk, stringToU8a('my VRF context'), stringToU8a('this is a message'), stringToU8a('extra param'));
|
||||
const isValid = wasm.vrfVerify(pk, stringToU8a('my VRF context'), stringToU8a('this is a message'), stringToU8a('extra param'), outAndProof);
|
||||
it('can sign and verify (2)', () => {
|
||||
const [, pk, sk] = randomPair(wasm);
|
||||
const outAndProof = wasm.vrfSign(sk, stringToU8a('my VRF context'), stringToU8a('this is a message'), stringToU8a('extra param'));
|
||||
const isValid = wasm.vrfVerify(pk, stringToU8a('my VRF context'), stringToU8a('this is a message'), stringToU8a('extra param'), outAndProof);
|
||||
|
||||
console.log('\tVRF', u8aToHex(outAndProof));
|
||||
console.log('\tRES', isValid);
|
||||
// console.log('\tVRF', u8aToHex(outAndProof));
|
||||
// console.log('\tRES', isValid);
|
||||
|
||||
assert(isValid, 'ERROR: Unable to verify VRF extra output & proof');
|
||||
assert(isValid, 'ERROR: Unable to verify VRF extra output & proof');
|
||||
});
|
||||
}
|
||||
|
||||
export function vrfSignAndVerifyDeterministic (wasm) {
|
||||
const [,, sk] = randomPair(wasm);
|
||||
const outAndProof1 = wasm.vrfSign(sk, stringToU8a('my VRF context'), stringToU8a('this is a message'), stringToU8a('extra param'));
|
||||
const outAndProof2 = wasm.vrfSign(sk, stringToU8a('my VRF context'), stringToU8a('this is a message'), stringToU8a('extra param'));
|
||||
const sig1 = u8aToHex(outAndProof1.slice(0, 32));
|
||||
const sig2 = u8aToHex(outAndProof2.slice(0, 32));
|
||||
it('has non-deterministic outputs', () => {
|
||||
const [,, sk] = randomPair(wasm);
|
||||
const outAndProof1 = wasm.vrfSign(sk, stringToU8a('my VRF context'), stringToU8a('this is a message'), stringToU8a('extra param'));
|
||||
const outAndProof2 = wasm.vrfSign(sk, stringToU8a('my VRF context'), stringToU8a('this is a message'), stringToU8a('extra param'));
|
||||
const sig1 = u8aToHex(outAndProof1.slice(0, 32));
|
||||
const sig2 = u8aToHex(outAndProof2.slice(0, 32));
|
||||
|
||||
console.log('\tSG1', sig1);
|
||||
console.log('\tSG2', sig2);
|
||||
// console.log('\tSG1', sig1);
|
||||
// console.log('\tSG2', sig2);
|
||||
|
||||
assert(sig1 === sig2, 'ERROR: VRF extra outputs are non-deterministic');
|
||||
assert(sig1 === sig2, 'ERROR: VRF extra outputs are non-deterministic');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,16 +2,24 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import * as wasm from '../build-deno/mod.ts';
|
||||
import { beforeAll, tests } from './all/index.js';
|
||||
import { initRun, tests } from './all/index.js';
|
||||
|
||||
declare const globalThis;
|
||||
declare const Deno: {
|
||||
test: (name: string, test: () => unknown) => unknown;
|
||||
}
|
||||
|
||||
await beforeAll('wasm', wasm);
|
||||
await initRun('wasm', wasm);
|
||||
|
||||
// We use it to denote the tests
|
||||
globalThis.it = (name: string, fn: () => void) => Deno.test(name, () => fn());
|
||||
|
||||
Object
|
||||
.entries<(wasm: unknown) => void>(tests)
|
||||
.forEach(([name, test]) => {
|
||||
Deno.test(name, () => test(wasm));
|
||||
.entries<{ [key: string]: (wasm: unknown) => void }>(tests)
|
||||
.forEach(([describeName, tests]) => {
|
||||
console.log('***', describeName);
|
||||
|
||||
Object
|
||||
.values(tests)
|
||||
.forEach((test) => test(wasm));
|
||||
});
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import * as wasm from '../build';
|
||||
import { beforeAll as beforeAllFn, tests } from './all/index.js';
|
||||
import { initRun, tests } from './all/index.js';
|
||||
|
||||
describe('wasm-crypto', () => {
|
||||
beforeAll(async () => {
|
||||
await beforeAllFn('wasm', wasm);
|
||||
});
|
||||
beforeAll(() => initRun('wasm', wasm));
|
||||
|
||||
it.each(Object.keys(tests))('%s', (name) =>
|
||||
tests[name](wasm)
|
||||
);
|
||||
describe.each(Object.keys(tests))('%s', (name) => {
|
||||
Object
|
||||
.values(tests[name])
|
||||
.forEach((fn) => fn(wasm));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -21,9 +21,8 @@ export function resolve (specifier, context, nextResolve) {
|
||||
.replace(/\/wasm-crypto-init\/build$/, '/wasm-crypto-init/build/wasm.js')
|
||||
.replace(/\/wasm-crypto-init\/build\/asm$/, '/wasm-crypto-init/build/asm.js')
|
||||
.replace(/\/wasm-crypto\/build\/initOnlyAsm$/, '/wasm-crypto/build/initOnlyAsm.js')
|
||||
.replace(/\/packageInfo$/, '/packageInfo.js')
|
||||
.replace(/\/build\/packageInfo$/, '/build/packageInfo.js')
|
||||
.replace(/\/build$/, '/build/index.js')
|
||||
|
||||
)
|
||||
).href
|
||||
};
|
||||
|
||||
@@ -2019,9 +2019,9 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@polkadot/dev@npm:^0.68.36":
|
||||
version: 0.68.36
|
||||
resolution: "@polkadot/dev@npm:0.68.36"
|
||||
"@polkadot/dev@npm:^0.68.37":
|
||||
version: 0.68.37
|
||||
resolution: "@polkadot/dev@npm:0.68.37"
|
||||
dependencies:
|
||||
"@babel/cli": ^7.20.7
|
||||
"@babel/core": ^7.20.12
|
||||
@@ -2085,7 +2085,7 @@ __metadata:
|
||||
jsdom: ^21.1.0
|
||||
madge: ^6.0.0
|
||||
prettier: ^2.8.4
|
||||
rollup: ^3.15.0
|
||||
rollup: ^3.17.1
|
||||
rollup-plugin-cleanup: ^3.2.1
|
||||
typescript: ^4.9.5
|
||||
webpack: ^5.75.0
|
||||
@@ -2124,7 +2124,7 @@ __metadata:
|
||||
polkadot-exec-swc: scripts/polkadot-exec-swc.mjs
|
||||
polkadot-exec-tsc: scripts/polkadot-exec-tsc.mjs
|
||||
polkadot-exec-webpack: scripts/polkadot-exec-webpack.mjs
|
||||
checksum: 90af4e228c910dc47d222e9166c46e2fcd0b74227763ffb06416d3669a689604b235adf64e22107617a26e8e1bd516b0fb73160dfb596a3d35fe229893016ca9
|
||||
checksum: 8ac76e1e7513cd2b608cf6526e3c3005f56c081e9cd1b6db33a04bf59223e82fceb1081b9fb9b629d9f46bed712b05914dc962c8ebe97b3bc72b6369f9901c93
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -10955,9 +10955,9 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"rollup@npm:^3.15.0":
|
||||
version: 3.15.0
|
||||
resolution: "rollup@npm:3.15.0"
|
||||
"rollup@npm:^3.17.1":
|
||||
version: 3.17.1
|
||||
resolution: "rollup@npm:3.17.1"
|
||||
dependencies:
|
||||
fsevents: ~2.3.2
|
||||
dependenciesMeta:
|
||||
@@ -10965,7 +10965,7 @@ __metadata:
|
||||
optional: true
|
||||
bin:
|
||||
rollup: dist/bin/rollup
|
||||
checksum: 1a75b09503f705b594dcfebf65b9e1ca4536e3facdf5716e10d4272697b4bb7e6bd8ab35be1b3fe1271f864d4c4a75d012bd77cbe7a55579d5138258b9e85c23
|
||||
checksum: 3c6076eba228a6623b886929154ebbb9ddfb6a044e567456eeb6257b5235f4fd740655836b539155b1b25697f38f5d189bf81b9ce0624ce5aafc6c8620bd80c0
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -10974,7 +10974,7 @@ __metadata:
|
||||
resolution: "root-workspace-0b6124@workspace:."
|
||||
dependencies:
|
||||
"@babel/core": ^7.20.12
|
||||
"@polkadot/dev": ^0.68.36
|
||||
"@polkadot/dev": ^0.68.37
|
||||
"@polkadot/util": ^10.4.1
|
||||
"@polkadot/x-randomvalues": ^10.4.1
|
||||
"@types/jest": ^29.4.0
|
||||
|
||||
Reference in New Issue
Block a user