GithubHelp home page GithubHelp logo

chainlink's Introduction

Overview

I've look on MerkleTreeSHA256.sol. This one is an expensive smart contract to be operated. It motivated me to think in another direction.

Questions & Answers

  1. If compute/verify proof off-chain cost less than compute it on-chain, why don't we make computation easier for on-chain?

    • Apparently off-chain computation is much more cheaper even for worst algorithm.
    • It's possible to reduce cost for on-chain computation by using simpler algorithm.
  2. What does it cost calculate/verify Merkle Tree

    • To calculate a Merkle tree with leaf is: n we need to calculate O(n) to verify it's O(log⁡2(n)) but for MerkleTreeSHA256 we are only store right-most 'frontier' then we had received no benefit.
    • In MerkleTreeSHA256, the cost to insert a new leaf is O(log2n) (n is total leafs)
  3. Is Merkle tree is the only way to archive that?.

    • No, we could use Hash Chain, it's even giving better result

Proposal

Denote that:

  • H: Hash function, denote H(x) is compute digest of x
  • ++: Byte array concat operator
  • A, B, C: Digests need to be used to construct proof (unsorted)
  • root: The first digest 0x0000000000000000000000000000000000000000000000000000000000000000

Whe have:

root <- H(root ++ A) <- H(H(root ++ A) ++ B) <- H(H(H(root ++ A) ++ B) ++ C) ...

In short:

root := H(root ++ X)

Hash Chain

These created a immutable chain of digests that only allow us to append to the end of the chain.

Benchmark result

Contract Append One Append 15 Digests
MerkleTreeSHA256 103,909 Gas 263,415 Gas
ChainLinkSha256 69,314 Gas 98,345 Gas
ChainLinkKeccak256 42,335 Gas 79,619 Gas

Conclusion

  • What does it cost?
  • O(n) to verify
  • O(1) to insert a new leaf

We could make it even more better with a Hash Chain for on-chain computation and hybrid of Hash Chain and Merkle Tree for off-chain computation (With the context we received from EVM, we could construct a Merkle Tree).

License

This project licensed under MIT License

chainlink's People

Contributors

chiro-hiro avatar

Stargazers

 avatar Minh avatar sam bacha avatar kt avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.