1

I'm building BEST MVP, a platform to tokenize brand equity via purchase proofs . Our ActivationManager uses a log10 formula for activation points: Any suggestions to optimize log10 with PRBMath or alternatives for precision/gas efficiency? Feedback welcome!

function calculatePoints(uint256 usdValue) internal pure returns (uint256) { uint256 logTerm = usdValue > 10000e8 ? Math.log10((usdValue / 10000e8) * 1e18) : 0; return (usdValue * 1e18) / (1e18 + logTerm); } 

1 Answer 1

2

Some initial thoughts for putting PRBMath to use:

  • Utilize the unsigned type assuming you are working with non-negative numbers (slightly more gas efficient too!)

  • Utilize mul/div functions to preserve precision and avoid overflow

  • Perform fixed point conversions once at the start/end to reduce recomputation and scaling errors

  • Include base case/early conditional returns to short-circuit cheap paths

  • Note that PRBMath works with user defined types

You can utilize unwrap to get the underlying primitive and ud to wrap into the defined type.

function log10From1e8 (uint256 usd) external returns (uint256){ uint256 x = usd * 1e10; // 1e8 -> 1e18 return unwrap(ud(x).log10()); } 

As an alternative to PRBMath, you can also check out Solady. The library contains a number of gas optimized utilities and had an audit completed by Spearbit (sponsored by Base) in March 2025.

4
  • Thank you for sharing your insightful suggestions on utilizing the PRBMath library in Ethereum smart contract development. Your recommendations, Commented Sep 12 at 6:55
  • including the use of unsigned types for gas efficiency, leveraging mul/div functions for precision, performing fixed-point conversions strategically, incorporating early conditional returns, and noting support for user-defined types, provide a solid foundation for optimizing mathematical operations in Solidity. Additionally, your mention of Solady as an alternative, with its gas-optimized utilities and recent audit by Spearbit in March 2025, offers a valuable perspective for evaluating library choices. Commented Sep 12 at 6:55
  • Building on your advice, our next development direction will focus on integrating PRBMath into our DeFi protocol's core arithmetic modules, starting with a prototype that emphasizes precision in token pricing and yield calculations. We plan to conduct comparative benchmarks between PRBMath and Solady to assess gas costs and precision trade-offs, aiming for deployment on testnets by Q4 2025. This will include enhanced input validation to mitigate overflow risks and modular refactoring to support user-defined types, Commented Sep 12 at 6:55
  • ensuring scalability for future features like advanced logarithmic computations in risk assessment algorithms. We appreciate your contribution and look forward to potential further discussions. Commented Sep 12 at 6:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.