-
Notifications
You must be signed in to change notification settings - Fork 2.3k
multi: harden Go module supply chain, internalize small deps #10836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Roasbeef
wants to merge
10
commits into
lightningnetwork:master
Choose a base branch
from
Roasbeef:module-hardening
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
25f7afc
build+ci: harden Go module supply chain against accidental updates
Roasbeef 960f1ad
sqldb: gate dockertest postgres fixtures behind test_db_postgres tag
Roasbeef 069f6c3
lnrpc+internal/zbase32: vendor tv42/zbase32 into the tree
Roasbeef 6ea2593
aezeed+aezeed/internal/bstream: vendor kkdai/bstream into the tree
Roasbeef cc651fa
signal+signal/internal/sdnotify: vendor coreos/go-systemd SdNotify
Roasbeef 4e1d0b1
nat+nat/internal/pmp: vendor jackpal NAT-PMP + gateway discovery
Roasbeef 8bd50a7
lncli+cmd_debug: replace brotli with stdlib compress/gzip
Roasbeef 854da9d
channeldb+multi: drop ltcsuite/ltcd from migration_01_to_11
Roasbeef 2bcc71a
lncli+cmd_commands/internal/asciitable: vendor go-pretty for one table
Roasbeef 2c6ede0
tor+discovery+tor/dnsclient: drop miekg/dns for stdlib dnsmessage
Roasbeef File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| // Package bstream provides a minimal bit-level stream reader/writer used by | ||
| // the aezeed cipher seed encoding. It is an internal fork of | ||
| // github.com/kkdai/bstream reduced to the call surface aezeed actually uses | ||
| // (NewBStreamReader, NewBStreamWriter, ReadBits, WriteBits, Bytes). | ||
| // | ||
| // IMPORTANT: this package implements the exact bit-packing scheme used to | ||
| // serialize aezeed cipher seeds. The wire format is consensus-frozen — any | ||
| // change to the bit ordering or padding semantics here will silently | ||
| // invalidate every existing 24-word seed mnemonic in the wild. Do not | ||
| // modify the algorithm; only refactor. | ||
| package bstream | ||
|
|
||
| import "io" | ||
|
|
||
| // bit is a typed boolean so the API matches the upstream package and the | ||
| // WriteBit / ReadBit semantics stay self-documenting. | ||
| type bit bool | ||
|
|
||
| // BStream is a bit-addressable buffer used to pack and unpack | ||
| // non-byte-aligned values (aezeed uses 11-bit indices into the mnemonic | ||
| // word list). | ||
| type BStream struct { | ||
| stream []byte | ||
|
|
||
| // rCount is the number of bits still unread from the first byte of | ||
| // the stream. | ||
| rCount uint8 | ||
|
|
||
| // wCount is the number of bits still empty in the last byte of the | ||
| // stream. | ||
| wCount uint8 | ||
| } | ||
|
|
||
| // NewBStreamReader wraps an existing buffer for reading. | ||
| func NewBStreamReader(data []byte) *BStream { | ||
| return &BStream{stream: data, rCount: 8} | ||
| } | ||
|
|
||
| // NewBStreamWriter pre-allocates a buffer with the given byte capacity for | ||
| // writing. | ||
| func NewBStreamWriter(nByte uint8) *BStream { | ||
| return &BStream{stream: make([]byte, 0, nByte), rCount: 8} | ||
| } | ||
|
|
||
| // writeBit appends a single bit to the stream, allocating a new byte when | ||
| // the trailing byte is full. | ||
| func (b *BStream) writeBit(input bit) { | ||
| if b.wCount == 0 { | ||
| b.stream = append(b.stream, 0) | ||
| b.wCount = 8 | ||
| } | ||
|
|
||
| latestIndex := len(b.stream) - 1 | ||
| if input { | ||
| b.stream[latestIndex] |= 1 << (b.wCount - 1) | ||
| } | ||
| b.wCount-- | ||
| } | ||
|
|
||
| // writeOneByte appends an 8-bit value, handling the unaligned case where | ||
| // the trailing byte still has wCount free bits. | ||
| func (b *BStream) writeOneByte(data byte) { | ||
| if b.wCount == 0 { | ||
| b.stream = append(b.stream, data) | ||
| return | ||
| } | ||
|
|
||
| latestIndex := len(b.stream) - 1 | ||
|
|
||
| b.stream[latestIndex] |= data >> (8 - b.wCount) | ||
| b.stream = append(b.stream, 0) | ||
| latestIndex++ | ||
| b.stream[latestIndex] = data << b.wCount | ||
| } | ||
|
|
||
| // WriteBits appends the low `count` bits of `data` to the stream, MSB | ||
| // first. | ||
| func (b *BStream) WriteBits(data uint64, count int) { | ||
| data <<= uint(64 - count) | ||
|
|
||
| // Handle full bytes first so the per-bit loop only runs for the | ||
| // trailing remainder. | ||
| for count >= 8 { | ||
| byt := byte(data >> (64 - 8)) | ||
| b.writeOneByte(byt) | ||
|
|
||
| data <<= 8 | ||
| count -= 8 | ||
| } | ||
|
|
||
| for count > 0 { | ||
| bi := data >> (64 - 1) | ||
| b.writeBit(bi == 1) | ||
|
|
||
| data <<= 1 | ||
| count-- | ||
| } | ||
| } | ||
|
|
||
| // readBit consumes a single bit from the front of the stream. | ||
| func (b *BStream) readBit() (bit, error) { | ||
| if len(b.stream) == 0 { | ||
| return false, io.EOF | ||
| } | ||
|
|
||
| // If the first byte is exhausted, advance to the next byte. | ||
| if b.rCount == 0 { | ||
| b.stream = b.stream[1:] | ||
|
|
||
| if len(b.stream) == 0 { | ||
| return false, io.EOF | ||
| } | ||
|
|
||
| b.rCount = 8 | ||
| } | ||
|
|
||
| retBit := b.stream[0] & (1 << (b.rCount - 1)) | ||
| b.rCount-- | ||
|
|
||
| return retBit != 0, nil | ||
| } | ||
|
|
||
| // readByte consumes a full 8-bit value from the stream, handling the | ||
| // unaligned case where the byte straddles two stream bytes. | ||
| func (b *BStream) readByte() (byte, error) { | ||
| if len(b.stream) == 0 { | ||
| return 0, io.EOF | ||
| } | ||
|
|
||
| if b.rCount == 0 { | ||
| b.stream = b.stream[1:] | ||
|
|
||
| if len(b.stream) == 0 { | ||
| return 0, io.EOF | ||
| } | ||
|
|
||
| b.rCount = 8 | ||
| } | ||
|
|
||
| // Aligned case: the next 8 bits coincide with a byte boundary. | ||
| if b.rCount == 8 { | ||
| byt := b.stream[0] | ||
| b.stream = b.stream[1:] | ||
| return byt, nil | ||
| } | ||
|
|
||
| retByte := b.stream[0] << (8 - b.rCount) | ||
| b.stream = b.stream[1:] | ||
|
|
||
| if len(b.stream) == 0 { | ||
| return 0, io.EOF | ||
| } | ||
|
|
||
| retByte |= b.stream[0] >> b.rCount | ||
| return retByte, nil | ||
| } | ||
|
|
||
| // ReadBits consumes the next `count` bits from the stream into the low | ||
| // bits of the returned uint64, MSB first. | ||
| func (b *BStream) ReadBits(count int) (uint64, error) { | ||
| var retValue uint64 | ||
|
|
||
| for count >= 8 { | ||
| retValue <<= 8 | ||
| byt, err := b.readByte() | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| retValue |= uint64(byt) | ||
| count -= 8 | ||
| } | ||
|
|
||
| for count > 0 { | ||
| retValue <<= 1 | ||
| bi, err := b.readBit() | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| if bi { | ||
| retValue |= 1 | ||
| } | ||
|
|
||
| count-- | ||
| } | ||
|
|
||
| return retValue, nil | ||
| } | ||
|
|
||
| // Bytes returns the backing buffer. For writers this is the packed | ||
| // output; for readers it is whatever input remains unread. | ||
| func (b *BStream) Bytes() []byte { | ||
| return b.stream | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential undefined behavior or zero shift if
countis 0. Whileaezeedcurrently uses fixed bit counts, adding a guard forcount > 0would make this internal utility more robust against future usage or invalid inputs.