// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package sha3

import (
	
	
	
	
	
)

type SHAKE struct {
	d Digest // SHA-3 state context and Read/Write operations

	// initBlock is the cSHAKE specific initialization set of bytes. It is initialized
	// by newCShake function and stores concatenation of N followed by S, encoded
	// by the method specified in 3.3 of [1].
	// It is stored here in order for Reset() to be able to put context into
	// initial state.
	initBlock []byte
}

func bytepad( []byte,  int) []byte {
	 := make([]byte, 0, 9+len()+-1)
	 = append(, leftEncode(uint64())...)
	 = append(, ...)
	if  :=  - len()%;  <  {
		 = append(, make([]byte, )...)
	}
	return 
}

func leftEncode( uint64) []byte {
	// Let n be the smallest positive integer for which 2^(8n) > x.
	 := (bits.Len64() + 7) / 8
	if  == 0 {
		 = 1
	}
	// Return n || x with n as a byte and x an n bytes in big-endian order.
	 := make([]byte, 9)
	byteorder.BEPutUint64([1:], )
	 = [9--1:]
	[0] = byte()
	return 
}

func newCShake(,  []byte, ,  int,  byte) *SHAKE {
	 := &SHAKE{d: Digest{rate: , outputLen: , dsbyte: }}
	.initBlock = make([]byte, 0, 9+len()+9+len()) // leftEncode returns max 9 bytes
	.initBlock = append(.initBlock, leftEncode(uint64(len())*8)...)
	.initBlock = append(.initBlock, ...)
	.initBlock = append(.initBlock, leftEncode(uint64(len())*8)...)
	.initBlock = append(.initBlock, ...)
	.Write(bytepad(.initBlock, .d.rate))
	return 
}

func ( *SHAKE) () int { return .d.BlockSize() }
func ( *SHAKE) () int      { return .d.Size() }

// Sum appends a portion of output to b and returns the resulting slice. The
// output length is selected to provide full-strength generic security: 32 bytes
// for SHAKE128 and 64 bytes for SHAKE256. It does not change the underlying
// state. It panics if any output has already been read.
func ( *SHAKE) ( []byte) []byte { return .d.Sum() }

// Write absorbs more data into the hash's state.
// It panics if any output has already been read.
func ( *SHAKE) ( []byte) ( int,  error) { return .d.Write() }

func ( *SHAKE) ( []byte) ( int,  error) {
	fips140.RecordApproved()
	// Note that read is not exposed on Digest since SHA-3 does not offer
	// variable output length. It is only used internally by Sum.
	return .d.read()
}

// Reset resets the hash to initial state.
func ( *SHAKE) () {
	.d.Reset()
	if len(.initBlock) != 0 {
		.Write(bytepad(.initBlock, .d.rate))
	}
}

// Clone returns a copy of the SHAKE context in its current state.
func ( *SHAKE) () *SHAKE {
	 := *
	return &
}

func ( *SHAKE) () ([]byte, error) {
	return .AppendBinary(make([]byte, 0, marshaledSize+len(.initBlock)))
}

func ( *SHAKE) ( []byte) ([]byte, error) {
	,  := .d.AppendBinary()
	if  != nil {
		return nil, 
	}
	 = append(, .initBlock...)
	return , nil
}

func ( *SHAKE) ( []byte) error {
	if len() < marshaledSize {
		return errors.New("sha3: invalid hash state")
	}
	if  := .d.UnmarshalBinary([:marshaledSize]);  != nil {
		return 
	}
	.initBlock = bytes.Clone([marshaledSize:])
	return nil
}

// NewShake128 creates a new SHAKE128 XOF.
func () *SHAKE {
	return &SHAKE{d: Digest{rate: rateK256, outputLen: 32, dsbyte: dsbyteShake}}
}

// NewShake256 creates a new SHAKE256 XOF.
func () *SHAKE {
	return &SHAKE{d: Digest{rate: rateK512, outputLen: 64, dsbyte: dsbyteShake}}
}

// NewCShake128 creates a new cSHAKE128 XOF.
//
// N is used to define functions based on cSHAKE, it can be empty when plain
// cSHAKE is desired. S is a customization byte string used for domain
// separation. When N and S are both empty, this is equivalent to NewShake128.
func (,  []byte) *SHAKE {
	if len() == 0 && len() == 0 {
		return NewShake128()
	}
	return newCShake(, , rateK256, 32, dsbyteCShake)
}

// NewCShake256 creates a new cSHAKE256 XOF.
//
// N is used to define functions based on cSHAKE, it can be empty when plain
// cSHAKE is desired. S is a customization byte string used for domain
// separation. When N and S are both empty, this is equivalent to NewShake256.
func (,  []byte) *SHAKE {
	if len() == 0 && len() == 0 {
		return NewShake256()
	}
	return newCShake(, , rateK512, 64, dsbyteCShake)
}