From 07844f0fca1e54f7f1b40d957beef018e10accea Mon Sep 17 00:00:00 2001 From: oceans404 Date: Thu, 10 Oct 2024 10:33:05 -0700 Subject: [PATCH 1/2] feat: add auction programs --- nada-project.toml | 10 ++++++++++ src/auction.py | 25 +++++++++++++++++++++++++ src/auction_can_tie.py | 29 +++++++++++++++++++++++++++++ tests/auction_can_tie_test_1.yaml | 15 +++++++++++++++ tests/auction_test_1.yaml | 11 +++++++++++ 5 files changed, 90 insertions(+) create mode 100644 src/auction.py create mode 100644 src/auction_can_tie.py create mode 100644 tests/auction_can_tie_test_1.yaml create mode 100644 tests/auction_test_1.yaml diff --git a/nada-project.toml b/nada-project.toml index cc3ae99..a69c87a 100644 --- a/nada-project.toml +++ b/nada-project.toml @@ -243,3 +243,13 @@ prime_size = 128 path = "src/shuffle_simple.py" name = "shuffle_simple" prime_size = 128 + +[[programs]] +path = "src/auction.py" +name = "auction" +prime_size = 128 + +[[programs]] +path = "src/auction_can_tie.py" +name = "auction_can_tie" +prime_size = 128 \ No newline at end of file diff --git a/src/auction.py b/src/auction.py new file mode 100644 index 0000000..e9356b2 --- /dev/null +++ b/src/auction.py @@ -0,0 +1,25 @@ +from nada_dsl import * + +def nada_main(): + # Define parties + auctioneer = Party(name="auctioneer") + parties = [Party(name=f"Party{i}") for i in range(5)] + + # Collect bids from each party + bids = [SecretInteger(Input(name=f"bid_{i}", party=parties[i])) for i in range(5)] + + # Initialize variables to track the highest bid and the winning party index + highest_bid = bids[0] + winning_index = Integer(0) + + # Compare bids to find the highest + for i in range(1, 5): + is_higher = bids[i] > highest_bid + highest_bid = is_higher.if_else(bids[i], highest_bid) + winning_index = is_higher.if_else(Integer(i), winning_index) + + # Output the highest bid and the winning party index + return [ + Output(highest_bid, "highest_bid", auctioneer), + Output(winning_index, "winning_index", auctioneer) + ] diff --git a/src/auction_can_tie.py b/src/auction_can_tie.py new file mode 100644 index 0000000..d7a1eee --- /dev/null +++ b/src/auction_can_tie.py @@ -0,0 +1,29 @@ +from nada_dsl import * + +def nada_main(): + # Define the auctioneer as the party responsible for overseeing the auction results + auctioneer = Party(name="auctioneer") + + # Create a list of 5 bidders participating in the auction, 'bidder_0' to 'bidder_4' + bidders = [Party(name=f"bidder_{i}") for i in range(5)] + + # Collect bids from each bidder, where each bid is a SecretInteger input unique to the respective bidder + bids = [SecretInteger(Input(name=f"bid_{i}", party=bidders[i])) for i in range(5)] + + # Determine the highest bid among all the bidders + # Start by initializing the highest bid to the first bidder's bid + highest_bid = bids[0] + # Iterate through the remaining bids to update the highest bid if a higher one is found + for i in range(1, 5): + is_higher = bids[i] > highest_bid + highest_bid = is_higher.if_else(bids[i], highest_bid) + + # Create a list of outputs for each bidder, indicating if their bid matches the highest bid + # Each output is a flag (1 if the bid matches the highest bid, 0 otherwise), visible to the auctioneer + placed_highest_bid = [ + Output((bids[i] == highest_bid).if_else(Integer(1), Integer(0)), f"bidder_{i}_placed_highest_bid", auctioneer) + for i in range(5) + ] + + # Return the highest bid and a list of flags indicating which bidders placed the highest bid + return [Output(highest_bid, "highest_bid", auctioneer)] + placed_highest_bid \ No newline at end of file diff --git a/tests/auction_can_tie_test_1.yaml b/tests/auction_can_tie_test_1.yaml new file mode 100644 index 0000000..7a44e9c --- /dev/null +++ b/tests/auction_can_tie_test_1.yaml @@ -0,0 +1,15 @@ +--- +program: auction_can_tie +inputs: + bid_0: 3 + bid_1: 3 + bid_2: 4 + bid_3: 3 + bid_4: 4 +expected_outputs: + bidder_0_placed_highest_bid: 0 + bidder_1_placed_highest_bid: 0 + bidder_2_placed_highest_bid: 1 + bidder_3_placed_highest_bid: 0 + bidder_4_placed_highest_bid: 1 + highest_bid: 4 diff --git a/tests/auction_test_1.yaml b/tests/auction_test_1.yaml new file mode 100644 index 0000000..cfee3e7 --- /dev/null +++ b/tests/auction_test_1.yaml @@ -0,0 +1,11 @@ +--- +program: auction +inputs: + bid_0: 10 + bid_1: 2 + bid_2: 3 + bid_3: 11 + bid_4: 9 +expected_outputs: + highest_bid: 11 + winning_index: 3 From ac52a6b39da85dff0dc8a35a5a5a23f1ac69d36b Mon Sep 17 00:00:00 2001 From: oceans404 Date: Thu, 10 Oct 2024 10:45:34 -0700 Subject: [PATCH 2/2] add variable for num_bidders --- src/auction_can_tie.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/auction_can_tie.py b/src/auction_can_tie.py index d7a1eee..b7d104c 100644 --- a/src/auction_can_tie.py +++ b/src/auction_can_tie.py @@ -5,16 +5,17 @@ def nada_main(): auctioneer = Party(name="auctioneer") # Create a list of 5 bidders participating in the auction, 'bidder_0' to 'bidder_4' - bidders = [Party(name=f"bidder_{i}") for i in range(5)] + num_bidders = 5 + bidders = [Party(name=f"bidder_{i}") for i in range(num_bidders)] # Collect bids from each bidder, where each bid is a SecretInteger input unique to the respective bidder - bids = [SecretInteger(Input(name=f"bid_{i}", party=bidders[i])) for i in range(5)] + bids = [SecretInteger(Input(name=f"bid_{i}", party=bidders[i])) for i in range(num_bidders)] # Determine the highest bid among all the bidders # Start by initializing the highest bid to the first bidder's bid highest_bid = bids[0] # Iterate through the remaining bids to update the highest bid if a higher one is found - for i in range(1, 5): + for i in range(1, num_bidders): is_higher = bids[i] > highest_bid highest_bid = is_higher.if_else(bids[i], highest_bid) @@ -22,7 +23,7 @@ def nada_main(): # Each output is a flag (1 if the bid matches the highest bid, 0 otherwise), visible to the auctioneer placed_highest_bid = [ Output((bids[i] == highest_bid).if_else(Integer(1), Integer(0)), f"bidder_{i}_placed_highest_bid", auctioneer) - for i in range(5) + for i in range(num_bidders) ] # Return the highest bid and a list of flags indicating which bidders placed the highest bid