-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into v2.0-testnet
- Loading branch information
Showing
77 changed files
with
2,833 additions
and
3,024 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
description = "Conflux math library" | ||
homepage = "https://www.confluxnetwork.org" | ||
license = "GPL-3.0" | ||
name = "cfx-math" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
num = "0.2" | ||
cfx-types = { path = "../cfx_types" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
extern crate cfx_types; | ||
extern crate num; | ||
|
||
use cfx_types::U256; | ||
use num::integer::Roots; | ||
|
||
pub fn sqrt_u256(input: U256) -> U256 { | ||
let bits = input.bits(); | ||
if bits <= 64 { | ||
return input.as_u64().sqrt().into(); | ||
} | ||
|
||
/************************************************************ | ||
** Step 1: pick the most significant 64 bits and estimate an | ||
** approximate root. | ||
************************************************************ | ||
**/ | ||
let significant_bits = 64 - bits % 2; | ||
// The `rest_bits` must be even number. | ||
let rest_bits = bits - significant_bits; | ||
// The `input >> rest_bits` has `significant_bits` | ||
let significant_word = (input >> rest_bits).as_u64(); | ||
// The `init_root` is slightly larger than the correct root. | ||
let init_root = | ||
U256::from(significant_word.sqrt() + 1u64) << (rest_bits / 2); | ||
|
||
/****************************************************************** | ||
** Step 2: use the Newton's method to estimate the accurate value. | ||
****************************************************************** | ||
**/ | ||
let mut root = init_root; | ||
// Will iterate for at most 4 rounds. | ||
while root * root > input { | ||
root = (input / root + root) / 2; | ||
} | ||
|
||
root | ||
} | ||
|
||
pub fn power_two_fractional(ratio: u64, increase: bool, precision: u8) -> U256 { | ||
assert!(precision <= 127); | ||
|
||
let mut base = U256::one(); | ||
base <<= 254usize; | ||
|
||
for i in 0..64u64 { | ||
if ratio & (1 << i) != 0 { | ||
if increase { | ||
base <<= 1usize; | ||
} else { | ||
base >>= 1usize; | ||
} | ||
} | ||
base = sqrt_u256(base); | ||
base <<= 127usize; | ||
} | ||
|
||
base >>= (254 - precision) as usize; | ||
// Computing error < 5.2 * 2 ^ -127 | ||
base | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.