[squeak-dev] DigitalSignatureAlgorithm cosmetic refactoring

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

[squeak-dev] DigitalSignatureAlgorithm cosmetic refactoring

Nicolas Cellier-3
Reading these classes, i see some possible cleaning:

ThirtyTwoBitRegister>>leftRotateBy: bits
[...]
        bitCount _ bits \\ 32.
        bitCount < 0 ifTrue: [bitCount _ bitCount + 32].

This second line is useless, bits \\ 32 will be positive
(not the case of (bits rem: 32) if bits negative).

--------------------------------------------------

This algorithm could avoid duplicating lowBit:

DigitalSignatureAlgorithm>>logOfLargestPowerOfTwoDividing:
aStrictlyPositiveInteger
        ^aStrictlyPositiveInteger lowBit - 1

--------------------------------------------------

DigitalSignatureAlgorithm>>inverseOf: x mod: n
[...]
        v _ x.
        u _ n.
        k _ 0.
        [x even and: [n even and: [u > 0]]] whileTrue: [  "eliminate common
factors of two"
                k _ k + 1.
                u _ u bitShift: -1.
                v _ v bitShift: -1].

This has not much sense.
Should be [u even and: [v even]] whileTrue: [...]

Or even better:
        k := u lowBit min: v lowBit.
        u := u >> k.
        v := v >> k.

Or even better:
        (u even and: [v even]) ifTrue: [self error: 'no inverse']
because a pair of integers {a. b} satisying
        (2 * u) * a + ((2 * v) * b) = 1
will be hard to find...

(Algorithm uselessly repeat (n highBit * 2) bitShift: before obtaining
luckily this 'no inverse' result)