summaryrefslogtreecommitdiff
path: root/SybilLimit.hs
blob: ad85cfdc504e8c76f14f01c7d33b29cdcbe35ff9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
{-# LANGUAGE StandaloneDeriving, TupleSections #-}
module SybilLimit where

import Data.List ( foldl', minimumBy )
import Data.Maybe ( fromMaybe )
import Data.Ord ( comparing )
import Data.Tuple ( swap )
import Data.IntMap.Strict ( IntMap, (!) )
import qualified Data.IntMap.Strict as IntMap
import Data.Traversable ( sequenceA )
import Control.Applicative
-- import System.Random
import Data.Map.Strict ( Map )
import qualified Data.Map as Map

import Stochastic

data NodeId

deriving instance Ord NodeId
deriving instance Eq NodeId

data ThisNode = ThisNode
    { selfId :: NodeId
    , friends :: IntMap FriendNode
    , friendById :: Map NodeId Int
    , routeTails :: IntMap (NodeId,NodeId)
    -- ^ Terminal edge by instance number.
    , tailCounters :: IntMap Int
    -- ^ verificaiton counter by instance number.
    , routeCount :: Int
    -- ^ The r parameter of SybilLimit.
    , pendingChecks :: Map NodeId PendingSybilCheck
    }

data FriendNode = FriendNode
    { friendId :: NodeId
    , routesTo :: IntMap Int
    -- ^ Forward random-route hop by instance number.
    , routesFrom :: IntMap Int
    -- ^ Backward random-route hop by instance number.
    , registeredTerminal :: Maybe NodeId
    -- ^ Currently registered terminal edge.
    -- The NodeId indicates the source vertex, friendId is the destination vertex.
    }


-- | When 'sybPendingTails' is empty, the intersection condition is passed if and
-- only if 'sybVerifiedTails' is not empty.
data PendingSybilCheck = PendingSybilCheck
    { sybPendingTails :: Map (NodeId,NodeId) [Int]
    , sybVerifiedTails :: Map (NodeId,NodeId) [Int]
    }

data PeerMessage
    = ForwardRRMessage { rrRoute :: Int
                       , rrCount :: Int
                       , rrSuspect :: NodeId }
    | BackwardRRMessage { rrRoute :: Int
                        , rrCount :: Int
                        , rrTail :: (NodeId,NodeId) }
    | RegistrationQuery { rrSuspect :: NodeId
                        , rrRegisteredFrom :: NodeId }
    | RegistrationResponse { rrSuspect :: NodeId
                           , rrRegisteredFrom :: NodeId
                           , rrValidRegistration :: Bool }


data SybilCheck
    = SybilCheck { chkSuspect :: NodeId
                 , chkAccepted :: Bool }

data MessageReaction = MessageReaction
    { changedState :: ThisNode
    , outgoingMessages :: [(NodeId, PeerMessage)]
    , sybilChecks :: [SybilCheck]
    }

data SuspectCredentials
    = SuspectCredentials { suspectId :: NodeId
                         , suspectTails :: [(NodeId,NodeId)]
                         }

friendNode :: NodeId -> FriendNode
friendNode nid = FriendNode nid IntMap.empty IntMap.empty Nothing

friendCount :: ThisNode -> Int
friendCount me = IntMap.size $ friends me

todo :: a
todo = error "unimplemented"

-- | @ addFriend @ adds a friend and updates the random route instances.
--
-- Arguments:
--
--   [@them@] The 'NodeId' of the friend being added.
--
--   [@me@]   'ThisNode' prior to the friend being added.
--
-- Returns the altered 'ThisNode' and the list of friend numbers
-- for which we should send notifications about the change.
--
addFriend :: NodeId -> ThisNode -> Stochastic ( ThisNode, [Int] )
addFriend them me = fmap (\ks -> (addFriend' ks them me,ks)) mks
 where
    d   = friendCount me
    mks = sequenceA $ replicate (routeCount me) $ random 0 d

    addFriend' :: [Int] -> NodeId -> ThisNode -> ThisNode
    addFriend' ks them me = me { friends = fs, friendById = ids }
     where
        d   = friendCount me
        fs0 = IntMap.insert d (friendNode them) $ friends me
        ids = Map.insert them d (friendById me)
        fs  = foldl' (flip $ uncurry updateRoute) fs0 edges
            where edges = zip [0..] (map mkEdge ks)
                  mkEdge k = if k==d then (d,d) else (k,d)

-- | @ updateRoute @ updates one of the random routes used by SybilLimit.
--
--  [@i@]     The random route instance number.
--
--  [@(k,j)@] A pair of friend numbers.  The routes inbound from friend @k@
--            will be routed out to friend @j@ for the s-direction (forwards)
--            and vice versa for the v-direction (backwards).
--
--  [@fs@]    The current friend map prior to changed route.
--
-- Returns:  The friend map with changes applied.
--
updateRoute :: Int -> (Int,Int) -> IntMap FriendNode -> IntMap FriendNode
updateRoute i (k,j) fs = jfromk $ ktoj fs
 where
    ktoj fs   = IntMap.insert k (setTo   i j (fs!k)) fs
    jfromk fs = IntMap.insert j (setFrom i k (fs!j)) fs

    setTo i x f   = f { routesTo   = IntMap.insert i x (routesTo f)}
    setFrom i x f = f { routesFrom = IntMap.insert i x (routesFrom f)}


-- | Required:
--
-- (1) srcId must be a valid friend node.
--
-- (2) msg must be either ForwardRRMessage or BackwardRRMessage
forwardMessage :: ThisNode -> (FriendNode -> IntMap Int) -> (NodeId,PeerMessage) -> (NodeId, PeerMessage)
forwardMessage me next (srcId,msg) = msg'
      where msg' = ( dest, msg { rrCount = rrCount msg + 1 } )
            srcno = friendById me Map.! srcId
            destno = next (friends me ! srcno) ! rrRoute msg
            dest = friendId (friends me ! destno)

reactToMessage :: Int -> (NodeId,PeerMessage) -> ThisNode -> MessageReaction
reactToMessage w (srcId, msg@(RegistrationResponse {})) me =
    MessageReaction me' [] sybils
 where
    me0 = me { pendingChecks = vs' }
    vs' :: Map NodeId PendingSybilCheck
    vs' = Map.adjust adj (rrSuspect msg) $ pendingChecks me
    tail = (rrRegisteredFrom msg, srcId)
    adj p = p { sybPendingTails = Map.delete tail $ sybPendingTails p
              , sybVerifiedTails =
                    if rrValidRegistration msg then goodTail else sybVerifiedTails p
              }
        where goodTail = Map.insert tail indexes
                                    $ sybVerifiedTails p
              indexes = fromMaybe [] $ Map.lookup tail $ sybPendingTails p
    (me',sybils) =
            case Map.lookup (rrSuspect msg) $ pendingChecks me0 of
              Nothing -> (me0,[])
              Just icheck -> if Map.null (sybPendingTails icheck)
                                then bcheck icheck
                                else (me0, [])
        where bcheck icheck
                = if Map.null (sybVerifiedTails icheck)
                        then (me0, dishonest)
                        else balanceCheck
                where
                  dishonest = [SybilCheck (rrSuspect msg) False]
                  balanceCheck = second chk $ balanceCondition tail indexes me0
                    where chk = (:[]) . SybilCheck (rrSuspect msg)
                          second f (x,y) = (x,f y)
                          indexes = fromMaybe []
                                    . Map.lookup tail
                                    $ sybVerifiedTails icheck

reactToMessage w (srcId, msg@(RegistrationQuery {})) me =
    MessageReaction me [(srcId,resp)] []
 where
    resp = RegistrationResponse { rrSuspect = rrSuspect msg
                                , rrRegisteredFrom = rrRegisteredFrom msg
                                , rrValidRegistration = check }
    check = fromMaybe False $ do
        key <- Map.lookup (rrRegisteredFrom msg) $ friendById me
        return $ registeredTerminal (friends me ! key) == Just (rrSuspect msg)

reactToMessage w (srcId, msg) me =
    case Map.lookup srcId $ friendById me of
        Nothing -> MessageReaction me [] []
        Just srcNo -> 
            if rrCount msg == w
                then terminate srcNo
                else MessageReaction me [forwardMessage me next (srcId,msg)] []
 where
    (terminate,next) = case msg of
                        ForwardRRMessage {} -> ( terminateForward, routesTo )
                        BackwardRRMessage {} -> ( terminateBackward, routesFrom )

    terminateForward srcNo = MessageReaction me { friends = friends' }
                                             [pong]
                                             []
      where pong =( srcId, BackwardRRMessage { rrCount = 1
                                             , rrRoute = rrRoute msg
                                             , rrTail = (srcId, selfId me)
                                             } )
            friends' = IntMap.adjust adj srcNo $ friends me
            adj f = f { registeredTerminal = Just $ rrSuspect msg }

    terminateBackward _ = MessageReaction me' [] []
      where me' = me { routeTails =
                        IntMap.insert (rrRoute msg) (rrTail msg)
                                     $ routeTails me
                     }

balanceCondition :: (NodeId,NodeId) -> [Int] -> ThisNode -> (ThisNode, Bool)
balanceCondition (ka,kb) routeNums me = (me',didPass)
 where
    me' = me { tailCounters = counters' }
     where
        counters' = if didPass then incremented else tailCounters me
        incremented = IntMap.adjust (+1) cmin $ tailCounters me

    didPass = fromIntegral (cmin + 1) <= b

    b = h * max (logBase 2 r) a :: Double
     where
        r = fromIntegral $ routeCount me
        a = fromIntegral ( 1 + sum (IntMap.elems $ tailCounters me) ) / r
        h = 4 -- h > 1 is some universal constant that is not too small (they used
              -- h = 4 in their experiments).
    cmin = minimumBy indexCompare routeNums
     where
        indexCompare ca cb = tieBreak $ comparing (tailCounters me !) ca cb
         where
            tieBreak EQ = compare ca cb
            tieBreak x = x

initiateSybilCheck :: SuspectCredentials -> ThisNode -> MessageReaction
initiateSybilCheck cred me = MessageReaction me' msgs []
 where
    me' = me { pendingChecks = Map.insert (suspectId cred) p
                                    $ pendingChecks me }
    msgs = map (\(a,b) -> (b, RegistrationQuery (suspectId cred) a))
              $ Map.keys tmap
    p = PendingSybilCheck { sybPendingTails = tmap
                          , sybVerifiedTails = Map.empty
                          }
    tmap = tmap0 `Map.intersection` smap
        where
            smap = Map.fromList $ map (,()) (suspectTails cred)
            tmap0 = foldl' build
                           Map.empty
                           (map swap $ IntMap.toList $ routeTails me)
            build mp (tl,i) = Map.alter insert tl mp
             where insert Nothing = Just [i]
                   insert (Just is) = Just (i:is)