nomos-specs/mixnet/config.py

70 lines
1.5 KiB
Python
Raw Normal View History

from __future__ import annotations
import random
from dataclasses import dataclass
2024-06-26 15:55:00 +09:00
from typing import List
from cryptography.hazmat.primitives.asymmetric.x25519 import (
X25519PrivateKey,
X25519PublicKey,
)
2024-06-26 15:55:00 +09:00
from pysphinx.sphinx import Node as SphinxNode
@dataclass
2024-06-27 17:32:22 +09:00
class GlobalConfig:
2024-07-10 09:55:51 +09:00
"""
Global parameters used across all nodes in the network
"""
2024-06-26 15:55:00 +09:00
membership: MixMembership
2024-06-27 17:32:22 +09:00
transmission_rate_per_sec: int # Global Transmission Rate
# TODO: use this to make the size of Sphinx packet constant
max_mix_path_length: int
@dataclass
2024-06-26 15:55:00 +09:00
class NodeConfig:
2024-07-10 09:55:51 +09:00
"""
Node-specific parameters
"""
2024-06-26 15:55:00 +09:00
private_key: X25519PrivateKey
mix_path_length: int
2024-07-11 10:10:29 +09:00
nomssip: NomssipConfig
2024-07-10 08:57:58 +09:00
@dataclass
2024-07-11 10:10:29 +09:00
class NomssipConfig:
2024-07-03 11:41:29 +09:00
# The target number of peers a node should maintain in its p2p network
2024-06-28 12:26:44 +09:00
peering_degree: int
@dataclass
2024-06-26 15:55:00 +09:00
class MixMembership:
2024-07-10 09:55:51 +09:00
"""
A list of public information of nodes in the network.
2024-07-11 10:02:42 +09:00
We assume that this list is known to all nodes in the network.
2024-07-10 09:55:51 +09:00
"""
2024-06-26 15:55:00 +09:00
nodes: List[NodeInfo]
2024-07-11 12:44:04 +09:00
def generate_route(self, length: int) -> list[NodeInfo]:
"""
2024-07-11 17:00:11 +09:00
Choose `length` nodes with replacement as a mix route.
"""
2024-07-11 17:00:11 +09:00
return random.choices(self.nodes, k=length)
@dataclass
2024-06-26 15:55:00 +09:00
class NodeInfo:
2024-07-10 09:55:51 +09:00
"""
Public information of a node to be shared to all nodes in the network
"""
public_key: X25519PublicKey
2024-06-26 15:55:00 +09:00
def sphinx_node(self) -> SphinxNode:
dummy_node_addr = bytes(32)
return SphinxNode(self.public_key, dummy_node_addr)