BLOGPOST
3327 Logo

Made with love️ for blockchain community by humans from MVP Workshop

Contact us at

[email protected]

All Rights reserved 2024 © 3327

Python Poseidon Had a Problem: My Journey in Identifying and Fixing It

April 7, 2025

Introduction

In the digital age, where data security and integrity are most important, cryptographic hash functions form the backbone of modern system, including blockchain technologies and zero-knowledge proof protocols. Poseidon hash, introduced in the research paper Poseidon: A New Hash Function for Zero-Knowledge Proof Systems, offers an innovative approach to hashing within finite fields. This algorithm is engineered for efficiency, security, and adaptability to meet the complex demands of contemporary digital systems.

My journey with Poseidon hash in Python wasn't without its challenges. I encountered a significant problem that sparked a deep dive into the inner workings of the algorithm that lead to an in-depth process of identifying and fixing the issue. This hands-on experience not only deepened my understanding of the underlying cryptography but also inspired me to develop a more flexible, precise, and adaptable implementation.

Drawing inspiration from established implementations in Noir, Circom, and the Go version from Go-iden3-crypto, I set out to create a Python solution that supports a variable number of inputs (from 1 to 16) and allows fine-tuning of parameters. Initially, when comparing hash outputs, discrepancies between my version and the other implementations became apparent. Everything I discovered intrigued me, motivating me to delve even deeper into debugging and refining my code.


What is Poseidon Hash?

Poseidon hash is specifically designed to operate within arithmetic circuits of finite fields, making it ideal for cryptographic protocols based on zero-knowledge proofs. The key characteristics of this hash function include:

  • Absorption of Data in Finite Fields: Operations are performed on elements of a finite field defined by a large prime number. This approach enables computations with very large numbers while ensuring security through modular arithmetic.
  • Round Structure: The algorithm alternates between full and partial rounds. In full rounds, a nonlinear function (typically exponentiation to the power of five) is applied to all elements, whereas in partial rounds, the transformation targets only one element. This combination ensures robust data diffusion with an optimal number of operations.
  • ARK (Add Round Key) and Mixing: Each round incorporates predefined constants and mixes the state using matrices. These steps introduce asymmetry and complexity, thwarting reverse-engineering attempts without knowledge of the secret keys.

The comprehensive mathematical model, security proofs, and performance optimizations are detailed in the research paper, which provides the theoretical underpinnings for practical applications.


My Python Implementation of Poseidon Hash

Inspired by the high standards set by Noir, Circom, and Go-iden3-crypto, I aimed to build a Python version that could be easily adapted and fine-tuned. However, during testing, I observed that the hash outputs in Python didn’t completely align with those from the other implementations. This discrepancy became the catalyst for a deeper exploration and the path to debugging and resolving the issue.

Key goals of my implementation included:

  • Input Flexibility: Support for a variable number of input values (from 1 to 16), accommodating diverse datasets and applications.
  • Adaptation to Theoretical Specifications: Building on the robust foundations of existing implementations while maintaining modularity to allow parameter tuning.
  • Modularity and Code Clarity: Clearly defined functions and structures (such as ARK, mixing, and nonlinear transformations) enable easy exploration, testing, and potential integration into larger systems.

Key components of the code include:

  • Element Class: Defines a finite field element with operations for addition, multiplication, and exponentiation, ensuring results are reduced modulo a large prime.
  • Nonlinear Functions: Implements operations like exp5 and exp5state that perform exponentiation to the fifth power, which is central to the algorithm.
  • ARK and Mix Operations: Functions such as ark and mix add predefined constants and mix the state using matrices (loaded from JSON files), ensuring consistent transformations.
  • Hashing Flexibility: The function hash_with_state_ex processes a variable number of inputs, ensuring they fall within the field boundaries, and applies a series of rounds to produce the final hash. Additionally, poseidon_hash_ex supports generating multiple outputs as needed.

Applications and Use Cases

The Poseidon hash function is more than a theoretical exercise—it has several practical applications:

  • High Efficiency and Security: Finite field operations enable fast, secure data processing. The combination of full and partial rounds provides high diffusion and resistance to attacks, essential in cryptographic applications.
  • Optimized for Zero-Knowledge Proofs: Poseidon hash is tailored for systems using zero-knowledge proofs, making it an excellent choice for decentralized applications (DApps) and blockchain protocols that require quick and reliable verifications.
  • Adaptability Across Scenarios: With support for a variable number of inputs, the implementation is versatile enough to handle both simple and complex datasets, catering to a broad range of applications.
  • Interoperability and Integration: Its modular design allows for straightforward integration into larger systems, whether in academic research or industrial applications.
  • Potential in Machine Learning: Secure hashing with Poseidon can aid in creating unique identifiers, verifying data authenticity, and ensuring integrity—useful for anonymization and dimensionality reduction in ML pipelines.

Conclusion and Future Plans

Developing my own implementation of the Poseidon hash function in Python was a challenging yet enlightening experience. Although my initial version didn’t fully align with implementations from Noir, Circom, and Go, the journey of identifying and fixing the issues led to a more robust and adaptable solution.

Looking ahead, I plan to:

  • Expand Testing: Develop additional test cases and benchmarks to ensure compatibility and reliability.
  • Enhance Integration: Explore seamless integration with systems based on Noir, Circom, and Go, boosting interoperability.
  • Collaborate and Document: Share findings, optimizations, and insights through further blog posts, workshops, and open-source projects.
  • Develop Poseidon2 Hash: Work on a Poseidon2 implementation in Python, which promises significant speed improvements and enhanced performance.

This project illustrates how tackling implementation challenges can drive innovation—proving that even when established solutions fall short, a creative approach and perseverance can lead to breakthroughs in cryptographic development.

SHARE

COMMENTS (0)

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

You may also find this interesting:

How Lens helps us build user-centric Web3 products?

In our previous blog post, we covered one of the biggest challenges Web3 faces: building user (de)centric products. The main points were: One of the ways to solve this challenge is to start with the validation and exploration of the problems users have rather than a tech solution that can be developed. We want to […]

By Milos Novitovic
December 15, 2022
Deep Dive DeFi: Derivatives

Derivatives DeFi has been an emerging market for the past three years in the Web3 world. Protocols have tried to bridge the gap and bring traditional financial instruments to the Web3world, offering users decentralization, full custody, and favorable conditions to make their own choices with minimum intermediation.  So far, we (Web3 users) have been successful […]

By Andrija Raicevic
December 8, 2022
ML meets Blockchain: The Clash of Buzzwords

Blockchain technology and ZK proofs may be the last missing puzzle pieces for creating trustless and secure decentralized machine learning networks. This complex symphony of technologies opens the door for accessing knowledge from private databases, such as hospitals or government databases, without leaking information while still providing verifiable models and predictions.  Intro Machine learning has […]

By Aleksandar Veljkovic
December 1, 2022
Layer Hack: zkSync’s Account Abstraction

About Layer Hack Encode Club hosted a great hackathon focused on Layer 2 scaling solutions - Layer Hack. The hackathon lasted for nearly a month (Oct. 17 - Nov. 13), and was sponsored by AltLayer, Boba Network, Metis, and zkSync - each sponsor had their track with its own topic.  Out of those four tracks, […]

By Milos Bojinovic
November 24, 2022
Smart pointer: Rust vs C++

In the previous blog post, we got the idea about the ownership concept and how the Rust compiler works. This blog post will acquaint us with smart pointers and basic concepts to write safe and clean programs. It will be shown through examples in parallel in C++ and Rust. Even if you are not familiar […]

By Marija Mijailovic
November 17, 2022
User (de)centric design in Web3

All in all, it's just another “de” in the world Decentralization, democracy, DeFi, dex, degen…🙈 Web3 is hyping us all, and somehow, we keep adding the prefix “de” as a common Web3 good to all of it. As a product manager, user research came to my attention early in my career. When I joined Web3 […]

By Dragana Koprivica
November 9, 2022
Blockchain Credentials ZK
Blockchain Credentials with a Spice of ZK Magic

Suppose you have a bank account or a company. For some reason, the administration will not simply trust your word when you tell them your name is John Doe, living in the Himalayas, and all the money from your account suddenly went to charity. Those nagging people demand some proof, so you must show an […]

By Aleksandar Veljkovic
August 31, 2022
Curvy - protocol for fast anonymous transactions on Ethereum

Have you heard of Stealth Addresses (SA)? Do you know what cryptography is used in their background? Have you heard of BaseSAP, Umbra, or Monero? If so, then find out here what the Curvy protocol is and how it differs from the above protocols. There is a great need to introduce private transactions on public […]

By Marija Mikic
September 24, 2024
Cosmos Blog Header
Ticking All the Boxes: How Madara Modifications Enable On-Chain Game Logic

One of the main lessons from the previous two years is that the blockchain gaming space is expanding rapidly, with many new projects and features being built. However, it still remains in its infancy stage, as no title has managed to grab the mainstream market. I’m not talking about metaverse and virtual reality on the blockchain, though it is a massive part of the gaming space. I’m talking about a core gameplay experience that entirely runs on-chain.

By Filip Moldvai
August 10, 2023
Let’s geek out together!
Would you love to work with us on Web3-related experiments and studies?
Drop us a message