Skip to content

Commit

Permalink
Implement libsodium vrf signature verification
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewWestberg committed Sep 5, 2024
1 parent 0f13f1c commit 20ecf3a
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ jobs:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install build dependencies macOS
if: matrix.os == 'macOS-latest'
run: brew install autoconf automake libtool

- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable
with:
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "pallas-crypto/contrib/libsodium"]
path = pallas-crypto/contrib/libsodium
url = https://github.com/input-output-hk/libsodium
12 changes: 11 additions & 1 deletion pallas-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ homepage = "https://github.com/txpipe/pallas"
documentation = "https://docs.rs/pallas-crypto"
license = "Apache-2.0"
readme = "README.md"
authors = ["Nicolas Di Prima <[email protected]>"]
authors = [
"Nicolas Di Prima <[email protected]>",
"Andrew Westberg <[email protected]>",
]
build = "build.rs"

[dependencies]
hex = "0.4"
Expand All @@ -24,3 +28,9 @@ quickcheck = "1.0"
quickcheck_macros = "1.0"
rand = "0.8"
serde_test = "1.0.143"

[build-dependencies]
autotools = "0.2"
pkg-config = "0.3"
cc = "1.1"
regex = "1.10"
2 changes: 1 addition & 1 deletion pallas-crypto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Crate with all the cryptographic material to support Cardano protocol:
- [x] Ed25519 Extended asymmetric key pair
- [ ] Bip32-Ed25519 key derivation
- [ ] BIP39 mnemonics
- [ ] VRF
- [x] VRF
- [ ] KES
- [ ] SECP256k1
- [x] Nonce calculations
205 changes: 205 additions & 0 deletions pallas-crypto/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
use std::process::Command;

macro_rules! ok (($expression:expr) => ($expression.unwrap()));
macro_rules! log {
($fmt:expr) => (println!(concat!("pallas-crypto/build.rs:{}: ", $fmt), line!()));
($fmt:expr, $($arg:tt)*) => (println!(concat!("pallas-crypto/build.rs:{}: ", $fmt),
line!(), $($arg)*));
}

fn main() {
// Build and link libsodium
run("git", |command| {
command
.arg("submodule")
.arg("update")
.arg("--init")
.arg("--recursive")
.arg("--force")
});

// if windows
#[cfg(target_os = "windows")]
{
// Build libsodium automatically (as part of rust build)
generate_version_h();
cc::Build::new()
.include("contrib/libsodium/src/libsodium/include")
.include("contrib/libsodium/src/libsodium/include/sodium")
.file("contrib/libsodium/src/libsodium/crypto_auth/hmacsha512/auth_hmacsha512.c")
.file("contrib/libsodium/src/libsodium/crypto_auth/crypto_auth.c")
.file("contrib/libsodium/src/libsodium/crypto_auth/hmacsha512256/auth_hmacsha512256.c")
.file("contrib/libsodium/src/libsodium/crypto_auth/hmacsha256/auth_hmacsha256.c")
.file("contrib/libsodium/src/libsodium/crypto_secretbox/crypto_secretbox.c")
.file("contrib/libsodium/src/libsodium/crypto_secretbox/xchacha20poly1305/secretbox_xchacha20poly1305.c")
.file("contrib/libsodium/src/libsodium/crypto_secretbox/xsalsa20poly1305/secretbox_xsalsa20poly1305.c")
.file("contrib/libsodium/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c")
.file("contrib/libsodium/src/libsodium/crypto_core/salsa/ref/core_salsa_ref.c")
.file("contrib/libsodium/src/libsodium/crypto_core/hsalsa20/ref2/core_hsalsa20_ref2.c")
.file("contrib/libsodium/src/libsodium/crypto_core/hsalsa20/core_hsalsa20.c")
.file("contrib/libsodium/src/libsodium/crypto_core/hchacha20/core_hchacha20.c")
.file("contrib/libsodium/src/libsodium/crypto_core/ed25519/core_h2c.c")
.file("contrib/libsodium/src/libsodium/crypto_core/ed25519/core_ristretto255.c")
.file("contrib/libsodium/src/libsodium/crypto_core/ed25519/core_ed25519.c")
.file("contrib/libsodium/src/libsodium/crypto_core/ed25519/ref10/ed25519_ref10.c")
.file("contrib/libsodium/src/libsodium/crypto_hash/sha256/hash_sha256.c")
.file("contrib/libsodium/src/libsodium/crypto_hash/sha256/cp/hash_sha256_cp.c")
.file("contrib/libsodium/src/libsodium/crypto_hash/sha512/cp/hash_sha512_cp.c")
.file("contrib/libsodium/src/libsodium/crypto_hash/sha512/hash_sha512.c")
.file("contrib/libsodium/src/libsodium/crypto_hash/crypto_hash.c")
.file("contrib/libsodium/src/libsodium/crypto_onetimeauth/crypto_onetimeauth.c")
.file("contrib/libsodium/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna.c")
.file("contrib/libsodium/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c")
.file("contrib/libsodium/src/libsodium/crypto_onetimeauth/poly1305/onetimeauth_poly1305.c")
.file("contrib/libsodium/src/libsodium/crypto_vrf/crypto_vrf.c")
.file("contrib/libsodium/src/libsodium/crypto_vrf/ietfdraft03/verify.c")
.file("contrib/libsodium/src/libsodium/crypto_vrf/ietfdraft03/prove.c")
.file("contrib/libsodium/src/libsodium/crypto_vrf/ietfdraft03/vrf.c")
.file("contrib/libsodium/src/libsodium/crypto_vrf/ietfdraft13/verify.c")
.file("contrib/libsodium/src/libsodium/crypto_vrf/ietfdraft13/prove.c")
.file("contrib/libsodium/src/libsodium/crypto_vrf/ietfdraft13/vrf.c")
.file("contrib/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe_frombytes_sandy2x.c")
.file("contrib/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/curve25519_sandy2x.c")
.file("contrib/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_invert.c")
.file("contrib/libsodium/src/libsodium/crypto_scalarmult/curve25519/scalarmult_curve25519.c")
.file("contrib/libsodium/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.c")
.file("contrib/libsodium/src/libsodium/crypto_scalarmult/ed25519/ref10/scalarmult_ed25519_ref10.c")
.file("contrib/libsodium/src/libsodium/crypto_scalarmult/ristretto255/ref10/scalarmult_ristretto255_ref10.c")
.file("contrib/libsodium/src/libsodium/crypto_scalarmult/crypto_scalarmult.c")
.file("contrib/libsodium/src/libsodium/crypto_sign/crypto_sign.c")
.file("contrib/libsodium/src/libsodium/crypto_sign/ed25519/sign_ed25519.c")
.file("contrib/libsodium/src/libsodium/crypto_sign/ed25519/ref10/obsolete.c")
.file("contrib/libsodium/src/libsodium/crypto_sign/ed25519/ref10/sign.c")
.file("contrib/libsodium/src/libsodium/crypto_sign/ed25519/ref10/open.c")
.file("contrib/libsodium/src/libsodium/crypto_sign/ed25519/ref10/keypair.c")
.file("contrib/libsodium/src/libsodium/crypto_generichash/crypto_generichash.c")
.file("contrib/libsodium/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-sse41.c")
.file("contrib/libsodium/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-avx2.c")
.file("contrib/libsodium/src/libsodium/crypto_generichash/blake2b/ref/generichash_blake2b.c")
.file("contrib/libsodium/src/libsodium/crypto_generichash/blake2b/ref/blake2b-ref.c")
.file("contrib/libsodium/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ref.c")
.file("contrib/libsodium/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ssse3.c")
.file("contrib/libsodium/src/libsodium/crypto_generichash/blake2b/generichash_blake2.c")
.file("contrib/libsodium/src/libsodium/crypto_secretstream/xchacha20poly1305/secretstream_xchacha20poly1305.c")
.file("contrib/libsodium/src/libsodium/crypto_kdf/blake2b/kdf_blake2b.c")
.file("contrib/libsodium/src/libsodium/crypto_kdf/crypto_kdf.c")
.file("contrib/libsodium/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c")
.file("contrib/libsodium/src/libsodium/randombytes/randombytes.c")
.file("contrib/libsodium/src/libsodium/randombytes/internal/randombytes_internal_random.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/crypto_pwhash.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-encoding.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/argon2/pwhash_argon2i.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx2.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ref.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx512f.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/argon2/blake2b-long.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/argon2/pwhash_argon2id.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-core.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/argon2/argon2.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/scrypt_platform.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/sse/pwhash_scryptsalsa208sha256_sse.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/crypto_scrypt-common.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/pbkdf2-sha256.c")
.file("contrib/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c")
.file("contrib/libsodium/src/libsodium/crypto_kx/crypto_kx.c")
.file("contrib/libsodium/src/libsodium/sodium/core.c")
.file("contrib/libsodium/src/libsodium/sodium/utils.c")
.file("contrib/libsodium/src/libsodium/sodium/runtime.c")
.file("contrib/libsodium/src/libsodium/sodium/version.c")
.file("contrib/libsodium/src/libsodium/sodium/codecs.c")
.file("contrib/libsodium/src/libsodium/crypto_aead/xchacha20poly1305/sodium/aead_xchacha20poly1305.c")
.file("contrib/libsodium/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c")
.file("contrib/libsodium/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c")
.file("contrib/libsodium/src/libsodium/crypto_box/crypto_box.c")
.file("contrib/libsodium/src/libsodium/crypto_box/curve25519xsalsa20poly1305/box_curve25519xsalsa20poly1305.c")
.file("contrib/libsodium/src/libsodium/crypto_box/curve25519xchacha20poly1305/box_curve25519xchacha20poly1305.c")
.file("contrib/libsodium/src/libsodium/crypto_box/curve25519xchacha20poly1305/box_seal_curve25519xchacha20poly1305.c")
.file("contrib/libsodium/src/libsodium/crypto_box/crypto_box_easy.c")
.file("contrib/libsodium/src/libsodium/crypto_box/crypto_box_seal.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/salsa208/ref/stream_salsa208_ref.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/salsa208/stream_salsa208.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/xsalsa20/stream_xsalsa20.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/chacha20/ref/chacha20_ref.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/chacha20/stream_chacha20.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/chacha20/dolbeau/chacha20_dolbeau-ssse3.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/chacha20/dolbeau/chacha20_dolbeau-avx2.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/crypto_stream.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/salsa20/ref/salsa20_ref.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/salsa20/stream_salsa20.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/salsa20/xmm6int/salsa20_xmm6int-avx2.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/salsa20/xmm6int/salsa20_xmm6int-sse2.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/salsa20/xmm6/salsa20_xmm6.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/xchacha20/stream_xchacha20.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/salsa2012/ref/stream_salsa2012_ref.c")
.file("contrib/libsodium/src/libsodium/crypto_stream/salsa2012/stream_salsa2012.c")
.file("contrib/libsodium/src/libsodium/crypto_verify/sodium/verify.c")
.file("contrib/libsodium/src/libsodium/crypto_shorthash/siphash24/ref/shorthash_siphash24_ref.c")
.file("contrib/libsodium/src/libsodium/crypto_shorthash/siphash24/ref/shorthash_siphashx24_ref.c")
.file("contrib/libsodium/src/libsodium/crypto_shorthash/siphash24/shorthash_siphash24.c")
.file("contrib/libsodium/src/libsodium/crypto_shorthash/siphash24/shorthash_siphashx24.c")
.file("contrib/libsodium/src/libsodium/crypto_shorthash/crypto_shorthash.c")
.compile("sodium");
}

// if not windows
#[cfg(not(target_os = "windows"))]
{
// Build libsodium automatically (as part of rust build)
let libsodium = autotools::Config::new("contrib/libsodium/")
.reconf("-vfi")
.enable_static()
.disable_shared()
.build();
println!(
"cargo:rustc-link-search=native={}",
libsodium.join("lib").display()
);
}
println!("cargo:rustc-link-lib=static=sodium");
println!("cargo:rerun-if-changed=build.rs");
}

#[cfg(target_os = "windows")]
fn generate_version_h() {
let configure_ac = "contrib/libsodium/configure.ac";
let version_h_in = "contrib/libsodium/src/libsodium/include/sodium/version.h.in";
let version_h = "contrib/libsodium/src/libsodium/include/sodium/version.h";

let configure_content = fs::read_to_string(configure_ac).expect("Failed to read configure.ac");

let version = extract_version(&configure_content, r"AC_INIT\(\[libsodium\],\[(.*?)\],");
let major = extract_version(&configure_content, r"SODIUM_LIBRARY_VERSION_MAJOR=(.*?)");
let minor = extract_version(&configure_content, r"SODIUM_LIBRARY_VERSION_MINOR=(.*?)");

let content = fs::read_to_string(version_h_in).expect("Failed to read version.h.in");
let content = content
.replace("@VERSION@", &version)
.replace("@SODIUM_LIBRARY_VERSION_MAJOR@", &major)
.replace("@SODIUM_LIBRARY_VERSION_MINOR@", &minor)
.replace("@SODIUM_LIBRARY_MINIMAL_DEF@", "#define SODIUM_LIBRARY_MINIMAL 1");

fs::write(version_h, content).expect("Failed to write version.h");
}

#[cfg(target_os = "windows")]
fn extract_version(content: &str, pattern: &str) -> String {
let re = regex::Regex::new(pattern).expect("Invalid regex pattern");
re.captures(content)
.and_then(|caps| caps.get(1).map(|m| m.as_str().to_string()))
.expect("Failed to extract version")
}

fn run<F>(name: &str, mut configure: F)
where
F: FnMut(&mut Command) -> &mut Command,
{
let mut command = Command::new(name);
let configured = configure(&mut command);
log!("Executing {:?}", configured);
if !ok!(configured.status()).success() {
panic!("failed to execute {:?}", configured);
}
log!("Command {:?} finished successfully", configured);
}
1 change: 1 addition & 0 deletions pallas-crypto/contrib/libsodium
Submodule libsodium added at dbb48c
1 change: 1 addition & 0 deletions pallas-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod hash;
pub mod key;
pub mod memsec;
pub mod nonce;
pub mod vrf;
Loading

0 comments on commit 20ecf3a

Please sign in to comment.