Files
salvium-rs/test/debug-commitment-extra.js
T
Matt Hess 7f01cafc62 Crypto layer consolidation: eliminate JS scalar ops, add cn_scan + RCT batch verify
Crypto Backend Refactoring
   ──────────────────────────
   - Delete src/ed25519.js — all scalar/point operations now route through
     the crypto provider (WASM/FFI/JSI backends only)
   - Remove duplicate JS BigInt implementations of scReduce32, scReduce64,
     scalarAdd, scalarMul from scanning.js, carrot.js, and carrot-scanning.js;
     delegate to Rust backend via crypto/index.js
   - Remove debug/test exports from index.js (randomPoint, testDouble,
     getBasePoint, isOnCurve, etc.) that were only used during development
   - Rebuild WASM binary (476KB → 487KB) with updated Rust crate

   Consolidated CryptoNote Scanner (cn_scan)
   ─────────────────────────────────────────
   - New Rust module crates/salvium-crypto/src/cn_scan.rs replaces 5-12
     individual FFI round-trips per output with a single native call:
     view tag check → derive subaddress pubkey → subaddress map lookup →
     amount decryption → commitment mask → key image generation
   - FFI wrapper salvium_cn_scan_output in ffi.rs + C header declaration
   - FFI backend scanCnOutput() with full subaddress map marshaling
     (32-byte key + u32 major/minor LE per entry) and JSON result parsing
   - JSI backend delegation via this.native.cnScanOutput()
   - wallet-sync.js _scanCNOutput() tries native path first when available
     (FFI/JSI), falls through to existing JS pipeline for WASM/JS backends
   - Change pub(crate) visibility on subaddress.rs cn_subaddress_secret_key
   - 8 Rust unit tests covering view tag, amount, commitment mask,
     subaddress matching, and key image generation
   - Verified identical results: WASM (JS fallback) and FFI (native cn_scan)
     produce same 964 outputs at same chain height; FFI is 3x faster sync
     (0.8s vs 2.5s) with 12x less heap (12MB vs 150MB)

   RCT Batch Signature Verification
   ─────────────────────────────────
   - New Rust module crates/salvium-crypto/src/rct_verify.rs — single-call
     verification of all ring signatures in a transaction (CLSAG + TCLSAG),
     avoiding N individual JS↔Rust boundary crossings
   - Computes pre-MLSAG message hash matching C++ get_pre_mlsag_hash
   - FFI export salvium_verify_rct_signatures with flat byte array interface
   - FFI backend verifyRctSignatures() method
   - JS backend stub returns null (validation.js handles JS fallback)
   - validation.js: 200+ lines of RCT verification logic including
     flattenKeyImages, packTclsagSigsFlat, packClsagSigsFlat helpers

   Transaction Expansion
   ─────────────────────
   - transaction.js: add expandTransaction() matching C++ expand_transaction_2
     (copies key images from prefix inputs into TCLSAG/CLSAG signature structs)
   - New test/expand-transaction.test.js (634 lines)
   - New test/rct-verify-testnet.test.js (430 lines)

   Mining Resilience
   ─────────────────
   - salvium-miner main.rs: retry get_info and get_block_template up to 5
     times with 2s delay for transient daemon errors
   - full-testnet.js mineTo(): retry miner up to 3 times with 3s delay,
     check for partial progress between attempts

   Testnet Tooling
   ───────────────
   - sync-only.js: CRYPTO_BACKEND env var for A/B testing (wasm vs ffi)
   - full-testnet.js: daemon URL update (node12.whiskymine.io)
   - Debug scripts for cn_scan development (debug-cn-scan/marshal/match/wasm)
   - Android .gitignore and build-bundle.sh for mobile builds
2026-02-14 17:02:35 +00:00

122 lines
5.1 KiB
JavaScript

#!/usr/bin/env bun
/**
* Check if SalviumOne commitments have extra components beyond mask*G + amount*H.
* If outPk != mask*G + amount*H, there might be an asset tag component.
*/
import { setCryptoBackend, commit, scalarMultBase } from '../src/crypto/index.js';
import { getCryptoBackend } from '../src/crypto/provider.js';
import { DaemonRPC } from '../src/rpc/daemon.js';
import { parseTransaction } from '../src/transaction/parsing.js';
await setCryptoBackend('wasm');
function hexToBytes(hex) {
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < bytes.length; i++) bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
return bytes;
}
function bytesToHex(bytes) {
return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
}
const daemon = new DaemonRPC({ url: 'http://node12.whiskymine.io:29081' });
const backend = getCryptoBackend();
// Test with one of our problematic TXs (rctType 9, SalviumOne)
const txHash = 'd2ad187cc0dde491ae6134c8ad2df9188646859ecf2974271375f5257a51ada2';
const txResp = await daemon.getTransactions([txHash], true, false);
const txData = txResp.result?.txs?.[0] || txResp.txs?.[0];
const parsed = parseTransaction(hexToBytes(txData.as_hex));
console.log(`TX: ${txHash.slice(0,16)}... rctType=${parsed.rct?.type}`);
console.log(`Outputs: ${parsed.prefix?.vout?.length}`);
// Also check a KNOWN GOOD rctType=6 (BulletproofPlus) TX for comparison
// Find one from our wallet...
import { MemoryStorage } from '../src/wallet-store.js';
import { readFileSync } from 'fs';
const storage = new MemoryStorage();
storage.load(JSON.parse(readFileSync(`${process.env.HOME}/testnet-wallet/wallet-a-sync.json`, 'utf-8')));
const allOutputs = await storage.getOutputs({ isSpent: false });
// Find a non-CARROT output with commitment and mask
const cnOutput = allOutputs.find(o => !o.isCarrot && o.commitment && o.mask);
if (cnOutput) {
console.log(`\n=== CryptoNote output (non-CARROT) for comparison ===`);
console.log(`TX: ${cnOutput.txHash?.slice(0,16)}... block=${cnOutput.blockHeight}`);
console.log(`Amount: ${cnOutput.amount}`);
console.log(`AssetType: ${cnOutput.assetType}`);
const maskBytes = hexToBytes(cnOutput.mask);
const c = commit(BigInt(cnOutput.amount), maskBytes);
console.log(`commit(amount, mask): ${bytesToHex(c)}`);
console.log(`stored commitment: ${cnOutput.commitment}`);
console.log(`MATCH: ${bytesToHex(c) === cnOutput.commitment}`);
}
// Now look at the salvium_data for our rctType 9 TX
console.log(`\n=== SalviumOne TX salvium_data ===`);
console.log(`salvium_data present: ${!!parsed.rct?.salvium_data}`);
if (parsed.rct?.salvium_data) {
const sd = parsed.rct.salvium_data;
console.log(` Keys: ${Object.keys(sd).join(', ')}`);
// Look for any asset-related data
for (const [key, val] of Object.entries(sd)) {
if (val instanceof Uint8Array) {
console.log(` ${key}: ${bytesToHex(val).slice(0,64)}... (${val.length} bytes)`);
} else if (Array.isArray(val)) {
console.log(` ${key}: [${val.length} items]`);
for (const item of val.slice(0, 3)) {
if (item instanceof Uint8Array) {
console.log(` ${bytesToHex(item).slice(0,64)}... (${item.length} bytes)`);
} else if (typeof item === 'object') {
console.log(` ${JSON.stringify(item).slice(0,80)}`);
} else {
console.log(` ${item}`);
}
}
} else {
console.log(` ${key}: ${JSON.stringify(val).slice(0,80)}`);
}
}
}
// Also fetch the TX as JSON from daemon to see all fields
const blockResp = await daemon.getBlock({ height: 34554 });
const blockJson = JSON.parse(blockResp.result.json);
console.log(`\n=== Block 34554 regular TXs ===`);
const txHashes = blockJson.tx_hashes || [];
console.log(`TX hashes: ${txHashes.length}`);
// Get JSON representation of our TX
const txJsonResp = await daemon.getTransactions([txHash], { decode_as_json: true, prune: false });
const txJsonData = txJsonResp.result?.txs?.[0] || txJsonResp.txs?.[0];
if (txJsonData?.as_json) {
const j = typeof txJsonData.as_json === 'string' ? JSON.parse(txJsonData.as_json) : txJsonData.as_json;
console.log(`\n=== TX JSON rct_signatures ===`);
console.log(` type: ${j.rct_signatures?.type}`);
console.log(` txnFee: ${j.rct_signatures?.txnFee}`);
const outPk = j.rct_signatures?.outPk;
if (outPk) {
console.log(` outPk: ${JSON.stringify(outPk).slice(0,120)}`);
}
const ecdhInfo = j.rct_signatures?.ecdhInfo;
if (ecdhInfo) {
console.log(` ecdhInfo: ${JSON.stringify(ecdhInfo).slice(0,120)}`);
}
// Check for any extra fields in rct_signatures
const knownFields = ['type', 'txnFee', 'outPk', 'ecdhInfo', 'pseudoOuts', 'p_r'];
const extra = Object.keys(j.rct_signatures || {}).filter(k => !knownFields.includes(k));
if (extra.length) {
console.log(` Extra rct fields: ${extra.join(', ')}`);
for (const k of extra) {
console.log(` ${k}: ${JSON.stringify(j.rct_signatures[k]).slice(0,100)}`);
}
}
// Check salvium_data in JSON
if (j.rct_signatures?.salvium_data) {
console.log(`\n=== salvium_data from JSON ===`);
console.log(JSON.stringify(j.rct_signatures.salvium_data, null, 2).slice(0, 500));
}
}