Skip to content

Commit

Permalink
first jab at Rust bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Oct 21, 2023
1 parent 4ccd8aa commit f18a618
Show file tree
Hide file tree
Showing 20 changed files with 814 additions and 501 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,15 @@ jobs:
shell: bash
run: |
cd constantine
nimble bindings --verbose
nimble make_libs --verbose
nimble test_bindings --verbose
nimble test_parallel --verbose
- name: Run Constantine tests (UNIX no Assembly)
if: runner.os != 'Windows' && matrix.target.BACKEND == 'NO_ASM'
shell: bash
run: |
cd constantine
CTT_ASM=0 nimble bindings --verbose
CTT_ASM=0 nimble make_libs --verbose
nimble test_bindings --verbose
CTT_ASM=0 nimble test_parallel --verbose
- name: Run Constantine tests (Windows with Assembly)
Expand All @@ -250,7 +250,7 @@ jobs:
shell: msys2 {0}
run: |
cd constantine
nimble bindings --verbose
nimble make_libs --verbose
nimble test_bindings --verbose
nimble test_parallel_no_gmp --verbose
- name: Run Constantine tests (Windows no Assembly)
Expand All @@ -260,6 +260,6 @@ jobs:
shell: msys2 {0}
run: |
cd constantine
CTT_ASM=0 nimble bindings --verbose
CTT_ASM=0 nimble make_libs --verbose
nimble test_bindings --verbose
CTT_ASM=0 nimble test_parallel_no_gmp --verbose
30 changes: 24 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
nimcache/

# Executables shall be put in an ignored build/ directory
# Ignore dynamic, static libs and libtool archive files
build/
Expand All @@ -10,8 +8,28 @@ build/
*.exe
*.dll

# Sage
*.sage.py
# Nim
# -----------------------------------------------------------------------------------------
nimcache/

# Rust
# -----------------------------------------------------------------------------------------

# Tests
test_*.txt
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# Sage
# -----------------------------------------------------------------------------------------
*.sage.py
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[workspace]

members = [
"constantine-rust/ctt-curve-bls12-381",
"constantine-rust/ctt-curve-bn254-snarks",
"constantine-rust/ctt-curve-pasta",
"constantine-rust/ctt-proto-ethereum-bls-signatures",
]

[profile.dev]
lto = "thin" # The Nim static library is compiled with LTO, always enable it
58 changes: 29 additions & 29 deletions bindings_generators/constantine_bls12_381.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
import ./gen_bindings, ./gen_header

type
bls12381_fr = Fr[BLS12_381]
bls12381_fp = Fp[BLS12_381]
bls12381_fp2 = Fp2[BLS12_381]
bls12381_ec_g1_aff = ECP_ShortW_Aff[Fp[BLS12_381], G1]
bls12381_ec_g1_jac = ECP_ShortW_Jac[Fp[BLS12_381], G1]
bls12381_ec_g1_prj = ECP_ShortW_Prj[Fp[BLS12_381], G1]
bls12381_ec_g2_aff = ECP_ShortW_Aff[Fp2[BLS12_381], G2]
bls12381_ec_g2_jac = ECP_ShortW_Jac[Fp2[BLS12_381], G2]
bls12381_ec_g2_prj = ECP_ShortW_Prj[Fp2[BLS12_381], G2]
bls12_381_fr = Fr[BLS12_381]
bls12_381_fp = Fp[BLS12_381]
bls12_381_fp2 = Fp2[BLS12_381]
bls12_381_ec_g1_aff = ECP_ShortW_Aff[Fp[BLS12_381], G1]
bls12_381_ec_g1_jac = ECP_ShortW_Jac[Fp[BLS12_381], G1]
bls12_381_ec_g1_prj = ECP_ShortW_Prj[Fp[BLS12_381], G1]
bls12_381_ec_g2_aff = ECP_ShortW_Aff[Fp2[BLS12_381], G2]
bls12_381_ec_g2_jac = ECP_ShortW_Jac[Fp2[BLS12_381], G2]
bls12_381_ec_g2_prj = ECP_ShortW_Prj[Fp2[BLS12_381], G2]

collectBindings(cBindings):
genBindingsField(bls12381_fr)
genBindingsField(bls12381_fp)
genBindingsFieldSqrt(bls12381_fp)
genBindingsExtField(bls12381_fp2)
genBindingsExtFieldSqrt(bls12381_fp2)
genBindings_EC_ShortW_Affine(bls12381_ec_g1_aff, bls12381_fp)
genBindings_EC_ShortW_NonAffine(bls12381_ec_g1_jac, bls12381_ec_g1_aff, bls12381_fp)
genBindings_EC_ShortW_NonAffine(bls12381_ec_g1_prj, bls12381_ec_g1_aff, bls12381_fp)
genBindings_EC_ShortW_Affine(bls12381_ec_g2_aff, bls12381_fp2)
genBindings_EC_ShortW_NonAffine(bls12381_ec_g2_jac, bls12381_ec_g2_aff, bls12381_fp2)
genBindings_EC_ShortW_NonAffine(bls12381_ec_g2_prj, bls12381_ec_g2_aff, bls12381_fp2)
genBindingsField(bls12_381_fr)
genBindingsField(bls12_381_fp)
genBindingsFieldSqrt(bls12_381_fp)
genBindingsExtField(bls12_381_fp2)
genBindingsExtFieldSqrt(bls12_381_fp2)
genBindings_EC_ShortW_Affine(bls12_381_ec_g1_aff, bls12_381_fp)
genBindings_EC_ShortW_NonAffine(bls12_381_ec_g1_jac, bls12_381_ec_g1_aff, bls12_381_fp)
genBindings_EC_ShortW_NonAffine(bls12_381_ec_g1_prj, bls12_381_ec_g1_aff, bls12_381_fp)
genBindings_EC_ShortW_Affine(bls12_381_ec_g2_aff, bls12_381_fp2)
genBindings_EC_ShortW_NonAffine(bls12_381_ec_g2_jac, bls12_381_ec_g2_aff, bls12_381_fp2)
genBindings_EC_ShortW_NonAffine(bls12_381_ec_g2_prj, bls12_381_ec_g2_aff, bls12_381_fp2)

# Write header
when isMainModule and defined(CTT_GENERATE_HEADERS):
Expand All @@ -55,23 +55,23 @@ when isMainModule and defined(CTT_GENERATE_HEADERS):
header &= '\n'
header &= genWordsRequired()
header &= '\n'
header &= genField("bls12381_fr", BLS12_381.getCurveOrderBitWidth())
header &= genField("bls12_381_fr", BLS12_381.getCurveOrderBitWidth())
header &= '\n'
header &= genField("bls12381_fp", BLS12_381.getCurveBitWidth())
header &= genField("bls12_381_fp", BLS12_381.getCurveBitWidth())
header &= '\n'
header &= genExtField("bls12381_fp2", 2, "bls12381_fp")
header &= genExtField("bls12_381_fp2", 2, "bls12_381_fp")
header &= '\n'
header &= genEllipticCurvePoint("bls12381_ec_g1_aff", "x, y", "bls12381_fp")
header &= genEllipticCurvePoint("bls12_381_ec_g1_aff", "x, y", "bls12_381_fp")
header &= '\n'
header &= genEllipticCurvePoint("bls12381_ec_g1_jac", "x, y, z", "bls12381_fp")
header &= genEllipticCurvePoint("bls12_381_ec_g1_jac", "x, y, z", "bls12_381_fp")
header &= '\n'
header &= genEllipticCurvePoint("bls12381_ec_g1_prj", "x, y, z", "bls12381_fp")
header &= genEllipticCurvePoint("bls12_381_ec_g1_prj", "x, y, z", "bls12_381_fp")
header &= '\n'
header &= genEllipticCurvePoint("bls12381_ec_g2_aff", "x, y", "bls12381_fp2")
header &= genEllipticCurvePoint("bls12_381_ec_g2_aff", "x, y", "bls12_381_fp2")
header &= '\n'
header &= genEllipticCurvePoint("bls12381_ec_g2_jac", "x, y, z", "bls12381_fp2")
header &= genEllipticCurvePoint("bls12_381_ec_g2_jac", "x, y, z", "bls12_381_fp2")
header &= '\n'
header &= genEllipticCurvePoint("bls12381_ec_g2_prj", "x, y, z", "bls12381_fp2")
header &= genEllipticCurvePoint("bls12_381_ec_g2_prj", "x, y, z", "bls12_381_fp2")
header &= '\n'
header &= declNimMain("bls12381")
header &= '\n'
Expand Down
62 changes: 31 additions & 31 deletions bindings_generators/constantine_bn254_snarks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
import ./gen_bindings, ./gen_header

type
bn254snarks_fr = Fr[BN254_Snarks]
bn254snarks_fp = Fp[BN254_Snarks]
bn254snarks_fp2 = Fp2[BN254_Snarks]
bn254snarks_ec_g1_aff = ECP_ShortW_Aff[Fp[BN254_Snarks], G1]
bn254snarks_ec_g1_jac = ECP_ShortW_Jac[Fp[BN254_Snarks], G1]
bn254snarks_ec_g1_prj = ECP_ShortW_Prj[Fp[BN254_Snarks], G1]
bn254snarks_ec_g2_aff = ECP_ShortW_Aff[Fp2[BN254_Snarks], G2]
bn254snarks_ec_g2_jac = ECP_ShortW_Jac[Fp2[BN254_Snarks], G2]
bn254snarks_ec_g2_prj = ECP_ShortW_Prj[Fp2[BN254_Snarks], G2]
bn254_snarks_fr = Fr[BN254_Snarks]
bn254_snarks_fp = Fp[BN254_Snarks]
bn254_snarks_fp2 = Fp2[BN254_Snarks]
bn254_snarks_ec_g1_aff = ECP_ShortW_Aff[Fp[BN254_Snarks], G1]
bn254_snarks_ec_g1_jac = ECP_ShortW_Jac[Fp[BN254_Snarks], G1]
bn254_snarks_ec_g1_prj = ECP_ShortW_Prj[Fp[BN254_Snarks], G1]
bn254_snarks_ec_g2_aff = ECP_ShortW_Aff[Fp2[BN254_Snarks], G2]
bn254_snarks_ec_g2_jac = ECP_ShortW_Jac[Fp2[BN254_Snarks], G2]
bn254_snarks_ec_g2_prj = ECP_ShortW_Prj[Fp2[BN254_Snarks], G2]

collectBindings(cBindings):
genBindingsField(bn254snarks_fr)
genBindingsField(bn254snarks_fp)
genBindingsFieldSqrt(bn254snarks_fp)
genBindingsExtField(bn254snarks_fp2)
genBindingsExtFieldSqrt(bn254snarks_fp2)
genBindings_EC_ShortW_Affine(bn254snarks_ec_g1_aff, bn254snarks_fp)
genBindings_EC_ShortW_NonAffine(bn254snarks_ec_g1_jac, bn254snarks_ec_g1_aff, bn254snarks_fp)
genBindings_EC_ShortW_NonAffine(bn254snarks_ec_g1_prj, bn254snarks_ec_g1_aff, bn254snarks_fp)
genBindings_EC_ShortW_Affine(bn254snarks_ec_g2_aff, bn254snarks_fp2)
genBindings_EC_ShortW_NonAffine(bn254snarks_ec_g2_jac, bn254snarks_ec_g2_aff, bn254snarks_fp2)
genBindings_EC_ShortW_NonAffine(bn254snarks_ec_g2_prj, bn254snarks_ec_g2_aff, bn254snarks_fp2)
genBindingsField(bn254_snarks_fr)
genBindingsField(bn254_snarks_fp)
genBindingsFieldSqrt(bn254_snarks_fp)
genBindingsExtField(bn254_snarks_fp2)
genBindingsExtFieldSqrt(bn254_snarks_fp2)
genBindings_EC_ShortW_Affine(bn254_snarks_ec_g1_aff, bn254_snarks_fp)
genBindings_EC_ShortW_NonAffine(bn254_snarks_ec_g1_jac, bn254_snarks_ec_g1_aff, bn254_snarks_fp)
genBindings_EC_ShortW_NonAffine(bn254_snarks_ec_g1_prj, bn254_snarks_ec_g1_aff, bn254_snarks_fp)
genBindings_EC_ShortW_Affine(bn254_snarks_ec_g2_aff, bn254_snarks_fp2)
genBindings_EC_ShortW_NonAffine(bn254_snarks_ec_g2_jac, bn254_snarks_ec_g2_aff, bn254_snarks_fp2)
genBindings_EC_ShortW_NonAffine(bn254_snarks_ec_g2_prj, bn254_snarks_ec_g2_aff, bn254_snarks_fp2)

# Write header
when isMainModule and defined(CTT_GENERATE_HEADERS):
Expand All @@ -55,31 +55,31 @@ when isMainModule and defined(CTT_GENERATE_HEADERS):
header &= '\n'
header &= genWordsRequired()
header &= '\n'
header &= genField("bn254snarks_fr", BN254_Snarks.getCurveOrderBitWidth())
header &= genField("bn254_snarks_fr", BN254_Snarks.getCurveOrderBitWidth())
header &= '\n'
header &= genField("bn254snarks_fp", BN254_Snarks.getCurveBitWidth())
header &= genField("bn254_snarks_fp", BN254_Snarks.getCurveBitWidth())
header &= '\n'
header &= genExtField("bn254snarks_fp2", 2, "bn254snarks_fp")
header &= genExtField("bn254_snarks_fp2", 2, "bn254_snarks_fp")
header &= '\n'
header &= genEllipticCurvePoint("bn254snarks_ec_g1_aff", "x, y", "bn254snarks_fp")
header &= genEllipticCurvePoint("bn254_snarks_ec_g1_aff", "x, y", "bn254_snarks_fp")
header &= '\n'
header &= genEllipticCurvePoint("bn254snarks_ec_g1_jac", "x, y, z", "bn254snarks_fp")
header &= genEllipticCurvePoint("bn254_snarks_ec_g1_jac", "x, y, z", "bn254_snarks_fp")
header &= '\n'
header &= genEllipticCurvePoint("bn254snarks_ec_g1_prj", "x, y, z", "bn254snarks_fp")
header &= genEllipticCurvePoint("bn254_snarks_ec_g1_prj", "x, y, z", "bn254_snarks_fp")
header &= '\n'
header &= genEllipticCurvePoint("bn254snarks_ec_g2_aff", "x, y", "bn254snarks_fp2")
header &= genEllipticCurvePoint("bn254_snarks_ec_g2_aff", "x, y", "bn254_snarks_fp2")
header &= '\n'
header &= genEllipticCurvePoint("bn254snarks_ec_g2_jac", "x, y, z", "bn254snarks_fp2")
header &= genEllipticCurvePoint("bn254_snarks_ec_g2_jac", "x, y, z", "bn254_snarks_fp2")
header &= '\n'
header &= genEllipticCurvePoint("bn254snarks_ec_g2_prj", "x, y, z", "bn254snarks_fp2")
header &= genEllipticCurvePoint("bn254_snarks_ec_g2_prj", "x, y, z", "bn254_snarks_fp2")
header &= '\n'
header &= declNimMain("bn254snarks")
header &= declNimMain("bn254_snarks")
header &= '\n'
header &= cBindings
header &= '\n'

header = genCpp(header)
header = genHeader("BN@%$SNARKS", header)
header = genHeader("BN254SNARKS", header)
header = genHeaderLicense() & header

writeFile(dir/"constantine_bn254_snarks.h", header)
Expand Down
9 changes: 9 additions & 0 deletions constantine-rust/ctt-curve-bls12-381/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ctt-curve-bls12-381"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

35 changes: 35 additions & 0 deletions constantine-rust/ctt-curve-bls12-381/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::env;
use std::path::PathBuf;
use std::process::{Command, Stdio};

const LIB_NAME: &str = "constantine_bls12_381";

fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let cargo_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let root_dir = cargo_dir
.parent()
.expect("rust library is nested")
.parent()
.expect("constantine-rust is nested");

let rust_lib_name = cargo_dir
.file_name()
.expect("Directory exist");

println!("Building Constantine library ...");

Command::new("nimble")
.arg("make_lib_rust")
.env("CTT_RUST_LIB", rust_lib_name)
.current_dir(root_dir)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.status()
.expect("failed to execute process");

println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=static={}", LIB_NAME);
// Avoid full recompilation
// println!("cargo:rerun-if-changed={}", root_dir.join("constantine").display());
}
14 changes: 14 additions & 0 deletions constantine-rust/ctt-curve-bls12-381/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
8 changes: 8 additions & 0 deletions constantine-rust/ctt-curve-bn254-snarks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "ctt-curve-bn254-snarks"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
35 changes: 35 additions & 0 deletions constantine-rust/ctt-curve-bn254-snarks/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::env;
use std::path::PathBuf;
use std::process::{Command, Stdio};

const LIB_NAME: &str = "constantine_bn254_snarks";

fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let cargo_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let root_dir = cargo_dir
.parent()
.expect("rust library is nested")
.parent()
.expect("constantine-rust is nested");

let rust_lib_name = cargo_dir
.file_name()
.expect("Directory exist");

println!("Building Constantine library ...");

Command::new("nimble")
.arg("make_lib_rust")
.env("CTT_RUST_LIB", rust_lib_name)
.current_dir(root_dir)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.status()
.expect("failed to execute process");

println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=static={}", LIB_NAME);
// Avoid full recompilation
// println!("cargo:rerun-if-changed={}", root_dir.join("constantine").display());
}
14 changes: 14 additions & 0 deletions constantine-rust/ctt-curve-bn254-snarks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
9 changes: 9 additions & 0 deletions constantine-rust/ctt-curve-pasta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ctt-curve-pasta"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

Loading

0 comments on commit f18a618

Please sign in to comment.