Skip to content

Commit b2d08ae

Browse files
spencer-xyzgmesika-coti
authored andcommitted
move decrypt functions to crypto_utils
1 parent bed1bdd commit b2d08ae

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

coti/crypto_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,36 @@ def build_string_input_text(plaintext, user_aes_key, sender, contract, func_sig,
152152

153153
return encrypted_str
154154

155+
def decrypt_uint(contract_value, user_key):
156+
# Convert ct to bytes (big-endian)
157+
byte_array = contract_value.to_bytes(32, byteorder='big')
158+
159+
# Split ct into two 128-bit arrays r and cipher
160+
cipher = byte_array[:block_size]
161+
r = byte_array[block_size:]
162+
163+
# Decrypt the cipher
164+
decrypted_message = decrypt(user_key, r, cipher)
165+
166+
# Print the decrypted cipher
167+
decrypted_balance = int.from_bytes(decrypted_message, 'big')
168+
169+
return decrypted_balance
170+
171+
def decrypt_string(contract_value, user_key):
172+
string_from_input_tx = ""
173+
for input_text_from_tx in contract_value:
174+
decrypted_input_from_tx = decrypt_uint(input_text_from_tx, user_key)
175+
byte_length = (decrypted_input_from_tx.bit_length() + 7) // 8 # calculate the byte length
176+
177+
# Convert the integer to bytes
178+
decrypted_bytes = decrypted_input_from_tx.to_bytes(byte_length, byteorder='big')
179+
180+
# Decode the bytes to a string
181+
string_from_input_tx += decrypted_bytes.decode('utf-8')
182+
183+
return string_from_input_tx
184+
155185
def generate_rsa_keypair():
156186
# Generate RSA key pair
157187
private_key = rsa.generate_private_key(

coti/utils.py

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from web3 import Web3
55
from web3.middleware import geth_poa_middleware
66

7-
from coti.crypto_utils import block_size, decrypt
8-
97
SOLC_VERSION = '0.8.19'
108

119

@@ -156,35 +154,4 @@ def sign_and_send_tx(web3, private_key, transaction):
156154
tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
157155
except Exception as e:
158156
raise Exception(f"Failed to wait for the transaction receipt: {e}")
159-
return tx_receipt
160-
161-
162-
def decrypt_uint(contract_value, user_key):
163-
# Convert ct to bytes (big-endian)
164-
byte_array = contract_value.to_bytes(32, byteorder='big')
165-
166-
# Split ct into two 128-bit arrays r and cipher
167-
cipher = byte_array[:block_size]
168-
r = byte_array[block_size:]
169-
170-
# Decrypt the cipher
171-
decrypted_message = decrypt(user_key, r, cipher)
172-
173-
# Print the decrypted cipher
174-
decrypted_balance = int.from_bytes(decrypted_message, 'big')
175-
176-
return decrypted_balance
177-
178-
def decrypt_string(contract_value, user_key):
179-
string_from_input_tx = ""
180-
for input_text_from_tx in contract_value:
181-
decrypted_input_from_tx = decrypt_uint(input_text_from_tx, user_key)
182-
byte_length = (decrypted_input_from_tx.bit_length() + 7) // 8 # calculate the byte length
183-
184-
# Convert the integer to bytes
185-
decrypted_bytes = decrypted_input_from_tx.to_bytes(byte_length, byteorder='big')
186-
187-
# Decode the bytes to a string
188-
string_from_input_tx += decrypted_bytes.decode('utf-8')
189-
190-
return string_from_input_tx
157+
return tx_receipt

0 commit comments

Comments
 (0)