Skip to content

Commit

Permalink
Add nullifiers objects
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Zhang <[email protected]>
  • Loading branch information
jimthematrix committed Jul 30, 2024
1 parent 04def24 commit 810ff91
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
25 changes: 25 additions & 0 deletions zkp/golang/internal/utxo/fungible.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
27 changes: 27 additions & 0 deletions zkp/golang/internal/utxo/nonfungible.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 8 additions & 0 deletions zkp/golang/pkg/utxo/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 810ff91

Please sign in to comment.