elliptic_curve 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: de1ce69b480667f278fcba941f9402e409243062
4
+ data.tar.gz: 0a9101abc08811d0bdb1d4f404d812291e39a40b
5
+ SHA512:
6
+ metadata.gz: d6f0453333e00a582d4d63e192ed2175867ff267e9e41383880efa425a9ec976b6acf7bae68bf0fa1d05c2114c9e8c43a326a3dd4271669d34c40e4e50ff4797
7
+ data.tar.gz: aab90bed91d58a7e2ca66e8725d1ded213dca089aa1cbf89eece6a2847e20b403b001a08f1b3f7c3b05408170602d7955aebe717b041607a90952984730dbefa
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,52 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="FacetManager">
4
+ <facet type="gem" name="Ruby Gem">
5
+ <configuration>
6
+ <option name="GEM_APP_ROOT_PATH" value="$MODULE_DIR$" />
7
+ <option name="GEM_APP_TEST_PATH" value="$MODULE_DIR$/test" />
8
+ <option name="GEM_APP_LIB_PATH" value="$MODULE_DIR$/lib" />
9
+ </configuration>
10
+ </facet>
11
+ </component>
12
+ <component name="ModuleRunConfigurationManager">
13
+ <configuration default="false" name="Run test 'ec_test': elliptic_curve" type="TestUnitRunConfigurationType" factoryName="Test::Unit/Shoulda/Minitest" temporary="true">
14
+ <predefined_log_file id="RUBY_TESTUNIT" enabled="true" />
15
+ <module name="elliptic_curve" />
16
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="RUBY_ARGS" VALUE="-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)" />
17
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="WORK DIR" VALUE="$MODULE_DIR$/test" />
18
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="SHOULD_USE_SDK" VALUE="false" />
19
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="ALTERN_SDK_NAME" VALUE="" />
20
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="myPassParentEnvs" VALUE="true" />
21
+ <envs>
22
+ <env name="JRUBY_OPTS" value="-X+O" />
23
+ </envs>
24
+ <EXTENSION ID="BundlerRunConfigurationExtension" bundleExecEnabled="false" />
25
+ <EXTENSION ID="JRubyRunConfigurationExtension" NailgunExecEnabled="false" />
26
+ <EXTENSION ID="RubyCoverageRunConfigurationExtension" enabled="false" sample_coverage="true" track_test_folders="true" runner="rcov">
27
+ <COVERAGE_PATTERN ENABLED="true">
28
+ <PATTERN REGEXPS="/.rvm/" INCLUDED="false" />
29
+ </COVERAGE_PATTERN>
30
+ </EXTENSION>
31
+ <EXTENSION ID="org.jetbrains.plugins.ruby.motion.run.MotionSimulatorRunExtension" />
32
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="TESTS_FOLDER_PATH" VALUE="" />
33
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="TEST_SCRIPT_PATH" VALUE="$MODULE_DIR$/test/ec_test.rb" />
34
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="TEST_FILE_MASK" VALUE="" />
35
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="TEST_METHOD_NAME" VALUE="" />
36
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="TEST_TEST_TYPE" VALUE="TEST_SCRIPT" />
37
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="DRB" VALUE="false" />
38
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="ZEUS" VALUE="false" />
39
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="SPRING" VALUE="false" />
40
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="RUNNER_OPTIONS" VALUE="" />
41
+ <method />
42
+ </configuration>
43
+ </component>
44
+ <component name="NewModuleRootManager">
45
+ <content url="file://$MODULE_DIR$">
46
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
47
+ </content>
48
+ <orderEntry type="jdk" jdkName="rbenv: 2.1.2" jdkType="RUBY_SDK" />
49
+ <orderEntry type="sourceFolder" forTests="false" />
50
+ <orderEntry type="library" scope="PROVIDED" name="rake (v10.4.2, rbenv: 2.1.2) [gem]" level="application" />
51
+ </component>
52
+ </module>
data/.idea/misc.xml ADDED
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectLevelVcsManager" settingsEditedManually="false">
4
+ <OptionsSetting value="true" id="Add" />
5
+ <OptionsSetting value="true" id="Remove" />
6
+ <OptionsSetting value="true" id="Checkout" />
7
+ <OptionsSetting value="true" id="Update" />
8
+ <OptionsSetting value="true" id="Status" />
9
+ <OptionsSetting value="true" id="Edit" />
10
+ <ConfirmationsSetting value="0" id="Add" />
11
+ <ConfirmationsSetting value="0" id="Remove" />
12
+ </component>
13
+ <component name="ProjectRootManager" version="2" project-jdk-name="ruby-1.8.7-p249" project-jdk-type="RUBY_SDK" />
14
+ </project>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/elliptic_curve.iml" filepath="$PROJECT_DIR$/.idea/elliptic_curve.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
data/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,46 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ChangeListManager">
4
+ <option name="TRACKING_ENABLED" value="true" />
5
+ <option name="SHOW_DIALOG" value="false" />
6
+ <option name="HIGHLIGHT_CONFLICTS" value="true" />
7
+ <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
8
+ <option name="LAST_RESOLUTION" value="IGNORE" />
9
+ </component>
10
+ <component name="ChangesViewManager" flattened_view="true" show_ignored="false" />
11
+ <component name="CreatePatchCommitExecutor">
12
+ <option name="PATCH_PATH" value="" />
13
+ </component>
14
+ <component name="NamedScopeManager">
15
+ <order />
16
+ </component>
17
+ <component name="ProjectLevelVcsManager" settingsEditedManually="false">
18
+ <OptionsSetting value="true" id="Add" />
19
+ <OptionsSetting value="true" id="Remove" />
20
+ <OptionsSetting value="true" id="Checkout" />
21
+ <OptionsSetting value="true" id="Update" />
22
+ <OptionsSetting value="true" id="Status" />
23
+ <OptionsSetting value="true" id="Edit" />
24
+ <ConfirmationsSetting value="0" id="Add" />
25
+ <ConfirmationsSetting value="0" id="Remove" />
26
+ </component>
27
+ <component name="PropertiesComponent">
28
+ <property name="options.lastSelected" value="configurable.group.appearance" />
29
+ <property name="options.splitter.main.proportions" value="0.3" />
30
+ <property name="options.splitter.details.proportions" value="0.2" />
31
+ </component>
32
+ <component name="ShelveChangesManager" show_recycled="false" />
33
+ <component name="TaskManager">
34
+ <task active="true" id="Default" summary="Default task">
35
+ <option name="number" value="Default" />
36
+ </task>
37
+ <servers />
38
+ </component>
39
+ <component name="VcsContentAnnotationSettings">
40
+ <option name="myLimit" value="2678400000" />
41
+ </component>
42
+ <component name="XDebuggerManager">
43
+ <breakpoint-manager />
44
+ <watches-manager />
45
+ </component>
46
+ </project>
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elliptic_curve.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # EllipticCurve
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/elliptic_curve`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'elliptic_curve'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install elliptic_curve
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/elliptic_curve/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "elliptic_curve"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'elliptic_curve/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'elliptic_curve'
8
+ spec.version = EllipticCurve::VERSION
9
+ spec.authors = ['Linus Gasser']
10
+ spec.email = ['ineiti@linusetviviane.ch']
11
+
12
+ spec.summary = %q{Simple implementation of the basic functionalities for working with
13
+ elliptic curves. }
14
+ spec.description = %q{This gives the basic for representing elliptic curves of the form
15
+ y^2 = x^3 + ax + b, including adding and (p^-1 modulo q).}
16
+ spec.homepage = "http://github.com/ineiti/elliptic_curve"
17
+
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = 'bin'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.9'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ end
@@ -0,0 +1,3 @@
1
+ module EllipticCurve
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,87 @@
1
+ require "elliptic_curve/version"
2
+
3
+ # This is a simple implementation to play around with elliptic curves
4
+ # Most of it was taken from
5
+ # http://www.apprendre-en-ligne.net/crypto/menu/index.html
6
+
7
+ module EllipticCurve
8
+ # The base class which holds an elliptic curve
9
+ class EC
10
+ attr_reader :a, :b, :p
11
+
12
+ # y^2 = x^3 + ax + b
13
+ # p: modulo
14
+ def initialize(a, b, p)
15
+ @a, @b, @p = a, b, p
16
+ end
17
+
18
+ # gets the inverse modulo p
19
+ # Copied from http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Modular_integers
20
+ def get_inv_p(s)
21
+ s < 0 and return get_inv_p(s % @p)
22
+ t, newt = 0, 1
23
+ r, newr = @p, s
24
+ while newr != 0 do
25
+ quotient = (r / newr).floor
26
+ t, newt = newt, t - quotient * newt
27
+ r, newr = newr, r - quotient * newr
28
+ end
29
+ if r > 1 then
30
+ raise "#{s} is not invertible"
31
+ end
32
+ if t < 0 then
33
+ return t + @p
34
+ else
35
+ return t
36
+ end
37
+ end
38
+ end
39
+
40
+ # A point on an elliptic curve
41
+ class P
42
+ attr_reader :ec, :x, :y
43
+
44
+ # ec: the elliptic curve
45
+ # x, y: coordinates
46
+ # if x == Float::INFINITY, y is ignored
47
+ def initialize(ec, x, y)
48
+ @ec, @x, @y = ec, x, y
49
+ end
50
+
51
+ # returns whether x is Float::INFINITY, ignores y
52
+ def is_infinity?
53
+ @x == Float::INFINITY
54
+ end
55
+
56
+ # Adds p2 to self
57
+ def +(p2)
58
+ if p2.is_infinity?
59
+ return self
60
+ elsif is_infinity?
61
+ return p2
62
+ elsif @x == p2.x
63
+ if @y == -p2.y
64
+ return P.new(ec, Float::INFINITY, Float::INFINITY)
65
+ else
66
+ k = (3 * @x**2+@ec.a) * @ec.get_inv_p(2 * @y)
67
+ x = (k**2 - 2 * @x) % @ec.p
68
+ return P.new(@ec, x, (k*(@x-x)-@y) % @ec.p)
69
+ end
70
+ else
71
+ k = (p2.y-@y) * @ec.get_inv_p(p2.x-@x)
72
+ x = (k**2-@x-p2.x) % @ec.p
73
+ return P.new(@ec, x, (k * (@x-x)-@y) % @ec.p)
74
+ end
75
+ end
76
+
77
+ # Multiplies itself by d
78
+ def *(d)
79
+ ([self] * d).inject(:+)
80
+ end
81
+
82
+ # Compare with another point
83
+ def ==(o)
84
+ @x == o.x && @y == o.y
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elliptic_curve
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Linus Gasser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: |-
42
+ This gives the basic for representing elliptic curves of the form
43
+ y^2 = x^3 + ax + b, including adding and (p^-1 modulo q).
44
+ email:
45
+ - ineiti@linusetviviane.ch
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - ".idea/elliptic_curve.iml"
52
+ - ".idea/misc.xml"
53
+ - ".idea/modules.xml"
54
+ - ".idea/vcs.xml"
55
+ - ".idea/workspace.xml"
56
+ - Gemfile
57
+ - README.md
58
+ - Rakefile
59
+ - bin/console
60
+ - bin/setup
61
+ - elliptic_curve.gemspec
62
+ - lib/elliptic_curve.rb
63
+ - lib/elliptic_curve/version.rb
64
+ homepage: http://github.com/ineiti/elliptic_curve
65
+ licenses: []
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.2.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Simple implementation of the basic functionalities for working with elliptic
87
+ curves.
88
+ test_files: []