Merge pull request #41 from vocdoni/handle-single-bigint

fix(tree_big): don't hash single bigints
This commit is contained in:
Jordi Pinyana
2026-02-24 12:54:36 +00:00
committed by GitHub
2 changed files with 28 additions and 24 deletions

View File

@@ -2,24 +2,13 @@ name: Lint
on: [ push, pull_request ]
jobs:
lint:
strategy:
matrix:
go-version: [1.23.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go-version }}
cache: true
go-version-file: go.mod
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v8
with:
version: latest
skip-cache: false
skip-pkg-cache: false
skip-build-cache: false
only-new-issues: true
version: v2.5

View File

@@ -181,13 +181,21 @@ func (t *Tree) leafToBigInts(bkey, value, serializedBigInts []byte) (
// reverse the process of bigints encoding
bigints = deserializeBigInts(serializedBigInts)
// reencode the leaf value of the tree to check if it matches the value
bigintsHash, err := HashBigInts(t.HashFunction(), bigints...)
if err != nil {
return nil, nil, err
var expectedLeafValue []byte
if len(bigints) == 1 {
expectedLeafValue = t.HashFunction().SafeBigInt(bigints[0])
if expectedLeafValue == nil {
return nil, nil, fmt.Errorf("value cannot be nil")
}
} else {
expectedLeafValue, err = HashBigInts(t.HashFunction(), bigints...)
if err != nil {
return nil, nil, err
}
}
// check if the value of the leaf node matches the value used to build the
// tree
if !bytes.Equal(bigintsHash, value) {
if !bytes.Equal(expectedLeafValue, value) {
return nil, nil, fmt.Errorf("LeafToBigInt: bigintsHash != value")
}
// convert the bytes of the key to a big.Int
@@ -258,9 +266,16 @@ func bigIntsToLeaf(hFn HashFunction, keyLen int, key *big.Int, bigints []*big.In
return nil, nil, nil, err
}
// calculate the value used to build the tree
bValue, err = HashBigInts(hFn, bigints...)
if err != nil {
return nil, nil, nil, err
if len(bigints) == 1 {
bValue = hFn.SafeBigInt(bigints[0])
if bValue == nil {
return nil, nil, nil, fmt.Errorf("value cannot be nil")
}
} else {
bValue, err = HashBigInts(hFn, bigints...)
if err != nil {
return nil, nil, nil, err
}
}
return bKey, bValue, serializedBigInts, nil
}