def test_pow( self, bits='0000000000000000000000000000000000000000000000000000000000000000' ): fun = lambda bs: bitlist( [0 if one_from_bytes(bs)**(0) is None else 255]).to_bytes() return check_or_generate_operation(self, fun, 1, bits)
def test_scl( self, bits='4df8fe738c097afa7f255b10c3ab118eeb73e38935605042ccb7581c73f1e5e9' ): oblivious.sodium = None if sodium_hidden else oblivious.sodium_restore fun = lambda bs: bitlist([1 if cls.scl(bs) is not None else 0]) return check_or_generate_operation(self, fun, [32], bits)
def attestation_performance(cursor, slot): # returns a dict showing the attestation performance of each validator active in this slot # as the minimum inclusion distance (or -1 if attestation was missed) cursor.execute( f"SELECT f_committee FROM t_beacon_committees WHERE f_slot = {slot} ORDER BY f_index" ) result = cursor.fetchall() # committee_lookup is a list of committees (themselves lists of validator indices) for this slot committee_lookup = [ result[committee_index][0] for committee_index in range(len(result)) ] query = f"""SELECT f_committee_index, f_inclusion_slot, f_aggregation_bits FROM t_attestations WHERE f_slot = {slot} ORDER BY f_inclusion_slot""" cursor.execute(query) attestations = cursor.fetchall() # committee_performance = minimum inclusion distance for each committee/validator this slot (or -1 if missed) committee_performance = [ [-1] * len(committee_lookup[committee_index]) for committee_index in range(len(committee_lookup)) ] for attestation in attestations: committee_index = attestation[0] committee_size = len(committee_lookup[committee_index]) inclusion_slot = attestation[1] inclusion_distance = inclusion_slot - slot - 1 # to get the aggregation bits in correct order, reverse order of the bytes and then reverse whole bitlist # also exclude any bits that were added as padding by the .tobytes() method aggregation_bits = bitlist( attestation[2].tobytes()[::-1])[:-(committee_size + 1):-1] # record the shortest inclusion_distance for each member for committee_position, bit in enumerate(aggregation_bits): if bit and ( committee_performance[committee_index][committee_position] == -1): committee_performance[committee_index][ committee_position] = inclusion_distance # flatten lookup tables validators = [ validator for committee in committee_lookup for validator in committee ] performance = [ inclusion_distance for committee in committee_performance for inclusion_distance in committee ] return dict(zip(validators, performance))
def check_or_generate_operation(test, fun, lengths, bits): # pylint: disable=R1710 """ This function does either of two things depending on `bits`: * checks that test inputs drawn from the fountains input bit stream produce the bits provided in the reference output bit vector, or * generates a reference output bit vector by applying the function to the fountains input bit stream. """ fs = fountains( # Generate the input bit stream. sum(lengths), seed=bytes(0), # This is also the default; explicit for clarity. limit=256, bits=bits, # Reference output bit vector. function=fun) if bits is None: # There is no output reference bit vector, so test is not possible. return bitlist( list(fs)).hex() # Return reference output bits for test. test.assertTrue(all(fs)) # Check that all outputs match.
def check_or_generate(self, fs, bits): if bits is not None: self.assertTrue(all(fs)) # Check that all tests succeeded. else: return bitlist(list(fs)).hex() # Return target bits for this test.
def fun(bs): return bitlist([1 if cls.scalar(bs) else 0])
def fun(bs): return bitlist([ge25519_p3.from_bytes(bs).is_on_main_subgroup()])
def fun(bs): return bitlist([ge25519_p3.from_bytes(bs).is_on_curve()])
def fun(bs): return bitlist([ge25519.has_small_order(bs)])
def fun(bs): return bitlist([ge25519.is_canonical(bs)])
def fun(bs): p3 = ge25519_p3.from_bytes_ristretto255(bs) return p3.to_bytes() if p3 is not None else bitlist([0])
def attestation_performance(cursor, slot): # returns a dict showing the attestation performance of each validator active in this slot # as the minimum inclusion distance (or -1 if attestation was missed) cursor.execute( f"SELECT f_committee FROM t_beacon_committees WHERE f_slot = {slot} ORDER BY f_index" ) result = cursor.fetchall() # committee_lookup is a list of committees (themselves lists of validator indices) for this slot committee_lookup = [ result[committee_index][0] for committee_index in range(len(result)) ] query = f"""SELECT f_committee_index, f_inclusion_slot, f_aggregation_bits, f_inclusion_block_root FROM t_attestations WHERE f_slot = {slot} ORDER BY f_inclusion_slot""" cursor.execute(query) attestations = cursor.fetchall() # committee_performance = minimum inclusion distance for each committee/validator this slot (or -1 if missed) committee_performance = [ [-1] * len(committee_lookup[committee_index]) for committee_index in range(len(committee_lookup)) ] for attestation in attestations: committee_index = attestation[0] committee_size = len(committee_lookup[committee_index]) inclusion_slot = attestation[1] # to get the aggregation bits in correct order, reverse order of the bytes and then reverse whole bitlist # also exclude any bits that were added as padding by the .tobytes() method aggregation_bits = bitlist( attestation[2].tobytes()[::-1])[:-(committee_size + 1):-1] inclusion_root = attestation[3].hex() cursor.execute( f"SELECT f_canonical FROM t_blocks WHERE f_root = '\\x{inclusion_root}'" ) canonical = cursor.fetchone()[0] if not canonical: # print(f"skipping attestation from non-canonical block in slot {inclusion_slot}") continue inclusion_distance = inclusion_slot - slot - 1 # record the shortest inclusion_distance for each member for committee_position, bit in enumerate(aggregation_bits): if bit and ( committee_performance[committee_index][committee_position] == -1): committee_performance[committee_index][ committee_position] = inclusion_distance # flatten lookup tables validators = [ validator for committee in committee_lookup for validator in committee ] performance = [ inclusion_distance for committee in committee_performance for inclusion_distance in committee ] cursor.execute( f"SELECT f_slot FROM t_blocks WHERE f_slot > {slot} AND f_canonical = true ORDER BY f_slot LIMIT 1" ) earliest_inclusion_slot = cursor.fetchone()[0] min_inclusion_distance = earliest_inclusion_slot - slot - 1 #for d in performance: # if 1 + d - min_inclusion_distance == 0 and d != -1: # print(slot, earliest_inclusion_slot, d, min_inclusion_distance) effectiveness = [ 0 if d == -1 else 1 / (1 + d - min_inclusion_distance) for d in performance ] return dict(zip(validators, performance)), dict(zip(validators, effectiveness))
def fun(bs): f = one_from_bytes(bs) return bitlist([ 0 if fe25519.from_bytes(f.to_bytes()) == f else 255 ]).to_bytes()
def fun(bs): f = one_from_bytes(bs) return bitlist([0 if eval(str(f)) == f else 255]).to_bytes()
def test_is_negative( self, bits='5d52a943b343f940dea032ee1e3e3c98d45f76c55ac2020a06cd007279141271' ): fun = lambda bs: bitlist([one_from_bytes(bs).is_negative()]) return check_or_generate_operation(self, fun, 1, bits)
def test_is_zero( self, bits='0000000000000000000000000000000000000000000000000000000000000000' ): fun = lambda bs: bitlist([one_from_bytes(bs).is_zero()]) return check_or_generate_operation(self, fun, 1, bits)