File size: 27,893 Bytes
f0f4f2b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 | /*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
if debug {println!("sf2= {}",self.to_string())}
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
use super::big;
use super::big::Big;
use super::dbig::DBig;
use crate::arch::{Chunk, DChunk};
use crate::rand::RAND;
/* Finite field support - for RSA, DH etc. */
/* RSA/DH modulus length as multiple of BIGBITS */
pub use super::rom::FFLEN;
pub const FF_BITS: usize = big::BIGBITS * FFLEN; /* Finite Field Size in bits - must be 256.2^n */
pub const HFLEN: usize = FFLEN / 2; /* Useful for half-size RSA private key operations */
pub const P_MBITS: usize = (big::MODBYTES as usize) * 8;
pub const P_OMASK: Chunk = (-1) << (P_MBITS % big::BASEBITS);
pub const P_FEXCESS: Chunk = 1 << (big::BASEBITS * big::NLEN - P_MBITS - 1);
pub const P_TBITS: usize = P_MBITS % big::BASEBITS;
pub struct FF {
v: Vec<Big>,
length: usize,
}
impl FF {
pub fn excess(a: &Big) -> Chunk {
return ((a.w[big::NLEN - 1] & P_OMASK) >> (P_TBITS)) + 1;
}
pub fn pexceed(a: &Big, b: &Big) -> bool {
let ea = FF::excess(a);
let eb = FF::excess(b);
if ((ea + 1) as DChunk) * ((eb + 1) as DChunk) > P_FEXCESS as DChunk {
return true;
}
return false;
}
pub fn sexceed(a: &Big) -> bool {
let ea = FF::excess(a);
if ((ea + 1) as DChunk) * ((ea + 1) as DChunk) > P_FEXCESS as DChunk {
return true;
}
return false;
}
/* Constructors */
#[inline(always)]
pub fn new_int(n: usize) -> FF {
let mut f = FF {
v: Vec::new(),
length: 0,
};
for _ in 0..n {
f.v.push(Big::new());
}
f.length = n;
return f;
}
pub fn zero(&mut self) {
for i in 0..self.length {
self.v[i].zero();
}
}
pub fn getlen(&self) -> usize {
return self.length;
}
/* set to integer */
pub fn set(&mut self, m: isize) {
self.zero();
self.v[0].set(0, m as Chunk);
}
/* copy from FF b */
pub fn copy(&mut self, b: &FF) {
for i in 0..self.length {
self.v[i] = b.v[i].clone();
}
}
/* x=y<<n */
pub fn dsucopy(&mut self, b: &FF) {
for i in 0..b.length {
self.v[b.length + i] = b.v[i].clone();
self.v[i].zero();
}
}
/* x=y */
pub fn dscopy(&mut self, b: &FF) {
for i in 0..b.length {
self.v[i] = b.v[i].clone();
self.v[b.length + i].zero();
}
}
/* x=y>>n */
pub fn sducopy(&mut self, b: &FF) {
for i in 0..self.length {
self.v[i] = b.v[self.length + i].clone();
}
}
pub fn one(&mut self) {
self.v[0].one();
for i in 1..self.length {
self.v[i].zero();
}
}
/* test equals 0 */
pub fn is_zilch(&mut self) -> bool {
for i in 0..self.length {
if !self.v[i].is_zilch() {
return false;
}
}
return true;
}
/* shift right by BIGBITS-bit words */
pub fn shrw(&mut self, n: usize) {
for i in 0..n {
self.v[i] = self.v[i + n].clone();
self.v[i + n].zero();
}
}
/* shift left by BIGBITS-bit words */
pub fn shlw(&mut self, n: usize) {
for i in 0..n {
self.v[n + i] = self.v[i].clone();
self.v[i].zero();
}
}
/* extract last bit */
pub fn parity(&self) -> isize {
return self.v[0].parity();
}
pub fn lastbits(&mut self, m: usize) -> isize {
return self.v[0].lastbits(m);
}
/* compare x and y - must be normalised, and of same length */
pub fn comp(a: &FF, b: &FF) -> isize {
let mut i = a.length - 1;
loop {
let j = Big::comp(&a.v[i], &b.v[i]);
if j != 0 {
return j;
}
if i == 0 {
break;
}
i -= 1;
}
return 0;
}
/* recursive add */
pub fn radd(&mut self, vp: usize, x: &FF, xp: usize, y: &FF, yp: usize, n: usize) {
for i in 0..n {
self.v[vp + i] = x.v[xp + i].clone();
self.v[vp + i].add(&y.v[yp + i]);
}
}
/* recursive inc */
pub fn rinc(&mut self, vp: usize, y: &FF, yp: usize, n: usize) {
for i in 0..n {
self.v[vp + i].add(&y.v[yp + i]);
}
}
pub fn rsinc(&mut self, n: usize) {
for i in 0..n {
let t = self.v[i].clone();
self.v[n + i].add(&t);
}
}
/* recursive sub */
pub fn rsub(&mut self, vp: usize, x: &FF, xp: usize, y: &FF, yp: usize, n: usize) {
for i in 0..n {
self.v[vp + i] = x.v[xp + i].clone();
self.v[vp + i].sub(&y.v[yp + i]);
}
}
/* recursive dec */
pub fn rdec(&mut self, vp: usize, y: &FF, yp: usize, n: usize) {
for i in 0..n {
self.v[vp + i].sub(&y.v[yp + i]);
}
}
/* simple add */
pub fn add(&mut self, b: &FF) {
for i in 0..self.length {
self.v[i].add(&b.v[i]);
}
}
/* simple sub */
pub fn sub(&mut self, b: &FF) {
for i in 0..self.length {
self.v[i].sub(&b.v[i]);
}
}
/* reverse sub */
pub fn revsub(&mut self, b: &FF) {
for i in 0..self.length {
self.v[i].rsub(&b.v[i]);
}
}
/* normalise - but hold any overflow in top part unless n<0 */
pub fn rnorm(&mut self, vp: usize, n: isize) {
let mut trunc = false;
let mut carry: Chunk;
let mut nn: usize = n as usize;
if n < 0 {
/* -v n signals to do truncation */
nn = (-n) as usize;
trunc = true;
}
for i in 0..nn - 1 {
carry = self.v[vp + i].norm();
self.v[vp + i].xor_top(carry << P_TBITS);
self.v[vp + i + 1].w[0] += carry;
}
carry = self.v[vp + nn - 1].norm();
if trunc {
self.v[vp + nn - 1].xor_top(carry << P_TBITS);
}
}
pub fn norm(&mut self) {
let n: isize = self.length as isize;
self.rnorm(0, n);
}
/* increment/decrement by a small integer */
pub fn inc(&mut self, m: isize) {
self.v[0].inc(m);
self.norm();
}
pub fn dec(&mut self, m: isize) {
self.v[0].dec(m);
self.norm();
}
/* shift left by one bit */
pub fn shl(&mut self) {
let mut delay_carry: isize = 0;
for i in 0..self.length - 1 {
let carry = self.v[i].fshl(1);
self.v[i].inc(delay_carry);
self.v[i].xor_top((carry as Chunk) << P_TBITS);
delay_carry = carry;
}
self.v[self.length - 1].fshl(1);
self.v[self.length - 1].inc(delay_carry);
}
/* shift right by one bit */
pub fn shr(&mut self) {
let mut i = self.length - 1;
while i > 0 {
let carry = self.v[i].fshr(1);
self.v[i - 1].xor_top((carry as Chunk) << P_TBITS);
i -= 1;
}
self.v[0].fshr(1);
}
/// To String
///
/// Converts `FF` to a hex string.
pub fn to_string(&mut self) -> String {
self.norm();
let mut s = String::new();
let mut i: usize = self.length - 1;
loop {
s = s + self.v[i].to_string().as_ref();
if i == 0 {
break;
}
i -= 1;
}
return s;
}
/* Convert FFs to/from byte arrays */
pub fn to_bytes(&mut self, b: &mut [u8]) {
for i in 0..self.length {
self.v[i].to_byte_array(b, (self.length - i - 1) * (big::MODBYTES as usize))
}
}
pub fn from_bytes(x: &mut FF, b: &[u8]) {
for i in 0..x.length {
x.v[i] = Big::from_byte_array(b, (x.length - i - 1) * (big::MODBYTES as usize))
}
}
/* in-place swapping using xor - side channel resistant - lengths must be the same */
pub fn cswap(a: &mut FF, b: &mut FF, d: isize) {
for i in 0..a.length {
a.v[i].cswap(&mut b.v[i], d);
}
}
/* z=x*y, t is workspace */
fn karmul(
&mut self,
vp: usize,
x: &FF,
xp: usize,
y: &FF,
yp: usize,
t: *mut FF,
tp: usize,
n: usize,
) {
if n == 1 {
let xx = x.v[xp].clone();
let yy = y.v[yp].clone();
let mut d = Big::mul(&xx, &yy);
self.v[vp + 1] = d.split(8 * big::MODBYTES);
self.v[vp].dcopy(&d);
return;
}
let nd2 = n / 2;
self.radd(vp, x, xp, x, xp + nd2, nd2);
self.rnorm(vp, nd2 as isize); /* Important - required for 32-bit build */
self.radd(vp + nd2, y, yp, y, yp + nd2, nd2);
self.rnorm(vp + nd2, nd2 as isize); /* Important - required for 32-bit build */
unsafe {
(*t).karmul(tp, self, vp, self, vp + nd2, t, tp + n, nd2);
}
self.karmul(vp, x, xp, y, yp, t, tp + n, nd2);
self.karmul(vp + n, x, xp + nd2, y, yp + nd2, t, tp + n, nd2);
unsafe {
(*t).rdec(tp, self, vp, n);
(*t).rdec(tp, self, vp + n, n);
self.rinc(vp + nd2, &(*t), tp, n);
}
self.rnorm(vp, (2 * n) as isize);
}
fn karsqr(&mut self, vp: usize, x: &FF, xp: usize, t: *mut FF, tp: usize, n: usize) {
if n == 1 {
let xx = x.v[xp].clone();
let mut d = Big::sqr(&xx);
self.v[vp + 1] = d.split(8 * big::MODBYTES);
self.v[vp].dcopy(&d);
return;
}
let nd2 = n / 2;
self.karsqr(vp, x, xp, t, tp + n, nd2);
self.karsqr(vp + n, x, xp + nd2, t, tp + n, nd2);
unsafe {
(*t).karmul(tp, x, xp, x, xp + nd2, t, tp + n, nd2);
self.rinc(vp + nd2, &(*t), tp, n);
self.rinc(vp + nd2, &(*t), tp, n);
}
self.rnorm(vp + nd2, n as isize);
}
/* Calculates Least Significant bottom half of x*y */
fn karmul_lower(
&mut self,
vp: usize,
x: &FF,
xp: usize,
y: &FF,
yp: usize,
t: *mut FF,
tp: usize,
n: usize,
) {
if n == 1 {
/* only calculate bottom half of product */
self.v[vp] = Big::smul(&x.v[xp], &y.v[yp]);
return;
}
let nd2 = n / 2;
self.karmul(vp, x, xp, y, yp, t, tp + n, nd2);
unsafe {
(*t).karmul_lower(tp, x, xp + nd2, y, yp, t, tp + n, nd2);
self.rinc(vp + nd2, &(*t), tp, nd2);
(*t).karmul_lower(tp, x, xp, y, yp + nd2, t, tp + n, nd2);
self.rinc(vp + nd2, &(*t), tp, nd2);
}
let sn: isize = nd2 as isize;
self.rnorm(vp + nd2, -sn); /* truncate it */
}
/* Calculates Most Significant upper half of x*y, given lower part */
fn karmul_upper(&mut self, x: &FF, y: &FF, t: *mut FF, n: usize) {
let nd2 = n / 2;
self.radd(n, x, 0, x, nd2, nd2);
self.radd(n + nd2, y, 0, y, nd2, nd2);
self.rnorm(n, nd2 as isize);
self.rnorm(n + nd2, nd2 as isize);
unsafe {
(*t).karmul(0, self, n + nd2, self, n, t, n, nd2); /* t = (a0+a1)(b0+b1) */
}
self.karmul(n, x, nd2, y, nd2, t, n, nd2); /* z[n]= a1*b1 */
/* z[0-nd2]=l(a0b0) z[nd2-n]= h(a0b0)+l(t)-l(a0b0)-l(a1b1) */
unsafe {
(*t).rdec(0, self, n, n); /* t=t-a1b1 */
self.rsinc(nd2); /* z[nd2-n]+=l(a0b0) = h(a0b0)+l(t)-l(a1b1) */
self.rdec(nd2, &(*t), 0, nd2); /* z[nd2-n]=h(a0b0)+l(t)-l(a1b1)-l(t-a1b1)=h(a0b0) */
}
let sn: isize = n as isize;
self.rnorm(0, -sn); /* a0b0 now in z - truncate it */
unsafe {
(*t).rdec(0, self, 0, n); /* (a0+a1)(b0+b1) - a0b0 */
self.rinc(nd2, &(*t), 0, n);
}
self.rnorm(nd2, sn);
}
/* z=x*y. Assumes x and y are of same length. */
#[inline(always)]
pub fn mul(x: &FF, y: &FF) -> FF {
let n = x.length;
let mut z = FF::new_int(2 * n);
let mut t = FF::new_int(2 * n);
z.karmul(0, &x, 0, &y, 0, &mut t, 0, n);
return z;
}
/* return low part of product this*y */
pub fn lmul(&mut self, y: &FF) {
let n = self.length;
let mut t = FF::new_int(2 * n);
let mut x = FF::new_int(n);
x.copy(&self);
self.karmul_lower(0, &x, 0, &y, 0, &mut t, 0, n);
}
/* Set b=b mod c */
pub fn rmod(&mut self, m: &FF) {
let mut k = 1;
let n = m.length;
let mut c = FF::new_int(n);
c.copy(m);
self.norm();
if FF::comp(&self, &c) < 0 {
return;
}
c.shl();
while FF::comp(&self, &c) >= 0 {
c.shl();
k += 1;
}
while k > 0 {
c.shr();
if FF::comp(&self, &c) >= 0 {
self.sub(&c);
self.norm();
}
k -= 1;
}
}
/* z=x^2 */
#[inline(always)]
pub fn sqr(x: &FF) -> FF {
let n = x.length;
let mut z = FF::new_int(2 * n);
let mut t = FF::new_int(2 * n);
z.karsqr(0, &x, 0, &mut t, 0, n);
return z;
}
/* return This mod modulus, ms is modulus, md is Montgomery Constant */
#[inline(always)]
pub fn reduce(&mut self, ms: &FF, md: &FF) -> FF {
/* fast karatsuba Montgomery reduction */
let n = ms.length;
let mut t = FF::new_int(2 * n);
let mut r = FF::new_int(n);
let mut m = FF::new_int(n);
r.sducopy(&self);
m.karmul_lower(0, &self, 0, &md, 0, &mut t, 0, n);
self.karmul_upper(&ms, &m, &mut t, n);
m.sducopy(self);
r.add(&ms);
r.sub(&m);
r.norm();
return r;
}
/* Set r=this mod b */
/* this is of length - 2*n */
/* r,b is of length - n */
#[inline(always)]
pub fn dmod(&mut self, b: &FF) -> FF {
let n = b.length;
let mut m = FF::new_int(2 * n);
let mut x = FF::new_int(2 * n);
let mut r = FF::new_int(n);
x.copy(&self);
x.norm();
m.dsucopy(&b);
let mut k = big::BIGBITS * n;
while FF::comp(&x, &m) >= 0 {
x.sub(&m);
x.norm();
}
while k > 0 {
m.shr();
if FF::comp(&x, &m) >= 0 {
x.sub(&m);
x.norm();
}
k -= 1;
}
r.copy(&x);
r.rmod(b);
return r;
}
/* Set return=1/this mod p. Binary method - a<p on entry */
pub fn invmodp(&mut self, p: &FF) {
let n = p.length;
let mut u = FF::new_int(n);
let mut v = FF::new_int(n);
let mut x1 = FF::new_int(n);
let mut x2 = FF::new_int(n);
let mut t = FF::new_int(n);
let mut one = FF::new_int(n);
one.one();
u.copy(&self);
v.copy(&p);
x1.copy(&one);
x2.zero();
// reduce n in here as well!
while FF::comp(&u, &one) != 0 && FF::comp(&v, &one) != 0 {
while u.parity() == 0 {
u.shr();
if x1.parity() != 0 {
x1.add(&p);
x1.norm();
}
x1.shr();
}
while v.parity() == 0 {
v.shr();
if x2.parity() != 0 {
x2.add(&p);
x2.norm();
}
x2.shr();
}
if FF::comp(&u, &v) >= 0 {
u.sub(&v);
u.norm();
if FF::comp(&x1, &x2) >= 0 {
x1.sub(&x2);
} else {
t.copy(&p);
t.sub(&x2);
x1.add(&t);
}
x1.norm();
} else {
v.sub(&u);
v.norm();
if FF::comp(&x2, &x1) >= 0 {
x2.sub(&x1);
} else {
t.copy(&p);
t.sub(&x1);
x2.add(&t);
}
x2.norm();
}
}
if FF::comp(&u, &one) == 0 {
self.copy(&x1);
} else {
self.copy(&x2);
}
}
/* nresidue mod m */
pub fn nres(&mut self, m: &FF) {
let n = m.length;
if n == 1 {
let mut d = DBig::new_scopy(&(self.v[0]));
d.shl(big::NLEN * (big::BASEBITS as usize));
self.v[0] = d.dmod(&(m.v[0]));
} else {
let mut d = FF::new_int(2 * n);
d.dsucopy(&self);
self.copy(&d.dmod(m));
}
}
pub fn redc(&mut self, m: &FF, md: &FF) {
let n = m.length;
if n == 1 {
let mut d = DBig::new_scopy(&(self.v[0]));
self.v[0] = Big::monty(
&(m.v[0]),
((1 as Chunk) << big::BASEBITS) - md.v[0].w[0],
&mut d,
);
} else {
let mut d = FF::new_int(2 * n);
self.rmod(m);
d.dscopy(&self);
self.copy(&d.reduce(&m, &md));
self.rmod(m);
}
}
pub fn mod2m(&mut self, m: usize) {
for i in m..self.length {
self.v[i].zero()
}
}
/* U=1/a mod 2^m - Arazi & Qi */
#[inline(always)]
pub fn invmod2m(&self) -> FF {
let n = self.length;
let mut b = FF::new_int(n);
let mut c = FF::new_int(n);
let mut u = FF::new_int(n);
u.zero();
u.v[0] = self.v[0].clone();
u.v[0].invmod2m();
let mut i = 1;
while i < n {
b.copy(&self);
b.mod2m(i);
let mut t = FF::mul(&u, &b);
t.shrw(i);
b.copy(&t);
c.copy(&self);
c.shrw(i);
c.mod2m(i);
c.lmul(&u);
c.mod2m(i);
b.add(&c);
b.norm();
b.lmul(&u);
b.mod2m(i);
c.one();
c.shlw(i);
b.revsub(&c);
b.norm();
b.shlw(i);
u.add(&b);
i <<= 1;
}
u.norm();
return u;
}
pub fn random(&mut self, rng: &mut RAND) {
let n = self.length;
for i in 0..n {
self.v[i] = Big::random(rng)
}
/* make sure top bit is 1 */
while self.v[n - 1].nbits() < (big::MODBYTES as usize) * 8 {
self.v[n - 1] = Big::random(rng);
}
}
/* generate random x less than p */
pub fn randomnum(&mut self, p: &FF, rng: &mut RAND) {
let n = self.length;
let mut d = FF::new_int(2 * n);
for i in 0..2 * n {
d.v[i] = Big::random(rng);
}
self.copy(&d.dmod(p));
}
/* this*=y mod p */
pub fn modmul(&mut self, y: &FF, p: &FF, nd: &FF) {
if FF::pexceed(&self.v[self.length - 1], &y.v[y.length - 1]) {
self.rmod(p)
}
let n = p.length;
if n == 1 {
let mut d = Big::mul(&self.v[0], &y.v[0]);
self.v[0] = Big::monty(
&(p.v[0]),
((1 as Chunk) << big::BASEBITS) - nd.v[0].w[0],
&mut d,
);
} else {
let mut d = FF::mul(&self, y);
self.copy(&d.reduce(p, nd));
}
}
/* this*=y mod p */
pub fn modsqr(&mut self, p: &FF, nd: &FF) {
if FF::sexceed(&self.v[self.length - 1]) {
self.rmod(p);
}
let n = p.length;
if n == 1 {
let mut d = Big::sqr(&self.v[0]);
self.v[0] = Big::monty(
&(p.v[0]),
((1 as Chunk) << big::BASEBITS) - nd.v[0].w[0],
&mut d,
);
} else {
let mut d = FF::sqr(&self);
d.norm();
self.copy(&d.reduce(p, nd));
}
}
/* this=this^e mod p using side-channel resistant Montgomery Ladder, for large e */
pub fn skpow(&mut self, e: &FF, p: &FF) {
let n = p.length;
let mut r0 = FF::new_int(n);
let mut r1 = FF::new_int(n);
let nd = p.invmod2m();
self.rmod(p);
r0.one();
r1.copy(&self);
r0.nres(p);
r1.nres(p);
let mut i = 8 * (big::MODBYTES as usize) * n - 1;
loop {
let b = (e.v[i / (big::BIGBITS as usize)]).bit(i % (big::BIGBITS as usize)) as isize;
self.copy(&r0);
self.modmul(&r1, p, &nd);
FF::cswap(&mut r0, &mut r1, b);
r0.modsqr(p, &nd);
r1.copy(&self);
FF::cswap(&mut r0, &mut r1, b);
if i == 0 {
break;
}
i -= 1;
}
self.copy(&r0);
self.redc(p, &nd);
}
/* this =this^e mod p using side-channel resistant Montgomery Ladder, for short e */
pub fn skpows(&mut self, e: &Big, p: &FF) {
let n = p.length;
let mut r0 = FF::new_int(n);
let mut r1 = FF::new_int(n);
let nd = p.invmod2m();
self.rmod(p);
r0.one();
r1.copy(&self);
r0.nres(p);
r1.nres(p);
let mut i = 8 * (big::MODBYTES as usize) - 1;
loop {
let b = e.bit(i);
self.copy(&r0);
self.modmul(&r1, p, &nd);
FF::cswap(&mut r0, &mut r1, b);
r0.modsqr(p, &nd);
r1.copy(&self);
FF::cswap(&mut r0, &mut r1, b);
if i == 0 {
break;
}
i -= 1;
}
self.copy(&r0);
self.redc(p, &nd);
}
/* raise to an integer power - right-to-left method */
pub fn power(&mut self, e: isize, p: &FF) {
let n = p.length;
let mut w = FF::new_int(n);
let nd = p.invmod2m();
let mut f = true;
let mut ee = e;
w.copy(&self);
w.nres(p);
if ee == 2 {
self.copy(&w);
self.modsqr(p, &nd);
} else {
loop {
if ee % 2 == 1 {
if f {
self.copy(&w);
} else {
self.modmul(&w, p, &nd)
}
f = false;
}
ee >>= 1;
if ee == 0 {
break;
}
w.modsqr(p, &nd);
}
}
self.redc(p, &nd);
}
/* this=this^e mod p, faster but not side channel resistant */
pub fn pow(&mut self, e: &FF, p: &FF) {
let n = p.length;
let mut w = FF::new_int(n);
let nd = p.invmod2m();
w.copy(&self);
self.one();
self.nres(p);
w.nres(p);
let mut i = 8 * (big::MODBYTES as usize) * n - 1;
loop {
self.modsqr(p, &nd);
let b = (e.v[i / (big::BIGBITS as usize)]).bit(i % (big::BIGBITS as usize)) as isize;
if b == 1 {
self.modmul(&w, p, &nd)
}
if i == 0 {
break;
}
i -= 1;
}
self.redc(p, &nd);
}
/* double exponentiation r=x^e.y^f mod p */
pub fn pow2(&mut self, e: &Big, y: &FF, f: &Big, p: &FF) {
let n = p.length;
let mut xn = FF::new_int(n);
let mut yn = FF::new_int(n);
let mut xy = FF::new_int(n);
let nd = p.invmod2m();
xn.copy(&self);
yn.copy(y);
xn.nres(p);
yn.nres(p);
xy.copy(&xn);
xy.modmul(&yn, p, &nd);
self.one();
self.nres(p);
let mut i = 8 * (big::MODBYTES as usize) - 1;
loop {
let eb = e.bit(i);
let fb = f.bit(i);
self.modsqr(p, &nd);
if eb == 1 {
if fb == 1 {
self.modmul(&xy, p, &nd);
} else {
self.modmul(&xn, p, &nd)
}
} else {
if fb == 1 {
self.modmul(&yn, p, &nd)
}
}
if i == 0 {
break;
}
i -= 1;
}
self.redc(p, &nd);
}
pub fn igcd(x: isize, y: isize) -> isize {
/* integer GCD, returns GCD of x and y */
if y == 0 {
return x;
}
let mut xx = x;
let mut yy = y;
loop {
let r = xx % yy;
if r == 0 {
break;
}
xx = yy;
yy = r;
}
return yy;
}
/* quick and dirty check for common factor with n */
pub fn cfactor(&self, s: isize) -> bool {
let n = self.length;
let mut x = FF::new_int(n);
let mut y = FF::new_int(n);
y.set(s);
x.copy(&self);
x.norm();
x.sub(&y);
x.norm();
while !x.is_zilch() && x.parity() == 0 {
x.shr()
}
while FF::comp(&x, &y) > 0 {
x.sub(&y);
x.norm();
while !x.is_zilch() && x.parity() == 0 {
x.shr()
}
}
let g = x.v[0].get(0) as isize;
let r = FF::igcd(s, g);
if r > 1 {
return true;
}
return false;
}
/* Miller-Rabin test for primality. Slow. */
pub fn prime(pp: &FF, rng: &mut RAND) -> bool {
let mut s = 0;
let n = pp.length;
let mut d = FF::new_int(n);
let mut x = FF::new_int(n);
let mut unity = FF::new_int(n);
let mut nm1 = FF::new_int(n);
let mut p = FF::new_int(n);
p.copy(pp);
let sf = 4849845; /* 3*5*.. *19 */
p.norm();
if p.cfactor(sf) {
return false;
}
unity.one();
nm1.copy(&p);
nm1.sub(&unity);
nm1.norm();
d.copy(&nm1);
while d.parity() == 0 {
d.shr();
s += 1;
}
if s == 0 {
return false;
}
for _ in 0..10 {
x.randomnum(&p, rng);
x.pow(&d, &p);
if FF::comp(&x, &unity) == 0 || FF::comp(&x, &nm1) == 0 {
continue;
}
let mut looper = false;
for _ in 1..s {
x.power(2, &p);
if FF::comp(&x, &unity) == 0 {
return false;
}
if FF::comp(&x, &nm1) == 0 {
looper = true;
break;
}
}
if looper {
continue;
}
return false;
}
return true;
}
}
|