Skip to content

Commit

Permalink
add generate curve utility
Browse files Browse the repository at this point in the history
  • Loading branch information
mj850 committed Nov 27, 2024
1 parent 258e510 commit 756335b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 17 deletions.
18 changes: 13 additions & 5 deletions pkg/zkproofs/ciphertext_ciphertext_equality.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zkproofs

import (
"crypto/rand"
"encoding/json"
"errors"

Expand Down Expand Up @@ -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()
Expand Down
20 changes: 15 additions & 5 deletions pkg/zkproofs/ciphertext_commitment_equality.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zkproofs

import (
"crypto/rand"
"encoding/json"
"errors"

Expand Down Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions pkg/zkproofs/ciphertext_validity.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zkproofs

import (
crand "crypto/rand"
"encoding/json"
"errors"
"math/big"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions pkg/zkproofs/pubkey_validity.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zkproofs

import (
"crypto/rand"
"encoding/json"
"errors"

Expand Down Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions pkg/zkproofs/utils.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 5 additions & 2 deletions pkg/zkproofs/zero_balance.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zkproofs

import (
"crypto/rand"
"encoding/json"
"errors"

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 756335b

Please sign in to comment.