Skip to content

Commit

Permalink
commetns
Browse files Browse the repository at this point in the history
  • Loading branch information
mj850 committed Nov 29, 2024
1 parent e864733 commit 63bb8ca
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 24 deletions.
8 changes: 4 additions & 4 deletions pkg/zkproofs/ciphertext_ciphertext_equality.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ func NewCiphertextCiphertextEqualityProof(

// Generate random scalars
curve := curves.ED25519()
ys, err := GenerateRandomScalar(curve)
ys, err := GenerateRandomNonZeroScalar(curve)
if err != nil {
return nil, err
}
yx, err := GenerateRandomScalar(curve)
yx, err := GenerateRandomNonZeroScalar(curve)
if err != nil {
return nil, err
}
yr, err := GenerateRandomScalar(curve)
yr, err := GenerateRandomNonZeroScalar(curve)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func VerifyCiphertextCiphertextEquality(
return false
}

// validate proof for nil values
// validate proof for nil and zero values
if !proof.validateContents() {
return false
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/zkproofs/ciphertext_commitment_equality.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ func NewCiphertextCommitmentEqualityProof(

// Generate random masking factors
curve := curves.ED25519()
ys, err := GenerateRandomScalar(curve)
ys, err := GenerateRandomNonZeroScalar(curve)
if err != nil {
return nil, err
}

yx, err := GenerateRandomScalar(curve)
yx, err := GenerateRandomNonZeroScalar(curve)
if err != nil {
return nil, err
}

yr, err := GenerateRandomScalar(curve)
yr, err := GenerateRandomNonZeroScalar(curve)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/zkproofs/ciphertext_validity.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ func NewCiphertextValidityProof(pedersenOpening *curves.Scalar, pubKey curves.Po

// Step 1: Generate random blinding factors for the proof
curve := curves.ED25519()
rBlind, err := GenerateRandomScalar(curve) // Blinding factor for random value r
rBlind, err := GenerateRandomNonZeroScalar(curve) // Blinding factor for random value r
if err != nil {
return nil, err
}

xBlind, err := GenerateRandomScalar(curve) // Blinding factor for random value x
xBlind, err := GenerateRandomNonZeroScalar(curve) // Blinding factor for random value x
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/zkproofs/pubkey_validity.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func NewPubKeyValidityProof(pubKey curves.Point, privKey curves.Scalar) (*PubKey

eg := elgamal.NewTwistedElgamal()
H := eg.GetH()

// Prover generates a random scalar y
curve := curves.ED25519()
y, err := GenerateRandomScalar(curve)
y, err := GenerateRandomNonZeroScalar(curve)
if err != nil {
return nil, err
}
Expand Down
26 changes: 14 additions & 12 deletions pkg/zkproofs/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ import (
"github.com/coinbase/kryptology/pkg/core/curves"
)

// Generates a non-zero random scalar. The chances of generating a zero scalar are very low.
func GenerateRandomScalar(curve *curves.Curve) (curves.Scalar, error) {
attempts := 0
scalar := curve.Scalar.Random(rand.Reader)
// Try 5 times to generate a non zero scalar. The chance that this fails with a normal random number generator is impossibly low.
for scalar.IsZero() && attempts < 5 {
curve.Scalar.Random(rand.Reader)
attempts += 1
}
// GenerateRandomNonZeroScalar Generates a non-zero random scalar.
// Parameters:
// - curve: The elliptic curve to use for scalar generation.
// Returns:
// - A non-zero random scalar.
// - An error if the scalar generation fails.
func GenerateRandomNonZeroScalar(curve *curves.Curve) (curves.Scalar, error) {
var scalar curves.Scalar

if scalar.IsZero() {
return nil, errors.New("failed to generate a non-zero scalar")
for attempts := 0; attempts < 5; attempts++ {
scalar = curve.Scalar.Random(rand.Reader)
if !scalar.IsZero() {
return scalar, nil
}
}

return scalar, nil
return nil, errors.New("failed to generate a non-zero scalar")
}
2 changes: 1 addition & 1 deletion pkg/zkproofs/zero_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewZeroBalanceProof(

// Generate random masking factor y
curve := curves.ED25519()
y, err := GenerateRandomScalar(curve)
y, err := GenerateRandomNonZeroScalar(curve)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 63bb8ca

Please sign in to comment.