Skip to content

Commit

Permalink
Use jacobi function in rust-tapyrus
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi authored and azuchi committed Aug 6, 2020
1 parent ed1597b commit 0aa16ad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 57 deletions.
14 changes: 7 additions & 7 deletions src/crypto/vss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::signer_node::BidirectionalSharedSecretMap;
use crate::signer_node::SharedSecretMap;
use crate::signer_node::ToSharedSecretMap;
use crate::signer_node::ToVerifiableSS;
use crate::util::jacobi;

use curv::arithmetic::traits::Converter;
use curv::cryptographic_primitives::secret_sharing::feldman_vss::VerifiableSS;
use curv::elliptic::curves::traits::ECScalar;
Expand All @@ -24,6 +24,7 @@ use std::io;
use std::str::FromStr;
use tapyrus::blockdata::block::Block;
use tapyrus::consensus::encode::{self, *};
use tapyrus::util::prime::jacobi;
use tapyrus::{PrivateKey, PublicKey};

// | name | size | explaination |
Expand Down Expand Up @@ -139,12 +140,11 @@ impl Vss {
block.header.signature_hash(),
);

let p = BigInt::from_str_radix(
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F",
16,
)
.unwrap();
let is_positive = jacobi(&shared_keys_for_positive.y.y_coor().unwrap(), &p) == 1;
let y = shared_keys_for_positive
.y
.y_coor()
.expect("can not get y_coor");
let is_positive = jacobi(&Converter::to_vec(&y)) == 1;
let (shared_keys, local_sig) = if is_positive {
(shared_keys_for_positive, local_sig_for_positive)
} else {
Expand Down
51 changes: 1 addition & 50 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use curv::{BigInt, GE};
use curv::GE;
use std::convert::TryFrom;
use std::os::raw::c_int;
use std::sync::atomic::AtomicUsize;
Expand All @@ -11,48 +11,6 @@ pub fn sum_point(points: &Vec<GE>) -> GE {
tail.fold(head.clone(), |acc, x| acc + x)
}

pub fn jacobi(a: &BigInt, n: &BigInt) -> i8 {
assert!(*n >= BigInt::from(3));
assert!(a < n);

if a.is_zero() {
return 0;
}
if *a == BigInt::from(1) {
return 1;
}

let mut a1: BigInt = a.clone();
let mut e = 0;
while a1.is_multiple_of(&BigInt::from(2)) {
a1 = a1 >> 1;
e += 1;
}
let mut s: i8 = if e & 1 == 0
|| n.modulus(&BigInt::from(8)) == BigInt::from(1)
|| n.modulus(&BigInt::from(8)) == BigInt::from(7)
{
1
} else if n.modulus(&BigInt::from(8)) == BigInt::from(3)
|| n.modulus(&BigInt::from(8)) == BigInt::from(5)
{
-1
} else {
0
};
if n.modulus(&BigInt::from(4)) == BigInt::from(3)
&& a1.modulus(&BigInt::from(4)) == BigInt::from(3)
{
s = -s
}

if a1 == BigInt::from(1) {
s
} else {
s * jacobi(&(n % a1.clone()), &a1.clone())
}
}

const STOP_SIGNALS: [usize; 6] = [
signal_hook::SIGABRT as usize,
signal_hook::SIGHUP as usize,
Expand Down Expand Up @@ -117,13 +75,6 @@ mod tests {
assert_eq!(sum, p6);
}

#[test]
fn test_jacobi() {
assert_eq!(jacobi(&BigInt::from(158), &BigInt::from(235)), -1);
assert_eq!(jacobi(&BigInt::from(5), &BigInt::from(12)), -1);
assert_eq!(jacobi(&BigInt::from(16), &BigInt::from(60)), 1);
}

#[test]
fn test_signals() {
let handler = set_stop_signal_handler().unwrap();
Expand Down

0 comments on commit 0aa16ad

Please sign in to comment.