Rust FFI bindings for StarkWare's crypto-cpp library

Overview

starkware-crypto-rs

Rust FFI bindings for StarkWare's crypto-cpp library

linting-badge tests-badge crates-badge

Note that currently target x86_64-pc-windows-msvc is not supported. If you're building on Windows, you need to use the GNU build of Rust.

Adding starkware-crypto-rs to your project

To use the crate from crates.io, add the following to your Cargo.toml file:

[dependencies]
starkware-crypto-sys = "0.1"

Running benchmark

To run benchmark:

$ cargo bench

On the author's machine, the results are:

pedersen_hash           time:   [267.08 us 270.36 us 274.40 us]

License

Licensed under either of

at your option.

Comments
  • Getting public from private key

    Getting public from private key

    Hi Jonathan,

    I am writing a program that has to compute the STARK public key based on the private key.

    I have to private key in the form of a hex encoded string. I then decoded it into a byte slice. However, I see that the get_public_key function accepts a fixed size byte slice of &[u8; 32]. Having looked online, I couldn't find any way of converting &[u8] into &[u8; 32]. So I created a helper function which takes in the input string and attempts to convert it:

    fn hex_to_32byte(inp: &str) -> Result<[u8; 32], AllErr> {
        let decoded = hex::decode(inp)?.as_slice();
        let out: [u8; 32];
        
        for i in 0..32 {
            out[i] = decoded[i];
        }
    
        Ok(out)
    }
    

    Is there a more efficient way?

    opened by rhobro 8
  • Unable to build on macOS

    Unable to build on macOS

    When building on macOS in a GitHub Actions workflow with cargo build, an error occurs:

      cargo:warning=lib/starkware-crypto/src/starkware/crypto/ffi/utils.cc:1:10: fatal error: 'endian.h' file not found
      cargo:warning=#include <endian.h>
      cargo:warning=         ^~~~~~~~~~
      exit status: 0
      running: "c++" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-arch" "x86_64" "-static" "-I" "lib/starkware-crypto/src" "-Wall" "-Wextra" "-std=c++17" "-Werror" "-Wall" "-Wextra" "-fno-strict-aliasing" "-fPIC" "-o" "/Users/runner/repos/starknet-rs/target/debug/build/starkware-crypto-sys-7e7a1853b703c604/out/lib/starkware-crypto/src/starkware/crypto/pedersen_hash.o" "-c" "lib/starkware-crypto/src/starkware/crypto/pedersen_hash.cc"
      cargo:warning=1 error generated.
      exit status: 1
      exit status: 0
      exit status: 0
    
      --- stderr
    
    opened by xJonathanLEI 1
  • Remove upstream endianness conversion?

    Remove upstream endianness conversion?

    After inspecting the upstream code, it's found that the library converts between little endian and host endian if they're different. This forces library users to be aware of host endianess.

    I'm gonna simply fork upstream and remove the conversion, and instead specifically document that all function inputs must be little-endian.

    Alternative solutions considered:

    • Explicitly document that function inputs must conform to host endianess

      Not good as it forces users to be aware of host endianess, which is mostly irrelevant today.

    • Use target_endian to convert to big endian first if the host is big-endian

      No need to fork upstream, but has unnecessary performance costs on big-endian systems.

    opened by xJonathanLEI 0
  • Unable to build with GCC 11

    Unable to build with GCC 11

    OS: Debian GNU/Linux 12 bookworm (testing, amd64) GCC version: 11.3.0

    error: failed to run custom build command for `starkware-crypto-sys v0.1.3`
    
    Caused by:
      process didn't exit successfully: `[...]/target/debug/build/starkware-crypto-sys-3a366d1558176dff/build-script-build` (exit status: 1)
      --- stdout
      TARGET = Some("x86_64-unknown-linux-gnu")
      HOST = Some("x86_64-unknown-linux-gnu")
      CXX_x86_64-unknown-linux-gnu = None
      CXX_x86_64_unknown_linux_gnu = None
      HOST_CXX = None
      CXX = None
      CXXFLAGS_x86_64-unknown-linux-gnu = None
      CXXFLAGS_x86_64_unknown_linux_gnu = None
      HOST_CXXFLAGS = None
      CXXFLAGS = None
      CRATE_CC_NO_DEFAULTS = None
      DEBUG = Some("true")
      running: "c++" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-static" "-I" "lib/starkware-crypto/src" "-Wall" "-Wextra" "-std=c++17" "-Werror" "-Wall" "-Wextra" "-fno-strict-aliasing" "-fPIC" "-o" "[...]/target/debug/build/starkware-crypto-sys-e7dc842a2c877560/out/lib/starkware-crypto/src/starkware/crypto/ecdsa.o" "-c" "lib/starkware-crypto/src/starkware/crypto/ecdsa.cc"
      running: "c++" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-static" "-I" "lib/starkware-crypto/src" "-Wall" "-Wextra" "-std=c++17" "-Werror" "-Wall" "-Wextra" "-fno-strict-aliasing" "-fPIC" "-o" "[...]/target/debug/build/starkware-crypto-sys-e7dc842a2c877560/out/lib/starkware-crypto/src/starkware/algebra/prime_field_element.o" "-c" "lib/starkware-crypto/src/starkware/algebra/prime_field_element.cc"
      exit status: 0
      running: "c++" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-static" "-I" "lib/starkware-crypto/src" "-Wall" "-Wextra" "-std=c++17" "-Werror" "-Wall" "-Wextra" "-fno-strict-aliasing" "-fPIC" "-o" "[...]/target/debug/build/starkware-crypto-sys-e7dc842a2c877560/out/lib/starkware-crypto/src/starkware/crypto/elliptic_curve_constants.o" "-c" "lib/starkware-crypto/src/starkware/crypto/elliptic_curve_constants.cc"
      exit status: 0
      running: "c++" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-static" "-I" "lib/starkware-crypto/src" "-Wall" "-Wextra" "-std=c++17" "-Werror" "-Wall" "-Wextra" "-fno-strict-aliasing" "-fPIC" "-o" "[...]/target/debug/build/starkware-crypto-sys-e7dc842a2c877560/out/lib/starkware-crypto/src/starkware/crypto/ffi/ecdsa.o" "-c" "lib/starkware-crypto/src/starkware/crypto/ffi/ecdsa.cc"
      exit status: 0
      running: "c++" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-static" "-I" "lib/starkware-crypto/src" "-Wall" "-Wextra" "-std=c++17" "-Werror" "-Wall" "-Wextra" "-fno-strict-aliasing" "-fPIC" "-o" "[...]/target/debug/build/starkware-crypto-sys-e7dc842a2c877560/out/lib/starkware-crypto/src/starkware/crypto/ffi/pedersen_hash.o" "-c" "lib/starkware-crypto/src/starkware/crypto/ffi/pedersen_hash.cc"
      exit status: 0
      running: "c++" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-static" "-I" "lib/starkware-crypto/src" "-Wall" "-Wextra" "-std=c++17" "-Werror" "-Wall" "-Wextra" "-fno-strict-aliasing" "-fPIC" "-o" "[...]/target/debug/build/starkware-crypto-sys-e7dc842a2c877560/out/lib/starkware-crypto/src/starkware/crypto/ffi/utils.o" "-c" "lib/starkware-crypto/src/starkware/crypto/ffi/utils.cc"
      cargo:warning=In file included from lib/starkware-crypto/src/starkware/crypto/pedersen_hash.h:4,
      cargo:warning=                 from lib/starkware-crypto/src/starkware/crypto/ffi/utils.h:6,
      cargo:warning=                 from lib/starkware-crypto/src/starkware/crypto/ffi/utils.cc:4:
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp: In instantiation of ‘constexpr gsl::span<T> gsl::span<T>::subspan(gsl::span<T>::index_type, gsl::span<T>::index_type) const [with T = gsl::byte; gsl::span<T>::index_type = long unsigned int]’:
      cargo:warning=lib/starkware-crypto/src/starkware/crypto/ffi/utils.cc:30:53:   required from here
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp:1726:15: error: comparison of unsigned expression in ‘>= 0’ is always true [-Werror=type-limits]
      cargo:warning= 1726 |             0 <= offset && offset <= this->size() &&
      cargo:warning=      |             ~~^~~~~~~~~
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp:579:50: note: in definition of macro ‘Expects’
      cargo:warning=  579 | # define Expects( x )  ::gsl::fail_fast_assert( (x) )
      cargo:warning=      |                                                  ^
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp:1727:15: error: comparison of unsigned expression in ‘>= 0’ is always true [-Werror=type-limits]
      cargo:warning= 1727 |             0 <= count  && count + offset <= this->size() );
      cargo:warning=      |             ~~^~~~~~~~
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp:579:50: note: in definition of macro ‘Expects’
      cargo:warning=  579 | # define Expects( x )  ::gsl::fail_fast_assert( (x) )
      cargo:warning=      |                                                  ^
      cargo:warning=cc1plus: all warnings being treated as errors
      exit status: 1
      running: "c++" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-static" "-I" "lib/starkware-crypto/src" "-Wall" "-Wextra" "-std=c++17" "-Werror" "-Wall" "-Wextra" "-fno-strict-aliasing" "-fPIC" "-o" "[...]/target/debug/build/starkware-crypto-sys-e7dc842a2c877560/out/lib/starkware-crypto/src/starkware/crypto/pedersen_hash.o" "-c" "lib/starkware-crypto/src/starkware/crypto/pedersen_hash.cc"
      cargo:warning=In file included from lib/starkware-crypto/src/starkware/crypto/pedersen_hash.h:4,
      cargo:warning=                 from lib/starkware-crypto/src/starkware/crypto/pedersen_hash.cc:1:
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp: In instantiation of ‘constexpr gsl::span<T> gsl::span<T>::subspan(gsl::span<T>::index_type) const [with T = const starkware::EcPoint<starkware::PrimeFieldElement>; gsl::span<T>::index_type = long unsigned int]’:
      cargo:warning=lib/starkware-crypto/src/starkware/crypto/pedersen_hash.cc:42:67:   required from here
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp:1719:20: error: comparison of unsigned expression in ‘>= 0’ is always true [-Werror=type-limits]
      cargo:warning= 1719 |         Expects( 0 <= offset && offset <= this->size() );
      cargo:warning=      |                  ~~^~~~~~~~~
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp:579:50: note: in definition of macro ‘Expects’
      cargo:warning=  579 | # define Expects( x )  ::gsl::fail_fast_assert( (x) )
      cargo:warning=      |                                                  ^
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp: In instantiation of ‘constexpr gsl::span<T> gsl::span<T>::subspan(gsl::span<T>::index_type, gsl::span<T>::index_type) const [with T = const starkware::EcPoint<starkware::PrimeFieldElement>; gsl::span<T>::index_type = long unsigned int]’:
      cargo:warning=lib/starkware-crypto/src/starkware/crypto/pedersen_hash.cc:45:57:   required from here
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp:1726:15: error: comparison of unsigned expression in ‘>= 0’ is always true [-Werror=type-limits]
      cargo:warning= 1726 |             0 <= offset && offset <= this->size() &&
      cargo:warning=      |             ~~^~~~~~~~~
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp:579:50: note: in definition of macro ‘Expects’
      cargo:warning=  579 | # define Expects( x )  ::gsl::fail_fast_assert( (x) )
      cargo:warning=      |                                                  ^
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp:1727:15: error: comparison of unsigned expression in ‘>= 0’ is always true [-Werror=type-limits]
      cargo:warning= 1727 |             0 <= count  && count + offset <= this->size() );
      cargo:warning=      |             ~~^~~~~~~~
      cargo:warning=lib/starkware-crypto/src/third_party/gsl/gsl-lite.hpp:579:50: note: in definition of macro ‘Expects’
      cargo:warning=  579 | # define Expects( x )  ::gsl::fail_fast_assert( (x) )
      cargo:warning=      |                                                  ^
      cargo:warning=cc1plus: all warnings being treated as errors
      exit status: 1
      exit status: 0
    
      --- stderr
    
    
      error occurred: Command "c++" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-static" "-I" "lib/starkware-crypto/src" "-Wall" "-Wextra" "-std=c++17" "-Werror" "-Wall" "-Wextra" "-fno-strict-aliasing" "-fPIC" "-o" "[...]/openlimits-crypto-com/target/debug/build/starkware-crypto-sys-e7dc842a2c877560/out/lib/starkware-crypto/src/starkware/crypto/ffi/utils.o" "-c" "lib/starkware-crypto/src/starkware/crypto/ffi/utils.cc" with args "c++" did not execute successfully (status code exit status: 1).
    
    opened by gsnoff 1
  • Unable to build on Windows

    Unable to build on Windows

    After the macOS build issue was resolved, it's discovered that the library also fails to build on Windows, for both x86_64-pc-windows-gnu and x86_64-pc-windows-msvc targets.

    Target triples to fix:

    • [ ] x86_64-pc-windows-msvc
    • [x] x86_64-pc-windows-gnu
    opened by xJonathanLEI 8
Owner
Jonathan LEI
:crab: Rewrite EVERYTHING in Rust :crab:
Jonathan LEI
A modern, portable, easy to use crypto library.

Sodium is a new, easy-to-use software library for encryption, decryption, signatures, password hashing and more. It is a portable, cross-compilable, i

Frank Denis 10.7k Jan 3, 2023
The parser library to parse messages from crypto-crawler.

crypto-msg-parser The parser library to parse messages from crypto-crawler. Architecture crypto-msg-parser is the parser library to parse messages fro

null 5 Jan 2, 2023
Safe, fast, small crypto using Rust

THE SOFTWARE IS PROVIDED "AS IS" AND BRIAN SMITH AND THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES

Brian Smith 3k Jan 2, 2023
Usable, easy and safe pure-Rust crypto

orion About Orion is a cryptography library written in pure Rust. It aims to provide easy and usable crypto while trying to minimize the use of unsafe

Johannes 476 Dec 22, 2022
Usable, easy and safe pure-Rust crypto

orion About Orion is a cryptography library written in pure Rust. It aims to provide easy and usable crypto while trying to minimize the use of unsafe

Orion - Rust cryptography library 477 Dec 29, 2022
Cross-chain hub for Crypto Asset on Polkadot

ChainX ChainX is a community-driven project built on the next-generation blockchain framework substrate, the largest Layer-2 network of Bitcoin using

ChainX 261 Dec 28, 2022
A young, simple and naive file crypto lib based on AES.

naive-file-crypto A young, simple and naive file crypto lib based on AES. The MAC implementation is not standard GCM, so it may be vulnerable. All cpu

DF_XYZ 1 Jan 16, 2022
Crypto in, power out

Cipo Crypto in, power out Cipo makes it easy to let users pay for electricity for their camper-van, electric cars, boat, caravan and other high load c

Jonny Heggheim 9 Dec 9, 2022
A boringssl-based rustls crypto provider

boring-rustls-provider This is supposed to be the start to a boringssl-based rustls crypto provider. Status This is just a dump of me figuring out how

Jan 5 Nov 28, 2023
Convenience crate for verifying crypto-signed messages

signature-verifier This crate provide an easy way to verify Solana and Ethereum wallet-signed messages. Installation Add the crate to your Cargo.toml

Tim Plotnikov 3 Apr 2, 2024
Core Rust-C FFI for Stackmate Wallet.

STACKMATE-CORE A Rust-C FFI library exposing composite functionality from rust-bitcoin & bdk; to create cross-platform descriptor wallets. Currently u

Vishal Menon 5 May 31, 2022
Sodium Oxide: Fast cryptographic library for Rust (bindings to libsodium)

sodiumoxide |Crate|Documentation|Gitter| |:---:|:-----------:|:--------:|:-----:|:------:|:----:| |||| NaCl (pronounced "salt") is a new easy-to-use h

sodiumoxide 642 Dec 17, 2022
rust-native-tls — Bindings for native TLS libraries

rust-native-tls Documentation An abstraction over platform-specific TLS implementations. Specifically, this crate uses SChannel on Windows (via the sc

Steven Fackler 371 Jan 8, 2023
OpenSSL bindings for Rust

rust-openssl OpenSSL bindings for the Rust programming language. Documentation. Release Support The current supported release of openssl is 0.10 and o

Steven Fackler 1k Jan 7, 2023
Python bindings for rage (age in Rust)

pyrage Python bindings for the Rust implementation of age. Index Installation Usage Development Licensing Installation You can install pyrage with pip

William Woodruff 14 Nov 23, 2022
Rust bindings for Solidity's AST and visitors

solc-ast solc-ast provides rust bindings for the solidity AST and visitors. The visitors were built to be 1-1 compatible with the visitors from solc.

Hari 35 May 7, 2023
Bindings to the macOS Security.framework

macOS/iOS Security framework for Rust Documentation Bindings to the Apple's Security.framework. Allows use of TLS and Keychain from Rust. License Lice

Kornel 172 Dec 24, 2022
OpenAI's tiktoken but with node bindings

⏳ tiktoken-node tiktoken is a fast BPE tokeniser for use with OpenAI's models. const tiktoken = require('tiktoken-node') let enc = tiktoken.getEncodin

Gabriel Francisco 9 Mar 23, 2023
Bindings to MLIR using the full C++ API

Mithril Oxide Mithril MLIR is hard to rust but we did it anyway. Rust bindings to MLIR via the C++ API. Project structure mithril-oxide: Our Rusty bin

Lambdaclass 6 Jun 10, 2023