Skip to content

Commit 23a28fb

Browse files
committed
add support for ctAddress
1 parent 62c7bd2 commit 23a28fb

File tree

2 files changed

+111
-5
lines changed

2 files changed

+111
-5
lines changed

README.md

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,30 @@ int_cipher_text, signature = build_string_input_text(plaintext, user_aes_key, se
246246

247247
- `input_text`: A dictionary of the form { "ciphertext": { "value": int[] }, "signature": bytes[] }
248248

249-
### 9. `generate_rsa_keypair()`
249+
### 9. `build_address_input_text(plaintext, user_aes_key, sender, contract, func_selector, signing_key)`
250+
251+
**Purpose:** Builds input text by encrypting the plaintext and signing it.
252+
253+
**Usage:**
254+
255+
```python
256+
int_cipher_text, signature = build_address_input_text(plaintext, user_aes_key, sender, contract, func_selector, signing_key)
257+
```
258+
259+
**Parameters:**
260+
261+
- `plaintext`: The plaintext message.
262+
- `user_aes_key`: The user's AES key.
263+
- `sender`: The sender's address.
264+
- `contract`: The contract address.
265+
- `func_selector`: The function selector.
266+
- `signing_key`: The private key used for signing.
267+
268+
**Returns:**
269+
270+
- `input_text`: A dictionary of the form { "ciphertext": { "ct1": int, "ct2": int, "ct3": int }, "signature1": bytes, "signature2": bytes, "signature3": bytes }
271+
272+
### 10. `generate_rsa_keypair()`
250273

251274
**Purpose:** Generates an RSA key pair.
252275

@@ -261,7 +284,7 @@ private_key_bytes, public_key_bytes = generate_rsa_keypair()
261284
- `private_key_bytes`: The serialized private key.
262285
- `public_key_bytes`: The serialized public key.
263286

264-
### 10. `encrypt_rsa(public_key_bytes, plaintext)`
287+
### 11. `encrypt_rsa(public_key_bytes, plaintext)`
265288

266289
**Purpose:** Encrypts plaintext using RSA encryption with a provided public key.
267290

@@ -280,7 +303,7 @@ ciphertext = encrypt_rsa(public_key_bytes, plaintext)
280303

281304
- `ciphertext`: The encrypted message.
282305

283-
### 11. `decrypt_rsa(private_key_bytes, ciphertext)`
306+
### 12. `decrypt_rsa(private_key_bytes, ciphertext)`
284307

285308
**Purpose:** Decrypts ciphertext using RSA decryption with a provided private key.
286309

@@ -299,7 +322,7 @@ plaintext = decrypt_rsa(private_key_bytes, ciphertext)
299322

300323
- `plaintext`: The decrypted message.
301324

302-
### 12. `decrypt_uint(ciphertext, user_key)`
325+
### 13. `decrypt_uint(ciphertext, user_key)`
303326

304327
**Purpose:** Decrypts a value stored in a contract using a user key
305328

@@ -318,7 +341,7 @@ plaintext = decrypt_uint(ciphertext, user_key)
318341

319342
- `result`: The decrypted value.
320343

321-
### 13. `decrypt_string(ciphertext, user_key)`
344+
### 14. `decrypt_string(ciphertext, user_key)`
322345

323346
**Purpose:** Decrypts a value stored in a contract using a user key
324347

@@ -337,6 +360,25 @@ plaintext = decrypt_string(ciphertext, user_key)
337360

338361
- `result`: The decrypted value.
339362

363+
### 15. `decrypt_address(ciphertext, user_key)`
364+
365+
**Purpose:** Decrypts a value stored in a contract and encrypted using a user key
366+
367+
**Usage:**
368+
369+
```python
370+
plaintext = decrypt_string(ciphertext, user_key)
371+
```
372+
373+
**Parameters:**
374+
375+
- `ciphertext`: A dictionary of the form { "ct1": int, "ct2": int, "ct3": int } where each cell holds a portion of the address encrypted
376+
- `userKey`: The user's AES key.
377+
378+
**Returns:**
379+
380+
- `result`: The decrypted address.
381+
340382
# Utilities (utils.py) Functions
341383

342384
### 1. `web3_connected(web3)`

coti/crypto_utils.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from cryptography.hazmat.primitives.asymmetric import rsa
77
from eth_keys import keys
88
from math import ceil
9+
from web3 import Web3
910

1011
block_size = AES.block_size
1112
address_size = 20
@@ -154,6 +155,47 @@ def build_string_input_text(plaintext, user_aes_key, sender, contract, function_
154155

155156
return input_text
156157

158+
def build_address_input_text(plaintext, user_aes_key, sender, contract, function_selector, signing_key):
159+
it_int_1 = build_input_text(
160+
int(plaintext[2:18], 16), # bytes 1 - 8
161+
user_aes_key,
162+
sender,
163+
contract,
164+
function_selector,
165+
signing_key
166+
)
167+
168+
it_int_2 = build_input_text(
169+
int(plaintext[18:34], 16), # bytes 9 - 16
170+
user_aes_key,
171+
sender,
172+
contract,
173+
function_selector,
174+
signing_key
175+
)
176+
177+
it_int_3 = build_input_text(
178+
int(plaintext[34:42], 16), # bytes 17 - 20
179+
user_aes_key,
180+
sender,
181+
contract,
182+
function_selector,
183+
signing_key
184+
)
185+
186+
input_text = {
187+
'ciphertext': {
188+
'ct1': it_int_1['ciphertext'],
189+
'ct2': it_int_2['ciphertext'],
190+
'ct3': it_int_3['ciphertext']
191+
},
192+
'signature1': it_int_1['signature'],
193+
'signature2': it_int_2['signature'],
194+
'signature3': it_int_3['signature']
195+
}
196+
197+
return input_text
198+
157199

158200
def decrypt_uint(ciphertext, user_key):
159201
# Convert ct to bytes (big-endian)
@@ -195,6 +237,28 @@ def decrypt_string(ciphertext, user_key):
195237

196238
return decrypted_string.strip('\0')
197239

240+
def decrypt_address(ciphertext, user_key):
241+
if isinstance(ciphertext, list): # format when reading ciphertext from a state variable
242+
__ciphertext = ciphertext
243+
else: # format when reading ciphertext from an event
244+
__ciphertext = list(ciphertext.values())
245+
246+
addr = '0x'
247+
248+
decrypted = decrypt_uint(__ciphertext[0], user_key)
249+
250+
addr += hex(decrypted)[2:].rjust(16, '0') # 8 bytes is 16 characters
251+
252+
decrypted = decrypt_uint(__ciphertext[1], user_key)
253+
254+
addr += hex(decrypted)[2:].rjust(16, '0') # 8 bytes is 16 characters
255+
256+
decrypted = decrypt_uint(__ciphertext[2], user_key)
257+
258+
addr += hex(decrypted)[2:].rjust(8, '0') # 4 bytes is 8 characters
259+
260+
return Web3.to_checksum_address(addr)
261+
198262

199263
def generate_rsa_keypair():
200264
# Generate RSA key pair

0 commit comments

Comments
 (0)