Skip to content

Instantly share code, notes, and snippets.

@rafaelescrich
Created April 17, 2023 00:26
Show Gist options
  • Save rafaelescrich/2a918c422485938df1ead4124eb10c03 to your computer and use it in GitHub Desktop.
Save rafaelescrich/2a918c422485938df1ead4124eb10c03 to your computer and use it in GitHub Desktop.
ZKP example in Golang
package main
import (
"crypto/elliptic"
"crypto/rand"
"fmt"
"math/big"
)
type Prover struct {
d *big.Int
}
type Verifier struct {
Px, Py, Qx, Qy *big.Int
}
func performZKP(prover *Prover, verifier *Verifier, curve elliptic.Curve, rounds int) bool {
for i := 0; i < rounds; i++ {
r, _ := rand.Int(rand.Reader, curve.Params().N)
Rx, Ry := curve.ScalarMult(verifier.Px, verifier.Py, r.Bytes())
c, _ := rand.Int(rand.Reader, curve.Params().N)
s := new(big.Int).Mul(prover.d, c)
s.Add(s, r)
s.Mod(s, curve.Params().N)
Sx, Sy := curve.ScalarMult(verifier.Px, verifier.Py, s.Bytes())
RxcQ, RycQ := curve.ScalarMult(verifier.Qx, verifier.Qy, c.Bytes())
RxcQxR, RxcQyR := curve.Add(Rx, Ry, RxcQ, RycQ)
if Sx.Cmp(RxcQxR) != 0 || Sy.Cmp(RxcQyR) != 0 {
return false
}
}
return true
}
func main() {
curve := elliptic.P256()
Px, Py := curve.ScalarBaseMult(nil)
d := big.NewInt(42)
Qx, Qy := curve.ScalarMult(Px, Py, d.Bytes())
prover := &Prover{d: d}
verifier := &Verifier{Px: Px, Py: Py, Qx: Qx, Qy: Qy}
success := performZKP(prover, verifier, curve, 10)
fmt.Printf("Zero-Knowledge Proof: %v\n", success)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment