[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6H021D6M6EREBEY01KA4WTY":3,"skill-same:01M6H021D6M6EREBEY01KA4WTY":71},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":16,"sourceHtmlUrl":17,"categories":18,"keywords":19,"publishedAt":29,"linkOnly":30,"markdownContent":31,"markdownBody":32,"frontMatter":33,"refName":34,"commitSha":35,"skillDirectory":36,"forksCount":37,"repositoryHtmlUrl":38,"repositoryHomepageUrl":6,"ownerHtmlUrl":39,"ownerBlogUrl":40,"ownerDescription":41,"updatedAt":42,"install":43,"resources":56},"01M6H021D6M6EREBEY01KA4WTY","rounding-direction-truncation",null,"Use when auditing integer maths in a contract that divides, multiplies with scaling, or converts between decimals. Checks each division rounds in the protocol's favour and guards against truncation to zero.","jiayaoqijia\u002Fcryptoskill","jiayaoqijia","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F1453879?v=4","01M6GMYTX7Q7S504VC6E756ENR","YQ","01M6GMYV1QKJ1CC75MTTGT9PR0",81,"MIT","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fpayments\u002F0xklen-rounding-direction-truncation\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fpayments\u002F0xklen-rounding-direction-truncation\u002FSKILL.md",[],[20,21,22,23,24,25,26,27,28],"direction","rounding direction truncation","protocol","ZeRO","use","scaling","contract","with","when","2026-11-10 10:12:30",false,"---\nname: rounding-direction-truncation\ndescription: Use when auditing integer maths in a contract that divides, multiplies with scaling, or converts between decimals. Checks each division rounds in the protocol's favour and guards against truncation to zero.\n---\n\n# Rounding direction and truncation\n\nSolidity integer division truncates toward zero, so every `a \u002F b` silently favours one party — the review is to decide which party and whether that is the safe one.\n\n## Procedure\n\n1. Find all divisions and scale conversions: `grep -nE '\u002F *[0-9a-zA-Z_(]|\u002F 1e|mulDiv|wadDiv|rayDiv' -r src\u002F`.\n2. For each, name the direction that is *safe*: shares minted to a user round DOWN, assets paid out to a user round DOWN, debt owed by a user rounds UP.\n3. Confirm the wrap: in OpenZeppelin `Math.mulDiv`, pass `Math.Rounding.Floor` when the user receives the result and `Math.Rounding.Ceil` when the protocol receives it.\n4. Check the multiply-before-divide rule: `(a * b) \u002F c` must not overflow; use `mulDiv`, which widens to 512-bit.\n5. Hunt truncation-to-zero: any `amount \u002F rate` where `rate > amount` returns 0, letting a user pay zero for a non-zero asset.\n6. Check fee maths: `fee = amount * feeBps \u002F 10_000` rounds down, so the protocol under-collects; verify a dust attacker cannot loop with `amount = 1` and pay `fee = 0`.\n7. Test the boundary with a fuzz test that asserts monotonicity:\n\n```solidity\nfunction testFuzz_noFreeShares(uint96 assets) public {\n    uint256 shares = vault.previewDeposit(assets);\n    if (assets > 0) assertGt(shares + vault.totalSupply(), 0);\n}\n```\n\n8. Confirm rounding is applied *after* aggregation, not per-leg, in multi-hop conversion functions.\n9. Verify a decimal conversion (`1e6` USDC to `1e18` shares) uses `10 ** (18 - 6)` and cannot be reversed by rounding.\n10. Record each division, its direction, and the party that benefits.\n\n## Pitfalls\n\n- `previewX` (view) and `X` (state-changing) use different rounding; integrators trust the wrong one and over-withdraw.\n- `mulDiv` default rounding is Floor; using it for a debt calculation rounds the attacker's debt down every block.\n- Percentage fee computed on a per-second drip: truncation to zero makes a flash-loan in-out free in the same block.\n- Casting `uint256` to `uint128`\u002F`int128` without a bounds check wraps silently.\n- Combining two truncated values (`a\u002Fb + c\u002Fd`) when `(a*d + b*c)\u002F(b*d)` was intended.\n\n## Verification\n\n    forge test --match-test \"testFuzz.*[Rr]ound\" -vv\n\nPass: the fuzz run reports 256+ runs with no counterexample. Add `fail_on_revert = true` under `[fuzz]` in `foundry.toml` so a revert is a failure, not a skip.\n\nReport each division site, its rounding direction, and the benefiting party.\n","# Rounding direction and truncation\n\nSolidity integer division truncates toward zero, so every `a \u002F b` silently favours one party — the review is to decide which party and whether that is the safe one.\n\n## Procedure\n\n1. Find all divisions and scale conversions: `grep -nE '\u002F *[0-9a-zA-Z_(]|\u002F 1e|mulDiv|wadDiv|rayDiv' -r src\u002F`.\n2. For each, name the direction that is *safe*: shares minted to a user round DOWN, assets paid out to a user round DOWN, debt owed by a user rounds UP.\n3. Confirm the wrap: in OpenZeppelin `Math.mulDiv`, pass `Math.Rounding.Floor` when the user receives the result and `Math.Rounding.Ceil` when the protocol receives it.\n4. Check the multiply-before-divide rule: `(a * b) \u002F c` must not overflow; use `mulDiv`, which widens to 512-bit.\n5. Hunt truncation-to-zero: any `amount \u002F rate` where `rate > amount` returns 0, letting a user pay zero for a non-zero asset.\n6. Check fee maths: `fee = amount * feeBps \u002F 10_000` rounds down, so the protocol under-collects; verify a dust attacker cannot loop with `amount = 1` and pay `fee = 0`.\n7. Test the boundary with a fuzz test that asserts monotonicity:\n\n```solidity\nfunction testFuzz_noFreeShares(uint96 assets) public {\n    uint256 shares = vault.previewDeposit(assets);\n    if (assets > 0) assertGt(shares + vault.totalSupply(), 0);\n}\n```\n\n8. Confirm rounding is applied *after* aggregation, not per-leg, in multi-hop conversion functions.\n9. Verify a decimal conversion (`1e6` USDC to `1e18` shares) uses `10 ** (18 - 6)` and cannot be reversed by rounding.\n10. Record each division, its direction, and the party that benefits.\n\n## Pitfalls\n\n- `previewX` (view) and `X` (state-changing) use different rounding; integrators trust the wrong one and over-withdraw.\n- `mulDiv` default rounding is Floor; using it for a debt calculation rounds the attacker's debt down every block.\n- Percentage fee computed on a per-second drip: truncation to zero makes a flash-loan in-out free in the same block.\n- Casting `uint256` to `uint128`\u002F`int128` without a bounds check wraps silently.\n- Combining two truncated values (`a\u002Fb + c\u002Fd`) when `(a*d + b*c)\u002F(b*d)` was intended.\n\n## Verification\n\n    forge test --match-test \"testFuzz.*[Rr]ound\" -vv\n\nPass: the fuzz run reports 256+ runs with no counterexample. Add `fail_on_revert = true` under `[fuzz]` in `foundry.toml` so a revert is a failure, not a skip.\n\nReport each division site, its rounding direction, and the benefiting party.\n",{"name":5,"description":7},"main","3df1d0d67b30ff15236561f8c0f9f06d33f42dfd","skills\u002Fpayments\u002F0xklen-rounding-direction-truncation",17,"https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia","https:\u002F\u002Feth2030.com","Hardcore infra. Building @alt-research and applied cryptography.","2026-11-12 02:28:42",{"source":8,"skillName":5,"commands":44,"prompt":54,"zipAvailable":55},[45,48,51],{"tool":46,"command":47},"npx","npx skills add jiayaoqijia\u002Fcryptoskill --skill rounding-direction-truncation",{"tool":49,"command":50},"bunx","bunx skills add jiayaoqijia\u002Fcryptoskill --skill rounding-direction-truncation",{"tool":52,"command":53},"pnpm","pnpm dlx skills add jiayaoqijia\u002Fcryptoskill --skill rounding-direction-truncation","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fpayments\u002F0xklen-rounding-direction-truncation\u002FSKILL.md",true,[57,61,67],{"path":58,"resourceType":58,"resourceTypeDesc":59,"sizeBytes":60,"sourceHtmlUrl":16},"LICENSE","许可证",1063,{"path":62,"resourceType":63,"resourceTypeDesc":64,"sizeBytes":65,"sourceHtmlUrl":66},"SOURCE.md","REFERENCE","参考文档",355,"https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fpayments\u002F0xklen-rounding-direction-truncation\u002FSOURCE.md",{"path":68,"resourceType":63,"resourceTypeDesc":64,"sizeBytes":69,"sourceHtmlUrl":70},"TRUST.auto.yaml",2217,"https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fpayments\u002F0xklen-rounding-direction-truncation\u002FTRUST.auto.yaml",[72,100,123,141,154,164],{"skillNo":73,"name":74,"nameZh":6,"description":75,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":76,"sourceHtmlUrl":77,"categories":78,"keywords":79,"publishedAt":29},"01M712C436EQYE43342Q4E7GG1","recipe-performance","Diagnose a specific MetaMask user flow's real-app performance and Sentry critical-user-flow telemetry with deterministic mm-harness recipes. Use for CUF boundaries, cold\u002Fwarm\u002Fbackground-resume behavior, latency regressions, lifecycle tags, duplicate or blocking network requests, long tasks, layout\u002Fpaint cost, screen redraws, React rerenders, or browser trace analysis where tests alone cannot prove the real app.","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fwallets\u002Fmetamask-official-recipe-performance\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fwallets\u002Fmetamask-official-recipe-performance\u002FSKILL.md",[],[80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99],"performance","recipe performance","recipe","mm","sentry","harness","app","deterministic","recipes","flow","cold","critical","analysis","tasks","react","browser","layout","screen","network","metamask",{"skillNo":101,"name":102,"nameZh":6,"description":103,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":104,"licenseFileUrl":105,"sourceHtmlUrl":106,"categories":107,"keywords":108,"publishedAt":29},"01M70XT2F8A5CQZ81JBP98KSW3","mexc-spot-websocket","MEXC Spot WebSocket workflows for public streams, protobuf handling, depth maintenance, listen keys, and private streams without exposing API keys.","Apache-2.0","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fexchanges\u002Fmncrftfrcnm-mexc-spot-websocket\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fexchanges\u002Fmncrftfrcnm-mexc-spot-websocket\u002FSKILL.md",[],[109,110,111,112,113,114,115,116,117,118,119,120,121,122],"mexc spot websocket","spot","websocket","api","workflows","public","listen","streams","depth","keys","for","api keys","maintenance","protobuf",{"skillNo":124,"name":125,"nameZh":6,"description":126,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":104,"licenseFileUrl":127,"sourceHtmlUrl":128,"categories":129,"keywords":130,"publishedAt":29},"01M70XT0MBXN3WZY1BKN0CPNKR","mexc-spot-rest","MEXC Spot REST workflows for public data, signed account requests, orders, wallet\u002Fadmin actions, and secure signing without exposing API keys.","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fexchanges\u002Fmncrftfrcnm-mexc-spot-rest\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fexchanges\u002Fmncrftfrcnm-mexc-spot-rest\u002FSKILL.md",[],[131,110,132,133,134,135,136,120,119,118,137,138,139,114,140,113,112],"mexc spot rest","REST","admin","signing","Wallet","requests","actions","secure","data","orders",{"skillNo":142,"name":143,"nameZh":6,"description":144,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":104,"licenseFileUrl":145,"sourceHtmlUrl":146,"categories":147,"keywords":148,"publishedAt":29},"01M70XSYSVZ2N0N91XCAR2CAK0","mexc-futures-websocket","MEXC Futures WebSocket workflows for public streams, private account\u002Forder\u002Fposition streams, ping\u002Fpong, subscriptions, and depth maintenance.","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fexchanges\u002Fmncrftfrcnm-mexc-futures-websocket\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fexchanges\u002Fmncrftfrcnm-mexc-futures-websocket\u002FSKILL.md",[],[149,150,111,151,152,121,119,153,117,116,114,113],"mexc futures websocket","futures","position","ping","subscriptions",{"skillNo":155,"name":156,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":157,"sourceHtmlUrl":158,"categories":159,"keywords":160,"publishedAt":29},"01M6H0ZWHVE5W04WG2DKQE2SQC","0xklen-version-and-refresh-sanctions-lists","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fpayments\u002F0xklen-version-and-refresh-sanctions-lists\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fpayments\u002F0xklen-version-and-refresh-sanctions-lists\u002FSKILL.md",[],[161,162,163],"refresh","0xklen version and refresh sanctions lists","version",{"skillNo":165,"name":166,"nameZh":6,"description":167,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":168,"sourceHtmlUrl":169,"categories":170,"keywords":171,"publishedAt":29},"01M6H0Z28Q3R8J0S799GJQWXM3","verify-tests-cover-changed-lines","Use when a PR adds code that the existing suite may not exercise. Measures patch coverage — the share of newly changed lines hit by tests — before approving.","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fpayments\u002F0xklen-verify-tests-cover-changed-lines\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fjiayaoqijia\u002Fcryptoskill\u002Fblob\u002Fmain\u002Fskills\u002Fpayments\u002F0xklen-verify-tests-cover-changed-lines\u002FSKILL.md",[],[172,173,174,175,176,177,178,24,179,180,181],"tests","cover","verify tests cover changed lines","verify","coverage","pr","exercise","patch","code","share"]