Skip to content

Commit

Permalink
Merge branch 'tinybid' into feat/auctions
Browse files Browse the repository at this point in the history
  • Loading branch information
oceans404 committed Oct 10, 2024
2 parents 8471b2f + ac52a6b commit 5faff80
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
10 changes: 10 additions & 0 deletions nada-project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions src/auction.py
Original file line number Diff line number Diff line change
@@ -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)
]
30 changes: 30 additions & 0 deletions src/auction_can_tie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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'
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(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, num_bidders):
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(num_bidders)
]

# 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
15 changes: 15 additions & 0 deletions tests/auction_can_tie_test_1.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions tests/auction_test_1.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5faff80

Please sign in to comment.