Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dssei committed Oct 30, 2024
1 parent 7e4fe72 commit a9e6ad5
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
24 changes: 24 additions & 0 deletions x/confidentialtransfers/types/zk.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (c *TransferProofs) ToProto(proofs *Proofs) *TransferProofs {
}

func (c *TransferProofs) FromProto() (*Proofs, error) {
err := c.Validate()
if err != nil {
return nil, err
}
remainingBalanceCommitmentValidityProof, err := c.RemainingBalanceCommitmentValidityProof.FromProto()
if err != nil {
return nil, err
Expand Down Expand Up @@ -138,6 +142,10 @@ func (c *CiphertextValidityProof) ToProto(zkp *zkproofs.CiphertextValidityProof)
}

func (c *CiphertextValidityProof) FromProto() (*zkproofs.CiphertextValidityProof, error) {
err := c.Validate()
if err != nil {
return nil, err
}

Check warning on line 148 in x/confidentialtransfers/types/zk.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/zk.go#L147-L148

Added lines #L147 - L148 were not covered by tests
ed25519Curve := curves.ED25519()

c1, err := ed25519Curve.Point.FromAffineCompressed(c.Commitment_1)
Expand Down Expand Up @@ -183,6 +191,10 @@ func (r *RangeProof) ToProto(zkp *zkproofs.RangeProof) *RangeProof {
}

func (r *RangeProof) FromProto() (*zkproofs.RangeProof, error) {
err := r.Validate()
if err != nil {
return nil, err
}

Check warning on line 197 in x/confidentialtransfers/types/zk.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/zk.go#L196-L197

Added lines #L196 - L197 were not covered by tests
ed25519Curve := curves.ED25519()
proof := bulletproof.NewRangeProof(ed25519Curve)

Expand Down Expand Up @@ -222,6 +234,10 @@ func (c *CiphertextCommitmentEqualityProof) ToProto(zkp *zkproofs.CiphertextComm
}

func (c *CiphertextCommitmentEqualityProof) FromProto() (*zkproofs.CiphertextCommitmentEqualityProof, error) {
err := c.Validate()
if err != nil {
return nil, err
}

Check warning on line 240 in x/confidentialtransfers/types/zk.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/zk.go#L239-L240

Added lines #L239 - L240 were not covered by tests
ed25519Curve := curves.ED25519()

y0, err := ed25519Curve.Point.FromAffineCompressed(c.Y0)
Expand Down Expand Up @@ -284,6 +300,10 @@ func (c *CiphertextCiphertextEqualityProof) ToProto(zkp *zkproofs.CiphertextCiph
}

func (c *CiphertextCiphertextEqualityProof) FromProto() (*zkproofs.CiphertextCiphertextEqualityProof, error) {
err := c.Validate()
if err != nil {
return nil, err
}

Check warning on line 306 in x/confidentialtransfers/types/zk.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/zk.go#L305-L306

Added lines #L305 - L306 were not covered by tests
ed25519Curve := curves.ED25519()

y0, err := ed25519Curve.Point.FromAffineCompressed(c.Y0)
Expand Down Expand Up @@ -361,6 +381,10 @@ func (a *Auditor) ToProto(transferAuditor *TransferAuditor) *Auditor {
}

func (a *Auditor) FromProto() (*TransferAuditor, error) {
err := a.Validate()
if err != nil {
return nil, err
}

Check warning on line 387 in x/confidentialtransfers/types/zk.go

View check run for this annotation

Codecov / codecov/patch

x/confidentialtransfers/types/zk.go#L386-L387

Added lines #L386 - L387 were not covered by tests
transferAmountLo, err := a.EncryptedTransferAmountLo.FromProto()
if err != nil {
return nil, err
Expand Down
120 changes: 120 additions & 0 deletions x/confidentialtransfers/types/zk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,123 @@ func TestTransferProofs_Validate(t *testing.T) {
})
}
}

func TestTransferProofs_FromProto(t *testing.T) {
tests := []struct {
name string
proofs TransferProofs
wantErr bool
errMsg string
}{
{
name: "missing RemainingBalanceCommitmentValidityProof",
proofs: TransferProofs{},
wantErr: true,
errMsg: "remaining balance commitment validity proof is required",
},
{
name: "missing SenderTransferAmountLoValidityProof",
proofs: TransferProofs{
RemainingBalanceCommitmentValidityProof: &CiphertextValidityProof{},
},
wantErr: true,
errMsg: "sender transfer amount lo validity proof is required",
},
{
name: "missing SenderTransferAmountHiValidityProof",
proofs: TransferProofs{
RemainingBalanceCommitmentValidityProof: &CiphertextValidityProof{},
SenderTransferAmountLoValidityProof: &CiphertextValidityProof{},
},
wantErr: true,
errMsg: "sender transfer amount hi validity proof is required",
},
{
name: "missing RecipientTransferAmountLoValidityProof",
proofs: TransferProofs{
RemainingBalanceCommitmentValidityProof: &CiphertextValidityProof{},
SenderTransferAmountLoValidityProof: &CiphertextValidityProof{},
SenderTransferAmountHiValidityProof: &CiphertextValidityProof{},
},
wantErr: true,
errMsg: "recipient transfer amount lo validity proof is required",
},
{
name: "missing RecipientTransferAmountHiValidityProof",
proofs: TransferProofs{
RemainingBalanceCommitmentValidityProof: &CiphertextValidityProof{},
SenderTransferAmountLoValidityProof: &CiphertextValidityProof{},
SenderTransferAmountHiValidityProof: &CiphertextValidityProof{},
RecipientTransferAmountLoValidityProof: &CiphertextValidityProof{},
},
wantErr: true,
errMsg: "recipient transfer amount hi validity proof is required",
},
{
name: "missing RemainingBalanceRangeProof",
proofs: TransferProofs{
RemainingBalanceCommitmentValidityProof: &CiphertextValidityProof{},
SenderTransferAmountLoValidityProof: &CiphertextValidityProof{},
SenderTransferAmountHiValidityProof: &CiphertextValidityProof{},
RecipientTransferAmountLoValidityProof: &CiphertextValidityProof{},
RecipientTransferAmountHiValidityProof: &CiphertextValidityProof{},
},
wantErr: true,
errMsg: "remaining balance range proof is required",
},
{
name: "missing RemainingBalanceEqualityProof",
proofs: TransferProofs{
RemainingBalanceCommitmentValidityProof: &CiphertextValidityProof{},
SenderTransferAmountLoValidityProof: &CiphertextValidityProof{},
SenderTransferAmountHiValidityProof: &CiphertextValidityProof{},
RecipientTransferAmountLoValidityProof: &CiphertextValidityProof{},
RecipientTransferAmountHiValidityProof: &CiphertextValidityProof{},
RemainingBalanceRangeProof: &RangeProof{},
},
wantErr: true,
errMsg: "remaining balance equality proof is required",
},
{
name: "missing TransferAmountLoEqualityProof",
proofs: TransferProofs{
RemainingBalanceCommitmentValidityProof: &CiphertextValidityProof{},
SenderTransferAmountLoValidityProof: &CiphertextValidityProof{},
SenderTransferAmountHiValidityProof: &CiphertextValidityProof{},
RecipientTransferAmountLoValidityProof: &CiphertextValidityProof{},
RecipientTransferAmountHiValidityProof: &CiphertextValidityProof{},
RemainingBalanceRangeProof: &RangeProof{},
RemainingBalanceEqualityProof: &CiphertextCommitmentEqualityProof{},
},
wantErr: true,
errMsg: "transfer amount lo equality proof is required",
},
{
name: "missing TransferAmountHiEqualityProof",
proofs: TransferProofs{
RemainingBalanceCommitmentValidityProof: &CiphertextValidityProof{},
SenderTransferAmountLoValidityProof: &CiphertextValidityProof{},
SenderTransferAmountHiValidityProof: &CiphertextValidityProof{},
RecipientTransferAmountLoValidityProof: &CiphertextValidityProof{},
RecipientTransferAmountHiValidityProof: &CiphertextValidityProof{},
RemainingBalanceRangeProof: &RangeProof{},
RemainingBalanceEqualityProof: &CiphertextCommitmentEqualityProof{},
TransferAmountLoEqualityProof: &CiphertextCiphertextEqualityProof{},
},
wantErr: true,
errMsg: "transfer amount hi equality proof is required",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tt.proofs.FromProto()
if tt.wantErr {
require.Error(t, err)
require.Contains(t, err.Error(), tt.errMsg)
} else {
require.NoError(t, err)
}
})
}
}

0 comments on commit a9e6ad5

Please sign in to comment.