Giter VIP home page Giter VIP logo

mmr.py's Introduction

MMR.py

Merkle Mountain Range

Install

pip3 install -U git+https://github.com/jjyr/mmr.py.git

Example

from mmr import MMR

def test_mmr():
    def serialize(i):
        return i.to_bytes(4, 'little')

    mmr = MMR()
    # push 0..11 into MMR, and record MMR positions
    positions = [mmr.add(serialize(i)) for i in range(0, 11)]
    merkle_root = mmr.get_root()
    # proof
    elem = 5
    pos = positions[elem]
    # generate proof for 5
    proof = mmr.gen_proof(pos)
    # verify proof
    result = proof.verify(root=merkle_root, pos=pos,
                          elem=serialize(elem))
    assert(result)
    print("Ok")

test_mmr()

See tests to learn more.

License

MIT

mmr.py's People

Contributors

jjyr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mmr.py's Issues

meaning of mmr_size

def get_peaks(mmr_size) -> List[int]:
    """
    return peaks positions from left to right
    """
    def get_right_peak(height, pos, mmr_size):
        """
        find next right peak
        peak not exsits if height is -1
        """
        # jump to right sibling
        pos += sibling_offset(height)
        # jump to left child
        while pos > mmr_size - 1:
            height -= 1
            if height < 0:
                # no right peak exists
                return (height, None)
            pos -= 2 << height
        return (height, pos)

    poss = []
    height, pos = left_peak_height_pos(mmr_size)
    poss.append(pos)
    while height > 0:
        height, pos = get_right_peak(height, pos, mmr_size)
        if height >= 0:
            poss.append(pos)
    return poss

Hello, there is a function named get_peaks(mmr_size) in your code, which finds position of all peaks, that function is called in line 183, the argument is self.last_pos+1, that means number of all nodes(not only leaves) in mmr.

And get_peaks() calls an another function left_peak_height_pos(mmr_size), which returns the height and position of the most left peak.

But in function left_peak_height_pos(mmr_size), for determining the height of that peak, the mmr_size for iteration stopping is the number of leaves, is that right?

So I feel confused the meaning the argument for left_peak_height_pos() when function get_peaks() calling it.

跳跃错误

父节点和左子节点 offset 为 2 ** height
兄弟节点间的 offset 为 2 ** (height + 1) - 1

代码运算 2^height应该是

1  <<  height

而不是

2 << height

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.