From 810ff91fef964a9702910ab3d2534208a8de6301 Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Wed, 24 Jul 2024 18:22:20 -0400 Subject: [PATCH] Add nullifiers objects Signed-off-by: Jim Zhang --- zkp/golang/internal/utxo/fungible.go | 25 +++++++++++++++++++++++ zkp/golang/internal/utxo/nonfungible.go | 27 +++++++++++++++++++++++++ zkp/golang/pkg/utxo/utxo.go | 8 ++++++++ 3 files changed, 60 insertions(+) diff --git a/zkp/golang/internal/utxo/fungible.go b/zkp/golang/internal/utxo/fungible.go index e3f139e..250d7d4 100644 --- a/zkp/golang/internal/utxo/fungible.go +++ b/zkp/golang/internal/utxo/fungible.go @@ -46,3 +46,28 @@ func (f *Fungible) CalculateIndex() (core.NodeIndex, error) { } return node.NewNodeIndexFromBigInt(hash) } + +// the "Owner" is the private key that must be properly hashed and trimmed to be +// compatible with the BabyJub curve. +// Reference: https://github.com/iden3/circomlib/blob/master/test/babyjub.js#L103 +type FungibleNullifier struct { + Amount *big.Int + Owner *big.Int + Salt *big.Int +} + +func NewFungibleNullifier(amount *big.Int, owner *big.Int, salt *big.Int) *FungibleNullifier { + return &FungibleNullifier{ + Amount: amount, + Owner: owner, + Salt: salt, + } +} + +func (f *FungibleNullifier) CalculateIndex() (core.NodeIndex, error) { + hash, err := poseidon.Hash([]*big.Int{f.Amount, f.Salt, f.Owner}) + if err != nil { + return nil, err + } + return node.NewNodeIndexFromBigInt(hash) +} diff --git a/zkp/golang/internal/utxo/nonfungible.go b/zkp/golang/internal/utxo/nonfungible.go index aaf5a34..219ea93 100644 --- a/zkp/golang/internal/utxo/nonfungible.go +++ b/zkp/golang/internal/utxo/nonfungible.go @@ -51,6 +51,33 @@ func (f *NonFungible) CalculateIndex() (core.NodeIndex, error) { return node.NewNodeIndexFromBigInt(hash) } +// the "Owner" is the private key that must be properly hashed and trimmed to be +// compatible with the BabyJub curve. +// Reference: https://github.com/iden3/circomlib/blob/master/test/babyjub.js#L103 +type NonFungibleNullifier struct { + TokenId *big.Int + TokenUri *big.Int // hash of the token uri string + Owner *big.Int + Salt *big.Int +} + +func NewNonFungibleNullifier(tokenId, tokenUri *big.Int, owner *big.Int, salt *big.Int) *NonFungibleNullifier { + return &NonFungibleNullifier{ + TokenId: tokenId, + TokenUri: tokenUri, + Owner: owner, + Salt: salt, + } +} + +func (f *NonFungibleNullifier) CalculateIndex() (core.NodeIndex, error) { + hash, err := poseidon.Hash([]*big.Int{f.TokenId, f.TokenUri, f.Salt, f.Owner}) + if err != nil { + return nil, err + } + return node.NewNodeIndexFromBigInt(hash) +} + func HashTokenUri(tokenUri string) (*big.Int, error) { hash := sha256.New() _, err := hash.Write([]byte(tokenUri)) diff --git a/zkp/golang/pkg/utxo/utxo.go b/zkp/golang/pkg/utxo/utxo.go index 89c7509..46d4e46 100644 --- a/zkp/golang/pkg/utxo/utxo.go +++ b/zkp/golang/pkg/utxo/utxo.go @@ -32,6 +32,14 @@ func NewNonFungible(tokenId, tokenUri *big.Int, owner *babyjub.PublicKey, salt * return utxo.NewNonFungible(tokenId, tokenUri, owner, salt) } +func NewFungibleNullifier(amount *big.Int, owner *big.Int, salt *big.Int) core.Indexable { + return utxo.NewFungibleNullifier(amount, owner, salt) +} + +func NewNonFungibleNullifier(tokenId *big.Int, tokenUri *big.Int, owner *big.Int, salt *big.Int) core.Indexable { + return utxo.NewNonFungibleNullifier(tokenId, tokenUri, owner, salt) +} + func NewIndexOnly(index core.NodeIndex) core.Indexable { return utxo.NewIndexOnly(index) }