Skip to content

Commit

Permalink
ext: more strict c compiler
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Kotov <[email protected]>
  • Loading branch information
q9f and kotovalexarian committed Oct 2, 2021
1 parent ef9da53 commit 42ecf94
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 34 deletions.
43 changes: 39 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
*.DS_Store

/test/test_vectors.rb
/ext/digest/Makefile
/ext/digest/keccak.so
/ext/digest/mkmf.log

*.o
*.bundle
Makefile
test/test_vectors.rb
*.so
*.gem
*.log
*.so
*.rbc
/.config
/.rake_tasks~
/coverage/
/InstalledFiles
/pkg/
/tmp/

# RSpec configuration and generated files:
/.rspec
/spec/examples.txt

# Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

# Environment normalization:
/.bundle/
/vendor/bundle/*
!/vendor/bundle/.keep
/lib/bundler/man/

# For a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
/Gemfile.lock
/.ruby-version
/.ruby-gemset

# Unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
3 changes: 3 additions & 0 deletions COPYRIGHT
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ https://github.com/teamhedge/digest-sha3-ruby
Copyright (c) 2018-2021 Seb's; licensed under MIT License
https://github.com/sydneyitguy/digest-sha3-ruby

Copyright (c) 2019-2021 Alex Kotov; licensed under MIT License
https://github.com/kotovalexarian/digest-keccak

Copyright (c) 2021-2022 Afri Schoedon; re-licensed under Apache 2.0
https://github.com/q9f/digest-sha3-ruby
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in keccak.gemspec
gemspec
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ This Ruby extension exposes the [Keccak](http://keccak.noekeon.org/) (SHA-3) dig

## Installation

The gem is called `keccak`.

```bash
bundle add keccak
gem install keccak
```

**Note**: as of version `v1.1.0`, `digest-sha3` requires Ruby 2.2. `keccak`version `v1.2.0` now also supports Ruby 3.0.
```ruby
gem 'keccak', '~> 1.2'
```

The last version that worked on older Ruby (1.x) versions was `v1.0.2`. It can be found at the no longer maintained [`digest-sha3` repository from 2015](https://github.com/phusion/digest-sha3-ruby/releases/tag/release-1.0.2).
**Note**: as of version `v1.1.0`, `digest-sha3` (historic name) requires Ruby 2.2. The new `keccak` version `v1.2.0` now also supports Ruby 3.0. The last version that worked on older Ruby (1.x) versions was `v1.0.2`. It can be found at the no longer maintained [`digest-sha3` repository from 2015](https://github.com/phusion/digest-sha3-ruby/releases/tag/release-1.0.2).

## Usage

Keccak supports five hash lengths: 224-bit, 256-bit, 384-bit, 512-bit and variable length. Variable length is not supported by this Ruby extension. Unless the user specifies otherwise, this Ruby extension assumes 512-bit.
This gem extends the `digest/*` module by a `digest/sha3` class (historic reference, see history section below).

```ruby
require 'digest/sha3'
Expand All @@ -45,26 +50,25 @@ digest.hexdigest # => "1597842a..."
digest = Digest::SHA3.new(224)
```

Keccak supports five hash lengths: 224-bit, 256-bit, 384-bit, 512-bit and variable length. Variable length is not supported by this Ruby extension. Unless the user specifies otherwise, this Ruby extension assumes 512-bit.

## Running the test suite

Run the test suite as follows:

```bash
gem install test-unit
bundle install
make test
```

A part of the test suite is automatically generated from Keccak's reference test suite.

## Warning: Keccak vs. SHA-3

**Note:** This gem still uses the `Digest::SHA3` namespace for reasons of backwards compatibility and long-term maintainability. See history section below.

:warning: This gem does **not** implement the final FIPS202 standard, today known as SHA-3 but rather an early version, commonly referred to as Keccak. The reason why this is kept around, is that Ethereum uses this earler version of Keccak. See also: [Ethereum: Difference between keccak256 and sha3](https://ethereum.stackexchange.com/questions/30369/difference-between-keccak256-and-sha3)

If you are looking for the final SHA-3 gem, please use the following: https://rubygems.org/gems/sha3


## History

This gem was initially developed and published as `digest-sha3`: https://github.com/phusion/digest-sha3-ruby
Expand All @@ -74,3 +78,4 @@ This gem was later patched multiple times:
* https://github.com/teamhedge/digest-sha3-ruby (KECCAK, as `digest-sha3-patched`)
* https://github.com/sydneyitguy/digest-sha3-ruby (KECCAK, as `digest-sha3-patched-ruby-3`)
* https://github.com/steakknife/digest-sha3-ruby (actual SHA-3, do not use for Ethereum)
* https://github.com/kotovalexarian/digest-keccak/ (KECCAK, as `digest-keccak`)
32 changes: 28 additions & 4 deletions ext/digest/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'mkmf'

have_header('ruby/digest.h')
have_func('rb_str_set_len')
def cflags(*args)
args.each do |str|
$CFLAGS += " #{str.shellescape} "
end
end

def have_header!(*args)
exit 1 unless have_header(*args)
end

def have_func!(header, *args)
exit 1 unless have_func(*args, header)
end

cflags '-std=c11'
cflags '-Wall'
cflags '-Wextra'
cflags '-fvisibility=hidden'

have_header! 'ruby/digest.h'
have_header! 'stdio.h'
have_header! 'string.h'

have_func! 'rb_str_set_len'

$CFLAGS << " -fvisibility=hidden"
create_makefile('digest/sha3')
create_makefile 'digest/sha3' or exit 1
6 changes: 3 additions & 3 deletions ext/digest/sha3.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#define MAX_DIGEST_SIZE 64
#define DEFAULT_DIGEST_LEN 512

static int sha3_init_func(hashState *ctx);
static int sha3_init_func();
static void sha3_update_func(hashState *ctx, unsigned char *str, size_t len);
static int sha3_finish_func(hashState *ctx, unsigned char *digest);

Expand All @@ -33,8 +33,8 @@ static rb_digest_metadata_t sha3 = {
we override initialize to do custom hash size, so we don't care too much here.
*/
static int
sha3_init_func(hashState *ctx) {
// Just return a 1 ' successful' we override the init function
sha3_init_func() {
// Just return a 1 'successful' we override the init function
// so this is not necessary
// the base class alloc calls this to initialize the algorithm
return 1;
Expand Down
41 changes: 28 additions & 13 deletions keccak.gemspec
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
require File.expand_path('lib/digest/sha3/version')
# frozen_string_literal: true

Gem::Specification.new do |s|
s.name = "keccak"
s.version = Digest::SHA3::Version::STRING
s.summary = "The Keccak (SHA-3) hash used by Ethereum."
s.email = "[email protected]"
s.homepage = "https://github.com/q9f/keccak.rb"
s.description = "The Keccak (SHA-3) hash use by Ethereum. This does not implement the final FIPS202 standard, today known as SHA3 but rather an early version, commonly referred to as Keccak."
s.authors = ["Afri Schoedon", "Chris Metcalfe", "Hongli Lai (Phusion)", "Keccak authors"]
s.extensions << "ext/digest/extconf.rb"
s.required_ruby_version = ">= 2.2"
s.license = "Apache-2.0"
lib = File.expand_path('lib', __dir__).freeze
$LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib

s.files = Dir[
require 'digest/sha3'

Gem::Specification.new do |spec|
spec.name = "keccak"
spec.version = Digest::SHA3::VERSION
spec.summary = "The Keccak (SHA-3) hash used by Ethereum."
spec.description = "The Keccak (SHA-3) hash use by Ethereum. This does not implement the final FIPS202 standard, today known as SHA3 but rather an early version, commonly referred to as Keccak."
spec.homepage = "https://github.com/q9f/keccak.rb"
spec.authors = ["Afri Schoedon", "Alex Kotov", "Chris Metcalfe", "Hongli Lai (Phusion)", "Keccak authors"]
spec.email = "%w[[email protected]]"
spec.extensions << "ext/digest/extconf.rb"
spec.platform = Gem::Platform::RUBY
spec.required_ruby_version = ">= 2.2", "< 4.0"
spec.license = "Apache-2.0"
spec.metadata = {
'homepage_uri' => 'https://github.com/q9f/keccak.rb',
'source_code_uri' => 'https://github.com/q9f/keccak.rb',
'bug_tracker_uri' =>
'https://github.com/q9f/keccak.rb/issues',
}.freeze
spec.require_paths = ['lib']
spec.files = Dir[
"README.md",
"COPYRIGHT",
"LICENSE",
Expand All @@ -21,4 +33,7 @@ Gem::Specification.new do |s|
"ext/**/*.{c,h,rb}",
"lib/**/*"
]
spec.test_files = spec.files.grep %r{^(test|spec|features)/}
spec.add_development_dependency 'bundler', '~> 2.2'
spec.add_development_dependency 'test-unit', '~> 3.4'
end
6 changes: 3 additions & 3 deletions lib/digest/sha3/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Digest
class SHA3
module Version
STRING = "1.2.0"
end
VERSION = '1.2.1'
end
end
1 change: 1 addition & 0 deletions test/generate_tests.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# This will generate a test suite.
# Based on python-sha3's test suite.
Expand Down
3 changes: 3 additions & 0 deletions test/test_all.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

$LOAD_PATH.unshift(File.expand_path("lib"))
$LOAD_PATH.unshift(File.expand_path("ext"))
require 'digest/sha3'
require File.expand_path('test/test_usage')
require File.expand_path('test/test_vectors')
require File.expand_path('test/test_new')
21 changes: 21 additions & 0 deletions test/test_new.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require 'test/unit'

class SHA3NewTests < Test::Unit::TestCase
def test_singleton_method_hexdigest_256_empty
result = Digest::SHA3.hexdigest '', 256
assert_instance_of String, result
assert_equal 'c5d2460186f7233c927e7db2dcc703c0' \
'e500b653ca82273b7bfad8045d85a470',
result
end

def test_singleton_method_hexdigest_256_sample
result = Digest::SHA3.hexdigest 'sample', 256
assert_instance_of String, result
assert_equal 'b80204f7e9243e4fca5489740ccd31dc' \
'd0a54619a7f4165cee73c191ef7271a1',
result
end
end
2 changes: 2 additions & 0 deletions test/test_usage.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'test/unit'

class SHA3UsageTest < Test::Unit::TestCase
Expand Down

0 comments on commit 42ecf94

Please sign in to comment.