XOR operator for DHT based protocol verification

48 views
Skip to first unread message

Earnshaw

unread,
Dec 2, 2021, 5:12:06 AM12/2/21
to tlaplus
Hello everyone:

I am now wirting a Spec to verfiy a DHT ( peer-to-peer distributed hash table)  protocol,  the key algorithm is based on an enhanced version of  Kademlia.

As we now, for such peer to peer network, the logic distance of two nodes
is computed as the exclusive or (XOR) of the two node IDs, taking the result as an unsigned integer number.

d(a,b)=ab


but how can I build such ⊕ operator or function in TLA+?

Of course, in my spec I can replace the logic distance computation with  
d(a,b)=|a-| ,   but this is not a ideal solution.

  


Andrew Helwer

unread,
Dec 2, 2021, 9:17:04 AM12/2/21
to tlaplus
I think you could do it as follows:
  1. Write a function NatToBinary[n \in Nat] converting a natural number to a Seq(BOOLEAN)
  2. Write a function XorBinary[a, b \in Seq(BOOLEAN)] XOR'ing two Seq(BOOLEAN) to a single Seq(BOOLEAN) (note that /= on each element is equivalent to XOR for this purpose)
  3. Write a function BinaryToNat[b \in Seq(BOOLEAN)] converting a Seq(BOOLEAN) to a natural number
Then given two node IDs n and m, evaluate BinaryToNat[XorBinary[NatToBinary[n], NatToBinary[m]]].

Andrew

Markus Kuppe

unread,
Dec 2, 2021, 3:58:17 PM12/2/21
to tla...@googlegroups.com
On 12/2/21 2:12 AM, Earnshaw wrote:
> Hello everyone:
>
> I am now wirting a Spec to verfiy a DHT ( peer-to-peer distributed hash
> table)  protocol,  the key algorithm is based on an enhanced version of
> Kademlia.
>
> As we now, for such peer to peer network, the logic distance of two nodes
> is computed as the /exclusive or (XOR)
> <https://en.wikipedia.org/wiki/Exclusive_or>/ of the two node IDs,
> taking the result as an unsigned integer number
> <https://en.wikipedia.org/wiki/Integer>.
>
> d(a,b)=a⊕b
>
>
> but how can I build such ⊕ operator or function in TLA+?
>
> Of course, in my spec I can replace the logic distance computation with
> d(a,b)=|a-b | ,   but this is not a ideal solution.


https://github.com/tlaplus/CommunityModules/blob/master/modules/Bitwise.tla#L39-L51

Siegfried Bublitz

unread,
Dec 8, 2021, 11:04:28 AM12/8/21
to tlaplus

Hi,
what about this?

EXTENDS Integers, Sequences

\* The Hamming Distance between two vectors of equal length is the number
\* of components at which they differ.

RECURSIVE HamDist(_,_) \* define as prefix operator
HamDist(a,b) == IF Len(a) # Len (b) THEN -1 \* or some undefined value
                              ELSE IF a = <<>> THEN 0     \* this implies b = <<>>
                                         ELSE IF Head(a) = Head(b) THEN HamDist(Tail(a),Tail(b))
                                                    ELSE 1 + HamDist(Tail(a),Tail(b))


a (+) b == HamDist(a,b) \* define as infix operator        

Siegfried
Reply all
Reply to author
Forward
0 new messages