It works correctly if the inputset has “small numbers” integer, however, I am getting an error when the compiler tries to compile the circuit based on the above input data:
" … of type int which is not acceptable either because of the type or because of overflow"
I need that to encrypt a private key of an Ethereum account. In the input set I put an example of private key.
Many thanks @umutsahin for your quick reply and great help. Nice catch. You code works for me perfectly for the example of sample = (0x123, 0x456). However, it seems there is an issue with calculating the results for real private key (big numbers) as follows:
from concrete import fhe
import secrets
from ast import literal_eval
import numpy as np
from concrete import fhe
def to_chunks(number, width=256, chunk_size=8):
assert width % chunk_size == 0
return [
(number >> i) & ((2**chunk_size) - 1)
for i in range(0, width, chunk_size)
]
def to_number(chunks, chunk_size=8):
return sum(byte << (chunk_size * i) for i, byte in enumerate(chunks))
def add(x, y):
return x + y
compiler = fhe.Compiler(add, {"x": "encrypted", "y": "clear"})
inputset = [
(
48915617476484211273115281063704461783033490425405257564258124598871191647089,
48915617476484211273115281063704461783033490425405257564258124598871191647089,
),
(
0x0,
0x0,
),
(
0x3350,
0x3350,
)
]
chunked_inputset = [tuple(to_chunks(value) for value in input) for input in inputset]
circuit = compiler.compile(chunked_inputset, show_graph=True)
circuit.keys.generate()
sk1 = secrets.token_hex(32)
print(sk1)
sk2 = secrets.token_hex(32)
print(sk2)
sample = (literal_eval("0x" + sk1), literal_eval("0x" + sk2))
chunked_sample = tuple(to_chunks(value) for value in sample)
chunked_result = circuit.encrypt_run_decrypt(*chunked_sample)
result = to_number(chunked_result)
print (result)
print (sample)
assert result == sample[0] + sample[1]
Thank you @umutsahin again. Yes, indeed it is much slower now. Even I waited for ages and I didn’t get it run. I just killed the process. I see that the statement: