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

Parameters:



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kzg/commitment.rb', line 11

def initialize(setting, polynomial)
  @setting = setting
  @polynomial = polynomial
  @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(&:+)
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.



28
29
30
31
32
33
34
# File 'lib/kzg/commitment.rb', line 28

def self.from_coeffs(setting, coeffs)
  if coeffs.length > setting.g1_points.length
    raise KZG::Error,
          "coeffs length is greater than the number of secret parameters."
  end
  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)


48
49
50
51
52
53
54
55
56
57
# File 'lib/kzg/commitment.rb', line 48

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.



39
40
41
42
43
# File 'lib/kzg/commitment.rb', line 39

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