diff options
Diffstat (limited to 'nizk/stage1.go')
-rw-r--r-- | nizk/stage1.go | 101 |
1 files changed, 49 insertions, 52 deletions
diff --git a/nizk/stage1.go b/nizk/stage1.go index ff42e7f..dd4a896 100644 --- a/nizk/stage1.go +++ b/nizk/stage1.go @@ -2,32 +2,35 @@ package nizk import . "kesim.org/seal/common" -// Implements the proof and verification of statements of the following form: -// [ Z=g^(xy) && X=g^x && Y=g^y && C=g^(αβ) && A=g^α && B=g^β ] -// || [ Z=g^(xr) && X=g^x && R=g^r && C=g^(αβ+1) && A=g^α && B=g^β ] -// for given Z, X, Y, R, C, A and B - type Stage1 struct { - // Original Bit - *Bit - - // New stage 1 private data x *Scalar y *Scalar r *Scalar + + com *Stage1Commitment + prf *Stage1Proof + + bit *Bit } type Stage1Commitment struct { - // Original Commitment - *Commitment - - // New R *Point X *Point Y *Point Z *Point } +// Represents the proof of statements of the following form: +// +// [ Z=g^(xy) && X=g^x && Y=g^y && C=g^(αβ) && A=g^α && B=g^β ] +// || [ Z=g^(xr) && X=g^x && R=g^r && C=g^(αβ+1) && A=g^α && B=g^β ] +// +// for given Z, X, Y, R, C, A and B +type Stage1Proof struct { + Ch [2]*Scalar + Rho [2][2]*Scalar +} + func (b *Bit) Stage1() *Stage1 { var x [3]*Scalar for i := range x { @@ -38,70 +41,64 @@ func (b *Bit) Stage1() *Stage1 { func (b *Bit) Stage1FromScalars(x, y, r *Scalar) *Stage1 { return &Stage1{ - x: x, - y: y, - r: r, - Bit: b, + x: x, + y: y, + r: r, + + bit: b, } } func (s *Stage1) commit() *Stage1Commitment { + if s.com != nil { + return s.com + } var Z *Point - φ := s.α.Mul(s.β) - if s.bitSet { + if s.bit.IsSet() { Z = G.Exp(s.x.Mul(s.r)) - φ = φ.Add(One) } else { Z = G.Exp(s.x.Mul(s.y)) } - return &Stage1Commitment{ + s.com = &Stage1Commitment{ Z: Z, X: G.Exp(s.x), Y: G.Exp(s.y), R: G.Exp(s.r), - - Commitment: &Commitment{ - A: G.Exp(s.α), - B: G.Exp(s.β), - C: G.Exp(φ), - }, } + return s.com } -type Stage1Proof struct { - Ch [2]*Scalar - Rho [2][2]*Scalar -} - -func (s *Stage1) proof(c *Stage1Commitment) *Stage1Proof { +func (s *Stage1) proof() *Stage1Proof { var ε [2][4]*Point var r1, r2, ρ1, ρ2, ω *Scalar for _, s := range []**Scalar{&r1, &r2, &ρ1, &ρ2, &ω} { *s = Curve.RandomScalar() } + c := s.commit() + bc, _ := s.bit.Commit() - if s.bitSet { + if s.bit.IsSet() { ε[0][0] = G.Exp(r1).Mul(c.X.Exp(ω)) - ε[0][1] = G.Exp(r2).Mul(c.A.Exp(ω)) + ε[0][1] = G.Exp(r2).Mul(bc.A.Exp(ω)) ε[0][2] = c.Y.Exp(r1).Mul(c.Z.Exp(ω)) - ε[0][3] = c.B.Exp(r2).Mul(c.C.Exp(ω)) + ε[0][3] = bc.B.Exp(r2).Mul(bc.C.Exp(ω)) ε[1][0] = G.Exp(ρ1) ε[1][1] = G.Exp(ρ2) ε[1][2] = c.R.Exp(ρ1) - ε[1][3] = c.B.Exp(ρ2) + ε[1][3] = bc.B.Exp(ρ2) } else { ε[0][0] = G.Exp(r1) ε[0][1] = G.Exp(r2) ε[0][2] = c.Y.Exp(r1) - ε[0][3] = c.B.Exp(r2) + ε[0][3] = bc.B.Exp(r2) ε[1][0] = G.Exp(ρ1).Mul(c.X.Exp(ω)) - ε[1][1] = G.Exp(ρ2).Mul(c.A.Exp(ω)) + ε[1][1] = G.Exp(ρ2).Mul(bc.A.Exp(ω)) ε[1][2] = c.R.Exp(ρ1).Mul(c.Z.Exp(ω)) - ε[1][3] = c.B.Exp(ρ2).Mul(c.C.Div(G).Exp(ω)) + ε[1][3] = bc.B.Exp(ρ2).Mul(bc.C.Div(G).Exp(ω)) } - p := []Bytes{G, c.A, c.B, c.C, c.R, c.X, c.Y, c.Z} + p := []Bytes{G, bc.A, bc.B, bc.C, c.R, c.X, c.Y, c.Z} for _, e := range ε[0] { p = append(p, e) } @@ -111,19 +108,20 @@ func (s *Stage1) proof(c *Stage1Commitment) *Stage1Proof { ch := Challenge(p...) pr := &Stage1Proof{} + α, _ := s.bit.Scalars() - if s.bitSet { + if s.bit.IsSet() { pr.Ch[0] = ω pr.Ch[1] = ch.Sub(ω) pr.Rho[0][0] = r1 pr.Rho[0][1] = r2 pr.Rho[1][0] = ρ1.Sub(s.x.Mul(pr.Ch[1])) - pr.Rho[1][1] = ρ2.Sub(s.α.Mul(pr.Ch[1])) + pr.Rho[1][1] = ρ2.Sub(α.Mul(pr.Ch[1])) } else { pr.Ch[0] = ch.Sub(ω) pr.Ch[1] = ω pr.Rho[0][0] = r1.Sub(s.x.Mul(pr.Ch[0])) - pr.Rho[0][1] = r2.Sub(s.α.Mul(pr.Ch[0])) + pr.Rho[0][1] = r2.Sub(α.Mul(pr.Ch[0])) pr.Rho[1][0] = ρ1 pr.Rho[1][1] = ρ2 } @@ -132,23 +130,22 @@ func (s *Stage1) proof(c *Stage1Commitment) *Stage1Proof { } func (s *Stage1) Commit() (*Stage1Commitment, *Stage1Proof) { - c := s.commit() - return c, s.proof(c) + return s.commit(), s.proof() } -func (c *Stage1Commitment) Verify(p *Stage1Proof) bool { +func (c1 *Stage1Commitment) Verify(c *Commitment, p *Stage1Proof) bool { var ε [2][4]*Point - ε[0][0] = G.Exp(p.Rho[0][0]).Mul(c.X.Exp(p.Ch[0])) + ε[0][0] = G.Exp(p.Rho[0][0]).Mul(c1.X.Exp(p.Ch[0])) ε[0][1] = G.Exp(p.Rho[0][1]).Mul(c.A.Exp(p.Ch[0])) - ε[0][2] = c.Y.Exp(p.Rho[0][0]).Mul(c.Z.Exp(p.Ch[0])) + ε[0][2] = c1.Y.Exp(p.Rho[0][0]).Mul(c1.Z.Exp(p.Ch[0])) ε[0][3] = c.B.Exp(p.Rho[0][1]).Mul(c.C.Exp(p.Ch[0])) - ε[1][0] = G.Exp(p.Rho[1][0]).Mul(c.X.Exp(p.Ch[1])) + ε[1][0] = G.Exp(p.Rho[1][0]).Mul(c1.X.Exp(p.Ch[1])) ε[1][1] = G.Exp(p.Rho[1][1]).Mul(c.A.Exp(p.Ch[1])) - ε[1][2] = c.R.Exp(p.Rho[1][0]).Mul(c.Z.Exp(p.Ch[1])) + ε[1][2] = c1.R.Exp(p.Rho[1][0]).Mul(c1.Z.Exp(p.Ch[1])) ε[1][3] = c.B.Exp(p.Rho[1][1]).Mul(c.C.Div(G).Exp(p.Ch[1])) - points := []Bytes{G, c.A, c.B, c.C, c.R, c.X, c.Y, c.Z} + points := []Bytes{G, c.A, c.B, c.C, c1.R, c1.X, c1.Y, c1.Z} for _, e := range ε[0] { points = append(points, e) } |