Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip #7

Open
wants to merge 4 commits into
base: add-pedersen-nft
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions demo/nft.code
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "./pedersenTree2.code" as verifyMerkleHash2
import "./pedersenTree3.code" as verifyMerkleHash3
import "hashes/sha256/512bitPadded.code" as sha512
import "hashes/sha256/1024bitPadded.code" as sha1024
import "./pedersenAndSha256Tree8.code" as verifyMerkleHash8
import "hashes/sha256/512bit.code" as sha512
import "hashes/sha256/1024bit.code" as sha1024
import "utils/pack/pack256.code" as pack256
import "utils/pack/unpack128.code" as unpack128
import "utils/casts/1024to256array.code" as splitTo256bitChunks
Expand All @@ -11,21 +11,38 @@ import "PACKING/split" as split

def concatBuyerRatingProofValue(field[160] buyerID, field[256] buyerPubkey, field[8] buyerRating) -> (field[512]):

field[424] data = [... buyerID, ...buyerPubkey, ...buyerRating]
field[88] padding = [0; 88]
field[424] data = [...buyerID, ...buyerPubkey, ...buyerRating]
field[23] padding = [0; 23]
field[55] dataPad = [0; 55]
field[9] length = [1,1,0,1,0,1,0,0,0]
field[64] dataLength = [...dataPad, ...length]

field[512] out = [...data, ...padding]
field[512] out = [...data, 1, ...padding, ...dataLength]
return out

def concatInvoiceBuyerLeaf(field[256] property, field[160] buyerValue, field[256] salt) ->(field[1024]):
def concatInvoiceBuyerLeaf(field[64] property, field[160] buyerValue, field[256] salt) ->(field[1024]):

field[672] data = [...property, ...buyerValue, ...salt]
field[352] padding = [0; 352]
field[480] data = [...property, ...buyerValue, ...salt]
field[479] padding = [0; 479]
field[55] dataPad = [0; 55]
field[9] length = [1,1,1,1,0,0,0,0,0]
field[64] dataLength = [...dataPad, ...length]

field[1024] out = [...data, ...padding]
field[1024] out = [...data, 1, ...padding, ...dataLength]
return out

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[256] invoiceAmountProperty, privatefield[256] invoiceAmountSalt, privatefield[3] invoiceAmountTreeDirection, privatefield[256] invoiceAmountTreeDigests0, privatefield[256] invoiceAmountTreeDigests1, privatefield[256] invoiceAmountTreeDigests2, privatefield[160] invoiceBuyerValue, privatefield[256] invoiceBuyerProperty, privatefield[256] invoiceBuyerSalt, privatefield[3] invoiceBuyerTreeDirection, privatefield[256] invoiceBuyerTreeDigests0, privatefield[256] invoiceBuyerTreeDigests1, privatefield[256] invoiceBuyerTreeDigests2, privatefield[2] SignatureR, privatefield SignatureS, privatefield[2] BuyerPubKey) -> (field):
def concatInvoiceAmountLeaf(field[64] property, field[256] amountValue, field[256] salt) ->(field[1024]):

field[576] data = [...property, ...amountValue, ...salt]
field[383] padding = [0; 383]
field[54] dataPad = [0; 54]
field[10] length = [1,0,0,1,0,0,0,0,0,0]
field[64] dataLength = [...dataPad, ...length]

field[1024] out = [...data, 1, ...padding, ...dataLength]
return out

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[2] SignatureR, privatefield SignatureS, privatefield[2] BuyerPubKey) -> (field):
context = context()

field[128] creditRatingRootHash0 = unpack128(creditRatingRootHashField[0])
Expand All @@ -37,27 +54,29 @@ def main(field[2] creditRatingRootHashField, field buyerRatingField, privatefiel
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)
field ratingTreeResult = verifyMerkleHash2(creditRatingRootHash, leafCreditRatingTree, directionCreditRatingTree, creditRatingTreeDigest0, creditRatingTreeDigest1)

// // Verfies that the document is owned by the buyer
// 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[256] padding = [0; 256]
field[256] leafInvoiceAmountTree = sha1024(invoiceAmountProperty, invoiceAmountValue, invoiceAmountSalt, padding)
field invoiceAmountTreeResult = verifyMerkleHash3(documentRootHash, leafInvoiceAmountTree, invoiceAmountTreeDirection, invoiceAmountTreeDigests0, invoiceAmountTreeDigests1, invoiceAmountTreeDigests2)
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)
a, b, c, d = splitTo256bitChunks(invoiceBuyerTreeValue)
field[256] leafInvoiceBuyerTree = sha1024(a, b, c, d)
field invoiceBuyerTreeResult = verifyMerkleHash3(documentRootHash, leafInvoiceBuyerTree, invoiceBuyerTreeDirection, invoiceBuyerTreeDigests0, invoiceBuyerTreeDigests1, invoiceBuyerTreeDigests2)
field invoiceBuyerTreeResult = verifyMerkleHash8(documentRootHash, leafInvoiceBuyerTree, invoiceBuyerTreeDirection, invoiceBuyerTreeDigests0, invoiceBuyerTreeDigests1, invoiceBuyerTreeDigests2, invoiceBuyerTreeDigests3, invoiceBuyerTreeDigests4, invoiceBuyerTreeDigests5, invoiceBuyerTreeDigests6, invoiceBuyerTreeDigests7)

// Ensures that the signature is valid
// 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
field out = if invoiceAmountValueField > nftAmount then 1 else 0 fi

return 1
return 1
Loading