def test_vectors4(): sk1 = PrivateKey.from_seed(bytes([1, 2, 3, 4, 5])) sk2 = PrivateKey.from_seed(bytes([1, 2, 3, 4, 5, 6])) pk1 = sk1.get_public_key() pk2 = sk2.get_public_key() m1 = bytes([7, 8, 9]) m2 = bytes([10, 11, 12]) sig9 = sk1.sign_prepend(m1) sig10 = sk2.sign_prepend(m2) assert (sig9.serialize() == bytes.fromhex( "d2135ad358405d9f2d4e68dc253d64b6049a821797817cffa5aa804086a8fb7b135175bb7183750e3aa19513db1552180f0b0ffd513c322f1c0c30a0a9c179f6e275e0109d4db7fa3e09694190947b17d890f3d58fe0b1866ec4d4f5a59b16ed" )) assert (sig10.serialize() == bytes.fromhex( "cc58c982f9ee5817d4fbf22d529cfc6792b0fdcf2d2a8001686755868e10eb32b40e464e7fbfe30175a962f1972026f2087f0495ba6e293ac3cf271762cd6979b9413adc0ba7df153cf1f3faab6b893404c2e6d63351e48cd54e06e449965f08" )) agg_sig = PrependSignature.aggregate([sig9, sig9, sig10]) message_hashes = [Util.hash256(m1), Util.hash256(m1), Util.hash256(m2)] pks = [pk1, pk1, pk2] assert (agg_sig.serialize() == bytes.fromhex( "c37077684e735e62e3f1fd17772a236b4115d4b581387733d3b97cab08b90918c7e91c23380c93e54be345544026f93505d41e6000392b82ab3c8af1b2e3954b0ef3f62c52fc89f99e646ff546881120396c449856428e672178e5e0e14ec894" )) assert (agg_sig.verify(message_hashes, pks))
def additional_python_methods(): private_key = PrivateKey.from_seed(b'123') s1 = private_key.sign(b'message') s2 = private_key.sign_prepend(b'message') assert s1.get_insecure_sig().verify([Util.hash256(b'message')], [private_key.get_public_key()]) assert s2.get_insecure_sig().verify([ Util.hash256(private_key.get_public_key().serialize() + Util.hash256(b'message')) ], [private_key.get_public_key()]) s1_b = Signature.from_insecure_sig(s1.get_insecure_sig()) s2_b = PrependSignature.from_insecure_sig(s2.get_insecure_sig()) assert s1 == s1_b and s2 == s2_b s3 = private_key.sign_insecure_prehashed(Util.hash256(b'456')) assert s3.verify([Util.hash256(b'456')], [private_key.get_public_key()]) esk = ExtendedPrivateKey.from_seed(b'789') epk = esk.get_public_key() s3 = private_key.sign(b'message3') s4 = private_key.sign_insecure(b'message3') assert bytes(private_key) == private_key.serialize() assert deepcopy(private_key) == private_key assert deepcopy(s1) == s1 assert deepcopy(s2) == s2 assert deepcopy(s3) == s3 assert deepcopy(s4) == s4 assert deepcopy( private_key.get_public_key()) == private_key.get_public_key() assert deepcopy(esk) == esk assert deepcopy(epk) == epk assert deepcopy(esk.get_chain_code()) == esk.get_chain_code()
async def request_header_signature( self, request: harvester_protocol.RequestHeaderSignature ): """ The farmer requests a signature on the header hash, for one of the proofs that we found. A signature is created on the header hash using the plot private key. """ if request.quality_string not in self.challenge_hashes: return _, filename, _ = self.challenge_hashes[request.quality_string] plot_sk = PrivateKey.from_bytes( bytes.fromhex(self.plot_config["plots"][filename]["sk"]) ) header_hash_signature: PrependSignature = plot_sk.sign_prepend( request.header_hash ) assert header_hash_signature.verify( [Util.hash256(request.header_hash)], [plot_sk.get_public_key()] ) response: harvester_protocol.RespondHeaderSignature = harvester_protocol.RespondHeaderSignature( request.quality_string, header_hash_signature, ) yield OutboundMessage( NodeType.FARMER, Message("respond_header_signature", response), Delivery.RESPOND, )
def additional_python_methods(): private_key = PrivateKey.from_seed(b'123') s1 = private_key.sign(b'message') s2 = private_key.sign_prepend(b'message') assert s1.get_insecure_sig().verify([Util.hash256(b'message')], [private_key.get_public_key()]) assert s2.get_insecure_sig().verify([ Util.hash256(private_key.get_public_key().serialize() + Util.hash256(b'message')) ], [private_key.get_public_key()]) s1_b = Signature.from_insecure_sig(s1.get_insecure_sig()) s2_b = PrependSignature.from_insecure_sig(s2.get_insecure_sig()) assert s1 == s1_b and s2 == s2_b s3 = private_key.sign_insecure_prehashed(Util.hash256(b'456')) assert s3.verify([Util.hash256(b'456')], [private_key.get_public_key()])
def test_threshold_instance(T, N): commitments = [] # fragments[i][j] = fragment held by player i, # received from player j fragments = [[None] * N for _ in range(N)] secrets = [] # Step 1 : Threshold.create for player in range(N): secret_key, commi, frags = Threshold.create(T, N) for target, frag in enumerate(frags): fragments[target][player] = frag commitments.append(commi) secrets.append(secret_key) # Step 2 : Threshold.verify_secret_fragment for player_source in range(1, N + 1): for player_target in range(1, N + 1): assert Threshold.verify_secret_fragment( player_target, fragments[player_target - 1][player_source - 1], commitments[player_source - 1], T) # Step 3 : master_pubkey = PublicKey.aggregate_insecure(...) # secret_share = PrivateKey.aggregate_insecure(...) master_pubkey = PublicKey.aggregate_insecure( [commitments[i][0] for i in range(N)]) secret_shares = [ PrivateKey.aggregate_insecure(fragment_row) for fragment_row in fragments ] master_privkey = PrivateKey.aggregate_insecure(secrets) msg = ("Test").encode("utf-8") signature_actual = master_privkey.sign_insecure(msg) # Step 4 : sig_share = Threshold.sign_with_coefficient(...) # Check every combination of T players for X in combinations(range(1, N + 1), T): # X: a list of T indices like [1, 2, 5] # Check signatures signature_shares = [ Threshold.sign_with_coefficient(secret_shares[x - 1], msg, x, X) for x in X ] signature_cand = InsecureSignature.aggregate(signature_shares) assert signature_cand == signature_actual # Check that the signature actually verifies the message assert signature_actual.verify([Util.hash256(msg)], [master_pubkey]) # Step 4b : Alternatively, we can add the lagrange coefficients # to 'unit' signatures. for X in combinations(range(1, N + 1), T): # X: a list of T indices like [1, 2, 5] # Check signatures signature_shares = [secret_shares[x - 1].sign_insecure(msg) for x in X] signature_cand = Threshold.aggregate_unit_sigs(signature_shares, X) assert signature_cand == signature_actual
async def respond_header_signature( self, response: harvester_protocol.RespondHeaderSignature ): """ Receives a signature on a block header hash, which is required for submitting a block to the blockchain. """ header_hash: bytes32 = self.harvester_responses_header_hash[ response.quality_string ] proof_of_space: bytes32 = self.harvester_responses_proofs[ response.quality_string ] plot_pubkey = self.harvester_responses_proofs[ response.quality_string ].plot_pubkey assert response.header_hash_signature.verify( [Util.hash256(header_hash)], [plot_pubkey] ) pos_hash: bytes32 = proof_of_space.get_hash() request = farmer_protocol.HeaderSignature( pos_hash, header_hash, response.header_hash_signature ) yield OutboundMessage( NodeType.FULL_NODE, Message("header_signature", request), Delivery.BROADCAST )
async def respond_partial_proof( self, response: harvester_protocol.RespondPartialProof): """ Receives a signature on the hash of the farmer payment target, which is used in a pool share, to tell the pool where to pay the farmer. """ farmer_target = bytes.fromhex(self.key_config["wallet_target"]) plot_pubkey = self.harvester_responses_proofs[ response.quality_string].plot_pubkey assert response.farmer_target_signature.verify( [Util.hash256(farmer_target)], [plot_pubkey])