def __init__(self, PK=None, SK=None): if not PK or not SK: self.d = Dilithium() else: self.d = Dilithium(PK, SK) self.PK = self.d.getPK() self.SK = self.d.getSK()
def create_ephemeral_channel(msg_id: bytes, ttl: int, ttr: int, addr_from: bytes, kyber_pk: bytes, kyber_sk: bytes, receiver_kyber_pk: bytes, dilithium_pk: bytes, dilithium_sk: bytes, prf512_seed: bytes, data: bytes, nonce: int): sender_kyber = Kyber(kyber_pk, kyber_sk) sender_kyber.kem_encode(receiver_kyber_pk) enc_aes256_symkey = bytes(sender_kyber.getCypherText()) aes256_symkey = sender_kyber.getMyKey() aes = AES(bytes(aes256_symkey)) sender_dilithium = Dilithium(dilithium_pk, dilithium_sk) ephemeral_data = EphemeralChannelPayload.create(addr_from, prf512_seed, data) ephemeral_data.dilithium_sign(msg_id, ttl, ttr, enc_aes256_symkey, nonce, sender_dilithium) encrypted_ephemeral_message = EncryptedEphemeralMessage() encrypted_ephemeral_message._data.msg_id = msg_id encrypted_ephemeral_message._data.ttl = ttl encrypted_ephemeral_message._data.ttr = ttr encrypted_ephemeral_message._data.channel.enc_aes256_symkey = enc_aes256_symkey encrypted_ephemeral_message._data.nonce = nonce encrypted_ephemeral_message._data.payload = aes.encrypt( ephemeral_data.to_json().encode()) return encrypted_ephemeral_message
def test_dilithium_reference2(self): pk = bytes(hstr2bin(self.PK1_HSTR)) sk = bytes(hstr2bin(self.SK1_HSTR)) dilithium = Dilithium(pk, sk) self.assertEqual(1472, len(dilithium.getPK())) self.assertEqual(3504, len(dilithium.getSK())) message = bytes(b"This is a test") message_signed = dilithium.sign(message) data_out = ucharVector(len(message_signed)) Dilithium.sign_open(data_out, message_signed, dilithium.getPK()) message_out = Dilithium.extract_message(data_out) signature_out = Dilithium.extract_signature(data_out) self.assertEqual(2715, len(data_out)) self.assertEqual(len(message_out), len(message_out)) self.assertEqual(2701, len(signature_out)) self.assertEqual(message, bytes(message_out)) self.assertEqual(b"This is a test", bytes(message_out))
class Dil(): def __init__(self, PK=None, SK=None): if not PK or not SK: self.d = Dilithium() else: self.d = Dilithium(PK, SK) self.PK = self.d.getPK() self.SK = self.d.getSK() def sign(self, message): #message should be bytes signature = self.d.sign(message) return signature def verify(self, message, signature, PK): data_out = ucharVector(len(signature)) self.d.sign_open(data_out, signature, PK) message_out = bytes(self.d.extract_message(data_out)) signature_out = self.d.extract_signature(data_out) if message_out != message: return False if len(signature_out) != 2701: return False return True
def test_dilithium_reference(self): dilithium = Dilithium() self.assertEqual(1472, len(dilithium.getPK())) self.assertEqual(3504, len(dilithium.getSK())) message = bytes(b"This is a test") message_signed = dilithium.sign(message) data_out = ucharVector(len(message_signed)) Dilithium.sign_open(data_out, message_signed, dilithium.getPK()) message_out = Dilithium.extract_message(data_out) signature_out = Dilithium.extract_signature(data_out) self.assertEqual(2715, len(data_out)) self.assertEqual(len(message_out), len(message_out)) self.assertEqual(2701, len(signature_out)) self.assertEqual(message, bytes(message_out)) self.assertEqual(b"This is a test", bytes(message_out)) print(bin2hstr(dilithium.getPK())) print(bin2hstr(dilithium.getSK()))
def test_add_4(self, mock_difficulty_tracker_get): with set_qrl_dir('wallet_ver1'): with State() as state: with mocked_genesis() as custom_genesis: chain_manager = ChainManager(state) chain_manager._difficulty_tracker = Mock() tmp_difficulty = StringToUInt256('2') tmp_target = DifficultyTracker.get_target(tmp_difficulty) mock_difficulty_tracker_get.return_value = [ tmp_difficulty, tmp_target ] alice_xmss = get_alice_xmss() slave_xmss = XMSS( XmssFast(alice_xmss.seed, alice_xmss.height)) random_xmss1 = get_random_xmss() random_kyber1 = Kyber() random_dilithium1 = Dilithium() random_xmss2 = get_random_xmss() random_kyber2 = Kyber() random_dilithium2 = Dilithium() message = b'Hello World How are you?' prf512_seed = b'10192' custom_genesis.genesis_balance.extend([ qrl_pb2.GenesisBalance(address=random_xmss1.address, balance=65000000000000000) ]) custom_genesis.genesis_balance.extend([ qrl_pb2.GenesisBalance(address=random_xmss2.address, balance=65000000000000000) ]) chain_manager.load(custom_genesis) with mock.patch('qrl.core.misc.ntp.getTime') as time_mock: time_mock.return_value = 1615270948 lattice_public_key_txn = LatticePublicKey.create( fee=1, kyber_pk=random_kyber1.getPK(), dilithium_pk=random_dilithium1.getPK(), xmss_pk=random_xmss1.pk) lattice_public_key_txn._data.nonce = 1 lattice_public_key_txn.sign(random_xmss1) genesis_block = GenesisBlock() tmp_block1 = Block.create( block_number=1, prev_block_headerhash=genesis_block.headerhash, prev_block_timestamp=genesis_block.timestamp, transactions=[lattice_public_key_txn], miner_address=slave_xmss.address) # Mine the nonce while not PoWValidator().validate_mining_nonce( state, tmp_block1.blockheader, False): tmp_block1.set_nonces(tmp_block1.mining_nonce + 1, 0) res = chain_manager.add_block(block=tmp_block1) self.assertTrue(res) # Need to move forward the time to align with block times time_mock.return_value += config.dev.minimum_minting_delay * 2 encrypted_eph_message = create_ephemeral_channel( msg_id=lattice_public_key_txn.txhash, ttl=time_mock.return_value, ttr=0, addr_from=random_xmss2.address, kyber_pk=random_kyber2.getPK(), kyber_sk=random_kyber2.getSK(), receiver_kyber_pk=random_kyber1.getPK(), dilithium_pk=random_dilithium2.getPK(), dilithium_sk=random_dilithium2.getSK(), prf512_seed=prf512_seed, data=message, nonce=1) chain_manager.state.update_ephemeral( encrypted_eph_message) eph_metadata = chain_manager.state.get_ephemeral_metadata( lattice_public_key_txn.txhash) # Decrypting Payload encrypted_eph_message = eph_metadata.encrypted_ephemeral_message_list[ 0] encrypted_payload = encrypted_eph_message.payload random_kyber1.kem_decode( encrypted_eph_message.channel.enc_aes256_symkey) aes_key = bytes(random_kyber1.getMyKey()) myAES = AES(aes_key) decrypted_payload = myAES.decrypt(encrypted_payload) ephemeral_channel_payload = EphemeralChannelPayload.from_json( decrypted_payload) self.assertEqual(ephemeral_channel_payload.prf512_seed, b'10192') self.assertEqual(ephemeral_channel_payload.data, b'Hello World How are you?') # TODO (cyyber): Add Ephemeral Testing code using Naive RNG tmp_block2 = Block.create( block_number=2, prev_block_headerhash=tmp_block1.headerhash, prev_block_timestamp=tmp_block1.timestamp, transactions=[], miner_address=slave_xmss.address) # Mine the nonce while not PoWValidator().validate_mining_nonce( state, tmp_block2.blockheader, False): tmp_block2.set_nonces(tmp_block2.mining_nonce + 1, 0) res = chain_manager.add_block(block=tmp_block2) self.assertTrue(res) # Need to move forward the time to align with block times time_mock.return_value += config.dev.minimum_minting_delay * 2 tmp_block3 = Block.create( block_number=3, prev_block_headerhash=tmp_block2.headerhash, prev_block_timestamp=tmp_block1.timestamp, transactions=[], miner_address=slave_xmss.address) # Mine the nonce while not PoWValidator().validate_mining_nonce( state, tmp_block3.blockheader, False): tmp_block3.set_nonces(tmp_block3.mining_nonce + 1, 0) res = chain_manager.add_block(block=tmp_block3) self.assertTrue(res) time_mock.return_value += config.dev.minimum_minting_delay tmp_block4 = Block.create( block_number=4, prev_block_headerhash=tmp_block3.headerhash, prev_block_timestamp=tmp_block1.timestamp, transactions=[], miner_address=slave_xmss.address) # Mine the nonce while not PoWValidator().validate_mining_nonce( state, tmp_block4.blockheader, False): tmp_block4.set_nonces(tmp_block4.mining_nonce + 1, 0) res = chain_manager.add_block(block=tmp_block4) self.assertTrue(res) address_state = chain_manager.get_address( random_xmss1.address) self.assertEqual( address_state.latticePK_list[0].kyber_pk, lattice_public_key_txn.kyber_pk) self.assertEqual( address_state.latticePK_list[0].dilithium_pk, lattice_public_key_txn.dilithium_pk) self.assertEqual(address_state.address, lattice_public_key_txn.addr_from) random_xmss1_state = chain_manager.get_address( random_xmss1.address) self.assertEqual(64999999999999999, random_xmss1_state.balance)