From 756335ba0e471bcbc1d12c6d77645e54f9b657e2 Mon Sep 17 00:00:00 2001 From: mj Date: Wed, 27 Nov 2024 02:29:26 -0500 Subject: [PATCH] add generate curve utility --- .../ciphertext_ciphertext_equality.go | 18 +++++++++++---- .../ciphertext_commitment_equality.go | 20 ++++++++++++---- pkg/zkproofs/ciphertext_validity.go | 13 ++++++++--- pkg/zkproofs/pubkey_validity.go | 8 +++++-- pkg/zkproofs/utils.go | 23 +++++++++++++++++++ pkg/zkproofs/zero_balance.go | 7 ++++-- 6 files changed, 72 insertions(+), 17 deletions(-) create mode 100644 pkg/zkproofs/utils.go diff --git a/pkg/zkproofs/ciphertext_ciphertext_equality.go b/pkg/zkproofs/ciphertext_ciphertext_equality.go index 37395c0..5eb8bc1 100644 --- a/pkg/zkproofs/ciphertext_ciphertext_equality.go +++ b/pkg/zkproofs/ciphertext_ciphertext_equality.go @@ -1,7 +1,6 @@ package zkproofs import ( - "crypto/rand" "encoding/json" "errors" @@ -68,10 +67,19 @@ func NewCiphertextCiphertextEqualityProof( r := *destinationOpening // Generate random scalars - ed25519 := curves.ED25519() - ys := ed25519.Scalar.Random(rand.Reader) - yx := ed25519.Scalar.Random(rand.Reader) - yr := ed25519.Scalar.Random(rand.Reader) + curve := curves.ED25519() + ys, err := GenerateRandomScalar(curve) + if err != nil { + return nil, err + } + yx, err := GenerateRandomScalar(curve) + if err != nil { + return nil, err + } + yr, err := GenerateRandomScalar(curve) + if err != nil { + return nil, err + } eg := elgamal.NewTwistedElgamal() G := eg.GetG() diff --git a/pkg/zkproofs/ciphertext_commitment_equality.go b/pkg/zkproofs/ciphertext_commitment_equality.go index 4f5e704..d91d550 100644 --- a/pkg/zkproofs/ciphertext_commitment_equality.go +++ b/pkg/zkproofs/ciphertext_commitment_equality.go @@ -1,7 +1,6 @@ package zkproofs import ( - "crypto/rand" "encoding/json" "errors" @@ -60,11 +59,22 @@ func NewCiphertextCommitmentEqualityProof( G := eg.GetG() // Fixed base point G H := eg.GetH() // Fixed base point H - ed25519 := curves.ED25519() // Generate random masking factors - ys := ed25519.Scalar.Random(rand.Reader) - yx := ed25519.Scalar.Random(rand.Reader) - yr := ed25519.Scalar.Random(rand.Reader) + curve := curves.ED25519() + ys, err := GenerateRandomScalar(curve) + if err != nil { + return nil, err + } + + yx, err := GenerateRandomScalar(curve) + if err != nil { + return nil, err + } + + yr, err := GenerateRandomScalar(curve) + if err != nil { + return nil, err + } // Compute Y0 = ys * P Y0 := P.Mul(ys) diff --git a/pkg/zkproofs/ciphertext_validity.go b/pkg/zkproofs/ciphertext_validity.go index 2577270..9445d61 100644 --- a/pkg/zkproofs/ciphertext_validity.go +++ b/pkg/zkproofs/ciphertext_validity.go @@ -1,7 +1,6 @@ package zkproofs import ( - crand "crypto/rand" "encoding/json" "errors" "math/big" @@ -48,8 +47,16 @@ func NewCiphertextValidityProof(pedersenOpening *curves.Scalar, pubKey curves.Po x, _ := ed25519.Scalar.SetBigInt(message) // Step 1: Generate random blinding factors for the proof - rBlind := ed25519.Scalar.Random(crand.Reader) // Blinding factor for random value r - xBlind := ed25519.Scalar.Random(crand.Reader) // Blinding factor for random value x + curve := curves.ED25519() + rBlind, err := GenerateRandomScalar(curve) // Blinding factor for random value r + if err != nil { + return nil, err + } + + xBlind, err := GenerateRandomScalar(curve) // Blinding factor for random value x + if err != nil { + return nil, err + } // Step 2: Create commitments rBlindH := H.Mul(rBlind) // rBlind * H diff --git a/pkg/zkproofs/pubkey_validity.go b/pkg/zkproofs/pubkey_validity.go index ed2925b..3ae5a29 100644 --- a/pkg/zkproofs/pubkey_validity.go +++ b/pkg/zkproofs/pubkey_validity.go @@ -1,7 +1,6 @@ package zkproofs import ( - "crypto/rand" "encoding/json" "errors" @@ -37,8 +36,13 @@ func NewPubKeyValidityProof(pubKey curves.Point, privKey curves.Scalar) (*PubKey eg := elgamal.NewTwistedElgamal() H := eg.GetH() + // Prover generates a random scalar y - y := curves.ED25519().Scalar.Random(rand.Reader) + curve := curves.ED25519() + y, err := GenerateRandomScalar(curve) + if err != nil { + return nil, err + } // Commitment Y = y * H Y := H.Mul(y) diff --git a/pkg/zkproofs/utils.go b/pkg/zkproofs/utils.go new file mode 100644 index 0000000..0141e85 --- /dev/null +++ b/pkg/zkproofs/utils.go @@ -0,0 +1,23 @@ +package zkproofs + +import ( + "crypto/rand" + "errors" + + "github.com/coinbase/kryptology/pkg/core/curves" +) + +func GenerateRandomScalar(curve *curves.Curve) (curves.Scalar, error) { + attempts := 0 + scalar := curve.Scalar.Random(rand.Reader) + for scalar.IsZero() && attempts < 5 { + curve.Scalar.Random(rand.Reader) + attempts += 1 + } + + if scalar.IsZero() { + return nil, errors.New("failed to generate a non-zero scalar") + } + + return scalar, nil +} diff --git a/pkg/zkproofs/zero_balance.go b/pkg/zkproofs/zero_balance.go index 2ee629b..07102f8 100644 --- a/pkg/zkproofs/zero_balance.go +++ b/pkg/zkproofs/zero_balance.go @@ -1,7 +1,6 @@ package zkproofs import ( - "crypto/rand" "encoding/json" "errors" @@ -38,7 +37,11 @@ func NewZeroBalanceProof( D := ciphertext.D // Generate random masking factor y - y := curves.ED25519().Scalar.Random(rand.Reader) + curve := curves.ED25519() + y, err := GenerateRandomScalar(curve) + if err != nil { + return nil, err + } // Compute Yp = y * P and Yd = y * D Yp := P.Mul(y)