cryptoSL2024/net.py

30 lines
736 B
Python
Raw Normal View History

2024-04-18 00:09:59 +02:00
import socket
import pickle
import struct
import ecpy.curves
2024-04-18 12:01:09 +02:00
from ot import curve
2024-04-18 00:09:59 +02:00
def send_point(s: socket.socket, point: ecpy.curves.Point):
send(s, (point.x, point.y))
def recv_point(s: socket.socket) -> ecpy.curves.Point:
point = recv(s)
return ecpy.curves.Point(point[0], point[1], curve)
def send(s: socket.socket, data):
data = pickle.dumps(data, protocol=4)
data = struct.pack('<I', len(data)) + data
s.sendall(data)
def recv(s: socket.socket):
length = s.recv(4)
length, *_ = struct.unpack('<I', length)
pickled = b""
toread = length
2024-04-18 17:46:56 +02:00
while toread > 0:
2024-04-18 00:09:59 +02:00
read = s.recv(toread)
pickled += read
toread -= len(pickled)
2024-04-18 17:46:56 +02:00
return pickle.loads(pickled[:length])