> For the complete documentation index, see [llms.txt](https://petroprotocol.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://petroprotocol.gitbook.io/docs/the-ecosystem/petro-crypto-wallet/wallet-cryptographic-protocols.md).

# Wallet Cryptographic Protocols

### **1. Elliptic Curve Cryptography (ECC)**

* Petro Wallet relies on **Elliptic Curve Cryptography** (ECC) for the generation of public and private keys. Specifically, it uses **secp256k1**, the elliptic curve standard employed by Ethereum (and Bitcoin).
* **secp256k1** is defined by the equation: `y2=x3+7y^2 = x^3 + 7y2=x3+7` This curve offers a high level of security while allowing efficient key generation and transaction signing.
* The private key is a 256-bit random number, while the public key is derived from this private key using elliptic curve multiplication. These keys are used for creating and verifying signatures on the blockchain.

### **2. Keccak-256 Hashing Algorithm (SHA-3 Family)**

* Petro Wallet uses the **Keccak-256** hashing algorithm for several critical operations, such as generating Ethereum addresses from public keys and transaction signing.
* Ethereum chose **Keccak-256** (a member of the **SHA-3** family) as its hashing algorithm because of its security and speed. The process looks like this:
  1. A user’s public key is hashed using Keccak-256.
  2. The last 20 bytes of this hash are used to generate the user’s Ethereum address.

Example:

`Ethereum Address=Keccak-256(Public Key)[12:]\text{Ethereum Address} = \text{Keccak-256}(\text{Public Key})[12:]Ethereum Address=Keccak-256(Public Key)[12:]`

### **3. ECDSA (Elliptic Curve Digital Signature Algorithm)**

Petro Wallet uses **ECDSA** for transaction signing. ECDSA is a variant of the Digital Signature Algorithm (DSA) that uses elliptic curve cryptography for higher efficiency and security.

* When a transaction is made, MetaMask:
  1. Hashes the transaction data using **Keccak-256**.
  2. Signs the hashed transaction with the user’s private key using **ECDSA**.
  3. Sends the signed transaction to the Ethereum network.

ECDSA signing involves generating a random number (**k**) and using the curve equation to calculate the signature:

r=(kG) x ​ modn(where G is the base point of the curve and n is the order of the curve)

𝑠𝑘 − 1 ( 𝐻 ( 𝑚 ) + 𝑟 ⋅ 𝑑 ) m o d     𝑛 (where 𝐻 ( 𝑚 ) is the hash of the message, and 𝑑 is the private key) s=k −1 (H(m)+r⋅d)modn(where H(m) is the hash of the message, and d is the private key)

This signature is then attached to the transaction and broadcast to the network.

### **5. Gas Estimation Algorithm**

Petro Wallet uses an internal **gas estimation algorithm** to determine the amount of gas required for a given transaction. Gas is the unit of computation on Ethereum, and the algorithm estimates how much gas is needed based on:

* **Transaction complexity** (e.g., the number of computational steps required to execute a smart contract).
* **Network congestion** and **current gas prices**.

Petro Wallet typically queries nodes (by default through **Infura**) to get current gas price estimates, and then it provides users with different gas fee options, such as low, medium, and high. Users can also manually adjust gas limits and prices.

### **6. Web3.js and EIP-1559 for Transaction Handling**

Petro Wallet leverages **Web3.js**, a JavaScript library, for interacting with the Ethereum blockchain. Web3.js enables Petro Wallet to send transactions, interact with smart contracts, and retrieve on-chain data.

* For Ethereum transactions, Petro Wallet also uses **EIP-1559** for gas fee handling. EIP-1559 introduced a base fee and a priority fee (or “tip”) to make gas pricing more predictable:
  * **Base fee**: Automatically determined by the network based on demand.
  * **Priority fee**: User-defined fee to prioritize the transaction.

### **7. Encryption Standards**

* **AES-256**: Petro Wallet uses **AES-256** encryption to locally encrypt and store the user’s private key on the device. This ensures that the private key is not accessible without decrypting the data with the user’s password.
* **PBKDF2 (Password-Based Key Derivation Function 2)**: Used for deriving a secure encryption key from the user’s password. It applies many rounds of hashing to slow down brute-force attacks.
