COTI V2 Documentation
  • Welcome
  • Networks
    • Faucet
    • Contracts Addresses
    • Adding the COTI Network to Metamask
  • How COTI Works
    • Introduction
      • EVM Introduction
      • Conceptual Overview
      • Use Cases and Applications
      • COTI Architecture
    • Advanced Topics
      • Garbled Circuits
      • AES Keys
      • Precompiles
      • Whitepaper
      • COTI vs others
  • Build on COTI
    • Core Concepts
      • Account Onboarding Procedure
      • Private Data Types
      • Supported Operations on Private Data Types
    • Quickstart
    • Guides
      • Basic Private Smart Contract
      • Account Onboard
      • Sending a Transaction with Encrypted Inputs
      • Resolving a Transaction's Encrypted Outputs
      • Writing a Private Smart Contract
      • Dos and Don'ts
        • Proper Use of Types
        • No Constant/Immutable Secret Types
        • No Public Contract Variables
      • Best Practices
        • Careful Onboarding
        • Careful Decrypting
        • Don't loop over an array without an index
        • Check Overflow
    • Tools
      • TypeScript SDK
      • Ethers.js
      • Python SDK
      • Web3.py
      • Contracts Library
        • MPC Core
        • Data Privacy Framework
        • Tokens
          • Private ERC20
          • Private ERC721
        • Onboard
      • Hardhat
      • Remix Plugin
      • COTI MetaMask Snap
      • Developer Sandbox
  • Running a COTI Node
    • COTI Node Ecosystem Litepaper
  • COTI Bridge
    • Swap COTI V1 Funds to COTI V2
  • Support and Community
    • Glossary
    • Telegram
    • Discord
    • GitHub
    • X
    • YouTube
  • COTI Builders Program
Powered by GitBook
On this page
  • Clone, Build & Test
  • Installation
  • Usage
  • Modules
  • Crypto

Was this helpful?

Edit on GitHub
  1. Build on COTI
  2. Tools

Python SDK

PreviousEthers.jsNextWeb3.py

Last updated 5 months ago

Was this helpful?

The provides a set of encryption, decryption, and cryptographic utilities, including RSA and AES encryption, message signing, and key handling functions. The utilities are primarily designed to work with cryptographic operations for secure communication and message signing, particularly within Ethereum smart contracts or similar environments.

Clone, Build & Test

Full instructions for building and testing the package locally are available in the

Installation

pip install coti-sdk

SDK versions < 1.0.0 are for use with the Devnet, versions >= 1.0.0 are for use with the Testnet

Usage

See the repository for code examples.

Modules

Crypto

def encrypt(key, plaintext)

Encrypts a 128-bit plaintext using the provided 128-bit AES key and ECB mode.

  • Parameters:

    • key (bytes): 128-bit AES key (16 bytes).

    • plaintext (bytes): 128-bit or smaller plaintext to encrypt.

  • Returns:

    • ciphertext (bytes): The encrypted text.

    • r (bytes): A random value used during encryption.

def decrypt(key, r, ciphertext)

Decrypts the ciphertext using the provided 128-bit AES key and the random value r.

  • Parameters:

    • key (bytes): 128-bit AES key.

    • r (bytes): Random value used during encryption.

    • ciphertext (bytes): Encrypted text to decrypt.

  • Returns:

    • plaintext (bytes): The decrypted original message.

def generate_aes_key()

Generates a random 128-bit AES key.

  • Returns:

    • key (bytes): Randomly generated 128-bit key (16 bytes).

def sign_input_text(sender, addr, function_selector, ct, key)

Signs an input message composed of multiple parts, including sender address, contract address, function selector, and ciphertext.

  • Parameters:

    • sender (bytes): Sender address.

    • addr (bytes): Contract address.

    • function_selector (str): Ethereum function selector (in hex).

    • ct (bytes): Ciphertext (concatenated).

    • key (bytes): Private key for signing.

  • Returns:

    • signature (bytes): Digital signature of the message.

def sign(message, key)

Signs a message using the provided key (Ethereum-style private key).

  • Parameters:

    • message (bytes): Message to sign.

    • key (bytes): Private key to use for signing.

  • Returns:

    • signature (bytes): The generated signature.

def build_input_text(plaintext, user_aes_key, sender, contract, function_selector, signing_key)

Encrypts a plaintext integer and signs the resulting ciphertext along with other parameters.

  • Parameters:

    • plaintext (int): Integer to encrypt.

    • user_aes_key (bytes): AES key for encryption.

    • sender (object): Ethereum-like sender object (with address attribute).

    • contract (object): Ethereum-like contract object (with address attribute).

    • function_selector (str): Function selector (in hex).

    • signing_key (bytes): Signing key for signature.

  • Returns:

    • dict: Contains the ciphertext and signature.

def build_string_input_text(plaintext, user_aes_key, sender, contract, function_selector, signing_key)

Encrypts and signs string-based input data, breaking it into 8-byte chunks.

  • Parameters:

    • plaintext (str): String to encrypt.

    • user_aes_key (bytes): AES key for encryption.

    • sender (object): Ethereum-like sender object.

    • contract (object): Ethereum-like contract object.

    • function_selector (str): Function selector (in hex).

    • signing_key (bytes): Signing key for signature.

  • Returns:

    • dict: Contains the ciphertext and signature.

def decrypt_uint(ciphertext, user_key)

Decrypts a ciphertext into an unsigned integer using AES.

  • Parameters:

    • ciphertext (CtUint): Ciphertext to decrypt (in integer format).

    • user_key (bytes): AES key to use for decryption.

  • Returns:

    • int: The decrypted integer.

def decrypt_string(ciphertext, user_key)

Decrypts a ciphertext back into a string, handling multiple formats of ciphertext.

  • Parameters:

    • ciphertext (CtString): Ciphertext to decrypt, can be in event or state variable format.

    • user_key (bytes): AES key to use for decryption.

  • Returns:

    • str: The decrypted string.

def generate_rsa_keypair()

Generates an RSA key pair for encryption and decryption.

  • Returns:

    • private_key_bytes (bytes): Serialized private key.

    • public_key_bytes (bytes): Serialized public key.

def decrypt_rsa(private_key_bytes, ciphertext)

Decrypts a ciphertext using RSA and a provided private key.

  • Parameters:

    • private_key_bytes (bytes): Private key used for decryption.

    • ciphertext (bytes): Ciphertext to decrypt.

  • Returns:

    • plaintext (bytes): Decrypted plaintext.

COTI Python SDK
coti-sdk-python GitHub repository
coti-sdk-python-examples