Skip to content

Commit 36c8fce

Browse files
committed
add support for ctAddress
1 parent 9126ab9 commit 36c8fce

2 files changed

Lines changed: 111 additions & 5 deletions

File tree

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
@@ -153,6 +154,47 @@ def build_string_input_text(plaintext, user_aes_key, sender, contract, function_
153154

154155
return input_text
155156

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

157199
def decrypt_uint(ciphertext, user_key):
158200
# Convert ct to bytes (big-endian)
@@ -194,6 +236,28 @@ def decrypt_string(ciphertext, user_key):
194236

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

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

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

0 commit comments

Comments
 (0)