// WebCrypto en Node const { webcrypto } = require('crypto'); global.crypto = webcrypto; const fs=require("fs"); const { B32, SECP, deriveChildPubkey, ripemd160, hash160, toBech32, deriveAddresses } = require('/tmp/cripto/crypto.js'); const hex = a => Array.from(a).map(b=>b.toString(16).padStart(2,'0')).join(''); let pass=0, fail=0; const check=(nombre, got, want)=>{ const ok = got===want; console.log(` ${ok?'✓':'✗'} ${nombre}`); if(!ok){ console.log(` obtenido: ${got}`); console.log(` esperado: ${want}`); fail++; } else pass++; }; (async () => { console.log("=== 1. RIPEMD-160 (vectores del estándar) ==="); const enc = s => new TextEncoder().encode(s); check('""', hex(ripemd160(enc(""))), "9c1185a5c5e9fc54612808977ee8f548b2258d31"); check('"a"', hex(ripemd160(enc("a"))), "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"); check('"abc"', hex(ripemd160(enc("abc"))), "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"); check('"message digest"', hex(ripemd160(enc("message digest"))), "5d0689ef49d2fae572b881b123a85ffa21595f36"); check('abcdefghijklmnopqrstuvwxyz', hex(ripemd160(enc("abcdefghijklmnopqrstuvwxyz"))), "f71c27109c692c1b56bbdceb5b9d2865b3708dbc"); check('1M x "a"', hex(ripemd160(enc("a".repeat(1000000)))), "52783243c1697bdbe16d37f97f68f08325dc1528"); console.log("\n=== 2. secp256k1: G y múltiplos conocidos ==="); const G = SECP.G; check('G comprimido', hex(SECP.compress(G)), "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"); check('2G', hex(SECP.compress(SECP.mulPoint(2n, G))), "02c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5"); check('3G', hex(SECP.compress(SECP.mulPoint(3n, G))), "02f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9"); // n-1 * G = -G (mismo x, y opuesta) const nm1 = SECP.mulPoint(SECP.N - 1n, G); check('(n-1)G tiene la x de G', nm1[0].toString(16), G[0].toString(16)); check('(n-1)G tiene y opuesta', ((nm1[1] + G[1]) % SECP.P).toString(), "0"); console.log("\n=== 3. compress → decompress (ida y vuelta) ==="); for (const k of [1n, 2n, 7n, 12345n, 0xdeadbeefn]) { const pt = SECP.mulPoint(k, G); const c = SECP.compress(pt); const d = SECP.decompress(c); check(`k=${k}`, hex(SECP.compress(d)), hex(c)); } console.log(`\nRESULTADO: ${pass} correctas, ${fail} incorrectas`); })();