Skip to content

Commit

Permalink
Merge pull request #12 from Jason-LI2020/main
Browse files Browse the repository at this point in the history
Fixed negative value in inclusion proof
  • Loading branch information
debuggor authored Apr 27, 2023
2 parents 3639b43 + 82b68c8 commit 8ad9bce
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
Binary file modified zk_STARK/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions zk_STARK/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

USER_NUM_INIT = 2**10 - 1
MAX_USER_NUM_FOR_ONE_BATCH = 2**12
MAX_USER_VALUE = 4**30//1000
MAIN_COINS_NUM = 21

COINS = ["BTC", "ETH", "USDT", "USDC", "XRP", "DOGE", "SOL", "OKB", "APT", "DASH", "DOT", "ELF", "EOS", "ETC", "FIL", "LINK", "OKT", "PEOPLE", "TON", "TRX", "UNI", "1INCH", "AAVE", "ADA", "AGLD", "AKITA", "ALGO", "ALPHA", "ANT", "APE", "API3", "AR", "ARB", "ATOM", "AVAX", "AXS", "BABYDOGE", "BADGER", "BAL", "BAND", "BAT", "BCH", "BETH", "BICO", "BLUR", "BNB", "BNT", "BSV", "BTM", "BZZ", "CEL", "CELO", "CELR", "CFX", "CHZ", "CLV", "COMP", "CONV", "CORE", "CQT", "CRO", "CRV", "CSPR", "CVC", "DOME", "DORA", "DYDX", "EFI", "EGLD", "ENJ", "ENS", "ETHW", "FITFI", "FLM", "FLOKI", "FLOW", "FTM", "GALA", "GFT", "GLMR", "GMT", "GMX", "GODS", "GRT", "HBAR", "ICP", "IMX", "IOST", "IOTA", "JST", "KISHU", "KLAY", "KNC", "KSM", "LAT", "LDO", "LON", "LOOKS", "LPT", "LRC", "LTC", "LUNA", "LUNC", "MAGIC", "MANA", "MASK", "MATIC", "MINA", "MKR", "NEAR", "NEO", "NFT", "OMG", "ONT", "OP", "PERP", "QTUM", "REN", "RSR", "RSS3", "RVN", "SAND", "SHIB", "SKL", "SLP", "SNT", "SNX", "STARL", "STORJ", "STX", "SUSHI", "SWEAT", "SWRV", "THETA", "TRB", "TUSD", "UMA", "USTC", "WAVES", "WOO", "XCH", "XLM", "XMR", "XTZ", "YFI", "YFII", "YGG", "ZEC", "ZEN", "ZIL", "ZRX"]
Expand Down
24 changes: 16 additions & 8 deletions zk_STARK/mk_and_verify_proofs.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,13 @@ def mk_inclusion_proof(batch_index, uts, input_batch_path, input_trunk_path, out
trunk_inclusion_proof = {}
trunk_inclusion_proof["trunk_mtree_root"] = trunk_mtree[1]
trunk_inclusion_proof["batch_id"] = str(batch_entry_data[(coin_num+1)*32:(coin_num+2)*32].hex())
trunk_inclusion_proof["total_value"] = int.from_bytes(batch_entry_data[:32], 'big')
trunk_inclusion_proof["total_value"] = str(int.from_bytes(batch_entry_data[:32], 'big'))
j = 0
for coin in COINS:
trunk_inclusion_proof[coin] = int.from_bytes(batch_entry_data[(j+1)*32:(j+2)*32], 'big')
value = int.from_bytes(batch_entry_data[(j+1)*32:(j+2)*32], 'big')
if value > MAX_USER_VALUE:
value = value - MODULUS
trunk_inclusion_proof[coin] = str(value)
j += 1
trunk_inclusion_proof["random_number"] = str(batch_entry_data[len(batch_entry_data)-32:].hex())
trunk_inclusion_proof["merkle_path"] = mk_branch(trunk_mtree, (UTS_FOR_TRUNK * (batch_index + 1) + UTS_FOR_TRUNK-2) * EXTENSION_FACTOR)
Expand All @@ -174,10 +177,13 @@ def mk_inclusion_proof(batch_index, uts, input_batch_path, input_trunk_path, out
batch_inclusion_proof = {}
batch_inclusion_proof["batch_mtree_root"] = batch_mtree[1]
batch_inclusion_proof["user_id"] = str(user_entry_data[(coin_num+1)*32:(coin_num+2)*32].hex())
batch_inclusion_proof["total_value"] = int.from_bytes(user_entry_data[:32], 'big')
batch_inclusion_proof["total_value"] = str(int.from_bytes(user_entry_data[:32], 'big'))
j = 0
for coin in COINS:
batch_inclusion_proof[coin] = int.from_bytes(user_entry_data[(j+1)*32:(j+2)*32], 'big')
value = int.from_bytes(user_entry_data[(j+1)*32:(j+2)*32], 'big')
if value > MAX_USER_VALUE:
value = value - MODULUS
batch_inclusion_proof[coin] = str(value)
j += 1
batch_inclusion_proof["random_number"] = str(user_entry_data[len(user_entry_data)-32:].hex())
batch_inclusion_proof["user_index"] = i
Expand Down Expand Up @@ -221,22 +227,24 @@ def verify_single_inclusion_proof(proof_file):
user_index = batch_inclusion_proof["user_index"]
uts = batch_inclusion_proof["uts"]
user_leaf = verify_branch(bytes.fromhex(batch_inclusion_proof["batch_mtree_root"]), (uts * (user_index + 1) + uts-2) * EXTENSION_FACTOR, hex_array_to_bytes(batch_inclusion_proof["merkle_path"]))
user_entry = batch_inclusion_proof["total_value"].to_bytes(32, 'big')
user_entry = int(batch_inclusion_proof["total_value"]).to_bytes(32, 'big')
j = 0
temp = b''
for coin in COINS:
temp = temp + batch_inclusion_proof[coin].to_bytes(32, 'big')
value = int(batch_inclusion_proof[coin]) % MODULUS
temp = temp + value.to_bytes(32, 'big')
j += 1
user_entry = user_entry + keccak_256(temp) + bytes.fromhex(batch_inclusion_proof["user_id"]) + bytes.fromhex(batch_inclusion_proof["random_number"])
assert user_leaf == keccak_256(user_entry)

trunk_inclusion_proof = inclusion_proof["trunk_inclusion_proof"]
batch_leaf = verify_branch(bytes.fromhex(trunk_inclusion_proof["trunk_mtree_root"]), (UTS_FOR_TRUNK * (batch_index + 1) + UTS_FOR_TRUNK-2) * EXTENSION_FACTOR, hex_array_to_bytes(trunk_inclusion_proof["merkle_path"]))
batch_entry = trunk_inclusion_proof["total_value"].to_bytes(32, 'big')
batch_entry = int(trunk_inclusion_proof["total_value"]).to_bytes(32, 'big')
j = 0
temp = b''
for coin in COINS:
temp = temp + trunk_inclusion_proof[coin].to_bytes(32, 'big')
value = int(trunk_inclusion_proof[coin]) % MODULUS
temp = temp + value.to_bytes(32, 'big')
j += 1
batch_entry = batch_entry + keccak_256(temp) + bytes.fromhex(trunk_inclusion_proof["batch_id"]) + bytes.fromhex(trunk_inclusion_proof["random_number"])
assert batch_leaf == keccak_256(batch_entry)
Expand Down
10 changes: 5 additions & 5 deletions zk_STARK/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import time

def test_full_process():
init_user_data(15, 0)
init_user_data(15, 1)
init_user_data(15, 2)
init_user_data(15, 3)
# init_user_data(15, 0)
# init_user_data(15, 1)
# init_user_data(15, 2)
# init_user_data(15, 3)

mk_batch_proof(16, "./user_data/batch0.json", "./sum_proof_data/batches/a0/")
mk_batch_proof(16, "./user_data/batch1.json", "./sum_proof_data/batches/a1/")
Expand Down Expand Up @@ -96,7 +96,7 @@ def test_negative_net_value():
def test_invalid_inclusion_proof():
with open("./inclusion_proof_data/a0/user_0_inclusion_proof.json", "r") as ff:
inclusion_proof_json = json.load(ff)
inclusion_proof_json["batch_inclusion_proof"]["total_value"] += 1
inclusion_proof_json["batch_inclusion_proof"]["total_value"] = str(int(inclusion_proof_json["batch_inclusion_proof"]["total_value"]) + 1)

with open("./inclusion_proof_data/a0/user_0_inclusion_proof.json", "w") as ff:
json.dump(inclusion_proof_json, ff)
Expand Down
Binary file removed zk_STARK/zk_STARK_Validator_mac
Binary file not shown.
Binary file removed zk_STARK/zk_STARK_Validator_windows.exe
Binary file not shown.

0 comments on commit 8ad9bce

Please sign in to comment.