Coverage for tropicsquare / ports / micropython / __init__.py: 0%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-27 21:24 +0000

1from random import getrandbits 

2 

3from tropicsquare import TropicSquare 

4from tropicsquare.ports.micropython.hkdf import HKDF 

5from tropicsquare.ports.micropython.x25519 import X25519 

6from tropicsquare.ports.micropython.aesgcm import AESGCM 

7 

8 

9class TropicSquareMicroPython(TropicSquare): 

10 def __init__(self, transport): 

11 """Initialize TropicSquare for MicroPython. 

12 

13 :param transport: L1 transport instance 

14 """ 

15 

16 super().__init__(transport) 

17 

18 

19 def _get_ephemeral_keypair(self): 

20 ehpriv = b'' 

21 for _ in range(8): 

22 ehpriv += getrandbits(32).to_bytes(4, "big") 

23 

24 return (ehpriv, X25519.pubkey(ehpriv)) 

25 

26 

27 def _hkdf(self, salt, shared_secret, length = 1): 

28 result = HKDF.derive(salt, shared_secret, length * 32) 

29 if length > 1: 

30 return [result[i*32:(i+1)*32] for i in range(length)] 

31 else: 

32 return result 

33 

34 

35 def _x25519_exchange(self, private_bytes, public_bytes): 

36 return X25519.exchange(private_bytes, public_bytes) 

37 

38 

39 def _aesgcm(self, key): 

40 return AESGCM(key)