The Trunk: System-nice.365.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

The Trunk: System-nice.365.mcz

commits-2
Nicolas Cellier uploaded a new version of System to project The Trunk:
http://source.squeak.org/trunk/System-nice.365.mcz

==================== Summary ====================

Name: System-nice.365
Author: nice
Time: 28 August 2010, 11:04:20.183 pm
UUID: 1bf5db4f-8a26-4a8b-b5e8-1341c113f4db
Ancestors: System-nice.364

Boost non primitive version of SecureHashAlgorithm by about 33% with these two simple things:
1) replace ThirtyTwoBitRegister>>load: postCondition with a cheaper preCondition
2) provide two instance creation message to avoid loading ThirtyTwoBitRegister value twice
Last, remove a useless ThirtyTwoBitRegister copy, though it does not make much difference.

=============== Diff against System-nice.364 ===============

Item was changed:
  ----- Method: SecureHashAlgorithm>>expandedBlock: (in category 'private') -----
  expandedBlock: aByteArray
  "Convert the given 64 byte buffer into 80 32-bit registers and answer the result."
  | out src v |
  out := Array new: 80.
  src := 1.
  1 to: 16 do: [:i |
+ out at: i put: (ThirtyTwoBitRegister fromByteArray: aByteArray at: src).
- out at: i put: (ThirtyTwoBitRegister new loadFrom: aByteArray at: src).
  src := src + 4].
 
  17 to: 80 do: [:i |
  v := (out at: i - 3) copy.
  v bitXor: (out at: i - 8);
  bitXor: (out at: i - 14);
  bitXor: (out at: i - 16);
  leftRotateBy: 1.
  out at: i put: v].
  ^ out
  !

Item was changed:
  ----- Method: SecureHashAlgorithm>>initializeTotals (in category 'private') -----
  initializeTotals
  "Initialize totalA through totalE to their seed values."
 
  "total registers for use when primitives are absent"
+ totalA := ThirtyTwoBitRegister fromInteger: 16r67452301.
+ totalB := ThirtyTwoBitRegister fromInteger: 16rEFCDAB89.
+ totalC := ThirtyTwoBitRegister fromInteger: 16r98BADCFE.
+ totalD := ThirtyTwoBitRegister fromInteger: 16r10325476.
+ totalE := ThirtyTwoBitRegister fromInteger: 16rC3D2E1F0.
- totalA := ThirtyTwoBitRegister new load: 16r67452301.
- totalB := ThirtyTwoBitRegister new load: 16rEFCDAB89.
- totalC := ThirtyTwoBitRegister new load: 16r98BADCFE.
- totalD := ThirtyTwoBitRegister new load: 16r10325476.
- totalE := ThirtyTwoBitRegister new load: 16rC3D2E1F0.
  self initializeTotalsArray.
  !

Item was changed:
  ----- Method: SecureHashAlgorithm class>>initialize (in category 'class initialization') -----
  initialize
  "SecureHashAlgorithm initialize"
  "For the curious, here's where these constants come from:
   #(2 3 5 10) collect: [:x | ((x sqrt / 4.0) * (2.0 raisedTo: 32)) truncated hex]"
 
+ K1 := ThirtyTwoBitRegister fromInteger: 16r5A827999.
+ K2 := ThirtyTwoBitRegister fromInteger: 16r6ED9EBA1.
+ K3 := ThirtyTwoBitRegister fromInteger: 16r8F1BBCDC.
+ K4 := ThirtyTwoBitRegister fromInteger: 16rCA62C1D6.
- K1 := ThirtyTwoBitRegister new load: 16r5A827999.
- K2 := ThirtyTwoBitRegister new load: 16r6ED9EBA1.
- K3 := ThirtyTwoBitRegister new load: 16r8F1BBCDC.
- K4 := ThirtyTwoBitRegister new load: 16rCA62C1D6.
  !

Item was changed:
  ----- Method: ThirtyTwoBitRegister>>load: (in category 'accessing') -----
  load: anInteger
  "Set my contents to the value of given integer."
 
+ (anInteger positive and: [anInteger digitLength <= 4])
- low := anInteger bitAnd: 16rFFFF.
- hi := (anInteger bitShift: -16) bitAnd: 16rFFFF.
- self asInteger = anInteger
  ifFalse: [self error: 'out of range: ', anInteger printString].
+ low := anInteger bitAnd: 16rFFFF.
+ hi := (anInteger bitShift: -16) bitAnd: 16rFFFF
  !

Item was added:
+ ----- Method: ThirtyTwoBitRegister class>>fromInteger: (in category 'instance creation') -----
+ fromInteger: aPositiveInteger
+ "Answer a new instance whose initial contents is copied from aPositiveInteger.
+ It is required that aPositiveInteger has no more than 32 bits."
+
+ ^ self basicNew load: aPositiveInteger
+ !

Item was changed:
  ----- Method: SecureHashAlgorithm>>processBuffer: (in category 'private') -----
  processBuffer: aByteArray
  "Process given 64-byte buffer, accumulating the results in totalA through totalE."
 
  | a b c d e w tmp |
  self primHasSecureHashPrimitive
  ifTrue: [^ self processBufferUsingPrimitives: aByteArray]
  ifFalse: [totals := nil].
 
  "initialize registers a through e from the current totals"
  a := totalA copy.
  b := totalB copy.
  c := totalC copy.
  d := totalD copy.
  e := totalE copy.
 
  "expand and process the buffer"
  w := self expandedBlock: aByteArray.
  1 to: 80 do: [:i |
  tmp := (a copy leftRotateBy: 5)
  += (self hashFunction: i of: b with: c with: d);
  += e;
  += (w at: i);
  += (self constantForStep: i).
  e := d.
  d := c.
+ c := b leftRotateBy: 30.
- c := b copy leftRotateBy: 30.
  b := a.
  a := tmp].
 
  "add a through e into total accumulators"
  totalA += a.
  totalB += b.
  totalC += c.
  totalD += d.
  totalE += e.
  !

Item was added:
+ ----- Method: ThirtyTwoBitRegister class>>fromByteArray:at: (in category 'instance creation') -----
+ fromByteArray: aByteArray at: startIndex
+ "Answer a new instance whose initial contents is copied from next four bytes from aByteArray starting at startIndex..
+ Convention is Most Significant Byte first (aka big endian)."
+
+ ^ self basicNew loadFrom: aByteArray at: startIndex
+ !

Item was changed:
  ----- Method: SecureHashAlgorithm>>hashInteger:seed: (in category 'public') -----
  hashInteger: aPositiveInteger seed: seedInteger
  "Hash the given positive integer. The integer to be hashed should have 512 or fewer bits. This entry point is used in the production of random numbers"
 
  | buffer dstIndex |
  "Initialize totalA through totalE to their seed values."
+ totalA := ThirtyTwoBitRegister
+ fromInteger: ((seedInteger bitShift: -128) bitAnd: 16rFFFFFFFF).
+ totalB := ThirtyTwoBitRegister
+ fromInteger: ((seedInteger bitShift: -96) bitAnd: 16rFFFFFFFF).
+ totalC := ThirtyTwoBitRegister
+ fromInteger: ((seedInteger bitShift: -64) bitAnd: 16rFFFFFFFF).
+ totalD := ThirtyTwoBitRegister
+ fromInteger: ((seedInteger bitShift: -32) bitAnd: 16rFFFFFFFF).
+ totalE := ThirtyTwoBitRegister
+ fromInteger: (seedInteger bitAnd: 16rFFFFFFFF).
- totalA := ThirtyTwoBitRegister new
- load: ((seedInteger bitShift: -128) bitAnd: 16rFFFFFFFF).
- totalB := ThirtyTwoBitRegister new
- load: ((seedInteger bitShift: -96) bitAnd: 16rFFFFFFFF).
- totalC := ThirtyTwoBitRegister new
- load: ((seedInteger bitShift: -64) bitAnd: 16rFFFFFFFF).
- totalD := ThirtyTwoBitRegister new
- load: ((seedInteger bitShift: -32) bitAnd: 16rFFFFFFFF).
- totalE := ThirtyTwoBitRegister new
- load: (seedInteger bitAnd: 16rFFFFFFFF).
  self initializeTotalsArray.
 
  "pad integer with zeros"
  buffer := ByteArray new: 64.
  dstIndex := 0.
  aPositiveInteger digitLength to: 1 by: -1 do: [:i |
  buffer at: (dstIndex := dstIndex + 1) put: (aPositiveInteger digitAt: i)].
 
  "process that one block"
  self processBuffer: buffer.
 
  ^ self finalHash
  !


Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: System-nice.365.mcz

Nicolas Cellier
2010/8/28  <[hidden email]>:

> Nicolas Cellier uploaded a new version of System to project The Trunk:
> http://source.squeak.org/trunk/System-nice.365.mcz
>
> ==================== Summary ====================
>
> Name: System-nice.365
> Author: nice
> Time: 28 August 2010, 11:04:20.183 pm
> UUID: 1bf5db4f-8a26-4a8b-b5e8-1341c113f4db
> Ancestors: System-nice.364
>
> Boost non primitive version of SecureHashAlgorithm by about 33% with these two simple things:

Hem, no, boost primitive version by 33%.
Non primitive has only 5% gain because load: time is masked by
expensive ThirtyTwoBitRegister operations

Nicolas

> 1) replace ThirtyTwoBitRegister>>load: postCondition with a cheaper preCondition
> 2) provide two instance creation message to avoid loading ThirtyTwoBitRegister value twice
> Last, remove a useless ThirtyTwoBitRegister copy, though it does not make much difference.
>