sleeping_kangaroo12 0.0.4 → 0.0.6

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: 6b89369bac3beb3d85045ea6f94f2c0d995d4e72e04a1887f83818b308e4bae7
4
- data.tar.gz: 464479f5b0d85d7d6c5453e208fc287b5637f1fcc99769101aeee75e9dd2ec58
3
+ metadata.gz: 43f29b860cf5a2c6dc1fac237ebbc490a309277f7e61fcaa078a2f0c097b6a26
4
+ data.tar.gz: 96f53173416460a9a64decd3312a5c613bf3875a68bec5bb43d8cfdb834c4517
5
5
  SHA512:
6
- metadata.gz: 7514b62f2f38a373d055f71dd6acce85ab37554403bc3fb294e2b516e768133120134469d29d81b69be36f6804dd002439ce49e9208bf6831d7e99d359bee21a
7
- data.tar.gz: 4a5dea0068e28db9180e65c3469863f9c6ada04948866999efe788c1ef941d7eebfdb6d1a18a816175b6dbd85d653d8f83bdd6613514c70e3c907245ee75911e
6
+ metadata.gz: 7c49649a2576b01241565d83c77e7738eec932239cda39a59cadefffaf3820d996487db856d4c7bc63c00f1b698b832bc871c8627483754bf8ee9a216df113df
7
+ data.tar.gz: 67cd342201f52cd5c94a4a35d2df005b942d3e1ca2ec5a0eb9b56e36644b996842ce4788635507460cf3f9e3dd92761d6e269224862875f9caf4d23220ce9ed7
data/.yardopts ADDED
@@ -0,0 +1,4 @@
1
+ lib/**/digest.rb
2
+ -
3
+ README.md
4
+ LICENSE.md
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # SleepingKangaroo12
2
2
 
3
+ [![GitHub version](https://badge.fury.io/gh/the-cave%2Fsleeping-kangaroo12.svg)](https://badge.fury.io/gh/the-cave%2Fsleeping-kangaroo12)
4
+ [![Gem Version](https://badge.fury.io/rb/sleeping_kangaroo12.svg)](https://badge.fury.io/rb/sleeping_kangaroo12)
5
+
3
6
  ## What is it?
4
7
 
5
8
  SleepingKangaroo12 is a Ruby binding of [KangarooTwelve](https://keccak.team/kangarootwelve.html), a fast cryptographic
@@ -42,6 +45,12 @@ In order to install the gem, your machine should be ready to build the K12 packa
42
45
  - xsltproc executable, normally comes with libxslt package
43
46
  - Ruby related stuffs
44
47
 
48
+ ### TL;DR for Ubuntu-liked OS
49
+
50
+ ~~~bash
51
+ sudo apt install build-essential xsltproc
52
+ ~~~
53
+
45
54
  ## Installation
46
55
 
47
56
  Add this line to your application's Gemfile:
@@ -50,7 +59,7 @@ Add this line to your application's Gemfile:
50
59
  gem 'sleeping_kangaroo12'
51
60
  ~~~
52
61
 
53
- And then execute:
62
+ Check the [prerequisites](#prerequisites); and then execute:
54
63
 
55
64
  $ bundle install
56
65
 
@@ -47,7 +47,7 @@ static void KangarooTwelve_F_Absorb(KangarooTwelve_F *instance, const unsigned c
47
47
  i = 0;
48
48
  curData = data;
49
49
  while(i < dataByteLen) {
50
- if ((instance->byteIOIndex == 0) && (dataByteLen >= (i + rateInBytes))) {
50
+ if ((instance->byteIOIndex == 0) && (dataByteLen-i >= rateInBytes)) {
51
51
  #ifdef KeccakP1600_12rounds_FastLoop_supported
52
52
  /* processing full blocks first */
53
53
  j = KeccakP1600_12rounds_FastLoop_Absorb(instance->state, K12_rateInLanes, curData, dataByteLen - i);
@@ -62,7 +62,7 @@ static void KangarooTwelve_F_Absorb(KangarooTwelve_F *instance, const unsigned c
62
62
  i = dataByteLen - j;
63
63
  } else {
64
64
  /* normal lane: using the message queue */
65
- if ((dataByteLen - i) + instance->byteIOIndex > (size_t)rateInBytes) {
65
+ if (dataByteLen - i > (size_t)rateInBytes - instance->byteIOIndex) {
66
66
  partialBlock = rateInBytes-instance->byteIOIndex;
67
67
  } else {
68
68
  partialBlock = (uint8_t)(dataByteLen - i);
@@ -112,7 +112,7 @@ static void KangarooTwelve_F_Squeeze(KangarooTwelve_F *instance, unsigned char *
112
112
  i = 0;
113
113
  curData = data;
114
114
  while(i < dataByteLen) {
115
- if ((instance->byteIOIndex == rateInBytes) && (dataByteLen >= (i + rateInBytes))) {
115
+ if ((instance->byteIOIndex == rateInBytes) && (dataByteLen-i >= rateInBytes)) {
116
116
  for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) {
117
117
  KeccakP1600_Permute_12rounds(instance->state);
118
118
  KeccakP1600_ExtractBytes(instance->state, curData, 0, rateInBytes);
@@ -125,9 +125,10 @@ static void KangarooTwelve_F_Squeeze(KangarooTwelve_F *instance, unsigned char *
125
125
  KeccakP1600_Permute_12rounds(instance->state);
126
126
  instance->byteIOIndex = 0;
127
127
  }
128
- partialBlock = (unsigned int)(dataByteLen - i);
129
- if (partialBlock+instance->byteIOIndex > rateInBytes)
128
+ if (dataByteLen-i > rateInBytes-instance->byteIOIndex)
130
129
  partialBlock = rateInBytes-instance->byteIOIndex;
130
+ else
131
+ partialBlock = (unsigned int)(dataByteLen - i);
131
132
  i += partialBlock;
132
133
 
133
134
  KeccakP1600_ExtractBytes(instance->state, curData, instance->byteIOIndex, partialBlock);
@@ -33,8 +33,8 @@ Please refer to the XKCP for more details.
33
33
  #define ROL64in256(d, a, o) d = _mm256_or_si256(_mm256_slli_epi64(a, o), _mm256_srli_epi64(a, 64-(o)))
34
34
  #define ROL64in256_8(d, a) d = _mm256_shuffle_epi8(a, CONST256(rho8))
35
35
  #define ROL64in256_56(d, a) d = _mm256_shuffle_epi8(a, CONST256(rho56))
36
- static const uint64_t rho8[4] ALIGN(32) = {0x0605040302010007, 0x0E0D0C0B0A09080F, 0x1615141312111017, 0x1E1D1C1B1A19181F};
37
- static const uint64_t rho56[4] ALIGN(32) = {0x0007060504030201, 0x080F0E0D0C0B0A09, 0x1017161514131211, 0x181F1E1D1C1B1A19};
36
+ static ALIGN(AVX2alignment) const uint64_t rho8[4] = {0x0605040302010007, 0x0E0D0C0B0A09080F, 0x1615141312111017, 0x1E1D1C1B1A19181F};
37
+ static ALIGN(AVX2alignment) const uint64_t rho56[4] = {0x0007060504030201, 0x080F0E0D0C0B0A09, 0x1017161514131211, 0x181F1E1D1C1B1A19};
38
38
  #define STORE256(a, b) _mm256_store_si256((__m256i *)&(a), b)
39
39
  #define STORE256u(a, b) _mm256_storeu_si256((__m256i *)&(a), b)
40
40
  #define XOR256(a, b) _mm256_xor_si256(a, b)
@@ -52,33 +52,37 @@ http://creativecommons.org/publicdomain/zero/1.0/
52
52
  </xsl:template>
53
53
 
54
54
  <xsl:template match="gcc">
55
- <!-- What follows is a shameless hack to avoid -march=native on aarch64 with clang -->
56
- <xsl:if test=".= '-march=native'">
57
- <xsl:text>ifneq ($(UNAME_M)$(findstring clang,$(CC)),aarch64clang)
55
+ <!-- What follows is a shameless hack to avoid -march/-mtune=native on arm64/aarch64 with clang -->
56
+ <xsl:if test=".= '-march=native' or .= '-mtune=native'">
57
+ <xsl:text>ifneq ($(UNAME_M),aarch64)
58
+ ifneq ($(UNAME_S),Darwin)
58
59
  </xsl:text>
59
60
  </xsl:if>
60
61
  <xsl:text>CFLAGS := $(CFLAGS) </xsl:text>
61
62
  <xsl:value-of select="."/>
62
63
  <xsl:text>
63
64
  </xsl:text>
64
- <xsl:if test=".= '-march=native'">
65
+ <xsl:if test=".= '-march=native' or .= '-mtune=native'">
65
66
  <xsl:text>endif
67
+ endif
66
68
  </xsl:text>
67
69
  </xsl:if>
68
70
  </xsl:template>
69
71
 
70
72
  <xsl:template match="gas">
71
- <!-- What follows is a shameless hack to avoid -march=native on aarch64 with clang -->
72
- <xsl:if test=".= '-march=native'">
73
- <xsl:text>ifneq ($(UNAME_M)$(findstring clang,$(CC)),aarch64clang)
73
+ <!-- What follows is a shameless hack to avoid -march/-mtune=native on arm64/aarch64 with clang -->
74
+ <xsl:if test=".= '-march=native' or .= '-mtune=native'">
75
+ <xsl:text>ifneq ($(UNAME_M),aarch64)
76
+ ifneq ($(UNAME_S),Darwin)
74
77
  </xsl:text>
75
78
  </xsl:if>
76
79
  <xsl:text>ASMFLAGS := $(ASMFLAGS) </xsl:text>
77
80
  <xsl:value-of select="."/>
78
81
  <xsl:text>
79
82
  </xsl:text>
80
- <xsl:if test=".= '-march=native'">
83
+ <xsl:if test=".= '-march=native' or .= '-mtune=native'">
81
84
  <xsl:text>endif
85
+ endif
82
86
  </xsl:text>
83
87
  </xsl:if>
84
88
  </xsl:template>
@@ -200,9 +204,11 @@ ifeq ($(UNAME_S),Linux)
200
204
  ASMFLAGS :=
201
205
  endif
202
206
  ifeq ($(UNAME_S),Darwin)
203
- ASMFLAGS := -x assembler-with-cpp -Wa,-defsym,macOS=1
207
+ ASMFLAGS := -x assembler-with-cpp -Wa,-defsym,old_gas_syntax=1 -Wa,-defsym,no_plt=1
208
+ endif
209
+ ifneq (,$(findstring mingw32,$(CC)))
210
+ ASMFLAGS := -x assembler-with-cpp -Wa,-defsym,old_gas_syntax=1 -Wa,-defsym,no_plt=1
204
211
  endif
205
-
206
212
  UNAME_M := $(shell uname -m)
207
213
 
208
214
  </xsl:text>
@@ -7,7 +7,6 @@ module SleepingKangaroo12
7
7
  module Build
8
8
  # mostly taken from:
9
9
  # https://github.com/ffi/ffi-compiler/blob/master/lib/ffi-compiler/platform.rb
10
-
11
10
  class Platform
12
11
  include ::Singleton
13
12
 
@@ -6,16 +6,30 @@ require 'objspace'
6
6
  require_relative 'binding'
7
7
 
8
8
  module SleepingKangaroo12
9
+ # @example basic usage
10
+ # digest = ::SleepingKangaroo12::Digest.new(output_length: 10)
11
+ # digest << 'some input'
12
+ # digest << 'some more input'
13
+ # digest.hexdigest
14
+ # #=> "cbea8144fbbf6150ceaf"
15
+ # See {file:README.md README} for more usage examples
9
16
  class Digest
17
+ module Error
18
+ end
19
+
10
20
  class UpdatingFailed < ::StandardError
21
+ include Error
11
22
  end
12
23
 
13
24
  class FinalizationFailed < ::StandardError
25
+ include Error
14
26
  end
15
27
 
16
28
  class Finalized < ::StandardError
29
+ include Error
17
30
  end
18
31
 
32
+ # Create a new Digest
19
33
  def initialize(output_length: 32, key: nil)
20
34
  raise ::TypeError, 'Hash length is not an Integer' unless output_length.is_a?(::Integer)
21
35
  raise ::ArgumentError, 'Hash length out of range' unless (1...(1 << 20)).include?(output_length)
@@ -31,6 +45,7 @@ module SleepingKangaroo12
31
45
  @result = nil
32
46
  end
33
47
 
48
+ # Feed in the data
34
49
  def update(data)
35
50
  raise Finalized if @finalized
36
51
  data_size = data.bytesize
@@ -42,10 +57,12 @@ module SleepingKangaroo12
42
57
  self
43
58
  end
44
59
 
60
+ # Alias for {#update}
45
61
  def <<(*args, **kwargs)
46
62
  update(*args, **kwargs)
47
63
  end
48
64
 
65
+ # Finalize and output a binary hash
49
66
  def digest
50
67
  @finalized = true
51
68
  return @_digest if @_digest
@@ -65,30 +82,45 @@ module SleepingKangaroo12
65
82
  @_digest = data_buffer.get_bytes(0, @output_length)
66
83
  end
67
84
 
85
+ # Finalize and output a hexadecimal-encoded hash
68
86
  def hexdigest
69
87
  @_hexdigest ||= digest.unpack1('H*')
70
88
  end
71
89
 
90
+ # Finalize and output a Base64-encoded hash
72
91
  def base64digest
73
92
  @_base64digest ||= ::Base64.strict_encode64(digest)
74
93
  end
75
94
 
76
95
  class << self
96
+ # @!visibility private
77
97
  # https://www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/
78
98
  def _create_finalizer(instance)
79
- proc {
99
+ proc do
80
100
  Binding.destroy(instance)
81
- }
101
+ end
82
102
  end
83
103
 
104
+ # Shortcut to calculate a raw digest
105
+ # @example basic usage
106
+ # ::SleepingKangaroo12::Digest.digest('some input')
107
+ # #=> "m\x9FJ\xDA\xE9\x96\xD1X\xC5K\xE83e(x\x8C\xD3o\xFBh\xB2\x17W ,\xD5\xED!\xE4D\xAF\xDD"
108
+ # @example with key (AKA: customization)
109
+ # ::SleepingKangaroo12::Digest.digest('some input', key: 'secret')
110
+ # #=> "\x96\xE2K\xC4\xCF\xFFGF\xE1\x05\xB9\xF6f\xF0-\xF8\x1F\a\n\xFC\xD7\xC9\x91\n\xFC\xFB\xA6hOx\x99<"
111
+ # @example controlled output length
112
+ # ::SleepingKangaroo12::Digest.digest('some input', output_length: 5)
113
+ # #=> "m\x9FJ\xDA\xE9"
84
114
  def digest(*args, **kwargs)
85
115
  _generic_digest(*args, **kwargs, &:digest)
86
116
  end
87
117
 
118
+ # Same as {.digest} but encode the output in hexadecimal format
88
119
  def hexdigest(*args, **kwargs)
89
120
  _generic_digest(*args, **kwargs, &:hexdigest)
90
121
  end
91
122
 
123
+ # Same as {.digest} but encode the output in Base64 format
92
124
  def base64digest(*args, **kwargs)
93
125
  _generic_digest(*args, **kwargs, &:base64digest)
94
126
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SleepingKangaroo12
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.6'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sleeping_kangaroo12
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sarun Rattanasiri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-18 00:00:00.000000000 Z
11
+ date: 2022-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -16,52 +16,53 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.15.5
19
+ version: 1.15.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.15.5
26
+ version: 1.15.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: posix-spawn
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.3.15
33
+ version: 0.3.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.3.15
40
+ version: 0.3.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 13.0.6
47
+ version: 13.0.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 13.0.6
55
- description: This gem brought the hash algorithm, KangarooTwelve, to Ruby. It uses
56
- the official library, K12, maintained by the Keccak team themselves. The implementation
57
- is highly optimized on popular hardware, including AVX512, AVX2, SSSE3 instruction
58
- sets.
54
+ version: 13.0.0
55
+ description: |-
56
+ KangarooTwelve binding for Ruby
57
+ The gem build on top of the official library, K12, maintained by the Keccak team themselves.
58
+ The implementation is highly optimized and supporting AVX512, AVX2, SSSE3 instruction sets.
59
59
  email: midnight_w@gmx.tw
60
60
  executables: []
61
61
  extensions:
62
62
  - ext/Rakefile
63
63
  extra_rdoc_files: []
64
64
  files:
65
+ - ".yardopts"
65
66
  - LICENSE.md
66
67
  - README.md
67
68
  - ext/Rakefile
@@ -107,7 +108,9 @@ licenses:
107
108
  - BSD-3-Clause
108
109
  metadata:
109
110
  homepage_uri: https://github.com/the-cave/sleeping-kangaroo12
110
- source_code_uri: https://github.com/the-cave/sleeping-kangaroo12/tree/v0.0.4
111
+ source_code_uri: https://github.com/the-cave/sleeping-kangaroo12/tree/v0.0.6
112
+ documentation_uri: https://rubydoc.info/gems/sleeping_kangaroo12/0.0.6
113
+ bug_tracker_uri: https://github.com/the-cave/sleeping-kangaroo12/issues
111
114
  post_install_message:
112
115
  rdoc_options: []
113
116
  require_paths:
@@ -123,8 +126,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
126
  - !ruby/object:Gem::Version
124
127
  version: '0'
125
128
  requirements: []
126
- rubygems_version: 3.2.32
129
+ rubygems_version: 3.3.7
127
130
  signing_key:
128
131
  specification_version: 4
129
- summary: A binding of the KangarooTwelve hash algorithm for Ruby
132
+ summary: KangarooTwelve, the hash algorithm, native binding for Ruby
130
133
  test_files: []