You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicstaticshr(a: u256,shift: i32): u256{shift&=255;if(shift==0)returna;constw=shift>>>6;// how many full 64-bit words to dropconstb=shift&63;// how many bits to shift within a word// Extract the wordsletlo1=a.lo1;letlo2=a.lo2;lethi1=a.hi1;lethi2=a.hi2;// Shift words down by w words// For w = 1, move lo2->lo1, hi1->lo2, hi2->hi1, and hi2 = 0// For w = 2, move hi1->lo1, hi2->lo2, and zeros in hi1, hi2// For w = 3, move hi2->lo1 and zeros in others// For w >= 4, everything is zero.if(w>=4){// Shifting by >= 256 bits zeros out everythingreturnu256.Zero;}elseif(w==3){lo1=hi2;lo2=0;hi1=0;hi2=0;}elseif(w==2){lo1=hi1;lo2=hi2;hi1=0;hi2=0;}elseif(w==1){lo1=lo2;lo2=hi1;hi1=hi2;hi2=0;}// Now apply the bit shift bif(b>0){// Bring down bits from the higher wordconstcarryLo2=hi1<<(64-b);constcarryLo1=lo2<<(64-b);constcarryHi1=hi2<<(64-b);lo1=(lo1>>>b)|carryLo1;lo2=(lo2>>>b)|carryLo2;hi1=(hi1>>>b)|carryHi1;hi2=hi2>>>b;}returnnewu256(lo1,lo2,hi1,hi2);}
The text was updated successfully, but these errors were encountered:
If you have the opportunity to do PR I will be very happy, because at the moment I have suspended the active maintenance of all my open source projects
The bit shifting doesn’t work for shift values >= 64.
Here’s testing data comparing u256.shr vs. fixed impl vs. JS bigint.shr. Copy out to code editor for better viewing.
Here is a functional implementation:
The text was updated successfully, but these errors were encountered: