Class: KZG::Commitment

Inherits:
Object
  • Object
show all
Defined in:
lib/kzg/commitment.rb

Overview

KZG commitment

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(setting, polynomial) ⇒ Commitment

Create commitment points to commit them with.

Parameters:

Raises:

  • (KZG::Error)

    Occur when the polynomial has more coefficients than the setup has



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kzg/commitment.rb', line 13

def initialize(setting, polynomial)
  # Checked here rather than only in from_coeffs, which is not the only way in: the
  # verification path builds its interpolation straight through new, and a coefficient
  # with no point to meet is otherwise a nil that fails as arithmetic.
  if polynomial.coeffs.length > setting.g1_points.length
    raise KZG::Error,
          "coeffs length is greater than the number of secret parameters."
  end

  @setting = setting
  @polynomial = polynomial
  # Seeded with the point at infinity so that the zero polynomial commits to it rather
  # than to nil. compute_proof reaches here with an empty quotient whenever the
  # polynomial is a constant, and that proof goes on to a pairing.
  @value =
    polynomial
      .coeffs
      .map
      .with_index do |c, i|
        c = c.is_a?(BLS::Fr) ? c : BLS::Fr.new(c)
        c.value.zero? ? BLS::PointG1::ZERO : setting.g1_points[i] * c
      end
      .inject(BLS::PointG1::ZERO, &:+)
end

Instance Attribute Details

#polynomialObject (readonly)

Returns the value of attribute polynomial.



6
7
8
# File 'lib/kzg/commitment.rb', line 6

def polynomial
  @polynomial
end

#settingObject (readonly)

Returns the value of attribute setting.



6
7
8
# File 'lib/kzg/commitment.rb', line 6

def setting
  @setting
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/kzg/commitment.rb', line 6

def value
  @value
end

Class Method Details

.from_coeffs(setting, coeffs) ⇒ Object

Create commitment using coefficients.

Parameters:

  • setting (KZG::Setting)
  • coeffs (Array(Integer | BLS::Fr))

    Coefficients of polynomial equation.

Raises:

  • (KZG::Error)

    Occur when there are more coefficients than setup points, as in new.



42
43
44
# File 'lib/kzg/commitment.rb', line 42

def self.from_coeffs(setting, coeffs)
  Commitment.new(setting, KZG::Polynomial.new(coeffs))
end

Instance Method Details

#compute_multi_proof(x) ⇒ BLS::PointG1

Compute KZG multi proof using list of x coordinate.

Parameters:

  • x (Array(Integer))

    An array of x coordinate.

Returns:

  • (BLS::PointG1)


58
59
60
61
62
63
64
65
66
67
# File 'lib/kzg/commitment.rb', line 58

def compute_multi_proof(x)
  y = x.map { |i| polynomial.eval_at(i) }
  # compute i(x)
  i_poly = Polynomial.lagrange_interpolate(x, y)
  # compute z(x)
  z_poly = Polynomial.zero_poly(x)
  # compute q(x) = (p(x) - i(x)) / z(x)
  quotient_poly = (polynomial - i_poly) / z_poly
  Commitment.new(setting, quotient_poly).value
end

#compute_proof(x) ⇒ BLS::PointG1

Compute KZG proof for polynomial in coefficient form at position x.

Parameters:

  • x (Integer)

    Position

Returns:

  • (BLS::PointG1)

    Proof.



49
50
51
52
53
# File 'lib/kzg/commitment.rb', line 49

def compute_proof(x)
  divisor = Polynomial.new([BLS::Fr.new(x).negate, BLS::Fr::ONE])
  quotient_poly = polynomial / divisor
  Commitment.new(setting, quotient_poly).value
end