Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements ToCanonicalJson for protocol parameters #461

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pallas-applying/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pallas-primitives = { version = "=0.25.0", path = "../pallas-primitives" }
pallas-traverse = { version = "=0.25.0", path = "../pallas-traverse" }
rand = "0.8"
hex = "0.4"
serde = { version = "1.0.136", optional = true, features = ["derive"] }
serde_json = { version = "1.0.79", optional = true }

[dev-dependencies]
hex = "0.4"
70 changes: 70 additions & 0 deletions pallas-applying/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
pub mod environment;
pub mod validation;

#[cfg(feature = "json")]
mod json;

pub use environment::*;
use pallas_addresses::{Address, ShelleyAddress, ShelleyPaymentPart};
use pallas_codec::{
Expand Down Expand Up @@ -342,3 +345,70 @@ pub fn compute_plutus_v2_script_hash(script: &PlutusV2Script) -> PolicyId {
payload.insert(0, 2);
pallas_crypto::hash::Hasher::<224>::hash(&payload)
}

/// Computes the greatest common divisor of two integers using Euclid's algorithm
/// (https://en.wikipedia.org/wiki/Euclidean_algorithm).
/// (Taken from: https://gist.github.com/victor-iyi/8a84185c1d52419b0d4915a648d5e3e1)
///
/// # Example
///
/// ```rust
/// assert_eq!(gcd(3, 5), 1);
///
/// assert_eq!(gcd(2 * 3 * 5 * 11 * 17, 3 * 7 * 11 * 13 * 19), 3 * 11);
/// ```
///
/// ## List of numbers.
///
/// ```rust
/// // Compute divisor one after the other.
/// let numbers: [u64; 4] = [3, 9, 21, 81];
///
/// // Method 1: Using for-loop.
/// let mut divisor: u64 = numbers[0];
/// for no in &numbers[1..] {
/// divisor = gcd(divisor, *no);
/// }
/// assert_eq!(divisor, 3);
///
/// // Method 2: Using iterator & fold.
/// let divisor: u64 = numbers.iter().fold(numbers[0], |acc, &x| gcd(acc, x));
/// assert_eq!(divisor, 3);
/// ```
pub fn gcd(mut n: u64, mut m: u64) -> u64 {
assert!(n != 0 && m != 0);
while m != 0 {
if m < n {
std::mem::swap(&mut m, &mut n);
}
m %= n;
}
n
}

#[test]
fn test_gcd() {
// Simple greatest common divisor.
assert_eq!(gcd(3, 5), 1);
assert_eq!(gcd(14, 15), 1);

// More complex greatest common divisor.
assert_eq!(gcd(2 * 3 * 5 * 11 * 17, 3 * 7 * 11 * 13 * 19), 3 * 11);
}

#[test]
fn test_multiple_gcd() {
// List of numbers.
let numbers: [u64; 4] = [3, 9, 21, 81];
// Compute divisor one after the other.
// Method 1: Using for-loop.
let mut divisor = numbers[0];
for no in &numbers[1..] {
divisor = gcd(divisor, *no);
}
assert_eq!(divisor, 3);

// Method 2: Using iterator & fold.
let divisor: u64 = numbers.iter().fold(numbers[0], |acc, &x| gcd(acc, x));
assert_eq!(divisor, 3);
}
Loading
Loading