3

I've been interested in precompiles recently and it seems asinine that things such as ecrecover, hash algorithms, etc. are all written as precompiles under the hood of the EV, however simple binary search tree algorithms, sorting and searching etc are not.

It's extremely un-practical to write most algorithms in solidity so most of the code is just complex getting and setting values for the most part.

Is anyone aware of any EIP attempting to accomplish this / move more of the commonly used algos on the client side.

1 Answer 1

2

Usually the cryptographic algorithms are considered most important for the blockchain functions, so they are implemented as precompiles first.

From what I understand, quite a bit of work and reasoning needs to be done about the effects of precompiles on the network. You don't want precompiles to be able to cause a denial of service attack on the network, where the precompile uses more computing resources that the gas properly accounts for. Having many precompiles also causes bloat inside of the EVM codebase that all validators need to run.

There is definitely potential to add more useful precompiles to the EVM in the future. I think the Ethereum foundation wants to take time and caution to make sure they are implemented correctly however.

Also, for things like binary search, or sorting algorithms, consider how smart contracts work in your implementations. It's often far better to expect a client to pass in a valid sorted list from offchain, then just do the verification that is is sorted onchain. The primary purpose of smart contracts is not to do heavier computational lifting, but rather to have a very reliable, trustable, and secure system people can rely on to trust in some programmable logic rather than trust each other.

A good rule of thumb is, if it can be created/a more complex algorithm can be run off chain, then the smart contract only has to verify the result is correct from whoever makes the call, that is a better implementation. A good example of this is merkle trees. We only store the root in a smart contract on chain, and a user who wants to verify something from the merkle tree passes in the proof to verify the data's inclusion in the merkle tree. You would never want to generate the whole merkle tree onchain, its just a waste of computational resources in most cases. Remember any code you run in a smart contract that is posted to the mainnet is repeated run and verified by thousands of nodes and validators to ensure correctness of the blockchain.

7
  • Yes - but custom precompiles have custom gas costs. For example ecrecover has a base gas fee of 36000 IIRC. Simulations can be ran and scaled for something like a sort function and other fundamental primitives in other languages.
    – Origami
    Oct 24, 2023 at 20:07
  • Yes, custom gas definitely helps, but they have to get that custom gas value exactly correct. It's also possible to add precompiles for custom environments, for example I know Harmony had some extra precompiles they added to the EVM when they forked it. I think partially why they don't add stuff like a sort precompile is to incentivize developers to properly implement off/onchain behavior. If a lot of people tried to do stuff like sorting alogrithms on chain, it would lead to a lot of network bloat, and increased gas prices.
    – Bruce
    Oct 26, 2023 at 17:15
  • Also, for the type of stuff you seem to be looking for, I think you should look at open source solidity libraries. There are a bunch of good ones out there, and usually if you want a specific algorithm implementation it's a good way to add it to your project. Here are some links related to libraries: forum.openzeppelin.com/t/list-of-solidity-libraries-in-the-wild/…, github.com/bkrem/awesome-solidity#libraries.
    – Bruce
    Oct 26, 2023 at 17:18
  • I think also it has to do with the fact that solidity is still intended as its primary use to write smart contracts on top of ethereum. It caters to things smart contracts need, and best practices smart contracts should use to run on the blockchain. It's not designed with the same goals of other languages that are meant to run on servers, or clusters, or operate on big amounts of data. It's a programming language so it can be adapted to everything if you want, but I think it's definitely currently intended to be very specialized for on chain smart contracts.
    – Bruce
    Oct 26, 2023 at 17:23
  • It seems for what I can research, not many good libraries exist currently for stuff like sorting and binary search trees. I think if you were running solidity in an offchain environment, or running a simulation where gas in not an issue, a library would work very similarly to a precompile for that purpose. If you end up needing to do this and write a library for these functions, I encourage you to open source it and share it so others can use it in the future!
    – Bruce
    Oct 26, 2023 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.