Created
September 11, 2018 10:35
-
-
Save arriqaaq/233d580d8a06bd6c5b20f6a37d5cdfa2 to your computer and use it in GitHub Desktop.
This file contains 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
// fingerEntry represents a single finger table entry | |
type fingerEntry struct { | |
Id []byte // ID hash of (n + 2^i) mod (2^m) | |
Node *internal.Node | |
} | |
func newFingerTable(node *internal.Node, m int) fingerTable { | |
ft := make([]*fingerEntry, m) | |
for i := range ft { | |
ft[i] = newFingerEntry(fingerID(node.Id, i, m), node) | |
} | |
return ft | |
} | |
// newFingerEntry returns an allocated new finger entry with the attributes set | |
func newFingerEntry(id []byte, node *internal.Node) *fingerEntry { | |
return &fingerEntry{ | |
Id: id, | |
Node: node, | |
} | |
} | |
// Computes the offset by (n + 2^i) mod (2^m) | |
func fingerID(n []byte, i int, m int) []byte { | |
// Convert the ID to a bigint | |
idInt := (&big.Int{}).SetBytes(n) | |
// Get the offset | |
two := big.NewInt(2) | |
offset := big.Int{} | |
offset.Exp(two, big.NewInt(int64(i)), nil) | |
// Sum | |
sum := big.Int{} | |
sum.Add(idInt, &offset) | |
// Get the ceiling | |
ceil := big.Int{} | |
ceil.Exp(two, big.NewInt(int64(m)), nil) | |
// Apply the mod | |
idInt.Mod(&sum, &ceil) | |
// Add together | |
return idInt.Bytes() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment