Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added poseidon2 hash function to barretenberg/crypto #3118

Merged
merged 18 commits into from
Nov 28, 2023

Conversation

zac-williamson
Copy link
Contributor

Preliminary work to add Poseidon2 hash function as a standard library primitive (https://eprint.iacr.org/2023/323.pdf)

Adds Poseidon2 to crypto module, following paper + specification at https://github.com/C2SP/C2SP/blob/792c1254124f625d459bfe34417e8f6bdd02eb28/poseidon-sponge.md

Checklist:

Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge.

  • If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag.
  • I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code.
  • Every change is related to the PR description.
  • I have linked this pull request to relevant issues (if any exist).

@AztecBot
Copy link
Collaborator

AztecBot commented Oct 30, 2023

Benchmark results

No metrics with a significant change found.

Detailed results

All benchmarks are run on txs on the Benchmarking contract on the repository. Each tx consists of a batch call to create_note and increment_balance, which guarantees that each tx has a private call, a nested private call, a public call, and a nested public call, as well as an emitted private note, an unencrypted log, and public storage read and write.

This benchmark source data is available in JSON format on S3 here.

Values are compared against data from master at commit 4735bdeb and shown if the difference exceeds 1%.

L2 block published to L1

Each column represents the number of txs on an L2 block published to L1.

Metric 8 txs 32 txs 128 txs
l1_rollup_calldata_size_in_bytes 45,444 179,588 716,132
l1_rollup_calldata_gas 222,924 868,184 3,449,504
l1_rollup_execution_gas 842,011 3,595,292 22,204,873
l2_block_processing_time_in_ms 2,076 (+1%) 7,810 (-1%) 31,680 (-1%)
note_successful_decrypting_time_in_ms 343 (-2%) 991 (-4%) 3,600 (-4%)
note_trial_decrypting_time_in_ms 53.1 (-14%) 118 (-6%) 198 (-1%)
l2_block_building_time_in_ms 25,482 101,548 403,436 (-1%)
l2_block_rollup_simulation_time_in_ms 17,898 71,299 (-1%) 281,890 (-1%)
l2_block_public_tx_process_time_in_ms 7,534 30,092 120,980

L2 chain processing

Each column represents the number of blocks on the L2 chain where each block has 16 txs.

Metric 5 blocks 10 blocks
node_history_sync_time_in_ms 23,279 (+3%) 43,807 (-1%)
note_history_successful_decrypting_time_in_ms 2,364 (+2%) 4,517 (+1%)
note_history_trial_decrypting_time_in_ms 162 (+1%) 230
node_database_size_in_bytes 3,316,086 3,662,515
pxe_database_size_in_bytes 29,748 59,307

Circuits stats

Stats on running time and I/O sizes collected for every circuit run across all benchmarks.

Circuit circuit_simulation_time_in_ms circuit_input_size_in_bytes circuit_output_size_in_bytes
private-kernel-init 399 43,077 20,441
private-kernel-ordering 127 (+1%) 25,833 9,689
base-rollup 3,183 659,500 873
root-rollup 93.6 (+4%) 4,072 1,097
private-kernel-inner 826 64,484 20,441
public-kernel-private-input 418 25,203 20,441
public-kernel-non-first-iteration 412 25,245 20,441
merge-rollup 10.9 2,592 873

Miscellaneous

Transaction sizes based on how many contracts are deployed in the tx.

Metric 0 deployed contracts 1 deployed contracts
tx_size_in_bytes 10,323 29,083

@kevaundray
Copy link
Contributor

Since the poseidon code is not comformant with the reference implementation, it probably makes sense to thoroughly review the changes outside of the poseidon2 module and the API of poseidon2.

Once we make poseidon2 conformant with a chosen reference implementation using test vectors, then it will become glaringly obvious if the implementation is "correct".

@zac-williamson what are your thoughts?

@kevaundray
Copy link
Contributor

Would also say that while its not conformant we likely don't want to integrate it into other parts of the system as it may cause breaking changes once we make it conformant

@zac-williamson
Copy link
Contributor Author

Since the poseidon code is not comformant with the reference implementation, it probably makes sense to thoroughly review the changes outside of the poseidon2 module and the API of poseidon2.

Once we make poseidon2 conformant with a chosen reference implementation using test vectors, then it will become glaringly obvious if the implementation is "correct".

@zac-williamson what are your thoughts?

I think this works and should be plan A, but it works only as long as we can get independent test vectors. The best I can find use t = 3 and we have t = 4. We might have to collaborate with another team to get an independent implementation that we can compare against

@zac-williamson
Copy link
Contributor Author

Would also say that while its not conformant we likely don't want to integrate it into other parts of the system as it may cause breaking changes once we make it conformant

Yes I agree. I think we should get all of the hash structures implemented and working (i.e. have stuff in the stdlib, ideally also have ACIR opcodes working), but to not actually use Poseidon downstream until we've made it conformant

Copy link
Contributor

@lucasxia01 lucasxia01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good to me. Had several questions and some nits.

cache_size += 1;
} else if (mode == Mode::SQUEEZE) {
// If we're in squeeze mode, switch to absorb mode and add the input into the cache.
// N.B. I don't think this code path can be reached?!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like a call to squeeze() and then absorb() will hit this condition

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it does seem like we would not like to ever have an absorb AFTER a squeeze. Maybe we could throw an error in this case?

auto new_output_elements = perform_duplex();
mode = Mode::SQUEEZE;
for (size_t i = 0; i < rate; ++i) {
cache[i] = new_output_elements[i];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we populate the cache with the same elements in state?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is because we return cache elements as our output, but this seems like a weird way to do things.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache contains data that has yet to be included in the sponge, and state is the active data in the sponge.

state acts as a mutable data structure and cache doesn't. I think this is a safer way to structure the code as perform_duplex mutates state

cache_size = rate;
}
// By this point, we should have a non-empty cache. Pop one item off the top of the cache and return it.
FF result = cache[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just return cache[cache_size-1]? and avoid this shifting process?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache is not a local variable but is a member of the Sponge class. It's state is important. We must remove the element that we are returning from the cache. Just returning cache[cache_size - 1] will not have the same behavior.

with regards to the manual action of shifting cache, we could have used a std::vector to define cache and used the pop method, but I wanted to use a fixed-size array to represent cache so that indexing the array out of bounds would more easily be caught by the compiler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant returning cache[cache_size-1] and also decrementing cache_size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thoroughness when developing the code. I wanted to rule out stale data in cache being a source of bugs.

@lucasxia01
Copy link
Contributor

Since the poseidon code is not comformant with the reference implementation, it probably makes sense to thoroughly review the changes outside of the poseidon2 module and the API of poseidon2.
Once we make poseidon2 conformant with a chosen reference implementation using test vectors, then it will become glaringly obvious if the implementation is "correct".
@zac-williamson what are your thoughts?

I think this works and should be plan A, but it works only as long as we can get independent test vectors. The best I can find use t = 3 and we have t = 4. We might have to collaborate with another team to get an independent implementation that we can compare against

Can we try using an existing implementation with t=3, and modify it so that it uses t=4?

@lucasxia01 lucasxia01 added the crypto cryptography label Nov 3, 2023
return output;
}

template <size_t out_len> static std::array<FF, out_len> hash_fixed_length(std::span<FF> input)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't see how there's a differentiation between fixed length and variable length here. It seems like we only have fixed length hashing since we only take in field elements of 254 bits.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the length refers to the number of field elements being hashed, not the number of bits in a field element.

Variable-length means that we are computing the hash of a dynamically-sized nubmer of field elements (i.e. the length varies from one hash to the next)

Fixed-length hash means we are computing the hash of a fixed-size nubmer of inputs (i.e. the length does not vary from one hash to the next)

A fixed-length hash is more efficient if we can get away with it, as we do not need to include the size of the hash in the preimage data being hashed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, seems like we are only doing variable length hashing then right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now. Merkle membership hashes will likely use fixed length

@zac-williamson
Copy link
Contributor Author

Since the poseidon code is not comformant with the reference implementation, it probably makes sense to thoroughly review the changes outside of the poseidon2 module and the API of poseidon2.
Once we make poseidon2 conformant with a chosen reference implementation using test vectors, then it will become glaringly obvious if the implementation is "correct".
@zac-williamson what are your thoughts?

I think this works and should be plan A, but it works only as long as we can get independent test vectors. The best I can find use t = 3 and we have t = 4. We might have to collaborate with another team to get an independent implementation that we can compare against

Can we try using an existing implementation with t=3, and modify it so that it uses t=4?

Yes I think we can do this.

@lucasxia01 lucasxia01 merged commit d47782b into master Nov 28, 2023
80 checks passed
@lucasxia01 lucasxia01 deleted the zw/poseidon2 branch November 28, 2023 15:58
spypsy pushed a commit that referenced this pull request Nov 28, 2023
🤖 I have created a release *beep* *boop*
---


<details><summary>aztec-packages: 0.16.1</summary>

##
[0.16.1](aztec-packages-v0.16.0...aztec-packages-v0.16.1)
(2023-11-28)


### Features

* Added poseidon2 hash function to barretenberg/crypto
([#3118](#3118))
([d47782b](d47782b))
* Aztec CI files in Noir
([#3430](#3430))
([1621f3a](1621f3a))
* Persistent archiver store
([#3410](#3410))
([4735bde](4735bde)),
closes
[#3361](#3361)


### Bug Fixes

* **ci:** Don't leave DRY_DEPLOY unset
([#3449](#3449))
([454e316](454e316))
* **ci:** Publishing dockerhub manifests
([#3451](#3451))
([a59e7f0](a59e7f0))
* Hotfix noir sync
([#3436](#3436))
([c4e4745](c4e4745))


### Miscellaneous

* **docs:** Core concepts page in getting-started
([#3401](#3401))
([1a62f73](1a62f73))
* Point acir tests at noir master branch
([#3440](#3440))
([106e690](106e690))


### Documentation

* Further updates to the gas and fees whitepaper
([#3448](#3448))
([4152ba6](4152ba6))
* Updates to gas and fees yellow paper
([#3438](#3438))
([5f0e1ca](5f0e1ca))
</details>

<details><summary>barretenberg.js: 0.16.1</summary>

##
[0.16.1](barretenberg.js-v0.16.0...barretenberg.js-v0.16.1)
(2023-11-28)


### Miscellaneous

* **barretenberg.js:** Synchronize aztec-packages versions
</details>

<details><summary>barretenberg: 0.16.1</summary>

##
[0.16.1](barretenberg-v0.16.0...barretenberg-v0.16.1)
(2023-11-28)


### Features

* Added poseidon2 hash function to barretenberg/crypto
([#3118](#3118))
([d47782b](d47782b))


### Miscellaneous

* Point acir tests at noir master branch
([#3440](#3440))
([106e690](106e690))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Maddiaa0 pushed a commit that referenced this pull request Nov 28, 2023
Preliminary work to add Poseidon2 hash function as a standard library
primitive (https://eprint.iacr.org/2023/323.pdf)

Adds Poseidon2 to crypto module, following paper + specification at
https://github.com/C2SP/C2SP/blob/792c1254124f625d459bfe34417e8f6bdd02eb28/poseidon-sponge.md

---------

Co-authored-by: lucasxia01 <[email protected]>
AztecBot added a commit to AztecProtocol/barretenberg that referenced this pull request Nov 29, 2023
🤖 I have created a release *beep* *boop*
---


<details><summary>aztec-packages: 0.16.1</summary>

##
[0.16.1](AztecProtocol/aztec-packages@aztec-packages-v0.16.0...aztec-packages-v0.16.1)
(2023-11-28)


### Features

* Added poseidon2 hash function to barretenberg/crypto
([#3118](AztecProtocol/aztec-packages#3118))
([d47782b](AztecProtocol/aztec-packages@d47782b))
* Aztec CI files in Noir
([#3430](AztecProtocol/aztec-packages#3430))
([1621f3a](AztecProtocol/aztec-packages@1621f3a))
* Persistent archiver store
([#3410](AztecProtocol/aztec-packages#3410))
([4735bde](AztecProtocol/aztec-packages@4735bde)),
closes
[#3361](AztecProtocol/aztec-packages#3361)


### Bug Fixes

* **ci:** Don't leave DRY_DEPLOY unset
([#3449](AztecProtocol/aztec-packages#3449))
([454e316](AztecProtocol/aztec-packages@454e316))
* **ci:** Publishing dockerhub manifests
([#3451](AztecProtocol/aztec-packages#3451))
([a59e7f0](AztecProtocol/aztec-packages@a59e7f0))
* Hotfix noir sync
([#3436](AztecProtocol/aztec-packages#3436))
([c4e4745](AztecProtocol/aztec-packages@c4e4745))


### Miscellaneous

* **docs:** Core concepts page in getting-started
([#3401](AztecProtocol/aztec-packages#3401))
([1a62f73](AztecProtocol/aztec-packages@1a62f73))
* Point acir tests at noir master branch
([#3440](AztecProtocol/aztec-packages#3440))
([106e690](AztecProtocol/aztec-packages@106e690))


### Documentation

* Further updates to the gas and fees whitepaper
([#3448](AztecProtocol/aztec-packages#3448))
([4152ba6](AztecProtocol/aztec-packages@4152ba6))
* Updates to gas and fees yellow paper
([#3438](AztecProtocol/aztec-packages#3438))
([5f0e1ca](AztecProtocol/aztec-packages@5f0e1ca))
</details>

<details><summary>barretenberg.js: 0.16.1</summary>

##
[0.16.1](AztecProtocol/aztec-packages@barretenberg.js-v0.16.0...barretenberg.js-v0.16.1)
(2023-11-28)


### Miscellaneous

* **barretenberg.js:** Synchronize aztec-packages versions
</details>

<details><summary>barretenberg: 0.16.1</summary>

##
[0.16.1](AztecProtocol/aztec-packages@barretenberg-v0.16.0...barretenberg-v0.16.1)
(2023-11-28)


### Features

* Added poseidon2 hash function to barretenberg/crypto
([#3118](AztecProtocol/aztec-packages#3118))
([d47782b](AztecProtocol/aztec-packages@d47782b))


### Miscellaneous

* Point acir tests at noir master branch
([#3440](AztecProtocol/aztec-packages#3440))
([106e690](AztecProtocol/aztec-packages@106e690))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crypto cryptography
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants