hensel_code 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +14 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +22 -0
- data/Gemfile.lock +73 -0
- data/LICENSE.txt +21 -0
- data/README.md +191 -0
- data/Rakefile +16 -0
- data/codecov +0 -0
- data/lib/hensel_code/tfpe_verifier.rb +36 -0
- data/lib/hensel_code/tools.rb +81 -0
- data/lib/hensel_code/truncated_finite_padic_expansion.rb +125 -0
- data/lib/hensel_code/version.rb +5 -0
- data/lib/hensel_code.rb +24 -0
- data/sig/hensel_code.rbs +4 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4ade0ad51689f11d1f36d2ac3e62c40a6ec61fef5142f11371e5403e195ad70a
|
4
|
+
data.tar.gz: 0f7288540b1000124be29836ef664ea31d2670a9f0390c24b075e90febba6e42
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 457aef94ccb7bf0d107e54e85731d26c8463d24f6a176f5769f55737adb602af753441e7964ea8c5b1ccd53e16357a54eada4221ba62e14b696204f72f76942c
|
7
|
+
data.tar.gz: 4604cc97de514dbb23cecf574a727e83510947158b6565909c6f556d621fab36b972cd2bbe128b89c693e715fc19d2123dcd44681b1bfe860b016eb93b7091eb
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in hensel_code.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "minitest", "~> 5.0"
|
11
|
+
|
12
|
+
gem "rubocop", "~> 1.21"
|
13
|
+
|
14
|
+
gem "rubocop-minitest", "~> 0.17.2"
|
15
|
+
|
16
|
+
gem "rubocop-rake", "~> 0.6"
|
17
|
+
|
18
|
+
gem "minitest-reporters", "~> 1.5"
|
19
|
+
|
20
|
+
gem "prime", "~> 0.1.2"
|
21
|
+
|
22
|
+
gem "codecov", require: false, group: :test
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hensel_code (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ansi (1.5.0)
|
10
|
+
ast (2.4.2)
|
11
|
+
builder (3.2.4)
|
12
|
+
codecov (0.6.0)
|
13
|
+
simplecov (>= 0.15, < 0.22)
|
14
|
+
docile (1.4.0)
|
15
|
+
forwardable (1.3.2)
|
16
|
+
minitest (5.15.0)
|
17
|
+
minitest-reporters (1.5.0)
|
18
|
+
ansi
|
19
|
+
builder
|
20
|
+
minitest (>= 5.0)
|
21
|
+
ruby-progressbar
|
22
|
+
parallel (1.21.0)
|
23
|
+
parser (3.1.1.0)
|
24
|
+
ast (~> 2.4.1)
|
25
|
+
prime (0.1.2)
|
26
|
+
forwardable
|
27
|
+
singleton
|
28
|
+
rainbow (3.1.1)
|
29
|
+
rake (13.0.6)
|
30
|
+
regexp_parser (2.2.1)
|
31
|
+
rexml (3.2.5)
|
32
|
+
rubocop (1.25.1)
|
33
|
+
parallel (~> 1.10)
|
34
|
+
parser (>= 3.1.0.0)
|
35
|
+
rainbow (>= 2.2.2, < 4.0)
|
36
|
+
regexp_parser (>= 1.8, < 3.0)
|
37
|
+
rexml
|
38
|
+
rubocop-ast (>= 1.15.1, < 2.0)
|
39
|
+
ruby-progressbar (~> 1.7)
|
40
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
41
|
+
rubocop-ast (1.16.0)
|
42
|
+
parser (>= 3.1.1.0)
|
43
|
+
rubocop-minitest (0.17.2)
|
44
|
+
rubocop (>= 0.90, < 2.0)
|
45
|
+
rubocop-rake (0.6.0)
|
46
|
+
rubocop (~> 1.0)
|
47
|
+
ruby-progressbar (1.11.0)
|
48
|
+
simplecov (0.21.2)
|
49
|
+
docile (~> 1.1)
|
50
|
+
simplecov-html (~> 0.11)
|
51
|
+
simplecov_json_formatter (~> 0.1)
|
52
|
+
simplecov-html (0.12.3)
|
53
|
+
simplecov_json_formatter (0.1.4)
|
54
|
+
singleton (0.1.1)
|
55
|
+
unicode-display_width (2.1.0)
|
56
|
+
|
57
|
+
PLATFORMS
|
58
|
+
ruby
|
59
|
+
x86_64-darwin-19
|
60
|
+
|
61
|
+
DEPENDENCIES
|
62
|
+
codecov
|
63
|
+
hensel_code!
|
64
|
+
minitest (~> 5.0)
|
65
|
+
minitest-reporters (~> 1.5)
|
66
|
+
prime (~> 0.1.2)
|
67
|
+
rake (~> 13.0)
|
68
|
+
rubocop (~> 1.21)
|
69
|
+
rubocop-minitest (~> 0.17.2)
|
70
|
+
rubocop-rake (~> 0.6)
|
71
|
+
|
72
|
+
BUNDLED WITH
|
73
|
+
2.3.8
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 David William Silva
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
# HenselCode
|
2
|
+
|
3
|
+
![example workflow](https://github.com/davidwilliam/hensel_code/actions/workflows/main.yml/badge.svg) [![codecov](https://codecov.io/gh/davidwilliam/hensel_code/branch/main/graph/badge.svg?token=XJ0C0U7P2M)](https://codecov.io/gh/davidwilliam/hensel_code) [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop) ![GitHub](https://img.shields.io/github/license/davidwilliam/hensel_code)
|
4
|
+
|
5
|
+
Hensel Code allows you to homomorphically encode rational numbers as integers using the finite-segment p-adic arithmetic, also known as Hensel codes.
|
6
|
+
|
7
|
+
## Mathematical Background
|
8
|
+
|
9
|
+
In our Wiki, you can find a brief [introduction to the mathematical background on Hensel codes](https://github.com/davidwilliam/hensel_code/wiki/Mathematical-Background). We will continue to update that area as we update the gem.
|
10
|
+
|
11
|
+
## Applications
|
12
|
+
|
13
|
+
You can also find information about [possible applications with Hensel codes](https://github.com/davidwilliam/hensel_code/wiki/Applications).
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'hensel_code'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle install
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install hensel_code
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
Let `p=257` and `r=3`. Given two rational numbers `rat1 = Rational(3,5)` and `rat2 = Rational(4,3)`, we encode `rat1` and `rat2` as follows:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
h1 = HenselCode::TruncatedFinitePadicExpansion.new(p, r, rat1)
|
37
|
+
# => [HenselCode: 13579675, prime: 257, exponent: 3, modulus: 16974593]
|
38
|
+
h2 = HenselCode::TruncatedFinitePadicExpansion.new(p, r, rat1)
|
39
|
+
# => [HenselCode: 5658199, prime: 257, exponent: 3, modulus: 16974593]
|
40
|
+
h1.class
|
41
|
+
# => HenselCode::TruncatedFinitePadicExpansion
|
42
|
+
h2.class
|
43
|
+
# => HenselCode::TruncatedFinitePadicExpansion
|
44
|
+
puts h1
|
45
|
+
# => 13579675
|
46
|
+
puts h2
|
47
|
+
# => 5658199
|
48
|
+
```
|
49
|
+
|
50
|
+
Now we can carry arithmetic computations on the `h1` and `h2` objects as if we were computing over `rat1` and `rat2`:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
h1_plus_h2 = h1 + h2
|
54
|
+
h1_minus_h2 = h1 - h2
|
55
|
+
h1_times_h2 = h1 * h2
|
56
|
+
h1_div_h2 = h1 / h2
|
57
|
+
```
|
58
|
+
|
59
|
+
All the computations are reduced modulo `p^r`.
|
60
|
+
|
61
|
+
At any moment, we can check the rational representation of the results we produced:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
h1_plus_h2.to_r
|
65
|
+
# => (29/15)
|
66
|
+
h1_minus_h2.to_r
|
67
|
+
# => (-11/15)
|
68
|
+
h1_times_h2.to_r
|
69
|
+
# => (4/5)
|
70
|
+
h1_div_h2.to_r
|
71
|
+
# => (9/20)
|
72
|
+
```
|
73
|
+
|
74
|
+
And we can verify that
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
rat1 + rat2
|
78
|
+
# => (29/15)
|
79
|
+
rat1 - rat2
|
80
|
+
# => (-11/15)
|
81
|
+
rat1 * rat2
|
82
|
+
# => (4/5)
|
83
|
+
```
|
84
|
+
|
85
|
+
### Constraints
|
86
|
+
|
87
|
+
In order to operate on two or more Hensel codes, they all must be of the same object type and have the same prime and exponent, otherwise HenselCode will raise an exception. Again, let `rat1 = Rational(3,5)` and `rat2 = Rational(4,3)`, and `p1 = 241`, `p2 = 251`, `r1 = 3`, and `r2 = 4`:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
h1 = HenselCode::TruncatedFinitePadicExpansion.new(p1, r1, rat1)
|
91
|
+
# => [HenselCode: 5599009, prime: 241, exponent: 3, modulus: 13997521]
|
92
|
+
h2 = HenselCode::TruncatedFinitePadicExpansion.new(p1, r2, rat1)
|
93
|
+
# => [HenselCode: 1349361025, prime: 241, exponent: 4, modulus: 3373402561]
|
94
|
+
h3 = HenselCode::TruncatedFinitePadicExpansion.new(p2, r1, rat1)
|
95
|
+
# => [HenselCode: 6325301, prime: 251, exponent: 3, modulus: 15813251]
|
96
|
+
h4 = HenselCode::TruncatedFinitePadicExpansion.new(p2, r2, rat1)
|
97
|
+
=> [HenselCode: 1587650401, prime: 251, exponent: 4, modulus: 3969126001]
|
98
|
+
```
|
99
|
+
|
100
|
+
The following operations will raise exceptions:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
h1 + h2
|
104
|
+
# => 5599009 has exponent 3 while 1349361025 has exponent 4 (HenselCode::HenselCodesWithDifferentExponents)
|
105
|
+
h1 + h3
|
106
|
+
# => 5599009 has prime 241 while 6325301 has prime 251 (HenselCode::HenselCodesWithDifferentPrimes)
|
107
|
+
h1 + h4
|
108
|
+
# => 5599009 has prime 241 and exponent 3 while 1587650401 has prime 251 and exponent 4 (HenselCode::HenselCodesWithDifferentPrimesAndExponents)
|
109
|
+
num = 5
|
110
|
+
h1 + num
|
111
|
+
# => 5599009 is a HenselCode::TruncatedFinitePadicExpansion while 5 is a Integer (HenselCode::IncompatibleOperandTypes)
|
112
|
+
```
|
113
|
+
|
114
|
+
### Manipulating Hensel Code Objects
|
115
|
+
|
116
|
+
Let `p = 541`, `r = 3`, `rat = Rational(11,5)`. We create a Hensel code as before:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
h = HenselCode::TruncatedFinitePadicExpansion.new p, r, rat
|
120
|
+
# => [HenselCode: 126672339, prime: 541, exponent: 3, modulus: 158340421]
|
121
|
+
```
|
122
|
+
|
123
|
+
We can change the prime and the exponent:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
p = 1223
|
127
|
+
r = 4
|
128
|
+
h.replace_prime(p)
|
129
|
+
# => [HenselCode: 731710629, prime: 1223, exponent: 3, modulus: 1829276567]
|
130
|
+
h.replace_exponent(r)
|
131
|
+
# => [HenselCode: 1789764193155, prime: 1223, exponent: 4, modulus: 2237205241441]
|
132
|
+
```
|
133
|
+
|
134
|
+
Any change in the prime and/or the exponent of a Hensel code object will change the Hensel code value and the modulus as well, however, the Hensel code object continues to refer to represent the same rational number:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
h.to_r
|
138
|
+
# => (11/5)
|
139
|
+
```
|
140
|
+
|
141
|
+
We can also change the rational number:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
rat = Rational(13,7)
|
145
|
+
h.replace_rational(rat)
|
146
|
+
# => [HenselCode: 1278402995111, prime: 1223, exponent: 4, modulus: 2237205241441]
|
147
|
+
h.to_r
|
148
|
+
# => (13/7)
|
149
|
+
```
|
150
|
+
|
151
|
+
We can initiate a Hensel code object with its Hensel code value, instead of a rational number:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
h = HenselCode::TruncatedFinitePadicExpansion.new p, r, 53673296543
|
155
|
+
```
|
156
|
+
|
157
|
+
and then we can check what is the rational number represented by the resulting object:
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
h.to_r
|
161
|
+
# => (706147/633690)
|
162
|
+
```
|
163
|
+
|
164
|
+
We can update the Hensel code value of an existing Hensel code object:
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
h.replace_hensel_code(38769823656)
|
168
|
+
# => (-685859/94809)
|
169
|
+
```
|
170
|
+
|
171
|
+
### Class Alias
|
172
|
+
|
173
|
+
Since `HenselCode::TruncatedFinitePadicExpansion` is a bit long, the alias `HenselCode::TFPE` can be used instead.
|
174
|
+
|
175
|
+
## Coming Soon
|
176
|
+
|
177
|
+
There are many types of Hensel codes. We are currently implementing only one. Very soon we will add more types of Hensel codes to the library, which will further expand what we can do with it.
|
178
|
+
|
179
|
+
## Development
|
180
|
+
|
181
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
182
|
+
|
183
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
184
|
+
|
185
|
+
## Contributing
|
186
|
+
|
187
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/davidwilliam/hensel_code.
|
188
|
+
|
189
|
+
## License
|
190
|
+
|
191
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
data/codecov
ADDED
Binary file
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HenselCode
|
4
|
+
# verifications pre-evaluation of hensel codes
|
5
|
+
module TFPEVerifier
|
6
|
+
def valid?(other)
|
7
|
+
incompatible_operand_type?(other)
|
8
|
+
different_prime_and_same_exponent?(other)
|
9
|
+
different_prime_and_different_exponent?(other)
|
10
|
+
same_prime_and_different_exponent?(other)
|
11
|
+
end
|
12
|
+
|
13
|
+
def incompatible_operand_type?(other)
|
14
|
+
message = "#{self} is a #{self.class} while #{other} is a #{other.class}"
|
15
|
+
raise IncompatibleOperandTypes, message unless instance_of?(other.class)
|
16
|
+
end
|
17
|
+
|
18
|
+
def different_prime_and_same_exponent?(other)
|
19
|
+
message = "#{self} has prime #{prime} while #{other} has prime #{other.prime}"
|
20
|
+
raise HenselCodesWithDifferentPrimes, message if prime != other.prime && exponent == other.exponent
|
21
|
+
end
|
22
|
+
|
23
|
+
def different_prime_and_different_exponent?(other)
|
24
|
+
message = <<~MSG
|
25
|
+
"#{self} has prime #{prime} and exponent #{exponent}
|
26
|
+
while #{other} has prime #{other.prime} and exponent #{other.exponent}
|
27
|
+
MSG
|
28
|
+
raise HenselCodesWithDifferentPrimesAndExponents, message if prime != other.prime && exponent != other.exponent
|
29
|
+
end
|
30
|
+
|
31
|
+
def same_prime_and_different_exponent?(other)
|
32
|
+
message = "#{self} has exponent #{exponent} while #{other} has exponent #{other.exponent}"
|
33
|
+
raise HenselCodesWithDifferentExponents, message if prime == other.prime && exponent != other.exponent
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HenselCode
|
4
|
+
# Required basics of number generation and modular
|
5
|
+
module Tools
|
6
|
+
def random_integer(bits)
|
7
|
+
OpenSSL::BN.rand(bits).to_i
|
8
|
+
end
|
9
|
+
|
10
|
+
def random_rational(bits)
|
11
|
+
numbers = [random_integer(bits)]
|
12
|
+
while numbers.size < 2
|
13
|
+
num = random_integer(bits)
|
14
|
+
numbers << num if numbers.last.gcd(num) == 1
|
15
|
+
end
|
16
|
+
Rational(*numbers)
|
17
|
+
end
|
18
|
+
|
19
|
+
def random_prime(bits)
|
20
|
+
# The OpenSSL library only generates random primes from 16 bits on
|
21
|
+
# The 3513 is the first 16-bit number
|
22
|
+
# which means there are 3512 primes under 16 bits in length
|
23
|
+
# Therefore for allowing values for the parameter 'bits' between 2 and 15,
|
24
|
+
# we generate all primes from 2 to 15 bits using Ruby's Prime library
|
25
|
+
# and select only those with bit length equal to the value of 'bits'
|
26
|
+
# so we sample one element
|
27
|
+
# We only do this if the value of 'bits' is less than 16, otherwise we
|
28
|
+
# use OpenSSL to generate the prime
|
29
|
+
if bits >= 2 && bits < 16
|
30
|
+
primes = Prime.first(3512)
|
31
|
+
primes.select { |p| p.bit_length == bits }.sample
|
32
|
+
elsif bits >= 16
|
33
|
+
OpenSSL::BN.generate_prime(bits).to_i
|
34
|
+
else
|
35
|
+
raise BadBitRangeForRandomPrime, "The bit length must be greater than or equal to 2"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def random_distinct_primes(quantity, bits)
|
40
|
+
primes = [random_prime(bits)]
|
41
|
+
while primes.size < quantity
|
42
|
+
prime = random_prime(bits)
|
43
|
+
primes << prime if prime != primes.last
|
44
|
+
end
|
45
|
+
primes
|
46
|
+
end
|
47
|
+
|
48
|
+
def eea_core(num1, num2, bound = 0)
|
49
|
+
setup = bound.zero? ? [1, 0] : [0, 1]
|
50
|
+
x0, x1, y0, y1 = [num1, num2] + setup
|
51
|
+
i = 1
|
52
|
+
while x1 > bound
|
53
|
+
q = x0 / x1
|
54
|
+
x0, x1 = x1, x0 - (q * x1)
|
55
|
+
y0, y1 = y1, y0 - (q * y1)
|
56
|
+
i += 1
|
57
|
+
end
|
58
|
+
[x0, y0, i, x1, y1]
|
59
|
+
end
|
60
|
+
|
61
|
+
def extended_gcd(num1, num2)
|
62
|
+
if num1.negative?
|
63
|
+
x, y = extended_gcd(-num1, num2)
|
64
|
+
[x, -y]
|
65
|
+
elsif num2.negative?
|
66
|
+
x, y = extended_gcd(num1, -num2)
|
67
|
+
[x, y]
|
68
|
+
else
|
69
|
+
x, y = eea_core(num1, num2)
|
70
|
+
[x, y]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def mod_inverse(num, mod)
|
75
|
+
x, y, = extended_gcd(num, mod)
|
76
|
+
raise ZeroDivisionError, "#{num} has no inverse modulo #{mod}" unless x == 1
|
77
|
+
|
78
|
+
y % mod
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HenselCode
|
4
|
+
# truncated finite p-adic expansion hensel code class
|
5
|
+
class TruncatedFinitePadicExpansion
|
6
|
+
include Tools
|
7
|
+
include TFPEVerifier
|
8
|
+
|
9
|
+
attr_accessor :prime, :exponent, :rational, :hensel_code, :n
|
10
|
+
private :prime=, :exponent=, :rational=, :hensel_code=
|
11
|
+
|
12
|
+
def initialize(prime, exponent, number)
|
13
|
+
@prime = prime
|
14
|
+
@exponent = exponent
|
15
|
+
@n = Integer.sqrt((modulus - 1) / 2)
|
16
|
+
|
17
|
+
valid_number?(number)
|
18
|
+
encode
|
19
|
+
end
|
20
|
+
|
21
|
+
def modulus
|
22
|
+
prime**exponent
|
23
|
+
end
|
24
|
+
|
25
|
+
def numerator
|
26
|
+
rational.numerator
|
27
|
+
end
|
28
|
+
|
29
|
+
def denominator
|
30
|
+
rational.denominator
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_r
|
34
|
+
decode
|
35
|
+
rational
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_i
|
39
|
+
hensel_code
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
hensel_code.to_s
|
44
|
+
end
|
45
|
+
|
46
|
+
def replace_prime(new_prime)
|
47
|
+
self.prime = new_prime
|
48
|
+
encode
|
49
|
+
decode
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
def replace_exponent(new_exponent)
|
54
|
+
self.exponent = new_exponent
|
55
|
+
encode
|
56
|
+
decode
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
def replace_rational(new_rational)
|
61
|
+
self.rational = new_rational
|
62
|
+
encode
|
63
|
+
decode
|
64
|
+
self
|
65
|
+
end
|
66
|
+
|
67
|
+
def replace_hensel_code(new_hensel_code)
|
68
|
+
self.hensel_code = new_hensel_code
|
69
|
+
decode
|
70
|
+
encode
|
71
|
+
self
|
72
|
+
end
|
73
|
+
|
74
|
+
def +(other)
|
75
|
+
valid?(other)
|
76
|
+
self.class.new prime, exponent, (hensel_code + other.hensel_code) % modulus
|
77
|
+
end
|
78
|
+
|
79
|
+
def -(other)
|
80
|
+
valid?(other)
|
81
|
+
self.class.new prime, exponent, (hensel_code - other.hensel_code) % modulus
|
82
|
+
end
|
83
|
+
|
84
|
+
def *(other)
|
85
|
+
valid?(other)
|
86
|
+
self.class.new prime, exponent, (hensel_code * other.hensel_code) % modulus
|
87
|
+
end
|
88
|
+
|
89
|
+
def /(other)
|
90
|
+
valid?(other)
|
91
|
+
h2_hensel_code_inverse = mod_inverse(other.hensel_code, modulus)
|
92
|
+
self.class.new prime, exponent, (hensel_code * h2_hensel_code_inverse) % modulus
|
93
|
+
end
|
94
|
+
|
95
|
+
def inspect
|
96
|
+
"[HenselCode: #{hensel_code}, prime: #{prime}, exponent: #{exponent}, modulus: #{modulus}]"
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def valid_number?(number)
|
102
|
+
case number
|
103
|
+
when Rational
|
104
|
+
@rational = number
|
105
|
+
when Integer
|
106
|
+
@hensel_code = number
|
107
|
+
decode
|
108
|
+
else
|
109
|
+
raise WrongHenselCodeInputType, "number must be a Rational or an\
|
110
|
+
Integer object and it was a #{number.class}"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def encode
|
115
|
+
denominator_inverse = mod_inverse(denominator, modulus)
|
116
|
+
@hensel_code = (numerator * denominator_inverse) % modulus
|
117
|
+
end
|
118
|
+
|
119
|
+
def decode
|
120
|
+
eea_vars = eea_core(modulus, hensel_code, n)
|
121
|
+
i, x, y = eea_vars[2..4]
|
122
|
+
@rational = Rational(*[x, y].map { |e| ((-1)**(i + 1)) * e })
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
data/lib/hensel_code.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "hensel_code/version"
|
4
|
+
require "prime"
|
5
|
+
require "openssl"
|
6
|
+
|
7
|
+
# Main classes definitions and loadings
|
8
|
+
module HenselCode
|
9
|
+
class Error < StandardError; end
|
10
|
+
class BadBitRangeForRandomPrime < StandardError; end
|
11
|
+
class WrongHenselCodeInputType < StandardError; end
|
12
|
+
class HenselCodesWithDifferentPrimes < StandardError; end
|
13
|
+
class HenselCodesWithDifferentPrimesAndExponents < StandardError; end
|
14
|
+
class HenselCodesWithDifferentExponents < StandardError; end
|
15
|
+
class IncompatibleOperandTypes < StandardError; end
|
16
|
+
|
17
|
+
autoload :Tools, "hensel_code/tools"
|
18
|
+
autoload :TFPEVerifier, "hensel_code/tfpe_verifier"
|
19
|
+
autoload :TruncatedFinitePadicExpansion, "hensel_code/truncated_finite_padic_expansion"
|
20
|
+
|
21
|
+
# aliases for classes with long names
|
22
|
+
TFPE = TruncatedFinitePadicExpansion
|
23
|
+
HCWDPAE = HenselCodesWithDifferentPrimesAndExponents
|
24
|
+
end
|
data/sig/hensel_code.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hensel_code
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David William Silva
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-03-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A Ruby library for homomorphically representing rational numbers as integers.
|
14
|
+
email:
|
15
|
+
- contact@davidwsilva.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rubocop.yml"
|
21
|
+
- CHANGELOG.md
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- codecov
|
28
|
+
- lib/hensel_code.rb
|
29
|
+
- lib/hensel_code/tfpe_verifier.rb
|
30
|
+
- lib/hensel_code/tools.rb
|
31
|
+
- lib/hensel_code/truncated_finite_padic_expansion.rb
|
32
|
+
- lib/hensel_code/version.rb
|
33
|
+
- sig/hensel_code.rbs
|
34
|
+
homepage: https://github.com/davidwilliam/hensel_code
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata:
|
38
|
+
homepage_uri: https://github.com/davidwilliam/hensel_code
|
39
|
+
source_code_uri: https://github.com/davidwilliam/hensel_code
|
40
|
+
changelog_uri: https://github.com/davidwilliam/hensel_code/blob/main/CHANGELOG.md
|
41
|
+
rubygems_mfa_required: 'true'
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 2.6.0
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubygems_version: 3.3.3
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Homomorphic encoding of rational numbers.
|
61
|
+
test_files: []
|