[BACK]Return to dmul CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2018 / lib

File: [local] / OpenXM_contrib2 / asir2018 / lib / dmul (download)

Revision 1.1, Wed Sep 19 05:45:08 2018 UTC (5 years, 7 months ago) by noro
Branch: MAIN
CVS Tags: HEAD

Added asir2018 for implementing full-gmp asir.

/*
 * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED 
 * All rights reserved.
 * 
 * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
 * non-exclusive and royalty-free license to use, copy, modify and
 * redistribute, solely for non-commercial and non-profit purposes, the
 * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
 * conditions of this Agreement. For the avoidance of doubt, you acquire
 * only a limited right to use the SOFTWARE hereunder, and FLL or any
 * third party developer retains all rights, including but not limited to
 * copyrights, in and to the SOFTWARE.
 * 
 * (1) FLL does not grant you a license in any way for commercial
 * purposes. You may use the SOFTWARE only for non-commercial and
 * non-profit purposes only, such as academic, research and internal
 * business use.
 * (2) The SOFTWARE is protected by the Copyright Law of Japan and
 * international copyright treaties. If you make copies of the SOFTWARE,
 * with or without modification, as permitted hereunder, you shall affix
 * to all such copies of the SOFTWARE the above copyright notice.
 * (3) An explicit reference to this SOFTWARE and its copyright owner
 * shall be made on your publication or presentation in any form of the
 * results obtained by use of the SOFTWARE.
 * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
 * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
 * for such modification or the source code of the modified part of the
 * SOFTWARE.
 * 
 * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
 * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
 * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
 * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
 * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
 * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
 * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
 * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
 * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
 * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
 * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
 * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
 * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
 * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
 * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
 *
 * $OpenXM: OpenXM_contrib2/asir2018/lib/dmul,v 1.1 2018/09/19 05:45:08 noro Exp $
*/
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)>(b)?(b):(a))

/* CAUTION: functions in this file are experimental. */

/*
	return: F1*F2
	if option 'proc' is supplied as a list of server id's, 
	F1*F2 is calculated by distributed computation.
*/

def d_mul(F1,F2)
{
	Procs = getopt(proc);
	if ( type(Procs) == -1 )
		Procs = [];
	Mod = getopt(mod);
	if ( type(Mod) == -1 )
		Mod = 0;
	NP = length(Procs)+1;
	V =var(F1);
	if ( !V ) {
		T = F1*F2;
		if ( Mod )
			return T % Mod;
		else
			return T;
	}
	D1 = deg(F1,V);
	D2 = deg(F2,V);
	Dmin = MIN(D1,D2);
	Dfft = p_mag(D1+D2+1)+1;
	Bound = maxblen(F1)+maxblen(F2)+p_mag(Dmin)+1;
	if ( Bound < 32 )
		Bound = 32;
	Marray = newvect(NP);
	MIarray = newvect(NP);
	for ( I = 0; I < NP; I++ ) {
		Marray[I] = 1;
		MIarray[I] = [];
	}

	for ( M = 1, I = 0, J = 0; p_mag(M) <= Bound; J = (J+1)%NP ) {
		T = get_next_fft_prime(I,Dfft);
		if ( !T )
			error("fft_mul_d : fft_prime exhausted.");
		Marray[J] *= T[1];
		MIarray[J] = cons(T[0],MIarray[J]);
		M *= T[1];
		I = T[0]+1;
	}
	/* Now, 
		Marray[J] = FFTprime[Marray[J][0]]*...*FFTprime[Marray[J][...]]
		M = Marray[0]*...*Marray[NP-1]
	*/
	C = newvect(NP);
	T0 = time();
	for ( J = 0; J < NP-1; J++ )
		ox_cmo_rpc(Procs[J],"call_umul",F1,F2,MIarray[J],Marray[J],M);
	T1 = time();
	R = call_umul(F1,F2,MIarray[NP-1],Marray[NP-1],M);
	T2 = time();
	for ( J = 0; J < NP-1; J++ )
		R += ox_pop_cmo(Procs[J]);
	T3 = time();
/*	print(["send",T1[3]-T0[3],"self",T2[3]-T1[3],"recv",T3[3]-T2[3]]); */
	if ( Mod )
		return (R%M)%Mod;
	else
		return uadj_coef(R%M,M,ishift(M,1));
}

/*
	return: F1^2
	if option 'proc' is supplied as a list of server id's, 
	F1^2 is calculated by distributed computation.
*/

def d_square(F1)
{
	Procs = getopt(proc);
	if ( type(Procs) == -1 )
		Procs = [];
	Mod = getopt(mod);
	if ( type(Mod) == -1 )
		Mod = 0;
	NP = length(Procs)+1;
	V =var(F1);
	if ( !V ) {
		T = F1^2;
		if ( Mod )
			return T % Mod;
		else
			return T;
	}
	D1 = deg(F1,V);
	Dfft = p_mag(2*D1+1)+1;
	Bound = 2*maxblen(F1)+p_mag(D1)+1;
	if ( Bound < 32 )
		Bound = 32;
	Marray = newvect(NP);
	MIarray = newvect(NP);
	for ( I = 0; I < NP; I++ ) {
		Marray[I] = 1;
		MIarray[I] = [];
	}

	for ( M = 1, I = 0, J = 0; p_mag(M) <= Bound; J = (J+1)%NP ) {
		T = get_next_fft_prime(I,Dfft);
		if ( !T )
			error("fft_mul_d : fft_prime exhausted.");
		Marray[J] *= T[1];
		MIarray[J] = cons(T[0],MIarray[J]);
		M *= T[1];
		I = T[0]+1;
	}
	/* Now, 
		Marray[J] = FFTprime[Marray[J][0]]*...*FFTprime[Marray[J][...]]
		M = Marray[0]*...*Marray[NP-1]
	*/
	C = newvect(NP);
	T0 = time();
	for ( J = 0; J < NP-1; J++ )
		ox_cmo_rpc(Procs[J],"call_usquare",F1,MIarray[J],Marray[J],M);
	T1 = time();
	R = call_usquare(F1,MIarray[NP-1],Marray[NP-1],M);
	T2 = time();
	for ( J = 0; J < NP-1; J++ )
		R += ox_pop_cmo(Procs[J]);
	T3 = time();
/*	print(["send",T1[3]-T0[3],"self",T2[3]-T1[3],"recv",T3[3]-T2[3]]); */
	if ( Mod )
		return (R%M)%Mod;
	else
		return uadj_coef(R%M,M,ishift(M,1));
}

/*
	return: F1^2 mod V^(D+1)
	if option 'proc' is supplied as a list of server id's, 
	F1*F2 mod V^(D+1) is calculated by distributed computation.
*/

def d_tmul(F1,F2,D)
{
	Procs = getopt(proc);
	if ( type(Procs) == -1 )
		Procs = [];
	Mod = getopt(mod);
	if ( type(Mod) == -1 )
		Mod = 0;
	NP = length(Procs)+1;
	V =var(F1);
	if ( !V ) {
		T = utrunc(F1*F2,D);
		if ( Mod )
			return T % Mod;
		else
			return T;
	}
	D1 = deg(F1,V);
	D2 = deg(F2,V);
	Dmin = MIN(D1,D2);
	Dfft = p_mag(D1+D2+1)+1;
	Bound = maxblen(F1)+maxblen(F2)+p_mag(Dmin)+1;
	if ( Bound < 32 )
		Bound = 32;
	Marray = newvect(NP);
	MIarray = newvect(NP);
	for ( I = 0; I < NP; I++ ) {
		Marray[I] = 1;
		MIarray[I] = [];
	}

	for ( M = 1, I = 0, J = 0; p_mag(M) <= Bound; J = (J+1)%NP ) {
		T = get_next_fft_prime(I,Dfft);
		if ( !T )
			error("fft_mul_d : fft_prime exhausted.");
		Marray[J] *= T[1];
		MIarray[J] = cons(T[0],MIarray[J]);
		M *= T[1];
		I = T[0]+1;
	}
	/* Now, 
		Marray[J] = FFTprime[Marray[J][0]]*...*FFTprime[Marray[J][...]]
		M = Marray[0]*...*Marray[NP-1]
	*/
	C = newvect(NP);
	T0 = time();
	for ( J = 0; J < NP-1; J++ )
		ox_cmo_rpc(Procs[J],"call_utmul",F1,F2,D,MIarray[J],Marray[J],M);
	T1 = time();
	R = call_utmul(F1,F2,D,MIarray[NP-1],Marray[NP-1],M);
	T2 = time();
	for ( J = 0; J < NP-1; J++ )
		R += ox_pop_cmo(Procs[J]);
	T3 = time();
/*	print(["send",T1[3]-T0[3],"self",T2[3]-T1[3],"recv",T3[3]-T2[3]]); */
	if ( Mod )
		return (R%M)%Mod;
	else
		return uadj_coef(R%M,M,ishift(M,1));
}

def d_rembymul(F1,F2,INVF2)
{
	Procs = getopt(proc);
	if ( type(Procs) == -1 )
		Procs = [];
	Mod = getopt(mod);
	if ( type(Mod) == -1 )
		Mod = 0;
	NP = length(Procs)+1;
	if ( !F2 )
		error("d_rembymul : division by 0");
	V =var(F1);
	if ( !V ) {
		T = srem(F1,F2);
		if ( Mod )
			return T % Mod;
		else
			return T;
	}
	D1 = deg(F1,V);
	D2 = deg(F2,V);
	if ( !F1 || !D2 )
		return 0;
	if ( D1 < D2 )
		return F1;
	D = D1-D2;
	R1 = utrunc(ureverse(F1),D);
	Q = ureverse(utrunc(d_tmul(R1,INVF2,D|proc=Procs,mod=Mod),D));
	if ( Mod )
		return (utrunc(F1,D2-1)-d_tmul(Q,F2,D2-1|proc=Procs,mod=Mod))%Mod;
	else
		return utrunc(F1,D2-1)-d_tmul(Q,F2,D2-1|proc=Procs);
}

def call_umul(F1,F2,Ind,M1,M)
{
	C = umul_specialmod(F1,F2,Ind);
	Mhat = idiv(M,M1);
	MhatInv = inv(Mhat,M1);
	return Mhat*((MhatInv*C)%M1);
}

def call_usquare(F1,Ind,M1,M)
{
	C = usquare_specialmod(F1,Ind);
	Mhat = idiv(M,M1);
	MhatInv = inv(Mhat,M1);
	return Mhat*((MhatInv*C)%M1);
}

def call_utmul(F1,F2,D,Ind,M1,M)
{
	C = utmul_specialmod(F1,F2,D,Ind);
	Mhat = idiv(M,M1);
	MhatInv = inv(Mhat,M1);
	return Mhat*((MhatInv*C)%M1);
}
end$