mirror of
https://github.com/logos-storage/rust-poseidon-bn254-pure.git
synced 2026-08-01 07:23:15 +00:00
remove all the unnecessary references
This commit is contained in:
parent
dd278668a2
commit
ee15a60f89
@ -27,15 +27,15 @@ fn initial_vector() -> [Felt; 3] {
|
||||
|
||||
pub fn poseidon1_permute_felt(input: [Felt; 3]) -> [Felt; 3] {
|
||||
let mut state: [Mont; 3] =
|
||||
[ Felt::to_mont(&input[0])
|
||||
, Felt::to_mont(&input[1])
|
||||
, Felt::to_mont(&input[2])
|
||||
[ Felt::to_mont(input[0])
|
||||
, Felt::to_mont(input[1])
|
||||
, Felt::to_mont(input[2])
|
||||
];
|
||||
state = poseidon::permutation::permute_mont_T3(state);
|
||||
let out: [Felt; 3] =
|
||||
[ Felt::from_mont(&state[0])
|
||||
, Felt::from_mont(&state[1])
|
||||
, Felt::from_mont(&state[2])
|
||||
[ Felt::from_mont(state[0])
|
||||
, Felt::from_mont(state[1])
|
||||
, Felt::from_mont(state[2])
|
||||
];
|
||||
out
|
||||
}
|
||||
@ -51,7 +51,7 @@ fn iterate_poseidon1(n: usize) -> [Felt; 3] {
|
||||
fn iterate_poseidon2(n: usize) -> Triple {
|
||||
let mut state: Triple = initial_triple();
|
||||
for _i in 0..n {
|
||||
state = poseidon2::permutation::permute_felt(&state);
|
||||
state = poseidon2::permutation::permute_felt(state);
|
||||
}
|
||||
state
|
||||
}
|
||||
|
||||
@ -32,87 +32,86 @@ const MONT3 : Mont = Mont::unsafe_make( [ 0x24a2de63 , 0xfbb9d8d0 , 0x671492ce ,
|
||||
|
||||
fn main() {
|
||||
|
||||
BigInt::print("PRIME",&FIELD_PRIME);
|
||||
BigInt::debug_print("PRIME", FIELD_PRIME);
|
||||
|
||||
println!("");
|
||||
|
||||
BigInt::print("R1",&BIG_R1);
|
||||
BigInt::print("R2",&BIG_R2);
|
||||
BigInt::print("R3",&BIG_R3);
|
||||
BigInt::debug_print("R1", BIG_R1);
|
||||
BigInt::debug_print("R2", BIG_R2);
|
||||
BigInt::debug_print("R3", BIG_R3);
|
||||
|
||||
println!("");
|
||||
|
||||
BigInt::print("BIG1",&BIG1);
|
||||
BigInt::print("BIG2",&BIG2);
|
||||
BigInt::print("BIG3",&BIG3);
|
||||
BigInt::debug_print("BIG1", BIG1);
|
||||
BigInt::debug_print("BIG2", BIG2);
|
||||
BigInt::debug_print("BIG3", BIG3);
|
||||
|
||||
println!("");
|
||||
|
||||
Mont::print_internal("MONT1",&MONT1);
|
||||
Mont::print_internal("MONT2",&MONT2);
|
||||
Mont::print_internal("MONT3",&MONT3);
|
||||
Mont::debug_print_internal("MONT1", MONT1);
|
||||
Mont::debug_print_internal("MONT2", MONT2);
|
||||
Mont::debug_print_internal("MONT3", MONT3);
|
||||
println!("-----");
|
||||
Mont::print_standard("MONT1",&MONT1);
|
||||
Mont::print_standard("MONT2",&MONT2);
|
||||
Mont::print_standard("MONT3",&MONT3);
|
||||
Mont::debug_print_standard("MONT1", MONT1);
|
||||
Mont::debug_print_standard("MONT2", MONT2);
|
||||
Mont::debug_print_standard("MONT3", MONT3);
|
||||
|
||||
println!("");
|
||||
|
||||
Felt::print("FELT1",&FELT1);
|
||||
Felt::print("FELT2",&FELT2);
|
||||
Felt::print("FELT3",&FELT3);
|
||||
Felt::debug_print("FELT1", FELT1);
|
||||
Felt::debug_print("FELT2", FELT2);
|
||||
Felt::debug_print("FELT3", FELT3);
|
||||
|
||||
println!("");
|
||||
println!("bigint:");
|
||||
println!("");
|
||||
|
||||
let (x,c) = BigInt::addCarry( &BIG1, &BIG2 );
|
||||
let (y,d) = BigInt::addCarry( &BIG2, &BIG3 );
|
||||
let (z,e) = BigInt::addCarry( &BIG3, &BIG1 );
|
||||
let (x,c) = BigInt::addCarry( BIG1, BIG2 );
|
||||
let (y,d) = BigInt::addCarry( BIG2, BIG3 );
|
||||
let (z,e) = BigInt::addCarry( BIG3, BIG1 );
|
||||
|
||||
println!("B1+B2 = {} + {}", &x, c);
|
||||
println!("B2+B3 = {} + {}", &y, d);
|
||||
println!("B3+B1 = {} + {}", &z, e);
|
||||
println!("B1+B2 = {} + {}", x, c);
|
||||
println!("B2+B3 = {} + {}", y, d);
|
||||
println!("B3+B1 = {} + {}", z, e);
|
||||
println!("-----");
|
||||
|
||||
let u: BigInt<16> = BigInt::<8>::mul( &BIG1, &BIG2 );
|
||||
println!("B1*B2 = {}", &u);
|
||||
let u: BigInt<16> = BigInt::<8>::mul( BIG1, BIG2 );
|
||||
println!("B1*B2 = {}", u);
|
||||
|
||||
println!("");
|
||||
println!("montgomery:");
|
||||
println!("");
|
||||
|
||||
println!("M1+M2 = {}", Mont::add( &MONT1, &MONT2) );
|
||||
println!("M2+M3 = {}", Mont::add( &MONT2, &MONT3) );
|
||||
println!("M3+M1 = {}", Mont::add( &MONT3, &MONT1) );
|
||||
println!("M1+M2 = {}", MONT1 + MONT2 );
|
||||
println!("M2+M3 = {}", MONT2 + MONT3 );
|
||||
println!("M3+M1 = {}", MONT3 + MONT1 );
|
||||
println!("-----");
|
||||
|
||||
println!("M1*M2 = {}", Mont::mul( &MONT1, &MONT2) );
|
||||
println!("M2*M3 = {}", Mont::mul( &MONT2, &MONT3) );
|
||||
println!("M3*M1 = {}", Mont::mul( &MONT3, &MONT1) );
|
||||
println!("M1*M2 = {}", MONT1 * MONT2 );
|
||||
println!("M2*M3 = {}", MONT2 * MONT3 );
|
||||
println!("M3*M1 = {}", MONT3 * MONT1 );
|
||||
|
||||
println!("");
|
||||
println!("felt (standard repr):");
|
||||
println!("");
|
||||
|
||||
println!("F1+F2 = {}", Felt::add( &FELT1, &FELT2) );
|
||||
println!("F2+F3 = {}", Felt::add( &FELT2, &FELT3) );
|
||||
println!("F3+F1 = {}", Felt::add( &FELT3, &FELT1) );
|
||||
println!("F1+F2 = {}", FELT1 + FELT2 );
|
||||
println!("F2+F3 = {}", FELT2 + FELT3 );
|
||||
println!("F3+F1 = {}", FELT3 + FELT1 );
|
||||
println!("-----");
|
||||
|
||||
println!("F1*F2 = {}", Felt::mul( &FELT1, &FELT2) );
|
||||
println!("F2*F3 = {}", Felt::mul( &FELT2, &FELT3) );
|
||||
println!("F3*F1 = {}", Felt::mul( &FELT3, &FELT1) );
|
||||
println!("F1*F2 = {}", FELT1 * FELT2 );
|
||||
println!("F2*F3 = {}", FELT2 * FELT3 );
|
||||
println!("F3*F1 = {}", FELT3 * FELT1 );
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
println!("");
|
||||
println!("poseidon2 KAT:");
|
||||
println!("");
|
||||
|
||||
let input = ( Felt::from_u32(0) , Felt::from_u32(1) , Felt::from_u32(2) );
|
||||
let output = permute_felt( &input );
|
||||
let output = permute_felt( input );
|
||||
|
||||
println!("x = {}", input.0 );
|
||||
println!("y = {}", input.1 );
|
||||
@ -137,7 +136,7 @@ fn main() {
|
||||
let now = Instant::now();
|
||||
let mut state: (Felt,Felt,Felt) = input.clone();
|
||||
for _i in 0..10000 {
|
||||
state = permute_felt(&state);
|
||||
state = permute_felt(state);
|
||||
}
|
||||
|
||||
// expected output:
|
||||
@ -152,25 +151,22 @@ fn main() {
|
||||
let elapsed = now.elapsed();
|
||||
println!("Elapsed: {:.3?}", elapsed);
|
||||
|
||||
*/
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
println!("");
|
||||
println!("sanity checking comparison with the prime");
|
||||
let one : Big = BigInt::from_u32(1);
|
||||
let a: Big = BigInt::sub(&FIELD_PRIME, &one);
|
||||
let b: Big = FIELD_PRIME ;
|
||||
let c: Big = BigInt::add(&FIELD_PRIME, &one);
|
||||
let a: Big = FIELD_PRIME + one;
|
||||
let b: Big = FIELD_PRIME ;
|
||||
let c: Big = FIELD_PRIME - one;
|
||||
println!("a = {}", a );
|
||||
println!("b = {}", b );
|
||||
println!("c = {}", c );
|
||||
println!("{} , {} , {}" ,
|
||||
BigInt::is_lt_prime(&a) ,
|
||||
BigInt::is_lt_prime(&b) ,
|
||||
BigInt::is_lt_prime(&c) );
|
||||
*/
|
||||
BigInt::is_lt_prime(a) ,
|
||||
BigInt::is_lt_prime(b) ,
|
||||
BigInt::is_lt_prime(c) );
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
@ -178,8 +174,8 @@ fn main() {
|
||||
println!("");
|
||||
println!("conversion to/from bytes");
|
||||
let a = FELT1;
|
||||
let xs = Felt::to_le_bytes(&a);
|
||||
let b = Felt::unsafe_from_le_bytes(&xs);
|
||||
let xs = Felt::to_le_bytes(a);
|
||||
let b = Felt::unsafe_from_le_bytes(xs);
|
||||
println!("a = {}",a);
|
||||
println!("b = {}",b);
|
||||
println!("le = {:?}",xs);
|
||||
@ -187,8 +183,8 @@ fn main() {
|
||||
|
||||
{
|
||||
let a = FELT2;
|
||||
let ys = Felt::to_be_bytes(&a);
|
||||
let b = Felt::unsafe_from_be_bytes(&ys);
|
||||
let ys = Felt::to_be_bytes(a);
|
||||
let b = Felt::unsafe_from_be_bytes(ys);
|
||||
println!("a = {}",a);
|
||||
println!("b = {}",b);
|
||||
println!("be = {:?}",ys);
|
||||
@ -199,24 +195,31 @@ fn main() {
|
||||
/*
|
||||
println!("underlying repr = {:?} ", MONT1);
|
||||
println!("in hex = {}", MONT1);
|
||||
println!("in dec = {}", Mont::to_decimal_string(&MONT1));
|
||||
println!("in dec = {}", Mont::to_decimal_string(MONT1));
|
||||
*/
|
||||
|
||||
// expected results:
|
||||
//
|
||||
// compress1 = 18586133768512220936620570745912940619677854269274689475585506675881198879027
|
||||
// compress2 = 7853200120776062878684798364095072458815029376092732009249414926327459813530
|
||||
// compress3 = 6542985608222806190361240322586112750744169038454362455181422643027100751666
|
||||
// compress4 = 18821383157269793795438455681495246036402687001665670618754263018637548127333
|
||||
|
||||
let in1: Felt = Felt::from_u32(1);
|
||||
let out1 = compress_1(in1);
|
||||
println!("compress(1) = {}", Felt::to_decimal_string(&out1) );
|
||||
println!("compress(1) = {}", Felt::to_decimal_string(out1) );
|
||||
|
||||
let in2: [Felt; 2] = [ Felt::from_u32(1) , Felt::from_u32(2) ];
|
||||
let out2 = compress_2(in2);
|
||||
println!("compress(2) = {}", Felt::to_decimal_string(&out2) );
|
||||
println!("compress(2) = {}", Felt::to_decimal_string(out2) );
|
||||
|
||||
let in3: [Felt; 3] = [ Felt::from_u32(1) , Felt::from_u32(2) , Felt::from_u32(3) ];
|
||||
let out3 = compress_3(in3);
|
||||
println!("compress(3) = {}", Felt::to_decimal_string(&out3) );
|
||||
println!("compress(3) = {}", Felt::to_decimal_string(out3) );
|
||||
|
||||
let in4: [Felt; 4] = [ Felt::from_u32(1) , Felt::from_u32(2) , Felt::from_u32(3) , Felt::from_u32(4) ];
|
||||
let out4 = compress_4(in4);
|
||||
println!("compress(4) = {}", Felt::to_decimal_string(&out4) );
|
||||
println!("compress(4) = {}", Felt::to_decimal_string(out4) );
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -14,7 +14,7 @@ fn main() {
|
||||
|
||||
let mut state: (Felt,Felt,Felt) = input.clone();
|
||||
for _i in 0..20 {
|
||||
state = permute_felt(&state);
|
||||
state = permute_felt(state);
|
||||
}
|
||||
println!("x' = {}", state.0 );
|
||||
println!("y' = {}", state.1 );
|
||||
|
||||
@ -39,7 +39,7 @@ impl<const N: usize> fmt::Display for BigInt<N> {
|
||||
}
|
||||
|
||||
impl<const N: usize> BigInt<N> {
|
||||
pub fn print(s: &str, A: &BigInt<N>) {
|
||||
pub fn debug_print(s: &str, A: BigInt<N>) {
|
||||
println!("{} = {}", s, A);
|
||||
}
|
||||
}
|
||||
@ -49,20 +49,20 @@ impl<const N: usize> BigInt<N> {
|
||||
|
||||
impl<const N: usize> Add for BigInt<N> {
|
||||
type Output = Self;
|
||||
fn add(self, other: Self) -> Self { BigInt::add(&self,&other) }
|
||||
fn add(self, other: Self) -> Self { BigInt::add(self,other) }
|
||||
}
|
||||
|
||||
impl<const N: usize> Sub for BigInt<N> {
|
||||
type Output = Self;
|
||||
fn sub(self, other: Self) -> Self { BigInt::sub(&self,&other) }
|
||||
fn sub(self, other: Self) -> Self { BigInt::sub(self,other) }
|
||||
}
|
||||
|
||||
impl<const N: usize> PartialOrd for BigInt<N> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(BigInt::cmp(&self,&other)) }
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(BigInt::cmp(*self, *other)) }
|
||||
}
|
||||
|
||||
impl<const N: usize> Ord for BigInt<N> {
|
||||
fn cmp(&self, other: &Self) -> Ordering { BigInt::cmp(&self,&other) }
|
||||
fn cmp(&self, other: &Self) -> Ordering { BigInt::cmp(*self, *other) }
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@ -104,7 +104,7 @@ impl<const N: usize> BigInt<N> {
|
||||
//------------------------------------
|
||||
// conversion to/from bytes
|
||||
|
||||
pub fn to_le_bytes(big: &BigInt<N>) -> [u8; 4*N] {
|
||||
pub fn to_le_bytes(big: BigInt<N>) -> [u8; 4*N] {
|
||||
let mut buf : [u8; 4*N] = [0; 4*N];
|
||||
for i in 0..N {
|
||||
let k = 4*i;
|
||||
@ -113,7 +113,7 @@ impl<const N: usize> BigInt<N> {
|
||||
buf
|
||||
}
|
||||
|
||||
pub fn from_le_bytes(buf : &[u8; 4*N]) -> BigInt<N> {
|
||||
pub fn from_le_bytes(buf : [u8; 4*N]) -> BigInt<N> {
|
||||
let mut ws: [u32; N] = [0; N];
|
||||
for i in 0..N {
|
||||
let k = 4*i;
|
||||
@ -125,7 +125,7 @@ impl<const N: usize> BigInt<N> {
|
||||
BigInt(ws)
|
||||
}
|
||||
|
||||
pub fn to_be_bytes(big: &BigInt<N>) -> [u8; 4*N] {
|
||||
pub fn to_be_bytes(big: BigInt<N>) -> [u8; 4*N] {
|
||||
let mut buf : [u8; 4*N] = [0; 4*N];
|
||||
for i in 0..N {
|
||||
let k = 4*i;
|
||||
@ -134,7 +134,7 @@ impl<const N: usize> BigInt<N> {
|
||||
buf
|
||||
}
|
||||
|
||||
pub fn from_be_bytes(buf: &[u8; 4*N]) -> BigInt<N> {
|
||||
pub fn from_be_bytes(buf: [u8; 4*N]) -> BigInt<N> {
|
||||
let mut ws: [u32; N] = [0; N];
|
||||
for i in 0..N {
|
||||
let k = 4*i;
|
||||
@ -149,7 +149,7 @@ impl<const N: usize> BigInt<N> {
|
||||
//------------------------------------
|
||||
// decimal printing
|
||||
|
||||
pub fn divmod_small(big: &BigInt<N>, modulus: u32) -> (BigInt<N> , u32) {
|
||||
pub fn divmod_small(big: BigInt<N>, modulus: u32) -> (BigInt<N> , u32) {
|
||||
let u64_modulus: u64 = modulus as u64;
|
||||
let mut carry: u32 = 0;
|
||||
let mut qs: [u32; N] = [0; N];
|
||||
@ -161,11 +161,11 @@ impl<const N: usize> BigInt<N> {
|
||||
(BigInt(qs), carry)
|
||||
}
|
||||
|
||||
pub fn to_decimal_string(input: &BigInt<N>) -> String {
|
||||
pub fn to_decimal_string(input: BigInt<N>) -> String {
|
||||
let mut digits: Vec<u8> = Vec::new();
|
||||
let mut big: BigInt<N> = input.clone();
|
||||
while( !BigInt::is_zero(&big) ) {
|
||||
let (q,r) = BigInt::divmod_small(&big, 10);
|
||||
while( !BigInt::is_zero(big) ) {
|
||||
let (q,r) = BigInt::divmod_small(big, 10);
|
||||
digits.push( 48 + (r as u8) );
|
||||
big = q;
|
||||
}
|
||||
@ -178,7 +178,7 @@ impl<const N: usize> BigInt<N> {
|
||||
|
||||
//------------------------------------
|
||||
|
||||
pub fn truncate1(big: &BigInt<{N+1}>) -> BigInt<N> {
|
||||
pub fn truncate1(big: BigInt<{N+1}>) -> BigInt<N> {
|
||||
// let small: [u32; N] = &big.limbs[0..N];
|
||||
let mut small: [u32; N] = [0; N];
|
||||
for i in 0..N { small[i] = big.0[i]; }
|
||||
@ -198,11 +198,11 @@ impl<const N: usize> BigInt<N> {
|
||||
//------------------------------------
|
||||
// comparison
|
||||
|
||||
pub fn is_zero(big: &BigInt<N>) -> bool {
|
||||
pub fn is_zero(big: BigInt<N>) -> bool {
|
||||
big.0.iter().all(|&x| x == 0)
|
||||
}
|
||||
|
||||
pub fn cmp(big1: &BigInt<N>, big2: &BigInt<N>) -> Ordering {
|
||||
pub fn cmp(big1: BigInt<N>, big2: BigInt<N>) -> Ordering {
|
||||
let mut res : Ordering = Ordering::Equal;
|
||||
for i in (0..N).rev() {
|
||||
if big1.0[i] < big2.0[i] {
|
||||
@ -222,7 +222,7 @@ impl<const N: usize> BigInt<N> {
|
||||
|
||||
#[inline(always)]
|
||||
#[unroll_for_loops]
|
||||
pub fn addCarry(big1: &BigInt<N>, big2: &BigInt<N>) -> (BigInt<N>, bool) {
|
||||
pub fn addCarry(big1: BigInt<N>, big2: BigInt<N>) -> (BigInt<N>, bool) {
|
||||
let mut c : bool = false;
|
||||
let mut zs : [u32; N] = [0; N];
|
||||
for i in 0..N {
|
||||
@ -236,7 +236,7 @@ impl<const N: usize> BigInt<N> {
|
||||
|
||||
#[inline(always)]
|
||||
#[unroll_for_loops]
|
||||
pub fn subBorrow(big1: &BigInt<N>, big2: &BigInt<N>) -> (BigInt<N>, bool) {
|
||||
pub fn subBorrow(big1: BigInt<N>, big2: BigInt<N>) -> (BigInt<N>, bool) {
|
||||
let mut c : bool = false;
|
||||
let mut zs : [u32; N] = [0; N];
|
||||
for i in 0..N {
|
||||
@ -248,12 +248,12 @@ impl<const N: usize> BigInt<N> {
|
||||
(big, c)
|
||||
}
|
||||
|
||||
pub fn add(big1: &BigInt<N>, big2: &BigInt<N>) -> BigInt<N> {
|
||||
pub fn add(big1: BigInt<N>, big2: BigInt<N>) -> BigInt<N> {
|
||||
let (out,_) = BigInt::addCarry(big1,big2);
|
||||
out
|
||||
}
|
||||
|
||||
pub fn sub(big1: &BigInt<N>, big2: &BigInt<N>) -> BigInt<N> {
|
||||
pub fn sub(big1: BigInt<N>, big2: BigInt<N>) -> BigInt<N> {
|
||||
let (out,_) = BigInt::subBorrow(big1,big2);
|
||||
out
|
||||
}
|
||||
@ -263,7 +263,7 @@ impl<const N: usize> BigInt<N> {
|
||||
|
||||
#[inline(always)]
|
||||
#[unroll_for_loops]
|
||||
pub fn is_lt_prime(big: &BigInt<N>) -> bool {
|
||||
pub fn is_lt_prime(big: BigInt<N>) -> bool {
|
||||
let mut less: bool = false;
|
||||
for i in (0..N).rev() {
|
||||
if big.0[i] < PRIME_ARRAY[i] {
|
||||
@ -278,13 +278,13 @@ impl<const N: usize> BigInt<N> {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn is_ge_prime(big: &BigInt<N>) -> bool {
|
||||
pub fn is_ge_prime(big: BigInt<N>) -> bool {
|
||||
!BigInt::is_lt_prime(big)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[unroll_for_loops]
|
||||
pub fn add_prime(big: &BigInt<N>) -> (BigInt<N>, bool) {
|
||||
pub fn add_prime(big: BigInt<N>) -> (BigInt<N>, bool) {
|
||||
let mut c : bool = false;
|
||||
let mut zs : [u32; N] = [0; N];
|
||||
for i in 0..N {
|
||||
@ -298,7 +298,7 @@ impl<const N: usize> BigInt<N> {
|
||||
|
||||
#[inline(always)]
|
||||
#[unroll_for_loops]
|
||||
pub fn subtract_prime(big: &BigInt<N>) -> (BigInt<N>, bool) {
|
||||
pub fn subtract_prime(big: BigInt<N>) -> (BigInt<N>, bool) {
|
||||
let mut c : bool = false;
|
||||
let mut zs : [u32; N] = [0; N];
|
||||
for i in 0..N {
|
||||
@ -311,9 +311,9 @@ impl<const N: usize> BigInt<N> {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn subtract_prime_if_necessary(big: &BigInt<N>) -> BigInt<N> {
|
||||
pub fn subtract_prime_if_necessary(big: BigInt<N>) -> BigInt<N> {
|
||||
if BigInt::is_lt_prime(big) {
|
||||
*big
|
||||
big
|
||||
}
|
||||
else {
|
||||
let (corrected, _) = BigInt::subtract_prime(big);
|
||||
@ -324,7 +324,7 @@ impl<const N: usize> BigInt<N> {
|
||||
//------------------------------------
|
||||
// multiplication
|
||||
|
||||
pub fn scale(scalar: u32, big2: &BigInt<N>) -> (BigInt<N>, u32) {
|
||||
pub fn scale(scalar: u32, big2: BigInt<N>) -> (BigInt<N>, u32) {
|
||||
let mut c : u32 = 0;
|
||||
let mut zs : [u32; N] = [0; N];
|
||||
for i in 0..N {
|
||||
@ -338,7 +338,7 @@ impl<const N: usize> BigInt<N> {
|
||||
|
||||
#[inline(always)]
|
||||
#[unroll_for_loops]
|
||||
pub fn scaleAdd(scalar: u32, vector: &BigInt<N>, add: &BigInt<N>) -> (BigInt<N>, u32) {
|
||||
pub fn scaleAdd(scalar: u32, vector: BigInt<N>, add: BigInt<N>) -> (BigInt<N>, u32) {
|
||||
let mut c : u32 = 0;
|
||||
let mut zs : [u32; N] = [0; N];
|
||||
for i in 0..N {
|
||||
@ -352,11 +352,11 @@ impl<const N: usize> BigInt<N> {
|
||||
|
||||
#[inline(always)]
|
||||
#[unroll_for_loops]
|
||||
pub fn multiply<const M: usize>(big1: &BigInt<N>, big2: &BigInt<M>) -> BigInt<{N+M}> {
|
||||
pub fn multiply<const M: usize>(big1: BigInt<N>, big2: BigInt<M>) -> BigInt<{N+M}> {
|
||||
let mut product : [u32; N+M] = [0; N+M];
|
||||
let mut state : [u32; N] = [0; N];
|
||||
for j in 0..M {
|
||||
let (scaled,carry) = BigInt::scaleAdd( big2.0[j], &big1, &BigInt(state) );
|
||||
let (scaled,carry) = BigInt::scaleAdd( big2.0[j], big1, BigInt(state) );
|
||||
product[j] = scaled.0[0];
|
||||
for i in 1..N { state[i-1] = scaled.0[i] }
|
||||
state[N-1] = carry;
|
||||
@ -369,18 +369,18 @@ impl<const N: usize> BigInt<N> {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn mul(big1: &BigInt<N>, big2: &BigInt<N>) -> BigInt<{N+N}> {
|
||||
pub fn mul(big1: BigInt<N>, big2: BigInt<N>) -> BigInt<{N+N}> {
|
||||
BigInt::multiply(big1,big2)
|
||||
}
|
||||
|
||||
// x*y + z
|
||||
#[inline(always)]
|
||||
pub fn mulAdd(big1: &BigInt<N>, big2: &BigInt<N>, big3: &BigInt<N>) -> BigInt<{N+N}> {
|
||||
pub fn mulAdd(big1: BigInt<N>, big2: BigInt<N>, big3: BigInt<N>) -> BigInt<{N+N}> {
|
||||
// first compute the product
|
||||
let mut product : [u32; N+N] = [0; N+N];
|
||||
let mut state : [u32; N] = [0; N];
|
||||
for j in 0..N {
|
||||
let (scaled,carry) = BigInt::scaleAdd( big2.0[j], &big1, &BigInt(state) );
|
||||
let (scaled,carry) = BigInt::scaleAdd( big2.0[j], big1, BigInt(state) );
|
||||
product[j] = scaled.0[0];
|
||||
for i in 1..N { state[i-1] = scaled.0[i] }
|
||||
state[N-1] = carry;
|
||||
@ -408,12 +408,12 @@ impl<const N: usize> BigInt<N> {
|
||||
|
||||
// x*y + (z << 256)
|
||||
#[inline(always)]
|
||||
pub fn mulAddShifted(big1: &BigInt<N>, big2: &BigInt<N>, big3: &BigInt<N>) -> BigInt<{N+N}> {
|
||||
pub fn mulAddShifted(big1: BigInt<N>, big2: BigInt<N>, big3: BigInt<N>) -> BigInt<{N+N}> {
|
||||
// first compute the product
|
||||
let mut product : [u32; N+N] = [0; N+N];
|
||||
let mut state : [u32; N] = [0; N];
|
||||
for j in 0..N {
|
||||
let (scaled,carry) = BigInt::scaleAdd( big2.0[j], &big1, &BigInt(state) );
|
||||
let (scaled,carry) = BigInt::scaleAdd( big2.0[j], big1, BigInt(state) );
|
||||
product[j] = scaled.0[0];
|
||||
for i in 1..N { state[i-1] = scaled.0[i] }
|
||||
state[N-1] = carry;
|
||||
@ -434,92 +434,15 @@ impl<const N: usize> BigInt<N> {
|
||||
}
|
||||
|
||||
// TODO: optimize this?!
|
||||
pub fn sqr_naive(big: &BigInt<N>) -> BigInt<{N+N}> {
|
||||
pub fn sqr_naive(big: BigInt<N>) -> BigInt<{N+N}> {
|
||||
BigInt::multiply(big,big)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn sqr(big: &BigInt<N>) -> BigInt<{N+N}> {
|
||||
pub fn sqr(big: BigInt<N>) -> BigInt<{N+N}> {
|
||||
BigInt::multiply(big,big)
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// half-assed optimization attempts...
|
||||
|
||||
/*
|
||||
pub fn sqr_also_slower(big: &BigInt<N>) -> BigInt<{N+N}> {
|
||||
|
||||
let mut mul_mtx : [[(u32,u32); N]; N] = [[(0,0); N]; N];
|
||||
for i in 0..N {
|
||||
for j in i..N {
|
||||
// i <= j
|
||||
let lo_hi = mulExt32( big.0[i] , big.0[j] );
|
||||
mul_mtx[i][j] = lo_hi;
|
||||
mul_mtx[j][i] = lo_hi;
|
||||
}
|
||||
}
|
||||
|
||||
let mut product : [u32; N+N] = [0; N+N];
|
||||
let mut state : [u32; N] = [0; N];
|
||||
for j in 0..N {
|
||||
// let (scaled,carry) = BigInt::scaleAdd( big2.0[j], &big1, &BigInt(state) );
|
||||
|
||||
let mut scaled : [u32; N] = [0; N];
|
||||
let mut carry : u32 = 0;
|
||||
for k in 0..N {
|
||||
// scalar = big2.0[j]
|
||||
// vector = big1
|
||||
let (lo,hi) = u64AddAdd32( mul_mtx[j][k], carry, state[k] );
|
||||
scaled[k] = lo;
|
||||
carry = hi;
|
||||
}
|
||||
|
||||
product[j] = scaled[0];
|
||||
for i in 1..N { state[i-1] = scaled[i] }
|
||||
state[N-1] = carry;
|
||||
}
|
||||
|
||||
for i in 0..N {
|
||||
product[i+N] = state[i]
|
||||
}
|
||||
|
||||
BigInt(product)
|
||||
}
|
||||
|
||||
// -----------------------------------
|
||||
|
||||
pub fn sqr_is_actually_slower(big: &BigInt<N>) -> BigInt<{N+N}> {
|
||||
|
||||
let mut product : [u32; N+N] = [0; N+N];
|
||||
let mut carry : u64 = 0;
|
||||
|
||||
for k in 0..(N+N-1) {
|
||||
|
||||
let mut sum_lo: u64 = carry;
|
||||
let mut sum_hi: u64 = 0;
|
||||
for i in 0..min(N,k+1) {
|
||||
let j = k - i;
|
||||
if j < N && i <= j {
|
||||
let (lo,hi) = mulExt32( big.limbs[i], big.limbs[j] );
|
||||
sum_lo += (lo as u64);
|
||||
sum_hi += (hi as u64);
|
||||
if i < j {
|
||||
sum_lo += (lo as u64);
|
||||
sum_hi += (hi as u64);
|
||||
}
|
||||
}
|
||||
}
|
||||
let (u,v) = takeApart64(sum_lo);
|
||||
product[k] = u;
|
||||
carry = sum_hi + (v as u64);
|
||||
}
|
||||
|
||||
product[N+N-1] = (carry as u32);
|
||||
BigInt { limbs: product }
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@ -33,7 +33,7 @@ impl fmt::Display for Felt {
|
||||
}
|
||||
|
||||
impl Felt {
|
||||
pub fn print(s: &str, A: &Felt) {
|
||||
pub fn debug_print(s: &str, A: Felt) {
|
||||
println!("{} = {}", s, A);
|
||||
}
|
||||
}
|
||||
@ -43,33 +43,33 @@ impl Felt {
|
||||
|
||||
impl Neg for Felt {
|
||||
type Output = Self;
|
||||
fn neg(self) -> Self { Felt::neg(&self) }
|
||||
fn neg(self) -> Self { Felt::neg(self) }
|
||||
}
|
||||
|
||||
impl Add for Felt {
|
||||
type Output = Self;
|
||||
fn add(self, other: Self) -> Self { Felt::add(&self,&other) }
|
||||
fn add(self, other: Self) -> Self { Felt::add(self, other) }
|
||||
}
|
||||
|
||||
impl Sub for Felt {
|
||||
type Output = Self;
|
||||
fn sub(self, other: Self) -> Self { Felt::sub(&self,&other) }
|
||||
fn sub(self, other: Self) -> Self { Felt::sub(self, other) }
|
||||
}
|
||||
|
||||
impl Mul for Felt {
|
||||
type Output = Self;
|
||||
fn mul(self, other: Self) -> Self { Felt::mul(&self,&other) }
|
||||
fn mul(self, other: Self) -> Self { Felt::mul(self, other) }
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// conversion traits
|
||||
|
||||
impl From<Mont> for Felt {
|
||||
fn from(mont: Mont) -> Self { Felt::from_mont(&mont) }
|
||||
fn from(mont: Mont) -> Self { Felt::from_mont(mont) }
|
||||
}
|
||||
|
||||
impl Into<Mont> for Felt {
|
||||
fn into(self: Self) -> Mont { Felt::to_mont(&self) }
|
||||
fn into(self: Self) -> Mont { Felt::to_mont(self) }
|
||||
}
|
||||
|
||||
// note: we dont implement `From<BigInt<8>>` as it's unsafe,
|
||||
@ -106,7 +106,7 @@ impl Felt {
|
||||
|
||||
pub fn checked_make( xs: [u32; 8] ) -> Felt {
|
||||
let big: Big = BigInt::make(xs);
|
||||
if BigInt::is_lt_prime(&big) {
|
||||
if BigInt::is_lt_prime(big) {
|
||||
Felt(big)
|
||||
}
|
||||
else {
|
||||
@ -114,40 +114,40 @@ impl Felt {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_valid(felt: &Felt) -> bool {
|
||||
BigInt::is_lt_prime(&felt.0)
|
||||
pub fn is_valid(felt: Felt) -> bool {
|
||||
BigInt::is_lt_prime(felt.0)
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
|
||||
pub fn to_le_bytes(felt: &Felt) -> [u8; 32] {
|
||||
BigInt::to_le_bytes(&felt.0)
|
||||
pub fn to_le_bytes(felt: Felt) -> [u8; 32] {
|
||||
BigInt::to_le_bytes(felt.0)
|
||||
}
|
||||
|
||||
pub fn unsafe_from_le_bytes(buf: &[u8; 32]) -> Felt {
|
||||
pub fn unsafe_from_le_bytes(buf: [u8; 32]) -> Felt {
|
||||
Felt(BigInt::from_le_bytes(buf))
|
||||
}
|
||||
|
||||
pub fn to_be_bytes(felt: &Felt) -> [u8; 32] {
|
||||
BigInt::to_be_bytes(&felt.0)
|
||||
pub fn to_be_bytes(felt: Felt) -> [u8; 32] {
|
||||
BigInt::to_be_bytes(felt.0)
|
||||
}
|
||||
|
||||
pub fn unsafe_from_be_bytes(buf: &[u8; 32]) -> Felt {
|
||||
pub fn unsafe_from_be_bytes(buf: [u8; 32]) -> Felt {
|
||||
Felt(BigInt::from_be_bytes(buf))
|
||||
}
|
||||
|
||||
// convert to Montgomery representation
|
||||
pub fn to_mont(fld: &Felt) -> Mont {
|
||||
Mont::unsafe_convert_from_big( &fld.0 )
|
||||
pub fn to_mont(fld: Felt) -> Mont {
|
||||
Mont::unsafe_convert_from_big( fld.0 )
|
||||
}
|
||||
|
||||
// convert from Montgomery representation
|
||||
pub fn from_mont(mont: &Mont) -> Felt {
|
||||
Felt(Mont::convert_to_big(&mont))
|
||||
pub fn from_mont(mont: Mont) -> Felt {
|
||||
Felt(Mont::convert_to_big(mont))
|
||||
}
|
||||
|
||||
pub fn to_decimal_string(input: &Felt) -> String {
|
||||
BigInt::to_decimal_string(&input.0)
|
||||
pub fn to_decimal_string(input: Felt) -> String {
|
||||
BigInt::to_decimal_string(input.0)
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
@ -160,25 +160,25 @@ impl Felt {
|
||||
Felt(BigInt::from_u32(x))
|
||||
}
|
||||
|
||||
pub fn neg(fld: &Felt) -> Felt {
|
||||
if BigInt::is_zero(&fld.0) {
|
||||
pub fn neg(fld: Felt) -> Felt {
|
||||
if BigInt::is_zero(fld.0) {
|
||||
Felt::zero()
|
||||
}
|
||||
else {
|
||||
Felt(BigInt::sub(&FIELD_PRIME, &fld.0))
|
||||
Felt(BigInt::sub(FIELD_PRIME, fld.0))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(fld1: &Felt, fld2: &Felt) -> Felt {
|
||||
let (A, _) = BigInt::addCarry(&fld1.0, &fld2.0);
|
||||
let B = BigInt::subtract_prime_if_necessary(&A);
|
||||
pub fn add(fld1: Felt, fld2: Felt) -> Felt {
|
||||
let (A, _) = BigInt::addCarry(fld1.0, fld2.0);
|
||||
let B = BigInt::subtract_prime_if_necessary(A);
|
||||
Felt(B)
|
||||
}
|
||||
|
||||
pub fn sub(fld1: &Felt, fld2: &Felt) -> Felt {
|
||||
let (big, carry) = BigInt::subBorrow(&fld1.0, &fld2.0);
|
||||
pub fn sub(fld1: Felt, fld2: Felt) -> Felt {
|
||||
let (big, carry) = BigInt::subBorrow(fld1.0, fld2.0);
|
||||
if carry {
|
||||
let (corrected, _) = BigInt::add_prime(&big);
|
||||
let (corrected, _) = BigInt::add_prime(big);
|
||||
Felt(corrected)
|
||||
}
|
||||
else {
|
||||
@ -186,19 +186,19 @@ impl Felt {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dbl(fld: &Felt) -> Felt {
|
||||
Felt::add(&fld, &fld)
|
||||
pub fn dbl(fld: Felt) -> Felt {
|
||||
Felt::add(fld, fld)
|
||||
}
|
||||
|
||||
pub fn sqr(fld: &Felt) -> Felt {
|
||||
let mont = Felt::to_mont(&fld);
|
||||
Felt::from_mont(&Mont::sqr(&mont))
|
||||
pub fn sqr(fld: Felt) -> Felt {
|
||||
let mont = Felt::to_mont(fld);
|
||||
Felt::from_mont(Mont::sqr(mont))
|
||||
}
|
||||
|
||||
pub fn mul(fld1: &Felt, fld2: &Felt) -> Felt {
|
||||
let mont1 = Felt::to_mont(&fld1);
|
||||
let mont2 = Felt::to_mont(&fld2);
|
||||
Felt::from_mont(&Mont::mul(&mont1,&mont2))
|
||||
pub fn mul(fld1: Felt, fld2: Felt) -> Felt {
|
||||
let mont1 = Felt::to_mont(fld1);
|
||||
let mont2 = Felt::to_mont(fld2);
|
||||
Felt::from_mont(Mont::mul(mont1,mont2))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ impl fmt::Debug for Mont {
|
||||
// prints the standard representation
|
||||
impl fmt::Display for Mont {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let big: Big = Mont::convert_to_big(&self);
|
||||
let big: Big = Mont::convert_to_big(*self);
|
||||
f.write_fmt(format_args!("{}",big))
|
||||
}
|
||||
}
|
||||
@ -52,22 +52,22 @@ impl fmt::Display for Mont {
|
||||
|
||||
impl Neg for Mont {
|
||||
type Output = Self;
|
||||
fn neg(self) -> Self { Mont::neg(&self) }
|
||||
fn neg(self) -> Self { Mont::neg(self) }
|
||||
}
|
||||
|
||||
impl Add for Mont {
|
||||
type Output = Self;
|
||||
fn add(self, other: Self) -> Self { Mont::add(&self,&other) }
|
||||
fn add(self, other: Self) -> Self { Mont::add(self, other) }
|
||||
}
|
||||
|
||||
impl Sub for Mont {
|
||||
type Output = Self;
|
||||
fn sub(self, other: Self) -> Self { Mont::sub(&self,&other) }
|
||||
fn sub(self, other: Self) -> Self { Mont::sub(self, other) }
|
||||
}
|
||||
|
||||
impl Mul for Mont {
|
||||
type Output = Self;
|
||||
fn mul(self, other: Self) -> Self { Mont::mul(&self,&other) }
|
||||
fn mul(self, other: Self) -> Self { Mont::mul(self, other) }
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@ -96,37 +96,37 @@ impl Mont {
|
||||
Mont(BigInt::make(xs))
|
||||
}
|
||||
|
||||
pub fn is_valid(mont: &Mont) -> bool {
|
||||
BigInt::is_lt_prime(&mont.0)
|
||||
pub fn is_valid(mont: Mont) -> bool {
|
||||
BigInt::is_lt_prime(mont.0)
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
// to/from bytes
|
||||
|
||||
// note: this exports the Montgomery representation!
|
||||
pub fn to_le_bytes(mont: &Mont) -> [u8; 32] {
|
||||
BigInt::to_le_bytes(&mont.0)
|
||||
pub fn to_le_bytes(mont: Mont) -> [u8; 32] {
|
||||
BigInt::to_le_bytes(mont.0)
|
||||
}
|
||||
|
||||
// note: this assumes the input is already in Montgomery representation!
|
||||
pub fn unsafe_from_le_bytes(buf: &[u8; 32]) -> Mont {
|
||||
pub fn unsafe_from_le_bytes(buf: [u8; 32]) -> Mont {
|
||||
let big = BigInt::from_le_bytes(buf);
|
||||
Mont(big)
|
||||
}
|
||||
|
||||
// note: this exports the Montgomery representation!
|
||||
pub fn to_be_bytes(mont: &Mont) -> [u8; 32] {
|
||||
BigInt::to_be_bytes(&mont.0)
|
||||
pub fn to_be_bytes(mont: Mont) -> [u8; 32] {
|
||||
BigInt::to_be_bytes(mont.0)
|
||||
}
|
||||
|
||||
// note: this assumes the input is already in Montgomery representation!
|
||||
pub fn unsafe_from_be_bytes(buf: &[u8; 32]) -> Mont {
|
||||
pub fn unsafe_from_be_bytes(buf: [u8; 32]) -> Mont {
|
||||
let big = BigInt::from_be_bytes(buf);
|
||||
Mont(big)
|
||||
}
|
||||
|
||||
pub fn to_decimal_string(input: &Mont) -> String {
|
||||
BigInt::to_decimal_string(&Mont::convert_to_big(&input))
|
||||
pub fn to_decimal_string(input: Mont) -> String {
|
||||
BigInt::to_decimal_string(Mont::convert_to_big(input))
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
@ -136,27 +136,27 @@ impl Mont {
|
||||
Mont(BigInt::zero())
|
||||
}
|
||||
|
||||
pub fn neg(mont: &Mont) -> Mont {
|
||||
if BigInt::is_zero(&mont.0) {
|
||||
pub fn neg(mont: Mont) -> Mont {
|
||||
if BigInt::is_zero(mont.0) {
|
||||
Mont::zero()
|
||||
}
|
||||
else {
|
||||
Mont(BigInt::sub(&FIELD_PRIME, &mont.0))
|
||||
Mont(BigInt::sub(FIELD_PRIME, mont.0))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn add(mont1: &Mont, mont2: &Mont) -> Mont {
|
||||
let (A, _) = BigInt::addCarry(&mont1.0, &mont2.0);
|
||||
let B = BigInt::subtract_prime_if_necessary(&A);
|
||||
pub fn add(mont1: Mont, mont2: Mont) -> Mont {
|
||||
let (A, _) = BigInt::addCarry(mont1.0, mont2.0);
|
||||
let B = BigInt::subtract_prime_if_necessary(A);
|
||||
Mont(B)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn sub(mont1: &Mont, mont2: &Mont) -> Mont {
|
||||
let (big, carry) = BigInt::subBorrow(&mont1.0, &mont2.0);
|
||||
pub fn sub(mont1: Mont, mont2: Mont) -> Mont {
|
||||
let (big, carry) = BigInt::subBorrow(mont1.0, mont2.0);
|
||||
if carry {
|
||||
let (corrected, _) = BigInt::add_prime(&big);
|
||||
let (corrected, _) = BigInt::add_prime(big);
|
||||
Mont(corrected)
|
||||
}
|
||||
else {
|
||||
@ -165,8 +165,8 @@ impl Mont {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn dbl(mont: &Mont) -> Mont {
|
||||
Mont::add(&mont, &mont)
|
||||
pub fn dbl(mont: Mont) -> Mont {
|
||||
Mont::add(mont, mont)
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
@ -200,15 +200,15 @@ impl Mont {
|
||||
for i in 0..9 { S[i] = T[8+i]; }
|
||||
|
||||
let A : BigInt<9> = BigInt::make(S);
|
||||
let (B,c) : (BigInt<9>,bool) = BigInt::subBorrow( &A , &PRIME_EXT );
|
||||
let (B,c) : (BigInt<9>,bool) = BigInt::subBorrow( A , PRIME_EXT );
|
||||
|
||||
if c {
|
||||
// `A - prime < 0` is equivalent to `A < prime`
|
||||
BigInt::truncate1(&A)
|
||||
BigInt::truncate1(A)
|
||||
}
|
||||
else {
|
||||
// `A - prime >= 0` is equivalent to `A >= prime`
|
||||
BigInt::truncate1(&B)
|
||||
BigInt::truncate1(B)
|
||||
}
|
||||
}
|
||||
*/
|
||||
@ -240,23 +240,23 @@ impl Mont {
|
||||
for i in 0..8 { S[i] = T[8+i]; }
|
||||
|
||||
let A : Big = BigInt::make(S);
|
||||
let B : Big = BigInt::subtract_prime_if_necessary(&A);
|
||||
let B : Big = BigInt::subtract_prime_if_necessary(A);
|
||||
B
|
||||
}
|
||||
|
||||
pub fn sqr(mont: &Mont) -> Mont {
|
||||
let large = BigInt::sqr(&mont.0);
|
||||
pub fn sqr(mont: Mont) -> Mont {
|
||||
let large = BigInt::sqr(mont.0);
|
||||
Mont(Mont::redc(large))
|
||||
}
|
||||
|
||||
pub fn mul(mont1: &Mont, mont2: &Mont) -> Mont {
|
||||
let large = BigInt::mul(&mont1.0, &mont2.0);
|
||||
pub fn mul(mont1: Mont, mont2: Mont) -> Mont {
|
||||
let large = BigInt::mul(mont1.0, mont2.0);
|
||||
Mont(Mont::redc(large))
|
||||
}
|
||||
|
||||
// x*y + z
|
||||
pub fn mulAdd(mont1: &Mont, mont2: &Mont, mont3: &Mont) -> Mont {
|
||||
let large = BigInt::mulAddShifted(&mont1.0, &mont2.0, &mont3.0);
|
||||
pub fn mulAdd(mont1: Mont, mont2: Mont, mont3: Mont) -> Mont {
|
||||
let large = BigInt::mulAddShifted(mont1.0, mont2.0, mont3.0);
|
||||
Mont(Mont::redc(large))
|
||||
}
|
||||
|
||||
@ -265,13 +265,13 @@ impl Mont {
|
||||
|
||||
// this does conversion from the standard representation
|
||||
// we assume the input is in the range `[0..p-1]`
|
||||
pub fn unsafe_convert_from_big(input: &Big) -> Mont {
|
||||
let mont: Mont = Mont(*input);
|
||||
Mont::mul( &mont , &MONT_R2 )
|
||||
pub fn unsafe_convert_from_big(input: Big) -> Mont {
|
||||
let mont: Mont = Mont(input);
|
||||
Mont::mul( mont , MONT_R2 )
|
||||
}
|
||||
|
||||
// this does conversion to the standard representation
|
||||
pub fn convert_to_big(mont: &Mont) -> Big {
|
||||
pub fn convert_to_big(mont: Mont) -> Big {
|
||||
let mut tmp: [u32; 16] = [0; 16];
|
||||
for i in 0..8 { tmp[i] = BigInt::to_limbs(mont.0)[i] }
|
||||
Mont::redc( BigInt::make(tmp) )
|
||||
@ -281,7 +281,7 @@ impl Mont {
|
||||
// and convert to Montgomery representation
|
||||
pub fn convert_from_u32(x: u32) -> Mont {
|
||||
let big: Big = BigInt::from_u32(x);
|
||||
Mont::unsafe_convert_from_big( &big )
|
||||
Mont::unsafe_convert_from_big( big )
|
||||
}
|
||||
|
||||
}
|
||||
@ -290,16 +290,16 @@ impl Mont {
|
||||
// debug printing
|
||||
|
||||
impl Mont {
|
||||
pub fn print_internal(s: &str, A: &Mont) {
|
||||
pub fn debug_print_internal(s: &str, A: Mont) {
|
||||
println!("{} = [{}]", s, A.0);
|
||||
}
|
||||
|
||||
pub fn print_standard(s: &str, A: &Mont) {
|
||||
pub fn debug_print_standard(s: &str, A: Mont) {
|
||||
println!("{} = {}", s, Mont::convert_to_big(A) ) ;
|
||||
}
|
||||
|
||||
pub fn print(s: &str, A: &Mont) {
|
||||
Mont::print_standard(&s, &A);
|
||||
pub fn debug_print(s: &str, A: Mont) {
|
||||
Mont::debug_print_standard(s, A);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -47,9 +47,9 @@ const fn internal_round_count(T: usize) -> usize {
|
||||
|
||||
#[inline(always)]
|
||||
fn sbox(x: Mont) -> Mont {
|
||||
let x2 = Mont::sqr(&x );
|
||||
let x4 = Mont::sqr(&x2);
|
||||
Mont::mul(&x,&x4)
|
||||
let x2 = Mont::sqr(x );
|
||||
let x4 = Mont::sqr(x2);
|
||||
Mont::mul(x,x4)
|
||||
}
|
||||
|
||||
fn matrix_mul<const T: usize>(input: [Mont; T], mtx: [Mont; T*T]) -> [Mont; T] {
|
||||
@ -57,7 +57,7 @@ fn matrix_mul<const T: usize>(input: [Mont; T], mtx: [Mont; T*T]) -> [Mont; T] {
|
||||
for i in 0..T {
|
||||
let mut acc: Mont = Mont::zero();
|
||||
for j in 0..T {
|
||||
acc = Mont::mulAdd( &mtx[j*T+i] , &input[j] , &acc );
|
||||
acc = Mont::mulAdd( mtx[j*T+i] , input[j] , acc );
|
||||
}
|
||||
out[i] = acc;
|
||||
}
|
||||
@ -68,25 +68,25 @@ fn mix_S<const T: usize>(input: [Mont; T], scoeffs: &[Mont]) -> [Mont; T] {
|
||||
let mut out: [Mont; T] = [Mont::zero(); T];
|
||||
let mut acc: Mont = Mont::zero();
|
||||
for j in 0..T {
|
||||
acc = Mont::mulAdd( &scoeffs[j] , &input[j] , &acc );
|
||||
acc = Mont::mulAdd( scoeffs[j] , input[j] , acc );
|
||||
}
|
||||
out[0] = acc;
|
||||
for j in 1..T {
|
||||
out[j] = Mont::mulAdd( &scoeffs[T+j-1] , &input[0] , &input[j] );
|
||||
out[j] = Mont::mulAdd( scoeffs[T+j-1] , input[0] , input[j] );
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn internal_round<const T: usize>(rc: Mont, scoeffs: &[Mont], input: [Mont; T]) -> [Mont; T] {
|
||||
let mut xs: [Mont; T] = input;
|
||||
xs[0] = Mont::add( &sbox( xs[0] ) , &rc );
|
||||
xs[0] = Mont::add( sbox( xs[0] ) , rc );
|
||||
mix_S::<T>(xs, scoeffs)
|
||||
}
|
||||
|
||||
fn external_round<const T: usize>(rcs: &[Mont], input: [Mont; T], mtx: [Mont; T*T]) -> [Mont; T] {
|
||||
let mut xs: [Mont; T] = [Mont::zero(); T];
|
||||
for j in 0..T {
|
||||
xs[j] = Mont::add( &sbox( input[j] ) , &rcs[j] );
|
||||
xs[j] = Mont::add( sbox( input[j] ) , rcs[j] );
|
||||
}
|
||||
matrix_mul::<T>(xs, mtx)
|
||||
}
|
||||
@ -119,7 +119,7 @@ pub fn permute_mont_T2(input: [Mont; 2]) -> [Mont; 2] {
|
||||
|
||||
let mut state: [Mont; T] = input;
|
||||
for j in 0..T {
|
||||
state[j] = Mont::add( &state[j] , &C[j] );
|
||||
state[j] = Mont::add( state[j] , C[j] );
|
||||
}
|
||||
for i in 0..4 {
|
||||
let rcs: &[Mont] = &C[ ((i+1)*T) .. ((i+2)*T) ];
|
||||
@ -155,7 +155,7 @@ pub fn permute_mont_T3(input: [Mont; 3]) -> [Mont; 3] {
|
||||
|
||||
let mut state: [Mont; T] = input;
|
||||
for j in 0..T {
|
||||
state[j] = Mont::add( &state[j] , &C[j] );
|
||||
state[j] = Mont::add( state[j] , C[j] );
|
||||
}
|
||||
for i in 0..4 {
|
||||
let rcs: &[Mont] = &C[ ((i+1)*T) .. ((i+2)*T) ];
|
||||
@ -191,7 +191,7 @@ pub fn permute_mont_T4(input: [Mont; 4]) -> [Mont; 4] {
|
||||
|
||||
let mut state: [Mont; T] = input;
|
||||
for j in 0..T {
|
||||
state[j] = Mont::add( &state[j] , &C[j] );
|
||||
state[j] = Mont::add( state[j] , C[j] );
|
||||
}
|
||||
for i in 0..4 {
|
||||
let rcs: &[Mont] = &C[ ((i+1)*T) .. ((i+2)*T) ];
|
||||
@ -227,7 +227,7 @@ pub fn permute_mont_T5(input: [Mont; 5]) -> [Mont; 5] {
|
||||
|
||||
let mut state: [Mont; T] = input;
|
||||
for j in 0..T {
|
||||
state[j] = Mont::add( &state[j] , &C[j] );
|
||||
state[j] = Mont::add( state[j] , C[j] );
|
||||
}
|
||||
for i in 0..4 {
|
||||
let rcs: &[Mont] = &C[ ((i+1)*T) .. ((i+2)*T) ];
|
||||
@ -254,43 +254,43 @@ pub fn permute_mont_T5(input: [Mont; 5]) -> [Mont; 5] {
|
||||
pub fn compress_1(input: Felt) -> Felt {
|
||||
let mut state: [Mont; 2] =
|
||||
[ Mont::zero()
|
||||
, Felt::to_mont(&input)
|
||||
, Felt::to_mont(input)
|
||||
];
|
||||
state = permute_mont_T2(state);
|
||||
Felt::from_mont(&state[0])
|
||||
Felt::from_mont(state[0])
|
||||
}
|
||||
|
||||
pub fn compress_2(input: [Felt;2]) -> Felt {
|
||||
let mut state: [Mont; 3] =
|
||||
[ Mont::zero()
|
||||
, Felt::to_mont(&input[0])
|
||||
, Felt::to_mont(&input[1])
|
||||
, Felt::to_mont(input[0])
|
||||
, Felt::to_mont(input[1])
|
||||
];
|
||||
state = permute_mont_T3(state);
|
||||
Felt::from_mont(&state[0])
|
||||
Felt::from_mont(state[0])
|
||||
}
|
||||
|
||||
pub fn compress_3(input: [Felt;3]) -> Felt {
|
||||
let mut state: [Mont; 4] =
|
||||
[ Mont::zero()
|
||||
, Felt::to_mont(&input[0])
|
||||
, Felt::to_mont(&input[1])
|
||||
, Felt::to_mont(&input[2])
|
||||
, Felt::to_mont(input[0])
|
||||
, Felt::to_mont(input[1])
|
||||
, Felt::to_mont(input[2])
|
||||
];
|
||||
state = permute_mont_T4(state);
|
||||
Felt::from_mont(&state[0])
|
||||
Felt::from_mont(state[0])
|
||||
}
|
||||
|
||||
pub fn compress_4(input: [Felt;4]) -> Felt {
|
||||
let mut state: [Mont; 5] =
|
||||
[ Mont::zero()
|
||||
, Felt::to_mont(&input[0])
|
||||
, Felt::to_mont(&input[1])
|
||||
, Felt::to_mont(&input[2])
|
||||
, Felt::to_mont(&input[3])
|
||||
, Felt::to_mont(input[0])
|
||||
, Felt::to_mont(input[1])
|
||||
, Felt::to_mont(input[2])
|
||||
, Felt::to_mont(input[3])
|
||||
];
|
||||
state = permute_mont_T5(state);
|
||||
Felt::from_mont(&state[0])
|
||||
Felt::from_mont(state[0])
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@ -13,41 +13,41 @@ pub type MontTriple = (Mont,Mont,Mont);
|
||||
|
||||
#[inline(always)]
|
||||
fn sbox(x: Mont) -> Mont {
|
||||
let x2 = Mont::sqr(&x );
|
||||
let x4 = Mont::sqr(&x2);
|
||||
Mont::mul(&x,&x4)
|
||||
let x2 = Mont::sqr(x );
|
||||
let x4 = Mont::sqr(x2);
|
||||
Mont::mul(x,x4)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn add3(x: Mont, y: Mont, z: Mont) -> Mont {
|
||||
Mont::add(&Mont::add(&x,&y),&z)
|
||||
Mont::add(Mont::add(x,y),z)
|
||||
}
|
||||
|
||||
fn linear(input: MontTriple) -> MontTriple {
|
||||
let s = add3(input.0, input.1, input.2);
|
||||
( Mont::add( &s , &input.0 )
|
||||
, Mont::add( &s , &input.1 )
|
||||
, Mont::add( &s , &input.2 )
|
||||
( Mont::add( s , input.0 )
|
||||
, Mont::add( s , input.1 )
|
||||
, Mont::add( s , input.2 )
|
||||
)
|
||||
}
|
||||
|
||||
fn internal_round(rc: Mont, input: MontTriple) -> MontTriple {
|
||||
let x = sbox( Mont::add( &input.0 , &rc ) );
|
||||
let x = sbox( Mont::add( input.0 , rc ) );
|
||||
let s = add3( x , input.1 , input.2 );
|
||||
( Mont::add( &s , &x )
|
||||
, Mont::add( &s , &input.1 )
|
||||
, Mont::add( &s , &Mont::dbl(&input.2) )
|
||||
( Mont::add( s , x )
|
||||
, Mont::add( s , input.1 )
|
||||
, Mont::add( s , Mont::dbl(input.2) )
|
||||
)
|
||||
}
|
||||
|
||||
fn external_round(rcs: MontTriple, input: MontTriple) -> MontTriple {
|
||||
let x = sbox( Mont::add( &input.0 , &rcs.0 ) );
|
||||
let y = sbox( Mont::add( &input.1 , &rcs.1 ) );
|
||||
let z = sbox( Mont::add( &input.2 , &rcs.2 ) );
|
||||
let x = sbox( Mont::add( input.0 , rcs.0 ) );
|
||||
let y = sbox( Mont::add( input.1 , rcs.1 ) );
|
||||
let z = sbox( Mont::add( input.2 , rcs.2 ) );
|
||||
let s = add3( x , y , z );
|
||||
( Mont::add( &s , &x )
|
||||
, Mont::add( &s , &y )
|
||||
, Mont::add( &s , &z )
|
||||
( Mont::add( s , x )
|
||||
, Mont::add( s , y )
|
||||
, Mont::add( s , z )
|
||||
)
|
||||
}
|
||||
|
||||
@ -61,12 +61,12 @@ pub fn permute_mont(input: MontTriple) -> MontTriple {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
pub fn permute_felt_iterated(input: &FeltTriple, count: usize) -> FeltTriple {
|
||||
pub fn permute_felt_iterated(input: FeltTriple, count: usize) -> FeltTriple {
|
||||
|
||||
let mut state: MontTriple =
|
||||
( Felt::to_mont(&input.0)
|
||||
, Felt::to_mont(&input.1)
|
||||
, Felt::to_mont(&input.2)
|
||||
( Felt::to_mont(input.0)
|
||||
, Felt::to_mont(input.1)
|
||||
, Felt::to_mont(input.2)
|
||||
);
|
||||
|
||||
for _i in 0..count {
|
||||
@ -74,16 +74,16 @@ pub fn permute_felt_iterated(input: &FeltTriple, count: usize) -> FeltTriple {
|
||||
}
|
||||
|
||||
let out: FeltTriple =
|
||||
( Felt::from_mont(&state.0)
|
||||
, Felt::from_mont(&state.1)
|
||||
, Felt::from_mont(&state.2)
|
||||
( Felt::from_mont(state.0)
|
||||
, Felt::from_mont(state.1)
|
||||
, Felt::from_mont(state.2)
|
||||
);
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
pub fn permute_felt(input: &FeltTriple) -> FeltTriple {
|
||||
permute_felt_iterated(&input, 1)
|
||||
pub fn permute_felt(input: FeltTriple) -> FeltTriple {
|
||||
permute_felt_iterated(input, 1)
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user