diff options
Diffstat (limited to 'nizk')
-rw-r--r-- | nizk/stage1.go | 59 | ||||
-rw-r--r-- | nizk/stage1_test.go | 32 | ||||
-rw-r--r-- | nizk/stage2.go | 68 | ||||
-rw-r--r-- | nizk/stage2_test.go | 12 |
4 files changed, 92 insertions, 79 deletions
diff --git a/nizk/stage1.go b/nizk/stage1.go index 21d63c6..fe75afd 100644 --- a/nizk/stage1.go +++ b/nizk/stage1.go @@ -6,7 +6,9 @@ type Stage struct { x *Scalar r *Scalar - com *StageCommitment + com *StageCommitment + rev *StageReveal + prf1 *Stage1Proof prf2 *Stage2Proof @@ -16,8 +18,11 @@ type Stage struct { type StageCommitment struct { R *Point X *Point - Y *Point +} + +type StageReveal struct { Z *Point + Y *Point } // Represents the proof of statements of the following form: @@ -40,39 +45,30 @@ func (b *Bit) stage(x, r *Scalar) *Stage { } } -func (b *Bit) CommitStage1(Xs ...*Point) (c *Stage, s *StageCommitment, p *Stage1Proof) { +func (b *Bit) CommitStage1(Xs ...*Point) (c *Stage, s *StageCommitment) { x := Curve.RandomScalar() r := Curve.RandomScalar() return b.CommitStage1FromScalars(x, r, Xs...) } -func (b *Bit) CommitStage1FromScalars(x, r *Scalar, Xs ...*Point) (s *Stage, c *StageCommitment, p *Stage1Proof) { +func (b *Bit) CommitStage1FromScalars(x, r *Scalar, Xs ...*Point) (s *Stage, c *StageCommitment) { s = b.stage(x, r) - return s, s.commit(false, Xs...), s.proof1() + return s, s.commit(false, Xs...) } func (s *Stage) commit(lost bool, Xs ...*Point) *StageCommitment { if s.com != nil { return s.com } - var Y, Z *Point - Y = G // TODO! BUG! THIS HAS TO BE Pj<i(X_j)/Pj>i(X_j) - if !lost && s.bit.IsSet() { - Z = G.Exp(s.x.Mul(s.r)) - } else { - Z = Y.Exp(s.x) - } s.com = &StageCommitment{ - Z: Z, X: G.Exp(s.x), - Y: Y, R: G.Exp(s.r), } return s.com } -func (s *Stage) proof1() *Stage1Proof { +func (s *Stage) RevealStage1(Xs ...*Point) (rev *StageReveal, pr *Stage1Proof) { var ε [2][4]*Point var r1, r2, ρ1, ρ2, ω *Scalar for _, s := range []**Scalar{&r1, &r2, &ρ1, &ρ2, &ω} { @@ -81,10 +77,22 @@ func (s *Stage) proof1() *Stage1Proof { c := s.commit(false) bc := s.bit.com + // TODO: Calculate Y based on the Xs and our own X_i + // as Π_(i<k) X_k / Π_(i>k) X_k + // For now: + Y := G.Exp(Curve.RandomScalar()) + + rev = &StageReveal{Y: Y} + if s.bit.IsSet() { + rev.Z = c.R.Exp(s.x) + } else { + rev.Z = rev.Y.Exp(s.x) + } + if s.bit.IsSet() { ε[0][0] = G.Exp(r1).Mul(c.X.Exp(ω)) ε[0][1] = G.Exp(r2).Mul(bc.A.Exp(ω)) - ε[0][2] = c.Y.Exp(r1).Mul(c.Z.Exp(ω)) + ε[0][2] = rev.Y.Exp(r1).Mul(rev.Z.Exp(ω)) ε[0][3] = bc.B.Exp(r2).Mul(bc.C.Exp(ω)) ε[1][0] = G.Exp(ρ1) ε[1][1] = G.Exp(ρ2) @@ -93,15 +101,15 @@ func (s *Stage) proof1() *Stage1Proof { } else { ε[0][0] = G.Exp(r1) ε[0][1] = G.Exp(r2) - ε[0][2] = c.Y.Exp(r1) + ε[0][2] = rev.Y.Exp(r1) ε[0][3] = bc.B.Exp(r2) ε[1][0] = G.Exp(ρ1).Mul(c.X.Exp(ω)) ε[1][1] = G.Exp(ρ2).Mul(bc.A.Exp(ω)) - ε[1][2] = c.R.Exp(ρ1).Mul(c.Z.Exp(ω)) + ε[1][2] = c.R.Exp(ρ1).Mul(rev.Z.Exp(ω)) ε[1][3] = bc.B.Exp(ρ2).Mul(bc.C.Div(G).Exp(ω)) } - p := []Bytes{G, bc.A, bc.B, bc.C, c.R, c.X, c.Y, c.Z} + p := []Bytes{G, bc.A, bc.B, bc.C, c.R, c.X, rev.Y, rev.Z} for _, e := range ε[0] { p = append(p, e) } @@ -110,7 +118,7 @@ func (s *Stage) proof1() *Stage1Proof { } ch := Challenge(p...) - pr := &Stage1Proof{} + pr = &Stage1Proof{} α, _ := s.bit.Scalars() if s.bit.IsSet() { @@ -129,23 +137,24 @@ func (s *Stage) proof1() *Stage1Proof { pr.Rho[1][1] = ρ2 } + s.rev = rev s.prf1 = pr - return pr + return rev, pr } -func (c *Commitment) VerifyStage1(sc *StageCommitment, p *Stage1Proof) bool { +func (c *Commitment) VerifyStage1(sc *StageCommitment, r *StageReveal, p *Stage1Proof) bool { var ε [2][4]*Point ε[0][0] = G.Exp(p.Rho[0][0]).Mul(sc.X.Exp(p.Ch[0])) ε[0][1] = G.Exp(p.Rho[0][1]).Mul(c.A.Exp(p.Ch[0])) - ε[0][2] = sc.Y.Exp(p.Rho[0][0]).Mul(sc.Z.Exp(p.Ch[0])) + ε[0][2] = r.Y.Exp(p.Rho[0][0]).Mul(r.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(sc.X.Exp(p.Ch[1])) ε[1][1] = G.Exp(p.Rho[1][1]).Mul(c.A.Exp(p.Ch[1])) - ε[1][2] = sc.R.Exp(p.Rho[1][0]).Mul(sc.Z.Exp(p.Ch[1])) + ε[1][2] = sc.R.Exp(p.Rho[1][0]).Mul(r.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, sc.R, sc.X, sc.Y, sc.Z} + points := []Bytes{G, c.A, c.B, c.C, sc.R, sc.X, r.Y, r.Z} for _, e := range ε[0] { points = append(points, e) } diff --git a/nizk/stage1_test.go b/nizk/stage1_test.go index fd54b57..8fc8970 100644 --- a/nizk/stage1_test.go +++ b/nizk/stage1_test.go @@ -6,23 +6,25 @@ import ( . "kesim.org/seal/common" ) -func TestStage1(t *testing.T) { +func TestStage1Simple(t *testing.T) { id := Curve.RandomScalar() b1 := NewBit(id, true) b2 := NewBit(id, false) bc1, _ := b1.Commit() bc2, _ := b2.Commit() - _, c1, pr1 := b1.CommitStage1() - _, c2, pr2 := b2.CommitStage1() - if !bc1.VerifyStage1(c1, pr1) { + s1, c1 := b1.CommitStage1() + s2, c2 := b2.CommitStage1() + r1, pr1 := s1.RevealStage1() // Note: no Xs. + r2, pr2 := s2.RevealStage1() // Note: no Xs + if !bc1.VerifyStage1(c1, r1, pr1) { t.Fatal("Could not verify st1 with c1 and pr1, plus=true case") } - if !bc2.VerifyStage1(c2, pr2) { + if !bc2.VerifyStage1(c2, r2, pr2) { t.Fatal("Could not verify st2 with c2 and pr2, plus=false case") } // Wrong proof test - if bc1.VerifyStage1(c1, pr2) { + if bc1.VerifyStage1(c1, r1, pr2) { t.Fatal("Shouldn't be able to verify c1 with pr2") } } @@ -38,19 +40,21 @@ func TestStage1FromScalars(t *testing.T) { bc1, _ := b1.Commit() bc2, _ := b2.Commit() - _, c1, pr1 := b1.CommitStage1FromScalars(r, x) - _, c2, pr2 := b2.CommitStage1FromScalars(x, r) - if !bc1.VerifyStage1(c1, pr1) { + s1, c1 := b1.CommitStage1FromScalars(r, x) + s2, c2 := b2.CommitStage1FromScalars(x, r) + r1, pr1 := s1.RevealStage1() // Note: no Xs + r2, pr2 := s2.RevealStage1() // Note: no Xs + if !bc1.VerifyStage1(c1, r1, pr1) { t.Fatal("Could not verify st1 with c1 and pr1, plus=true case") } - if !bc2.VerifyStage1(c2, pr2) { + if !bc2.VerifyStage1(c2, r2, pr2) { t.Fatal("Could not verify st2 with c2 and pr2, plus=false case") } // Wrong proof test - if bc1.VerifyStage1(c1, pr2) || - bc1.VerifyStage1(c2, pr2) || - bc2.VerifyStage1(c1, pr2) || - bc2.VerifyStage1(c2, pr1) { + if bc1.VerifyStage1(c1, r1, pr2) || + bc1.VerifyStage1(c2, r2, pr2) || + bc2.VerifyStage1(c1, r1, pr2) || + bc2.VerifyStage1(c2, r2, pr1) { t.Fatal("Shouldn't be able to verify bc_i with c_j or pr_j") } } diff --git a/nizk/stage2.go b/nizk/stage2.go index 8bf94dd..efdda9c 100644 --- a/nizk/stage2.go +++ b/nizk/stage2.go @@ -52,29 +52,38 @@ func (s *Stage) proof2(lost bool, prev *Stage) *Stage2Proof { c := s.commit(lost) bc := prev.bit.com pc := prev.com + rvp := prev.rev + rev := &StageReveal{ + Y: G.Exp(Curve.RandomScalar()), // TODO! BUG! THIS HAS TO BE Pj<i(X_j)/Pj>i(X_j) + } + if s.bit.IsSet() { + rev.Z = c.R.Exp(s.x) + } else { + rev.Z = rev.Y.Exp(s.x) + } if lost { e1[0] = G.Exp(r1[0]).Mul(c.X.Exp(w[0])) e1[1] = G.Exp(r1[1]).Mul(pc.X.Exp(w[0])) e1[2] = G.Exp(r1[2]).Mul(bc.A.Exp(w[0])) - e1_[0] = c.R.Exp(r1[0]).Mul(c.Z.Exp(w[0])) - e1_[1] = pc.R.Exp(r1[1]).Mul(pc.Z.Exp(w[0])) + e1_[0] = c.R.Exp(r1[0]).Mul(rev.Z.Exp(w[0])) + e1_[1] = pc.R.Exp(r1[1]).Mul(rvp.Z.Exp(w[0])) e1_[2] = bc.B.Exp(r1[2]).Mul(bc.C.Div(G).Exp(w[0])) e2[0] = G.Exp(r2[0]).Mul(c.X.Exp(w[1])) e2[1] = G.Exp(r2[1]).Mul(pc.X.Exp(w[1])) e2[2] = G.Exp(r2[2]).Mul(bc.A.Exp(w[1])) - e2_[0] = c.Y.Exp(r2[0]).Mul(c.Z.Exp(w[1])) - e2_[1] = pc.R.Exp(r2[1]).Mul(pc.Z.Exp(w[1])) + e2_[0] = rev.Y.Exp(r2[0]).Mul(rev.Z.Exp(w[1])) + e2_[1] = pc.R.Exp(r2[1]).Mul(rvp.Z.Exp(w[1])) e2_[2] = bc.B.Exp(r2[2]).Mul(bc.C.Exp(w[1])) e3[0] = G.Exp(r3[0]) e3[1] = G.Exp(r3[1]) - e3_[0] = c.Y.Exp(r3[0]) - e3_[1] = pc.Y.Exp(r3[1]) + e3_[0] = rev.Y.Exp(r3[0]) + e3_[1] = rvp.Y.Exp(r3[1]) } else { if s.bit.IsSet() { e1[0] = G.Exp(r1[0]) @@ -89,41 +98,41 @@ func (s *Stage) proof2(lost bool, prev *Stage) *Stage2Proof { e2[1] = G.Exp(r2[1]).Mul(pc.X.Exp(w[0])) e2[2] = G.Exp(r2[2]).Mul(bc.A.Exp(w[0])) - e2_[0] = c.Y.Exp(r2[0]).Mul(c.Z.Exp(w[0])) - e2_[1] = pc.R.Exp(r2[1]).Mul(pc.Z.Exp(w[0])) + e2_[0] = rev.Y.Exp(r2[0]).Mul(rev.Z.Exp(w[0])) + e2_[1] = pc.R.Exp(r2[1]).Mul(rvp.Z.Exp(w[0])) e2_[2] = bc.B.Exp(r2[2]).Mul(bc.C.Exp(w[0])) e3[0] = G.Exp(r3[0]).Mul(c.X.Exp(w[1])) e3[1] = G.Exp(r3[1]).Mul(pc.X.Exp(w[1])) - e3_[0] = c.Y.Exp(r3[0]).Mul(c.Z.Exp(w[1])) - e3_[1] = pc.Y.Exp(r3[1]).Mul(pc.Z.Exp(w[1])) + e3_[0] = rev.Y.Exp(r3[0]).Mul(rev.Z.Exp(w[1])) + e3_[1] = rvp.Y.Exp(r3[1]).Mul(rvp.Z.Exp(w[1])) } else { e1[0] = G.Exp(r1[0]).Mul(c.X.Exp(w[0])) e1[1] = G.Exp(r1[1]).Mul(pc.X.Exp(w[0])) e1[2] = G.Exp(r1[2]).Mul(bc.A.Exp(w[0])) - e1_[0] = c.R.Exp(r1[0]).Mul(c.Z.Exp(w[0])) - e1_[1] = pc.R.Exp(r1[1]).Mul(pc.Z.Exp(w[0])) + e1_[0] = c.R.Exp(r1[0]).Mul(rev.Z.Exp(w[0])) + e1_[1] = pc.R.Exp(r1[1]).Mul(rvp.Z.Exp(w[0])) e1_[2] = bc.B.Exp(r1[2]).Mul(bc.C.Div(G).Exp(w[0])) e2[0] = G.Exp(r2[0]) e2[1] = G.Exp(r2[1]) e2[2] = G.Exp(r2[2]) - e2_[0] = c.Y.Exp(r2[0]) + e2_[0] = rev.Y.Exp(r2[0]) e2_[1] = pc.R.Exp(r2[1]) e2_[2] = bc.B.Exp(r2[2]) e3[0] = G.Exp(r3[0]).Mul(c.X.Exp(w[1])) e3[1] = G.Exp(r3[1]).Mul(pc.X.Exp(w[1])) - e3_[0] = c.Y.Exp(r3[0]).Mul(c.Z.Exp(w[1])) - e3_[1] = pc.Y.Exp(r3[1]).Mul(pc.Z.Exp(w[1])) + e3_[0] = rev.Y.Exp(r3[0]).Mul(rev.Z.Exp(w[1])) + e3_[1] = rvp.Y.Exp(r3[1]).Mul(rvp.Z.Exp(w[1])) } } - points := []Bytes{G, bc.A, bc.B, bc.C, c.R, c.X, c.Y, c.Z, pc.R, pc.X, pc.Y, pc.Z} + points := []Bytes{G, bc.A, bc.B, bc.C, c.R, c.X, rev.Y, rev.Z, pc.R, pc.X, rvp.Y, rvp.Z} points = append(points, e1[:]...) points = append(points, e2[:]...) points = append(points, e3[:]...) @@ -183,38 +192,39 @@ func (s *Stage) proof2(lost bool, prev *Stage) *Stage2Proof { } } + s.prf2 = pr return pr } -func (c *Commitment) VerifyStage2(prev, curr *StageCommitment, p *Stage2Proof) bool { +func (c *Commitment) VerifyStage2(pcom, ccom *StageCommitment, prev, crev *StageReveal, p *Stage2Proof) bool { var ( e1, e1_ [3]Bytes e2, e2_ [3]Bytes e3, e3_ [2]Bytes ) - e1[0] = G.Exp(p.R1[0]).Mul(curr.X.Exp(p.Ch[0])) - e1[1] = G.Exp(p.R1[1]).Mul(prev.X.Exp(p.Ch[0])) + e1[0] = G.Exp(p.R1[0]).Mul(ccom.X.Exp(p.Ch[0])) + e1[1] = G.Exp(p.R1[1]).Mul(pcom.X.Exp(p.Ch[0])) e1[2] = G.Exp(p.R1[2]).Mul(c.A.Exp(p.Ch[0])) - e1_[0] = curr.R.Exp(p.R1[0]).Mul(curr.Z.Exp(p.Ch[0])) - e1_[1] = prev.R.Exp(p.R1[1]).Mul(prev.Z.Exp(p.Ch[0])) + e1_[0] = ccom.R.Exp(p.R1[0]).Mul(crev.Z.Exp(p.Ch[0])) + e1_[1] = pcom.R.Exp(p.R1[1]).Mul(prev.Z.Exp(p.Ch[0])) e1_[2] = c.B.Exp(p.R1[2]).Mul(c.C.Div(G).Exp(p.Ch[0])) - e2[0] = G.Exp(p.R2[0]).Mul(curr.X.Exp(p.Ch[1])) - e2[1] = G.Exp(p.R2[1]).Mul(prev.X.Exp(p.Ch[1])) + e2[0] = G.Exp(p.R2[0]).Mul(ccom.X.Exp(p.Ch[1])) + e2[1] = G.Exp(p.R2[1]).Mul(pcom.X.Exp(p.Ch[1])) e2[2] = G.Exp(p.R2[2]).Mul(c.A.Exp(p.Ch[1])) - e2_[0] = curr.Y.Exp(p.R2[0]).Mul(curr.Z.Exp(p.Ch[1])) - e2_[1] = prev.R.Exp(p.R2[1]).Mul(prev.Z.Exp(p.Ch[1])) + e2_[0] = crev.Y.Exp(p.R2[0]).Mul(crev.Z.Exp(p.Ch[1])) + e2_[1] = pcom.R.Exp(p.R2[1]).Mul(prev.Z.Exp(p.Ch[1])) e2_[2] = c.B.Exp(p.R2[2]).Mul(c.C.Exp(p.Ch[1])) - e3[0] = G.Exp(p.R3[0]).Mul(curr.X.Exp(p.Ch[2])) - e3[1] = G.Exp(p.R3[1]).Mul(prev.X.Exp(p.Ch[2])) + e3[0] = G.Exp(p.R3[0]).Mul(ccom.X.Exp(p.Ch[2])) + e3[1] = G.Exp(p.R3[1]).Mul(pcom.X.Exp(p.Ch[2])) - e3_[0] = curr.Y.Exp(p.R3[0]).Mul(curr.Z.Exp(p.Ch[2])) + e3_[0] = crev.Y.Exp(p.R3[0]).Mul(crev.Z.Exp(p.Ch[2])) e3_[1] = prev.Y.Exp(p.R3[1]).Mul(prev.Z.Exp(p.Ch[2])) - points := []Bytes{G, c.A, c.B, c.C, curr.R, curr.X, curr.Y, curr.Z, prev.R, prev.X, prev.Y, prev.Z} + points := []Bytes{G, c.A, c.B, c.C, ccom.R, ccom.X, crev.Y, crev.Z, pcom.R, pcom.X, prev.Y, prev.Z} points = append(points, e1[:]...) points = append(points, e2[:]...) points = append(points, e3[:]...) diff --git a/nizk/stage2_test.go b/nizk/stage2_test.go index 7edcf80..75c99ef 100644 --- a/nizk/stage2_test.go +++ b/nizk/stage2_test.go @@ -3,20 +3,10 @@ package nizk import ( "testing" - . "kesim.org/seal/common" +// . "kesim.org/seal/common" ) func TestStage2Simple(t *testing.T) { - id := Curve.RandomScalar() - b1 := NewBit(id, false) - b2 := NewBit(id, false) - s1, c1, _ := b1.CommitStage1() - curr, _ := b2.Commit() - - _, c2, p2 := b2.CommitStage2(true, s1) - if !curr.VerifyStage2(c1, c2, p2) { - t.Fatal("Could not verify stage2") - } } |