package org.bouncycastle.pqc.crypto.sphincsplus;

class HarakaSXof
    extends HarakaSBase
{
    public String getAlgorithmName()
    {
        return "Haraka-S";
    }

    public HarakaSXof(byte[] pkSeed)
    {
        byte[] buf = new byte[640];
        update(pkSeed, 0, pkSeed.length);
        doFinal(buf, 0, buf.length);
        haraka512_rc = new long[10][8];
        haraka256_rc = new int[10][8];
        for (int i = 0; i < 10; ++i)
        {
            interleaveConstant32(haraka256_rc[i], buf, i << 5);
            interleaveConstant(haraka512_rc[i], buf, i << 6);
        }
    }

    public void update(byte[] input, int inOff, int len)
    {
        int i = inOff, j, loop = (len + off) >> 5;
        for (j = 0; j < loop; ++j)
        {
            while (off < 32)
            {
                buffer[off++] ^= input[i++];
            }
            haraka512Perm(buffer);
            off = 0;
        }
        while (i < inOff + len)
        {
            buffer[off++] ^= input[i++];
        }
    }

    public void update(byte input)
    {
        buffer[off++] ^= input;
        if (off == 32)
        {
            haraka512Perm(buffer);
            off = 0;
        }
    }

    public int doFinal(byte[] out, int outOff, int len)
    {
        int outLen = len;
        //Finalize
        buffer[off] ^= 0x1F;
        buffer[31] ^= 128;
        off = 0;
        //Squeeze
        while (len > 0)
        {
            haraka512Perm(buffer);
            int i = 0;
            while (i < 32 && i + outOff < out.length)
            {
                out[i + outOff] = buffer[i];
                i++;
            }
            outOff += i;
            len -= i;
        }
        if (len != 0)
        {
            byte[] d = new byte[64];
            haraka512Perm(d);
            System.arraycopy(d, 0, out, outOff, -len);
        }

        reset();
        
        return outLen;
    }
}

@@ -1280,24 +1280,25 @@
 tLen = len;%0A
+%0A
         //Fi
@@ -1360,32 +1360,16 @@
 %5E= 128;%0A
-        off = 0;
 %0A       
@@ -1399,18 +1399,20 @@
 e (len %3E
- 0
+= 32
 )%0A      
@@ -1466,166 +1466,53 @@
     
-int i = 0;%0A            while (i %3C 32 && i + outOff %3C out.length)%0A            %7B%0A                out%5Bi + outOff%5D = buffer%5Bi%5D;%0A                i++;%0A            %7D
+System.arraycopy(buffer, 0, out, outOff, 32);
 %0A   
@@ -1530,17 +1530,18 @@
 tOff += 
-i
+32
 ;%0A      
@@ -1553,17 +1553,18 @@
  len -= 
-i
+32
 ;%0A      
@@ -1587,10 +1587,9 @@
 len 
-!=
+%3E
  0)%0A
@@ -1602,45 +1602,8 @@
   %7B%0A
-            byte%5B%5D d = new byte%5B64%5D;%0A
     
@@ -1624,17 +1624,22 @@
 512Perm(
-d
+buffer
 );%0A     
@@ -1662,17 +1662,22 @@
 raycopy(
-d
+buffer
 , 0, out
@@ -1686,17 +1686,16 @@
 outOff, 
--
 len);%0A  
@@ -1720,24 +1720,16 @@
 eset();%0A
-        
 %0A       

Fix Harakas XOF API compliance