diff --git a/pkg/zkproofs/ciphertext_ciphertext_equality.go b/pkg/zkproofs/ciphertext_ciphertext_equality.go index 5eb8bc1..cd60ff0 100644 --- a/pkg/zkproofs/ciphertext_ciphertext_equality.go +++ b/pkg/zkproofs/ciphertext_ciphertext_equality.go @@ -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 } @@ -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 } diff --git a/pkg/zkproofs/ciphertext_commitment_equality.go b/pkg/zkproofs/ciphertext_commitment_equality.go index d91d550..f30e68d 100644 --- a/pkg/zkproofs/ciphertext_commitment_equality.go +++ b/pkg/zkproofs/ciphertext_commitment_equality.go @@ -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 } diff --git a/pkg/zkproofs/ciphertext_validity.go b/pkg/zkproofs/ciphertext_validity.go index 9445d61..527a8a5 100644 --- a/pkg/zkproofs/ciphertext_validity.go +++ b/pkg/zkproofs/ciphertext_validity.go @@ -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 } diff --git a/pkg/zkproofs/pubkey_validity.go b/pkg/zkproofs/pubkey_validity.go index 3ae5a29..e7ed513 100644 --- a/pkg/zkproofs/pubkey_validity.go +++ b/pkg/zkproofs/pubkey_validity.go @@ -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 } diff --git a/pkg/zkproofs/utils.go b/pkg/zkproofs/utils.go index c8e6b94..461511e 100644 --- a/pkg/zkproofs/utils.go +++ b/pkg/zkproofs/utils.go @@ -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") } diff --git a/pkg/zkproofs/zero_balance.go b/pkg/zkproofs/zero_balance.go index 07102f8..234b491 100644 --- a/pkg/zkproofs/zero_balance.go +++ b/pkg/zkproofs/zero_balance.go @@ -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 }