2023-05-18 16:29:28 +00:00
|
|
|
from typing import Tuple
|
|
|
|
from unittest import TestCase
|
|
|
|
|
2024-01-10 01:23:03 +00:00
|
|
|
from carnot.beacon import *
|
2023-05-18 16:29:28 +00:00
|
|
|
from random import randint
|
|
|
|
|
|
|
|
|
|
|
|
class TestRandomBeaconVerification(TestCase):
|
|
|
|
|
|
|
|
@staticmethod
|
2023-05-25 08:26:35 +00:00
|
|
|
def happy_beacon_and_pk(view: View) -> Tuple[RandomBeacon, PublicKey]:
|
2023-05-18 16:29:28 +00:00
|
|
|
sk = generate_random_sk()
|
|
|
|
beacon = NormalMode.generate_beacon(sk, view)
|
2023-05-25 08:26:35 +00:00
|
|
|
return beacon, sk.get_g1()
|
2023-05-18 16:29:28 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2023-05-25 08:26:35 +00:00
|
|
|
def unhappy_beacon(last_beacon: Entropy, view: View) -> RandomBeacon:
|
2023-05-18 16:29:28 +00:00
|
|
|
return RecoveryMode.generate_beacon(last_beacon, view)
|
|
|
|
|
|
|
|
def setUp(self):
|
2023-05-25 08:26:35 +00:00
|
|
|
beacon, pk = self.happy_beacon_and_pk(0)
|
|
|
|
self.beacon = RandomBeaconHandler(beacon)
|
2023-05-18 16:29:28 +00:00
|
|
|
|
|
|
|
def test_happy(self):
|
|
|
|
for i in range(3):
|
2023-05-25 08:26:35 +00:00
|
|
|
new_beacon, pk = self.happy_beacon_and_pk(i)
|
|
|
|
self.beacon.verify_happy(new_beacon, pk, i)
|
2023-05-18 16:29:28 +00:00
|
|
|
|
|
|
|
def test_unhappy(self):
|
|
|
|
for i in range(1, 3):
|
2023-05-25 08:26:35 +00:00
|
|
|
new_beacon = self.unhappy_beacon(self.beacon.last_beacon.entropy(), i)
|
|
|
|
self.beacon.verify_unhappy(new_beacon, i)
|
2023-05-18 16:29:28 +00:00
|
|
|
|
|
|
|
def test_mixed(self):
|
|
|
|
for i in range(1, 6, 2):
|
2023-05-25 08:26:35 +00:00
|
|
|
new_beacon, pk = self.happy_beacon_and_pk(i)
|
|
|
|
self.beacon.verify_happy(new_beacon, pk, i)
|
|
|
|
new_beacon = self.unhappy_beacon(self.beacon.last_beacon.entropy(), i+1)
|
|
|
|
self.beacon.verify_unhappy(new_beacon, i+1)
|