Skip to content

Commit

Permalink
Remove non-standard byte hasher and sponge hash construct on top of P…
Browse files Browse the repository at this point in the history
…oseidon. Remove BJJ wrapper functions.
  • Loading branch information
OBrezhniev committed Jan 10, 2025
1 parent 625bf56 commit 61cccdd
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 924 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Go implementation of some cryptographic primitives (that fit inside the SNARK fi
* Poseidon hash for BN254
* Poseidon hash for Goldilocks
* MIMC7
* Pedersen

## Contributing

Expand Down
81 changes: 0 additions & 81 deletions babyjub/babyjub_wrapper.go

This file was deleted.

65 changes: 0 additions & 65 deletions babyjub/babyjub_wrapper_test.go

This file was deleted.

117 changes: 0 additions & 117 deletions poseidon/poseidon.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ const NROUNDSF = 8
// NROUNDSP constant from Poseidon paper
var NROUNDSP = []int{56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68}

const spongeChunkSize = 31
const spongeInputs = 16

func zero() *ff.Element {
return ff.NewElement()
}
Expand Down Expand Up @@ -155,117 +152,3 @@ func Hash(inpBI []*big.Int) (*big.Int, error) {
func HashEx(inpBI []*big.Int, nOuts int) ([]*big.Int, error) {
return HashWithStateEx(inpBI, big.NewInt(0), nOuts)
}

// HashBytes returns a sponge hash of a msg byte slice split into blocks of 31 bytes
func HashBytes(msg []byte) (*big.Int, error) {
return HashBytesX(msg, spongeInputs)
}

// HashBytesX returns a sponge hash of a msg byte slice split into blocks of 31 bytes
func HashBytesX(msg []byte, frameSize int) (*big.Int, error) {
if frameSize < 2 || frameSize > 16 {
return nil, errors.New("incorrect frame size")
}

// not used inputs default to zero
inputs := make([]*big.Int, frameSize)
for j := 0; j < frameSize; j++ {
inputs[j] = new(big.Int)
}
dirty := false
var hash *big.Int
var err error

k := 0
for i := 0; i < len(msg)/spongeChunkSize; i++ {
dirty = true
inputs[k].SetBytes(msg[spongeChunkSize*i : spongeChunkSize*(i+1)])
if k == frameSize-1 {
hash, err = Hash(inputs)
dirty = false
if err != nil {
return nil, err
}
inputs = make([]*big.Int, frameSize)
inputs[0] = hash
for j := 1; j < frameSize; j++ {
inputs[j] = new(big.Int)
}
k = 1
} else {
k++
}
}

if len(msg)%spongeChunkSize != 0 {
// the last chunk of the message is less than 31 bytes
// zero padding it, so that 0xdeadbeaf becomes
// 0xdeadbeaf000000000000000000000000000000000000000000000000000000
var buf [spongeChunkSize]byte
copy(buf[:], msg[(len(msg)/spongeChunkSize)*spongeChunkSize:])
inputs[k] = new(big.Int).SetBytes(buf[:])
dirty = true
}

if dirty {
// we haven't hashed something in the main sponge loop and need to do hash here
hash, err = Hash(inputs)
if err != nil {
return nil, err
}
}

return hash, nil
}

// SpongeHash returns a sponge hash of inputs (using Poseidon with frame size of 16 inputs)
func SpongeHash(inputs []*big.Int) (*big.Int, error) {
return SpongeHashX(inputs, spongeInputs)
}

// SpongeHashX returns a sponge hash of inputs using Poseidon with configurable frame size
func SpongeHashX(inputs []*big.Int, frameSize int) (*big.Int, error) {
if frameSize < 2 || frameSize > 16 {
return nil, errors.New("incorrect frame size")
}

// not used frame default to zero
frame := make([]*big.Int, frameSize)
for j := 0; j < frameSize; j++ {
frame[j] = new(big.Int)
}
dirty := false
var hash *big.Int
var err error

k := 0
for i := 0; i < len(inputs); i++ {
dirty = true
frame[k] = inputs[i]
if k == frameSize-1 {
hash, err = Hash(frame)
dirty = false
if err != nil {
return nil, err
}
frame = make([]*big.Int, frameSize)
frame[0] = hash
for j := 1; j < frameSize; j++ {
frame[j] = new(big.Int)
}
k = 1
} else {
k++
}
}

if dirty {
// we haven't hashed something in the main sponge loop and need to do hash here
hash, err = Hash(frame)
if err != nil {
return nil, err
}
}

return hash, nil
}
118 changes: 0 additions & 118 deletions poseidon/poseidon_test.go

Large diffs are not rendered by default.

60 changes: 0 additions & 60 deletions poseidon/poseidon_wrapper.go

This file was deleted.

483 changes: 0 additions & 483 deletions poseidon/poseidon_wrapper_test.go

This file was deleted.

0 comments on commit 61cccdd

Please sign in to comment.