"Fossies" - the Fresh Open Source Software Archive

Member "julia-1.1.1/share/julia/stdlib/v1.1/SHA/src/sha1.jl" (16 May 2019, 2605 Bytes) of package /linux/misc/julia-1.1.1-linux-i686.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Julia source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # This file is a part of Julia. License is MIT: https://julialang.org/license
    2 
    3 # Nonlinear functions, in order to encourage inlining, these sadly are not an array of lambdas
    4 function Round0(b,c,d)
    5     return UInt32((b & c) | (~b & d))
    6 end
    7 
    8 function Round1And3(b,c,d)
    9     return UInt32(b ⊻ c ⊻ d)
   10 end
   11 
   12 function Round2(b,c,d)
   13     return UInt32((b & c) | (b & d) | (c & d))
   14 end
   15 
   16 function transform!(context::SHA1_CTX)
   17     # Buffer is 16 elements long, we expand to 80
   18     pbuf = buffer_pointer(context)
   19     for i in 1:16
   20         context.W[i] = bswap(unsafe_load(pbuf, i))
   21     end
   22 
   23     # First round of expansions
   24     for i in 17:32
   25         @inbounds begin
   26             context.W[i] = lrot(1, context.W[i-3] ⊻ context.W[i-8] ⊻ context.W[i-14] ⊻ context.W[i-16], 32)
   27         end
   28     end
   29 
   30     # Second round of expansions (possibly 4-way SIMD-able)
   31     for i in 33:80
   32         @inbounds begin
   33             context.W[i] = lrot(2, context.W[i-6] ⊻ context.W[i-16] ⊻ context.W[i-28] ⊻ context.W[i-32], 32)
   34         end
   35     end
   36 
   37     # Initialize registers with the previous intermediate values (our state)
   38     a = context.state[1]
   39     b = context.state[2]
   40     c = context.state[3]
   41     d = context.state[4]
   42     e = context.state[5]
   43 
   44     # Run our rounds, manually separated into the four rounds, unfortunately using an array of lambdas
   45     # really kills performance and causes a huge number of allocations, so we make it easy on the compiler
   46     for i = 1:20
   47         @inbounds begin
   48             temp = UInt32(lrot(5, a, 32) + Round0(b,c,d) + e + context.W[i] + K1[1])
   49             e = d
   50             d = c
   51             c = lrot(30, b, 32)
   52             b = a
   53             a = temp
   54         end
   55     end
   56 
   57     for i = 21:40
   58         @inbounds begin
   59             temp = UInt32(lrot(5, a, 32) + Round1And3(b,c,d) + e + context.W[i] + K1[2])
   60             e = d
   61             d = c
   62             c = lrot(30, b, 32)
   63             b = a
   64             a = temp
   65         end
   66     end
   67 
   68     for i = 41:60
   69         @inbounds begin
   70             temp = UInt32(lrot(5, a, 32) + Round2(b,c,d) + e + context.W[i] + K1[3])
   71             e = d
   72             d = c
   73             c = lrot(30, b, 32)
   74             b = a
   75             a = temp
   76         end
   77     end
   78 
   79     for i = 61:80
   80         @inbounds begin
   81             temp = UInt32(lrot(5, a, 32) + Round1And3(b,c,d) + e + context.W[i] + K1[4])
   82             e = d
   83             d = c
   84             c = lrot(30, b, 32)
   85             b = a
   86             a = temp
   87         end
   88     end
   89 
   90     context.state[1] += a
   91     context.state[2] += b
   92     context.state[3] += c
   93     context.state[4] += d
   94     context.state[5] += e
   95 end