cryptoSL2024/net.py
Mateusz Piątkowski d925523e4d otp -> ot
2024-04-18 10:01:09 +00:00

30 lines
723 B
Python

import socket
import pickle
import struct
import ecpy.curves
from ot import curve
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
while toread:
read = s.recv(toread)
pickled += read
toread -= len(pickled)
return pickle.loads(pickled)