Search code examples
matlabsimulink

How to use subsystem output as its input in the next iteration in simulink


I am trying to create SHA256 hash function in Simulink (2015a) while abstaining as much as possible from using user-defined functions - using them is not the answer I am looking for.

I have tried using Delay block, but it also delays my a-h variables from initialization (it means, for example, my T1 will be 0), so the problem lies there probably

I am new to this kind of thing so I am sorry if it's a question that has already been answered I simply don't know what to google to get my answer since English is not my first language, anyhow here is my scheme:

For loop (but with placeholder const 1)

get a...g function

First iteration is giving me the correct output

It should represent this MATLAB code:

% H = 1x8 const
% constK = 1x64 const

a = H(1);
b = H(2);
c = H(3);
d = H(4);
e = H(5);
f = H(6);
g = H(7);
h = H(8);

% Calc H1 subsystem in Simulink
for t = 1:64
    K = constK(t);
    temp1 = addMsgSch(h, cSigma1(e), choice(e, f, g), K, W(t));
    temp2 = addMsgSch(cSigma0(a), majority(a, b, c));
    h = g;
    g = f;
    f = e;
    e = addMsgSch(d, temp1);
    d = c;
    c = b;
    b = a;
    a = addMsgSch(temp1, temp2);
end

% addMsgSch(a, b... z) = a + b + ... + z;

So my question is: How can I (re)start the loop with newly assigned variables?


Solution

  • To preface this: I don't have 2015a (and so can't readily test this), and am not specifically familiar with the ins and outs of SHA256.

    The Delay block's default behavior is to initialize its output signal with all zeroes, but that is not its only available behavior. The initial condition can be set on the mask, or (at least in recent versions) there is a check box on the mask that will "magically" make an additional input port appear:

    x0 — Initial condition scalar | vector

    Initial condition specified as inherited from an input port. Enabled when you select the Initial Condition: Source parameter as Input port.

    (A distressing number of built-in Simulink blocks have similar "polymorphic" abilities to change the input or output ports)

    After enabling this additional port, you can route the H signal into it (and presumably H1 into the "main" input of the Delay block).

    For your awareness, a number of Simulink blocks have some mechanism to set initial conditions, usually with a mask parameter. There is also the related IC block, which can set initial conditions on a signal based on a mask parameter.

    FWIW, in my experience implementing something like this in "pure" Simulink doesn't really play to Simulink's strengths. Simulink is great for managing signal (and thus information) routing, and its dynamics engine is quite powerful. But for doing "simple" math, the pile-up of blocks is often less clear than using a Matlab Function block. The chunk of code inside your m-code for loop is much more straightforward to me than the equivalent Simulink (and your Simulink is laid out in a pretty reasonable way).

    Also FYI, the Subsystem block that you are using to split H into a...h could probably be replaced with a Demux.

    And a further FYI, it appears to me that your use of addMsgSch(a, b... z) could be replaced with sum([a, b... z]) or just regular addition, either of which would improve readability.