Skip to content

Commit

Permalink
single party compute examples complete (modulo correlation coefficient)
Browse files Browse the repository at this point in the history
  • Loading branch information
Davetbutler committed Jun 6, 2024
1 parent 7d0ec75 commit 621bc00
Show file tree
Hide file tree
Showing 30 changed files with 1,471 additions and 1,116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,62 @@

# The 1st Party stores a secret
async def main():

cluster_id = os.getenv("NILLION_CLUSTER_ID")

cluster_id = os.getenv("NILLION_CLUSTER_ID")
client_1 = create_nillion_client(
getUserKeyFromFile(CONFIG_PARTY_1["userkey_file"]), getNodeKeyFromFile(CONFIG_PARTY_1["nodekey_file"])
)
party_id_1 = client_1.party_id
user_id_1 = client_1.user_id

program_mir_path = f"../../programs-compiled/{CONFIG_PROGRAM_NAME}.nada.bin"

# 1st Party stores program
action_id = await client_1.store_program(
cluster_id, CONFIG_PROGRAM_NAME, program_mir_path
)

program_id = f"{user_id_1}/{CONFIG_PROGRAM_NAME}"

program_name = "simple"

program_id = f"{Test.programs_namespace}/{program_name}"

permissions = py_nillion_client.Permissions.default_for_user(client.user_id)
permissions.add_compute_permissions({client.user_id: {program_id}})

inputs = Test.load_inputs(program_name)
receipt = await self.pay(
client, py_nillion_client.Operation.store_secrets(inputs.store_secrets)
)
store_id = await client.store_secrets(
self.cluster_id, inputs.store_secrets, permissions, receipt
)

bindings = py_nillion_client.ProgramBindings(program_id)
bindings.add_input_party("Dealer", client.party_id)
bindings.add_output_party("Result", client.party_id)
receipt = await self.pay(
client,
py_nillion_client.Operation.compute(program_id, inputs.compute_secrets),
)
uuid = await client.compute(
self.cluster_id,
bindings,
[store_id],
inputs.compute_secrets,
inputs.compute_public_variables,
receipt,
)


######





program_mir_path=f"../../programs-compiled/{CONFIG_PROGRAM_NAME}.nada.bin"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
from cosmpy.aerial.wallet import LocalWallet
from cosmpy.crypto.keypairs import PrivateKey


sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
from helpers.nillion_client_helper import create_nillion_client, pay, create_payments_config
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
from helpers.nillion_client_helper import (
create_nillion_client,
pay,
create_payments_config,
)
from helpers.nillion_keypath_helper import getUserKeyFromFile, getNodeKeyFromFile

load_dotenv()


# 1 Party running simple addition on 1 stored secret and 1 compute time secret
async def main():
cluster_id = os.getenv("NILLION_CLUSTER_ID")
Expand All @@ -26,53 +30,61 @@ async def main():
client = create_nillion_client(userkey, nodekey)
party_id = client.party_id
user_id = client.user_id
party_name="Party1"
program_name="addition_simple"
program_mir_path=f"../../programs-compiled/{program_name}.nada.bin"
party_name = "Party1"
program_name = "addition_simple"
program_mir_path = f"../../programs-compiled/{program_name}.nada.bin"

# Create payments config and set up Nillion wallet with a private key to pay for storage and compute operations
payments_config = create_payments_config(chain_id, grpc_endpoint)
payments_client = LedgerClient(payments_config)
payments_wallet = LocalWallet(
PrivateKey(bytes.fromhex(os.getenv("NILLION_WALLET_PRIVATE_KEY"))), prefix="nillion"
PrivateKey(bytes.fromhex(os.getenv("NILLION_WALLET_PRIVATE_KEY"))),
prefix="nillion",
)

##### STORE PROGRAM
print('-----STORE PROGRAM')
print("-----STORE PROGRAM")

# Get cost quote, then pay for operation to store program
receipt_store_program = await pay(
client,
nillion.Operation.store_program(),
payments_wallet,
payments_client,
cluster_id)
cluster_id,
)

# Store program, passing in the receipt that shows proof of payment
action_id = await client.store_program(
cluster_id, program_name, program_mir_path, receipt_store_program
)

# Print details about stored program
program_id=f"{user_id}/{program_name}"
print('Stored program. action_id:', action_id)
print('Stored program_id:', program_id)
program_id = f"{user_id}/{program_name}"
print("Stored program. action_id:", action_id)
print("Stored program_id:", program_id)

##### STORE SECRETS
print('-----STORE SECRETS')
print("-----STORE SECRETS")

# Create a secret
stored_secret = nillion.Secrets({
"my_int1": nillion.SecretInteger(500),
})
stored_secret = nillion.Secrets(
{
"my_int1": nillion.SecretInteger(500),
}
)

# Create a permissions object to attach to the stored secret
# Create a permissions object to attach to the stored secret
permissions = nillion.Permissions.default_for_user(client.user_id)
permissions.add_compute_permissions({client.user_id: {program_id}})

# Get cost quote, then pay for operation to store the secret
receipt_store = await pay(
client, nillion.Operation.store_secrets(stored_secret), payments_wallet, payments_client, cluster_id
client,
nillion.Operation.store_secrets(stored_secret),
payments_wallet,
payments_client,
cluster_id,
)

# Store a secret, passing in the receipt that shows proof of payment
Expand All @@ -81,7 +93,7 @@ async def main():
)

##### COMPUTE
print('-----COMPUTE')
print("-----COMPUTE")

# Bind the parties in the computation to the client to set input and output parties
compute_bindings = nillion.ProgramBindings(program_id)
Expand All @@ -91,7 +103,10 @@ async def main():
# Get cost quote, then pay for operation to compute
receipt_compute = await pay(
client,
nillion.Operation.compute(program_id, stored_secret), payments_wallet, payments_client, cluster_id
nillion.Operation.compute(program_id, stored_secret),
payments_wallet,
payments_client,
cluster_id,
)

# Create a computation time secret to use
Expand All @@ -117,11 +132,13 @@ async def main():
print(f"✅ Compute complete for compute_id {compute_event.uuid}")
print(f"🖥️ The result is {compute_event.result.value}")
return compute_event.result.value



if __name__ == "__main__":
asyncio.run(main())


@pytest.mark.asyncio
async def test_main():
result = await main()
assert result == {'my_output': 510}
assert result == {"my_output": 510}
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,82 @@

from dotenv import load_dotenv

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
from helpers.nillion_client_helper import create_nillion_client
from cosmpy.aerial.client import LedgerClient
from cosmpy.aerial.wallet import LocalWallet
from cosmpy.crypto.keypairs import PrivateKey

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
from helpers.nillion_client_helper import (
create_nillion_client,
pay,
create_payments_config,
)
from helpers.nillion_keypath_helper import getUserKeyFromFile, getNodeKeyFromFile

load_dotenv()


# 1 Party running a simple circuit on 2 stored secrets and 2 compute time secrets
async def main():
cluster_id = os.getenv("NILLION_CLUSTER_ID")
grpc_endpoint = os.getenv("NILLION_GRPC")
chain_id = os.getenv("NILLION_CHAIN_ID")
userkey = getUserKeyFromFile(os.getenv("NILLION_USERKEY_PATH_PARTY_1"))
nodekey = getNodeKeyFromFile(os.getenv("NILLION_NODEKEY_PATH_PARTY_1"))
client = create_nillion_client(userkey, nodekey)
party_id = client.party_id
user_id = client.user_id
party_name="Party1"
program_name="circuit_simple"
program_mir_path=f"../../programs-compiled/{program_name}.nada.bin"
party_name = "Party1"
program_name = "circuit_simple"
program_mir_path = f"../../programs-compiled/{program_name}.nada.bin"

payments_config = create_payments_config(chain_id, grpc_endpoint)
payments_client = LedgerClient(payments_config)
payments_wallet = LocalWallet(
PrivateKey(bytes.fromhex(os.getenv("NILLION_WALLET_PRIVATE_KEY"))),
prefix="nillion",
)

receipt_store_program = await pay(
client,
nillion.Operation.store_program(),
payments_wallet,
payments_client,
cluster_id,
)

# store program
action_id = await client.store_program(
cluster_id, program_name, program_mir_path
cluster_id, program_name, program_mir_path, receipt_store_program
)

program_id=f"{user_id}/{program_name}"
print('Stored program. action_id:', action_id)
print('Stored program_id:', program_id)
program_id = f"{user_id}/{program_name}"
print("Stored program. action_id:", action_id)
print("Stored program_id:", program_id)

# Set permissions for the client to compute on the program
permissions = nillion.Permissions.default_for_user(client.user_id)
permissions.add_compute_permissions({client.user_id: {program_id}})

# Create a secret
stored_secret = nillion.Secrets({
"my_int1": nillion.SecretInteger(3),
"my_int2": nillion.SecretInteger(4),
})
secret_bindings = nillion.ProgramBindings(program_id)
secret_bindings.add_input_party(party_name, party_id)
stored_secret = nillion.Secrets(
{
"my_int1": nillion.SecretInteger(3),
"my_int2": nillion.SecretInteger(4),
}
)

receipt_store = await pay(
client,
nillion.Operation.store_secrets(stored_secret),
payments_wallet,
payments_client,
cluster_id,
)

# Store a secret
store_id = await client.store_secrets(
cluster_id, secret_bindings, stored_secret, None
cluster_id, stored_secret, permissions, receipt_store
)

# Bind the parties in the computation to the client to set input and output parties
Expand All @@ -54,18 +92,27 @@ async def main():
print(f"Computing using program {program_id}")
print(f"Use secret store_id: {store_id}")

computation_time_secrets = nillion.Secrets({
"my_int3": nillion.SecretInteger(2),
"my_int4": nillion.SecretInteger(5)
})

computation_time_secrets = nillion.Secrets(
{"my_int3": nillion.SecretInteger(2), "my_int4": nillion.SecretInteger(5)}
)

# Pay for the compute
receipt_compute = await pay(
client,
nillion.Operation.compute(program_id, computation_time_secrets),
payments_wallet,
payments_client,
cluster_id,
)

# Compute on the secret
compute_id = await client.compute(
cluster_id,
compute_bindings,
[store_id],
computation_time_secrets,
nillion.PublicVariables({}),
receipt_compute,
)

# Print compute result
Expand All @@ -76,11 +123,13 @@ async def main():
print(f"✅ Compute complete for compute_id {compute_event.uuid}")
print(f"🖥️ The result is {compute_event.result.value}")
return compute_event.result.value



if __name__ == "__main__":
asyncio.run(main())


@pytest.mark.asyncio
async def test_main():
result = await main()
assert result == {'my_output': 22}
assert result == {"my_output": 22}
Loading

0 comments on commit 621bc00

Please sign in to comment.