File size: 2,635 Bytes
91b6074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import gradio as gr
import pandas as pd
from pryvx import OPRF, PHE

hash_key = OPRF.get_key()

phe1 = PHE()
public_key, private_key = phe1.keygen()


def display_csv_a():
    # ADD YOUR DATASET HERE
    global dataset_0
    dataset_0 = pd.DataFrame({
        'location': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Miami']
    })
    return dataset_0.head().to_markdown()

def process_query(query):
    
    query = query.lower()
    dataset_0[dataset_0.columns[0]] = dataset_0[dataset_0.columns[0]].str.lower()

    encrypted_db = []
    for x in dataset_0[dataset_0.columns[0]].to_list():
        encrypted_db.append(phe1.encrypt(public_key, int(dataset_0.index[dataset_0[dataset_0.columns[0]] == x][0])))
    
    dataset_1 = pd.DataFrame({
        dataset_0.columns[0]: encrypted_db
    })

    index_x = dataset_0.index[dataset_0[dataset_0.columns[0]] == query]

    if len(index_x):

        enc_query = phe1.encrypt(public_key, int(index_x[0]))

        results = []
        for enc_value in encrypted_db:
            results.append(phe1.homomorphic_sub(enc_value, enc_query, public_key))

        decrypted_results = []
        for x in results:
            decrypted_results.append(phe1.decrypt(public_key, private_key, x) == 0)
        
        if sum(decrypted_results):
            return enc_query, dataset_1.head().to_markdown(), "True"
        else:
            return enc_query, dataset_1.head().to_markdown(), "False"
    else:
        return "None", dataset_1.head().to_markdown(), "False"

with gr.Blocks() as app:
    gr.Markdown("# Encrypted Search")
    gr.Markdown("### This is demo on Private information retrieval (PIR) which is a cryptographic protocol that allows a user to retrieve a specific item from a database server without revealing which item was requested. The main goal of PIR is to protect the privacy of the user's query.")

    with gr.Row():
        # Party A Section
        with gr.Column():
            gr.Markdown("### Private Database on Server")
            output_a = gr.Markdown()

            input_query = gr.Textbox(label="Enter item to be searched")
            search_button_a = gr.Button("Encrypted Search")

            output_result = gr.Textbox(label="Item Found")
            encrypted_query = gr.Textbox(label="Encrypted Input")
            encrypted_db = gr.Markdown(label="Encrypted Database")

            app.load(display_csv_a, inputs=None, outputs=output_a)

            search_button_a.click(
                process_query,
                inputs=[input_query],
                outputs=[encrypted_query, encrypted_db, output_result]
            )

app.launch()