-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7cf57f0
commit b9b2de3
Showing
6 changed files
with
218 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# GNARK Pedersen commitment prove & verification | ||
|
||
[](https://opensource.org/licenses/MIT) | ||
|
||
This package implement the ZK-SNARk circuit for verifying of the knowledge of the openings to the Pedersen commitment. | ||
|
||
Check tests [circuit_test.go](./circuit_test.go) for usage example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Package pedersen_gnark | ||
// Copyright 2024 Oleg Fomenko. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
package pedersen_gnark | ||
|
||
import ( | ||
tedwards "github.com/consensys/gnark-crypto/ecc/twistededwards" | ||
"github.com/consensys/gnark/frontend" | ||
"github.com/consensys/gnark/std/algebra/native/twistededwards" | ||
) | ||
|
||
type Circuit struct { | ||
Amount frontend.Variable | ||
Randomness frontend.Variable | ||
Commitment twistededwards.Point `gnark:",public"` | ||
H twistededwards.Point `gnark:",public"` | ||
G twistededwards.Point `gnark:",public"` | ||
} | ||
|
||
func (c *Circuit) Define(api frontend.API) error { | ||
curve, err := twistededwards.NewEdCurve(api, tedwards.BN254) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
recoveredCommitment := curve.Add(curve.ScalarMul(c.H, c.Amount), curve.ScalarMul(c.G, c.Randomness)) | ||
curve.API().AssertIsEqual(recoveredCommitment.X, c.Commitment.X) | ||
curve.API().AssertIsEqual(recoveredCommitment.Y, c.Commitment.Y) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Package pedersen_gnark | ||
// Copyright 2024 Oleg Fomenko. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
package pedersen_gnark | ||
|
||
import ( | ||
"crypto/rand" | ||
"github.com/consensys/gnark-crypto/ecc" | ||
"github.com/consensys/gnark-crypto/ecc/bn254/fr" | ||
bn254 "github.com/consensys/gnark-crypto/ecc/bn254/twistededwards" | ||
"github.com/consensys/gnark/backend/groth16" | ||
"github.com/consensys/gnark/frontend" | ||
"github.com/consensys/gnark/frontend/cs/r1cs" | ||
"github.com/consensys/gnark/std/algebra/native/twistededwards" | ||
"math/big" | ||
"testing" | ||
) | ||
|
||
var curve = bn254.GetEdwardsCurve() | ||
|
||
func getPoint() *bn254.PointAffine { | ||
k, err := rand.Int(rand.Reader, &curve.Order) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
defer func() { k = nil }() | ||
|
||
return new(bn254.PointAffine).ScalarMultiplication(&curve.Base, k) | ||
} | ||
|
||
func TestCircuitProveAndVerify(t *testing.T) { | ||
var circuit Circuit | ||
r1cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
pk, vk, err := groth16.Setup(r1cs) | ||
|
||
var assignment Circuit | ||
|
||
H := getPoint() | ||
G := getPoint() | ||
|
||
assignment.H = twistededwards.Point{ | ||
X: H.X, | ||
Y: H.Y, | ||
} | ||
|
||
assignment.G = twistededwards.Point{ | ||
X: G.X, | ||
Y: G.Y, | ||
} | ||
|
||
amount := big.NewInt(10) | ||
randomness := big.NewInt(20) | ||
|
||
assignment.Amount = new(fr.Element).SetBigInt(amount) | ||
assignment.Randomness = new(fr.Element).SetBigInt(randomness) | ||
|
||
commitment := new(bn254.PointAffine).Add( | ||
new(bn254.PointAffine).ScalarMultiplication(H, amount), | ||
new(bn254.PointAffine).ScalarMultiplication(G, randomness), | ||
) | ||
|
||
assignment.Commitment = twistededwards.Point{ | ||
X: commitment.X, | ||
Y: commitment.Y, | ||
} | ||
|
||
witness, err := frontend.NewWitness(&assignment, ecc.BN254.ScalarField()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
publicWitness, err := witness.Public() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
proof, err := groth16.Prove(r1cs, pk, witness) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = groth16.Verify(proof, vk, publicWitness) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |