Skip to content

Instantly share code, notes, and snippets.

@lucasvo
Last active July 16, 2019 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasvo/92d56fb42c2cfeb4bef1a2eab826f2b0 to your computer and use it in GitHub Desktop.
Save lucasvo/92d56fb42c2cfeb4bef1a2eab826f2b0 to your computer and use it in GitHub Desktop.
Centrifuge ZK NFT Demo
# See: https://github.com/centrifuge/zk-nft-demo-contract/blob/5ef74b64a9500937a26a0a3abc4db7539c39c2b8/src/circuit/nft.code#L45-L82
def main(field[2] creditRatingRootHashField, field buyerRatingField, privatefield[160] buyerID, privatefield[256] buyerPubkey, privatefield[2] directionCreditRatingTree, privatefield[256] creditRatingTreeDigest0, privatefield[256] creditRatingTreeDigest1, field nftAmount, field[2] documentRootHashField, privatefield[256] invoiceAmountValue, privatefield[64] invoiceAmountProperty, privatefield[256] invoiceAmountSalt, privatefield[8] invoiceAmountTreeDirection, privatefield[256] invoiceAmountTreeDigests0, privatefield[256] invoiceAmountTreeDigests1, privatefield[256] invoiceAmountTreeDigests2, privatefield[256] invoiceAmountTreeDigests3, privatefield[256] invoiceAmountTreeDigests4, privatefield[256] invoiceAmountTreeDigests5, privatefield[256] invoiceAmountTreeDigests6, privatefield[256] invoiceAmountTreeDigests7, privatefield[160] invoiceBuyerValue, privatefield[64] invoiceBuyerProperty, privatefield[256] invoiceBuyerSalt, privatefield[8] invoiceBuyerTreeDirection, privatefield[256] invoiceBuyerTreeDigests0, privatefield[256] invoiceBuyerTreeDigests1, privatefield[256] invoiceBuyerTreeDigests2, privatefield[256] invoiceBuyerTreeDigests3, privatefield[256] invoiceBuyerTreeDigests4, privatefield[256] invoiceBuyerTreeDigests5, privatefield[256] invoiceBuyerTreeDigests6, privatefield[256] invoiceBuyerTreeDigests7, privatefield[2] SignatureR, privatefield SignatureS, privatefield[2] BuyerPubKey) -> (field):
context = context()
field[128] creditRatingRootHash0 = unpack128(creditRatingRootHashField[0])
field[128] creditRatingRootHash1 = unpack128(creditRatingRootHashField[1])
field[256] creditRatingRootHash = [...creditRatingRootHash0, ...creditRatingRootHash1]
// Verifies that the buyer is in the registry and the score matches
field[254] buyerRatingFieldBits = split(buyerRatingField)
field[8] buyerRating = buyerRatingFieldBits[246..254]
field[512] buyerRatingProofValue = concatBuyerRatingProofValue(buyerID, buyerPubkey, buyerRating)
field[256] leafCreditRatingTree = sha512(buyerRatingProofValue[0..256], buyerRatingProofValue[256..512])
field ratingTreeResult = verifyMerkleHash2(creditRatingRootHash, leafCreditRatingTree, directionCreditRatingTree, creditRatingTreeDigest0, creditRatingTreeDigest1)
// Verfies that the document is owned by the buyer
field[128] documentRootHash0 = unpack128(documentRootHashField[0])
field[128] documentRootHash1 = unpack128(documentRootHashField[1])
field[256] documentRootHash = [...documentRootHash0, ...documentRootHash1]
field[1024] invoiceAmountTreeValue = concatInvoiceAmountLeaf(invoiceAmountProperty, invoiceAmountValue, invoiceAmountSalt)
a, b, c, d = splitTo256bitChunks(invoiceAmountTreeValue)
field[256] leafInvoiceAmountTree = sha1024(a, b, c, d)
field invoiceAmountTreeResult = verifyMerkleHash8(documentRootHash, leafInvoiceAmountTree, invoiceAmountTreeDirection, invoiceAmountTreeDigests0, invoiceAmountTreeDigests1, invoiceAmountTreeDigests2, invoiceAmountTreeDigests3, invoiceAmountTreeDigests4, invoiceAmountTreeDigests5, invoiceAmountTreeDigests6, invoiceAmountTreeDigests7)
field[1024] invoiceBuyerTreeValue = concatInvoiceBuyerLeaf(invoiceBuyerProperty, invoiceBuyerValue, invoiceBuyerSalt)
a, b, c, d = splitTo256bitChunks(invoiceBuyerTreeValue)
field[256] leafInvoiceBuyerTree = sha1024(a, b, c, d)
field invoiceBuyerTreeResult = verifyMerkleHash8(documentRootHash, leafInvoiceBuyerTree, invoiceBuyerTreeDirection, invoiceBuyerTreeDigests0, invoiceBuyerTreeDigests1, invoiceBuyerTreeDigests2, invoiceBuyerTreeDigests3, invoiceBuyerTreeDigests4, invoiceBuyerTreeDigests5, invoiceBuyerTreeDigests6, invoiceBuyerTreeDigests7)
// Ensures that the signature is valid
field[256] padding = [0; 256]
field isVerified = verifyEddsa(SignatureR, SignatureS, BuyerPubKey, documentRootHash, padding, context)
// NFT amount needs to be smaller than invoice amount
invoiceAmountValueField = pack256(invoiceAmountValue)
field out = if invoiceAmountValueField > nftAmount then 1 else 0 fi
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment