Skip to content

Commit

Permalink
Implement ToCanonicalJson for protocol params
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantumplation committed May 19, 2024
1 parent a89c526 commit 20455f9
Show file tree
Hide file tree
Showing 3 changed files with 599 additions and 0 deletions.
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 @@ -336,3 +339,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

0 comments on commit 20455f9

Please sign in to comment.