rsa-accumulator 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6b5bb2ad698c4440d30bdbd29b3be321a3829b78bad6e188d06b351e8d81f337
4
- data.tar.gz: e9270d41f253c25b4f2a44ca51d4fd03bc8ff323a173b8fff845ba5c8af5ef3c
3
+ metadata.gz: 9f38593cbe657293995ebc98f360b248664b552acaf748201f5fa5d4bf27d6aa
4
+ data.tar.gz: b6d45c16e546ba04b788c41b9426943266cd5bff76b848f1f6fb5eac1e8ad193
5
5
  SHA512:
6
- metadata.gz: 3bf39ed19c73847ec804b63032d940e55595f38f6b3e4abce3aba7bceb7524f06bc7a7b015264309e4b2c0123b22c999a110d08bb5982a16790deee658b00bc7
7
- data.tar.gz: b91fac654230b7f9e5837eb4249881f71e915e3f7911dd63e7bf4065ce8b7daa50bdd8beb0c3ff59283b39ff73972afec66bb4558824ac75918e0b43c1580761
6
+ metadata.gz: 4b43ce23e5961c58adfab5a752a599117a2d13733fdbdab01f2cf608df8a2a99e949edee002e140dcaa3ab94664793b877420c9c9bdc14faa400e6aa6ab9f94f
7
+ data.tar.gz: 24d9067f9931519c80159879ef4d5d0b125807d197c0ea3985e6f7ff8b0321915fa61e14463378856b8683f16d973a49d292d858b8cc332e51d89955bff82eb7
data/README.md CHANGED
@@ -65,4 +65,16 @@ You can remove elements from the accumulator by providing the inclusion proof.
65
65
 
66
66
  acc.member?(proof)
67
67
  => false
68
+
69
+ ### Holding the product of all elements
70
+
71
+ This feature is experimental and has not been checked against large amounts of data.
72
+
73
+ acc = RSA::Accumulator.generate_rsa2048(hold_elements: true)
74
+ acc.add('a', 'b', 'c')
75
+ acc.add('d', 'e')
76
+
77
+ # acc has product of all elements in acc#products, so you can get membership proof.
78
+ proof = acc.prove_membership('b')
79
+
68
80
 
@@ -1,5 +1,5 @@
1
1
  module RSA
2
2
  module ACC
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -16,30 +16,36 @@ module RSA
16
16
 
17
17
  attr_reader :n
18
18
  attr_accessor :value
19
- attr_reader :g # Initial value
19
+ attr_reader :g # Initial value
20
+ attr_reader :hold_elements # tha flag which indicate hold product of all elements.
21
+ attr_accessor :products # (Optional) product of all elements in Accumulator
20
22
 
21
23
  # Generate accumulator using RSA2048 modulus.
22
24
  # @return [RSA::Accumulator]
23
- def self.generate_rsa2048
24
- new(RSA2048_MODULUS, RSA2048_UNKNOWN_ELEM)
25
+ def self.generate_rsa2048(hold_elements: false)
26
+ new(RSA2048_MODULUS, RSA2048_UNKNOWN_ELEM, hold_elements)
25
27
  end
26
28
 
27
29
  # Generate accumulator with random modulus.
28
30
  # @param [Integer] bit_length bit length of accumulator. Default: 3072 bits.
29
31
  # @return [RSA::Accumulator]
30
- def self.generate_random(bit_length = 3072)
32
+ def self.generate_random(bit_length = 3072, hold_elements: false)
31
33
  n = OpenSSL::PKey::RSA.generate(bit_length).n.to_i
32
- new(n, SecureRandom.random_number(n))
34
+ new(n, SecureRandom.random_number(n), hold_elements)
33
35
  end
34
36
 
35
37
  # Initialize accumulator
36
38
  # @param [Integer] n modulus
37
39
  # @param [Integer] value initial value
40
+ # @param [Boolean] hold_elements
38
41
  # @return [RSA::Accumulator]
39
- def initialize(n, value)
42
+ def initialize(n, value, hold_elements)
40
43
  @n = n
41
44
  @value = value
42
45
  @g = value
46
+ @hold_elements = hold_elements
47
+ @products = 1 if hold_elements
48
+ puts "The feature which hold product of all elements is practical feature." if hold_elements
43
49
  end
44
50
 
45
51
  # Add element to accumulator and get inclusion proof.
@@ -48,7 +54,13 @@ module RSA
48
54
  def add(*elements)
49
55
  current_acc = value
50
56
  p = elements_to_prime(elements)
51
- @value = value.pow(p, n)
57
+ self.value = value.pow(p, n)
58
+ if hold_elements
59
+ elements.each do |e|
60
+ p = hash_to_prime(e)
61
+ self.products *= p unless products.modulo(p) == 0
62
+ end
63
+ end
52
64
  RSA::ACC::MembershipProof.new(elements, current_acc, value, RSA::ACC::PoE.prove(current_acc, p, value, n))
53
65
  end
54
66
 
@@ -77,6 +89,19 @@ module RSA
77
89
  RSA::ACC::PoE.verify(proof.d, x, proof.gv_inv, proof.poe_proof, n)
78
90
  end
79
91
 
92
+ # Generate membership proof for +elements+.
93
+ # This method is only available if hold_elements is set to true when the accumulator is initialized.
94
+ # @param [Array[String]] elements The elements for which you want to generate an membership proof.
95
+ # @return [RSA::ACC::MembershipProof] a membership proof for +elements+. If +elements+ does not exist in accumulator, return nil.
96
+ # @raise RSA::ACC::Error.new This exception is raised when hold_elements is set to false.
97
+ def prove_membership(*elements)
98
+ raise RSA::ACC::Error.new 'This accumulator does not hold the product of the elements.' unless hold_elements
99
+ x = elements_to_prime(elements)
100
+ return nil unless products.modulo(x) == 0
101
+ witness = g.pow(products / x, n)
102
+ RSA::ACC::MembershipProof.new(elements, witness, value, RSA::ACC::PoE.prove(witness, x, value, n))
103
+ end
104
+
80
105
  # Generate non-membership proof using set of elements in current acc and non membership elements.
81
106
  # @param [Array[String]] members The entire set of elements contained within this accumulator.
82
107
  # @param [Array[String]] non_members Elements not included in this accumulator that you want to prove non-membership.
@@ -119,8 +144,8 @@ module RSA
119
144
  proof_product *= w[0]
120
145
  end
121
146
  end
122
-
123
- @value = new_value
147
+ self.products = self.products / proof_product if hold_elements
148
+ self.value = new_value
124
149
  RSA::ACC::MembershipProof.new(proofs.map{|p|p.element}.flatten, value, current_value, RSA::ACC::PoE.prove(value, proof_product, current_value, n))
125
150
  end
126
151
 
@@ -130,8 +155,8 @@ module RSA
130
155
  def root_factor(*f)
131
156
  return [value] if f.size == 1
132
157
  half_n = f.size / 2
133
- g_l = RSA::Accumulator.new(n, value.pow(f[0...half_n].map.inject(:*), n))
134
- g_r = RSA::Accumulator.new(n, value.pow(f[half_n..-1].map.inject(:*), n))
158
+ g_l = RSA::Accumulator.new(n, value.pow(f[0...half_n].map.inject(:*), n), false)
159
+ g_r = RSA::Accumulator.new(n, value.pow(f[half_n..-1].map.inject(:*), n), false)
135
160
  l = g_r.root_factor(*f[0...half_n])
136
161
  r = g_l.root_factor(*f[half_n..-1])
137
162
  [l, r].flatten
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsa-accumulator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-23 00:00:00.000000000 Z
11
+ date: 2020-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rbnacl
@@ -94,7 +94,7 @@ files:
94
94
  - lib/rsa/acc/proof.rb
95
95
  - lib/rsa/acc/version.rb
96
96
  - lib/rsa/accumulator.rb
97
- - rsa-accumulatorrb.gemspec
97
+ - rsa-accumulator.gemspec
98
98
  homepage: https://github.com/chaintope/rsa-accumulatorrb
99
99
  licenses:
100
100
  - MIT