hensel_code 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,39 +2,11 @@
2
2
 
3
3
  module HenselCode
4
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
-
5
+ class TruncatedFinitePadicExpansion < PAdicBase
21
6
  def modulus
22
7
  prime**exponent
23
8
  end
24
9
 
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
10
  def to_i
39
11
  hensel_code
40
12
  end
@@ -43,61 +15,28 @@ module HenselCode
43
15
  hensel_code.to_s
44
16
  end
45
17
 
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
18
+ def inspect
19
+ "<HenselCode: #{hensel_code}>"
82
20
  end
83
21
 
84
- def *(other)
85
- valid?(other)
86
- self.class.new prime, exponent, (hensel_code * other.hensel_code) % modulus
22
+ def inverse
23
+ new_hensel_code = mod_inverse(hensel_code, modulus)
24
+ self.class.new prime, exponent, new_hensel_code
87
25
  end
88
26
 
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
27
+ private
94
28
 
95
- def inspect
96
- "[HenselCode: #{hensel_code}, prime: #{prime}, exponent: #{exponent}, modulus: #{modulus}]"
29
+ def evaluate(operation, other)
30
+ if operation == "/"
31
+ other_hensel_code = mod_inverse(other.hensel_code, modulus)
32
+ op = "*"
33
+ else
34
+ other_hensel_code = other.hensel_code
35
+ op = operation
36
+ end
37
+ self.class.new prime, exponent, hensel_code.send(op, other_hensel_code) % modulus
97
38
  end
98
39
 
99
- private
100
-
101
40
  def valid_number?(number)
102
41
  case number
103
42
  when Rational
@@ -111,6 +50,11 @@ module HenselCode
111
50
  end
112
51
  end
113
52
 
53
+ def valid_hensel_code?(new_hensel_code)
54
+ message = "must be an integer up to #{modulus - 1}"
55
+ raise WrongHenselCodeInputType, message unless new_hensel_code < modulus
56
+ end
57
+
114
58
  def encode
115
59
  denominator_inverse = mod_inverse(denominator, modulus)
116
60
  @hensel_code = (numerator * denominator_inverse) % modulus
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HenselCode
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/hensel_code.rb CHANGED
@@ -13,12 +13,21 @@ module HenselCode
13
13
  class HenselCodesWithDifferentPrimesAndExponents < StandardError; end
14
14
  class HenselCodesWithDifferentExponents < StandardError; end
15
15
  class IncompatibleOperandTypes < StandardError; end
16
+ class NonInitializableClass < StandardError; end
16
17
 
17
- autoload :Tools, "hensel_code/tools"
18
- autoload :TFPEVerifier, "hensel_code/tfpe_verifier"
19
- autoload :TruncatedFinitePadicExpansion, "hensel_code/truncated_finite_padic_expansion"
18
+ autoload :Tools, "hensel_code/tools"
19
+ autoload :PAdicBase, "hensel_code/padic_base"
20
+ autoload :GAdicBase, "hensel_code/gadic_base"
21
+ autoload :Polynomial, "hensel_code/polynomial"
22
+ autoload :PAdicVerifier, "hensel_code/padic_verifier"
23
+ autoload :GAdicVerifier, "hensel_code/gadic_verifier"
24
+ autoload :ModularArithmetic, "hensel_code/modular_arithmetic"
25
+ autoload :FinitePadicExpansion, "hensel_code/finite_padic_expansion"
26
+ autoload :TruncatedFinitePadicExpansion, "hensel_code/truncated_finite_padic_expansion"
27
+ autoload :TruncatedFiniteGadicExpansion, "hensel_code/truncated_finite_gadic_expansion"
20
28
 
21
29
  # aliases for classes with long names
22
30
  TFPE = TruncatedFinitePadicExpansion
23
31
  HCWDPAE = HenselCodesWithDifferentPrimesAndExponents
32
+ WHIT = WrongHenselCodeInputType
24
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hensel_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David William Silva
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-03 00:00:00.000000000 Z
11
+ date: 2022-03-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A Ruby library for homomorphically representing rational numbers as integers.
14
14
  email:
@@ -17,6 +17,7 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - ".codecov.yml"
20
21
  - ".rubocop.yml"
21
22
  - CHANGELOG.md
22
23
  - Gemfile
@@ -26,8 +27,15 @@ files:
26
27
  - Rakefile
27
28
  - codecov
28
29
  - lib/hensel_code.rb
29
- - lib/hensel_code/tfpe_verifier.rb
30
+ - lib/hensel_code/finite_padic_expansion.rb
31
+ - lib/hensel_code/gadic_base.rb
32
+ - lib/hensel_code/gadic_verifier.rb
33
+ - lib/hensel_code/modular_arithmetic.rb
34
+ - lib/hensel_code/padic_base.rb
35
+ - lib/hensel_code/padic_verifier.rb
36
+ - lib/hensel_code/polynomial.rb
30
37
  - lib/hensel_code/tools.rb
38
+ - lib/hensel_code/truncated_finite_gadic_expansion.rb
31
39
  - lib/hensel_code/truncated_finite_padic_expansion.rb
32
40
  - lib/hensel_code/version.rb
33
41
  - sig/hensel_code.rbs