This repository has been archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Primality testing #67
Open
lovesh
wants to merge
10
commits into
hyperledger-indy:master
Choose a base branch
from
lovesh:primality
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e649ef6
expose primality check
f5cfc68
work with big numbers
ee46238
fix test
8a1949e
add debugging info
86b4732
Merge branch 'master' into primality
lovesh db106a1
fix test
061233a
Merge branch 'primality' of github.com:lovesh/indy-crypto into primality
6c61327
Merge branch 'master' into primality
lovesh fa6a723
Merge branch 'master' into primality
lovesh 0dabe68
Merge branch 'master' into primality
lovesh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,78 @@ | ||
use bn::BigNumber; | ||
|
||
use ffi::ErrorCode; | ||
use errors::ToErrorCode; | ||
use std::slice; | ||
|
||
#[no_mangle] | ||
pub extern fn indy_crypto_primality_check(big_endian_number: *const u8, | ||
size_in_bytes: usize, | ||
is_prime: *mut bool) -> ErrorCode { | ||
trace!("indy_crypto_primality_check: >>> big_endian_number: {:?}, size_in_bytes: {:?}, is_prime: {:?}", big_endian_number, size_in_bytes, is_prime); | ||
println!("before {:?}", big_endian_number); | ||
check_useful_c_byte_array!(big_endian_number, size_in_bytes, ErrorCode::CommonInvalidParam1, ErrorCode::CommonInvalidParam2); | ||
check_useful_c_ptr!(is_prime, ErrorCode::CommonInvalidParam3); | ||
|
||
trace!("indy_crypto_primality_check: big_endian_number: {:?}, size_in_bytes: {:?}, is_prime: {:?}", big_endian_number, size_in_bytes, is_prime); | ||
|
||
println!("after {:?}", big_endian_number); | ||
let res = match BigNumber::from_bytes(&big_endian_number) { | ||
Ok(big_number) => { | ||
match big_number.is_prime(None) { | ||
Ok(valid) => { | ||
println!("{:?}", big_number); | ||
println!("to bytes {:?}", big_number.to_bytes()); | ||
trace!("indy_crypto_primality_check: big_endian_number: {:?}", big_number); | ||
unsafe { *is_prime = valid; } | ||
ErrorCode::Success | ||
} | ||
Err(err) => err.to_error_code() | ||
} | ||
|
||
} | ||
Err(err) => err.to_error_code() | ||
}; | ||
|
||
trace!("indy_crypto_primality_check: <<< res: {:?}", res); | ||
res | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn indy_crypto_primality_check_works() { | ||
let number1 = vec![29]; | ||
let mut valid = false; | ||
let err_code = indy_crypto_primality_check(number1.as_ptr(),1, &mut valid); | ||
assert_eq!(err_code, ErrorCode::Success); | ||
assert!(valid); | ||
|
||
let number2 = vec![1, 153, 25]; // number 104729 | ||
let err_code = indy_crypto_primality_check(number2.as_ptr(), 3,&mut valid); | ||
assert_eq!(err_code, ErrorCode::Success); | ||
assert!(valid); | ||
|
||
let number3 = vec![9, 252, 51, 8, 129]; // number 42885908609 | ||
let err_code = indy_crypto_primality_check(number3.as_ptr(),5, &mut valid); | ||
assert_eq!(err_code, ErrorCode::Success); | ||
assert!(valid); | ||
|
||
let number4 = [116, 9, 191, 244, 10]; // number 47055833460 | ||
let err_code = indy_crypto_primality_check(number4.as_ptr(),5, &mut valid); | ||
assert_eq!(err_code, ErrorCode::Success); | ||
assert!(!valid); | ||
|
||
let number6 = [204, 248, 234, 50, 24, 50, 123, 244, 109, 76, 16, 66, 12, 245, 54, 77]; // number 272454950813783527414999934504692692557 | ||
let err_code = indy_crypto_primality_check(number6.as_ptr(),16, &mut valid); | ||
assert_eq!(err_code, ErrorCode::Success); | ||
assert!(valid); | ||
|
||
// let number5 = vec![175, 151, 131, 108, 102, 141, 162, 107, 99, 34, 90, 210, 161, 21, 95, 135, 74, 195, 151, 217, 185, 90, 220, 50, 204, 96, 223, 214, 10, 240, 182, 15].as_ptr(); // number 79422449460098942399106282402512198969536520971550757303162642879618420356623 | ||
let number5 = vec![175, 151, 131, 108, 102, 141, 162, 107, 99, 34, 90, 210, 161, 21, 95, 135, 74, 195, 151, 217, 185, 90, 220, 50, 204, 96, 223, 214, 10, 240, 182, 15]; // number 79422449460098942399106282402512198969536520971550757303162642879618420356623 | ||
let err_code = indy_crypto_primality_check(number5.as_ptr(),32, &mut valid); | ||
assert_eq!(err_code, ErrorCode::Success); | ||
assert!(valid); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod cl; | ||
pub mod bls; | ||
pub mod bn; | ||
|
||
use env_logger; | ||
|
||
|
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 |
---|---|---|
|
@@ -120,4 +120,4 @@ macro_rules! check_useful_c_str { | |
return $e | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
from indy_crypto.error import IndyCryptoError | ||
from indy_crypto.error import IndyCryptoError |
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,32 @@ | ||
import logging | ||
import math | ||
from ctypes import c_bool, byref, c_int8 | ||
|
||
from .lib import do_call | ||
|
||
|
||
class BigNumber: | ||
@staticmethod | ||
def is_prime(number) -> bool: | ||
logger = logging.getLogger(__name__) | ||
logger.debug( | ||
"BigNumber::is_prime: >>> number: %r", number) | ||
|
||
if not isinstance(number, int) or number < 1: | ||
raise ValueError('Need a positive integer, not {}', number) | ||
|
||
int_array = list( | ||
number.to_bytes(math.ceil(number.bit_length() / 8), | ||
'big') or b'\0') | ||
logger.debug("BigNumber::is_prime: <<< array is: %r", int_array) | ||
array_size = len(int_array) | ||
pointer = (c_int8 * array_size)(*int_array) | ||
valid = c_bool() | ||
do_call('indy_crypto_primality_check', | ||
pointer, | ||
array_size, | ||
byref(valid)) | ||
|
||
res = valid | ||
logger.debug("BigNumber::is_prime: <<< res: %r", res) | ||
return res |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import logging | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
logging.basicConfig(level=logging.DEBUG) |
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,33 @@ | ||
import pytest | ||
|
||
from indy_crypto.big_number import BigNumber | ||
|
||
|
||
def test_bad_input(): | ||
for n in [-90, 0, 2.2, 91.0]: | ||
with pytest.raises(ValueError): | ||
assert BigNumber.is_prime(n) | ||
|
||
|
||
def test_check_primes(): | ||
for number in [2, 23, 31, 263, 104729, 42885908609, 24473809133, 47055833459]: | ||
assert BigNumber.is_prime(number), number | ||
|
||
|
||
def test_check_non_primes(): | ||
for number in [24, 42885908610, 24473809134, 47055833460]: | ||
assert not BigNumber.is_prime(number), number | ||
|
||
|
||
def test_big_primes(): | ||
big_primes_256bit = [ | ||
79422449460098942399106282402512198969536520971550757303162642879618420356623, | ||
111239814848601840476117025037343156956140807592049293111743976116251852031961, | ||
110807377343103593013767585056169865297439509005813309754958294117148328724967 | ||
] | ||
for number in big_primes_256bit: | ||
assert BigNumber.is_prime(number), number | ||
|
||
for number in big_primes_256bit: | ||
# Since each prime is odd, prime+1 will not be prime | ||
assert not BigNumber.is_prime(number+1), number |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to ask for some changes to follow design and code structure. I believe we need to add is_prime method to BN entity instead of providing static non-entity related call. Now you provides different Rust API and FFI API.
It will allow to expose big nums in more clean and re-usable way. FFI API can look like this:
it will later allow to expose complete big nums API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same needs to be added to python wrapper. Just follow the same entity model as Rust API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
is_prime
to whichBN
entity?