Class: KZG::Setting

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(g1_points, g2_points) ⇒ Setting

Returns a new instance of Setting.

Parameters:

  • g1s (Array[BLS::PointG1])
  • g2s (Array[BLS::PointG2])


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

def initialize(g1_points, g2_points)
  if !g1_points.is_a?(Array) || !g2_points.is_a?(Array)
    raise KZG::Error, "g1_points and g2_points must be array."
  end
  unless g1_points.all? { |g| g.is_a?(BLS::PointG1) }
    raise KZG::Error, "All elements of g1_points must be BLS::PointG1."
  end
  unless g2_points.all? { |g| g.is_a?(BLS::PointG2) }
    raise KZG::Error, "All elements of g2_points must be BLS::PointG2."
  end

  @g1_points = g1_points
  @g2_points = g2_points
end

Instance Attribute Details

#g1_pointsObject (readonly)

Returns the value of attribute g1_points.



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

def g1_points
  @g1_points
end

#g2_pointsObject (readonly)

Returns the value of attribute g2_points.



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

def g2_points
  @g2_points
end

Instance Method Details

#==(other) ⇒ Object



25
26
27
# File 'lib/kzg/setting.rb', line 25

def ==(other)
  g1_points == other.g1_points && g2_points == other.g2_points
end

#valid_proof?(commit_point, proof, x, y) ⇒ Boolean

Check a proof for a KZG commitment for an evaluation f(x) = y

Parameters:

  • commit_point (BLS::PointG1)
  • proof (BLS::PointG1)
  • x (Integer|BLS::Fr)
  • y (Integer|BLS::Fr)

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kzg/setting.rb', line 34

def valid_proof?(commit_point, proof, x, y)
  x = x.is_a?(BLS::Fr) ? x : BLS::Fr.new(x)
  y = y.is_a?(BLS::Fr) ? y : BLS::Fr.new(y)
  xg2 = x.value.zero? ? BLS::PointG2::ZERO : BLS::PointG2::BASE * x
  yg = y.value.zero? ? BLS::PointG1::ZERO : BLS::PointG1::BASE * y

  # e([commitment - y]^(-1), [1]) * e([proof],  [s - x]) = 1
  lhs =
    BLS.pairing(
      (commit_point - yg).negate,
      BLS::PointG2::BASE,
      with_final_exp: false
    )
  rhs = BLS.pairing(proof, g2_points[1] - xg2, with_final_exp: false)
  exp = (lhs * rhs).final_exponentiate
  exp == BLS::Fq12::ONE
end