Git Product home page Git Product logo

go-iden3-crypto's Introduction

go-iden3-crypto Go Report Card Test Status Lint Status GoDoc

Go implementation of some cryptographic primitives (that fit inside the SNARK field) used in iden3:

  • BabyJubJub curve arithmetics & EdDSA on it
  • Goldilocks curve arithmetics
  • Poseidon hash for BN254
  • Poseidon hash for Goldilocks
  • MIMC7

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as below, without any additional terms or conditions.

License

© 2023 0kims Association

This project is licensed under either of

at your option.

go-iden3-crypto's People

Contributors

arnaubennassar avatar arnaucube avatar cool-develope avatar ed255 avatar ilya-korotya avatar mikelle avatar obrezhniev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

go-iden3-crypto's Issues

Add a function to check if a big int fits in the ZK field

Instead of doing

cryptoUtils.CheckBigIntArrayInField(bigInts, cryptoConstants.Q)

Just use the Q inside a util so that it looks like this:

cryptoUtils.CheckBigIntArrayInFieldFOO(bigInts)

Also, if it's not there, add a function for a single big int

Poseidon `Hash` function is broken!

The Hash function in poseidon for arbitrary length inputs is calculated like this:

r_0 = 0
r_(i+1) = r_i + hash(a_(i_0), ..., a_(i_4))
output = r_(n+1)

This means that creating a collision is trivial, since any group of 5 elements aligned at 5 elements can be swapped by another group of 5 elements aligned at 5 elements leading to the same hash result. This is checked in the TestPoseidonBrokenChunks, which currently doesn't pass.

Let's discuss a solution.

Poseidion `Hash` is broken (2)!

Poseidon Hash adds 0 value elements as padding to work in multiples of 5 elements. This means that creating a collision is trivial: H(x) == H(x, 0) == H(x, 0, 0) == ...

This is checked in the TestPoseidonBrokenPadding, which currently doesn't pass.

Justify why poseidon Hash uses chunks of 5 elements

Poseidon Hash uses chunks of 5 elements:

// Hash performs the Poseidon hash over a *big.Int array
// in chunks of 5 elements
func Hash(arr []*big.Int) (*big.Int, error) {
	if !utils.CheckBigIntArrayInField(arr, constants.fqR.Q) {
		return nil, errors.New("inputs values not inside Finite Field")
	}

	r := constants.fqR.Zero()
	for i := 0; i < len(arr); i = i + 5 {
		var fiveElems [5]*big.Int
		for j := 0; j < 5; j++ {
			if i+j < len(arr) {
				fiveElems[j] = arr[i+j]
			} else {
				fiveElems[j] = _constants.Zero
			}
		}
		ph, err := PoseidonHash(fiveElems[:])
		if err != nil {
			return nil, err
		}
		r = constants.fqR.Add(
			r,
			ph)
	}

	return r, nil
}

Whereas the basic poseidon hash function uses groups of 6 elements (T = 6):

// PoseidonHash computes the Poseidon hash for the given inputs
func PoseidonHash(inp []*big.Int) (*big.Int, error) {
	if len(inp) == 0 || len(inp) > T {
		return nil, errors.New("wrong inputs length")
	}
	if !utils.CheckBigIntArrayInField(inp, constants.fqR.Q) {
		return nil, errors.New("inputs values not inside Finite Field")
	}
	state := inp
	for i := len(inp); i < T; i++ {
		state = append(state, constants.fqR.Zero())
	}

	// ARK --> SBox --> M, https://eprint.iacr.org/2019/458.pdf pag.5
	for i := 0; i < NROUNDSF+NROUNDSP; i++ {
		state = ark(state, constants.c[i])
		state = sbox(state, i)
		state = mix(state, constants.m)
	}
	return state[0], nil
}

Why Hash designed this way?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.