Skip to content

Commit

Permalink
Merge branch 'main' into rp/audit
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp authored May 29, 2024
2 parents 7bcad37 + 9fb3c91 commit fedff1a
Show file tree
Hide file tree
Showing 10 changed files with 402 additions and 215 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Go

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:

jobs:
Expand Down Expand Up @@ -37,6 +37,7 @@ jobs:
go test -v -coverpkg=./... -covermode=atomic -coverprofile coverage.txt ./...
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
6 changes: 3 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ on:
pull_request:
push:
branches:
- master
- main
jobs:
# Runs golangci-lint over the nmt repository
# This workflow is run on every pull request and push to master
# This workflow is run on every pull request and push to main
# The `golangci` job will pass without running if no *.{go, mod, sum} files have been modified.
golangci:
name: golangci-lint
Expand All @@ -20,7 +20,7 @@ jobs:
.go
.mod
.sum
- uses: golangci/golangci-lint-action@v4
- uses: golangci/golangci-lint-action@v6
with:
version: latest
args: --timeout 10m
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

![Go version](https://img.shields.io/badge/go-1.21-blue.svg)
[![Go Reference](https://pkg.go.dev/badge/github.com/celestiaorg/nmt.svg)](https://pkg.go.dev/github.com/celestiaorg/nmt)
![golangci-lint](https://github.com/celestiaorg/nmt/workflows/golangci-lint/badge.svg?branch=master)
![golangci-lint](https://github.com/celestiaorg/nmt/workflows/lint/badge.svg)<!-- it retrieves the lint workflow status badge for the default branch i.e., main, for a different branch, specify the branch parameter in the URL() -->
![Go](https://github.com/celestiaorg/nmt/workflows/Go/badge.svg)
![codecov.io](https://codecov.io/github/celestiaorg/nmt/coverage.svg?branch=master)
![codecov.io](https://codecov.io/github/celestiaorg/nmt/coverage.svg?branch=main)

A Namespaced Merkle Tree is
> [...] an ordered Merkle tree that uses a modified hash function
Expand Down
6 changes: 3 additions & 3 deletions docs/nmt-lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ idSize := tree.NamespaceSize() // outputs 1
If the NMT is configured with `IgnoreMaxNamespace` set to true (the flag is explained [here](#nmt-initialization-and-configuration)), then the calculation of the namespace ID range of non-leaf nodes in the [namespace hash function](./spec/nmt.md#namespaced-hash) will change slightly.
That is, if the right child of a node is entirely filled with leaves with the maximum possible namespace `maxPossibleNamespace`, i.e., its minimum and maximum namespace are equal to the `maxPossibleNamespace`, then the right child is excluded from the calculation of the namespace ID range of the parent node, and the parent node inherits the namespace range of the left child.
In the preceding code example with the ID size of `1` byte, the value of `maxPossibleNamespace` is $2^8-1 = 0xFF$.
Concretely, consider a node `n` with children `l` and `r`. If `r.minNs` and `r.maxNs` are both equal to `maxPossibleNamespace` (indicating that it represents the root of a subtree whose leaves all have the namespace ID of `maxPossibleNamespace`), then the namespace ID range of `n` is set to the namespace range of `l`, i.e., `n.MinNs = l.MinNs` and `n.MaxNs = l.MaxNs`.
Concretely, consider a node `n` with children `l` and `r`. If `r.minNs` and `r.maxNs` are both equal to `maxPossibleNamespace` (indicating that it represents the root of a subtree whose leaves all have the namespace ID of `maxPossibleNamespace`), then the namespace ID range of `n` is set to the namespace range of `l`, i.e., `n.MinNs = l.MinNs` and `n.MaxNs = l.MaxNs`.
Otherwise, the namespace ID range of `n` is set as normal i.e., `n.minNs = min(l.minNs, r.minNs)` and `n.maxNs = max(l.maxNs, r.maxNs)`.

Note that the `IgnoreMaxNamespace` flag is Celestia-specific and is motivated by the fact that half of the data items in the NMT are associated with reserved namespace IDs (i.e., the highest possible value within the ID size) and do not need to be queried using their namespace IDs.
Expand Down Expand Up @@ -80,7 +80,7 @@ d2 := namespace.PrefixedData(append(namespace.ID{1}, []byte("leaf_2")...))
if err := tree.Push(d2); err != nil {
// something went wrong
}
d3 := namespace.PrefixedData(append(namespace.ID{3}, []byte("leaf_3")...))
d3 := namespace.PrefixedData(append(namespace.ID{3}, []byte("leaf_3")...))
if err := tree.Push(d3); err != nil {
// something went wrong
}
Expand Down Expand Up @@ -186,7 +186,7 @@ In the example given earlier, each node is `34` bytes in length and takes the fo

## Verify Namespace Proof

The correctness of a namespace `Proof` for a specific namespace ID `nID` can be verified using the [`VerifyNamespace`](https://github.com/celestiaorg/nmt/blob/master/proof.go) method.
The correctness of a namespace `Proof` for a specific namespace ID `nID` can be verified using the [`VerifyNamespace`](https://github.com/celestiaorg/nmt/blob/main/proof.go) method.

```go
func (proof Proof) VerifyNamespace(h hash.Hash, nID namespace.ID, leaves [][]byte, root []byte) bool
Expand Down
14 changes: 7 additions & 7 deletions docs/spec/nmt.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,21 @@ More formally, the short namespace absence proof consists of the following compo
1) Find the index of a leaf in the tree that meets two conditions:
1) Its namespace is the smallest namespace greater than `NS`.
1) The namespace of the leaf to its left is smaller than `NS`.
1) Traverse up the branch connecting that leaf to the root and locate one of the parents/grandparents of that leaf whose namespace range does not overlap with the queried namespace.
1) Traverse up the branch connecting that leaf to the root and locate one of the parents/grandparents of that leaf whose namespace range does not overlap with the queried namespace.
The `SubtreeHash` is the hash of that node.
1) `start` and `end` range: These represent the indices of the `SubtreeHash` within its respective level.
1) `start` and `end` range: These represent the indices of the `SubtreeHash` within its respective level.
Nodes at each level are indexed from left to right starting at index `0`.
1) `nodes`: This set comprises the index-based Merkle inclusion proof of the `SubtreeHash` to the tree root `T`.

Below, we illustrate the short namespace absence proof for namespace `NS = 02` in an 8-leaf tree:
The namespace `03` is the smallest namespace larger than `02`.
By traversing the branch from the leaf with namespace `03` to the root, we find a node with hash `03 04 52c7c03` whose namespace range doesn't overlap with `02`.
The namespace `03` is the smallest namespace larger than `02`.
By traversing the branch from the leaf with namespace `03` to the root, we find a node with hash `03 04 52c7c03` whose namespace range doesn't overlap with `02`.
This node is the highest such node along the branch.
The `SubtreeHash` is the hash of that node, which is `03 04 52c7c03`.
The `start` and `end` indices indicate its position in the respective level.
In this case, `start = 1` and `end = 2`.
The `start` and `end` indices indicate its position in the respective level.
In this case, `start = 1` and `end = 2`.
Note that node indices start at `0` from left to right at each level.
The `nodes` form the index-based Merkle inclusion proof of the `SubtreeHash` to the tree root `T`.
The `nodes` form the index-based Merkle inclusion proof of the `SubtreeHash` to the tree root `T`.
The `nodes` set includes `00 00 ead8d25`, the left sibling of `03 04 52c7c03`.

In summary, the short namespace absence proof for `NS = 02` in this tree consists of `SubtreeHash = 03 04 52c7c03`, `start = 1`, `end = 2`, and the `nodes` set containing `00 00 ead8d25`.
Expand Down
4 changes: 2 additions & 2 deletions nmt.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package nmt contains an NMT implementation.
// The specifications can be found in https://github.com/celestiaorg/nmt/blob/master/docs/spec/nmt.md.
// The specifications can be found in https://github.com/celestiaorg/nmt/blob/main/docs/spec/nmt.md.
package nmt

import (
Expand Down Expand Up @@ -69,7 +69,7 @@ func NamespaceIDSize(size int) Option {
// IgnoreMaxNamespace sets whether the largest possible namespace.ID MAX_NID
// should be 'ignored'. If set to true, this allows for shorter proofs in
// particular use-cases. E.g., see:
// https://github.com/celestiaorg/celestiaorg-specs/blob/master/specs/data_structures.md#namespace-merkle-tree
// https://github.com/celestiaorg/celestiaorg-specs/blob/main/specs/data_structures.md#namespace-merkle-tree
// Defaults to true.
func IgnoreMaxNamespace(ignore bool) Option {
return func(opts *Options) {
Expand Down
8 changes: 4 additions & 4 deletions nmt_quint_model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ is necessary.
translates them to function calls.)
The generation of the tests happens through simulation.
In that sense, it is similar to the existing tests
[fuzz_test.go](https://github.com/celestiaorg/nmt/blob/master/fuzz_test.go),
[fuzz_test.go](https://github.com/celestiaorg/nmt/blob/main/fuzz_test.go),
except that it also adds corruption of the data.
- Having specifications written in Quint makes it possible to change
tests/specs quickly:
Expand Down Expand Up @@ -110,13 +110,13 @@ verifyInclusionProof(proofZero, c_root, 0, [{ value: ("data", 0), namespaceId: 2
import nmtProofVerification.*
// runs the initialization action
init
init
// runs one step of the model
step
step
// runs another step of the model
step
step
```

After getting acquainted with all the operators,
Expand Down
Loading

0 comments on commit fedff1a

Please sign in to comment.