bn128 curve root_of_unity与omega计算关系。
程序代码为:
template<typename FieldT>
typename std::enable_if<!std::is_same<FieldT, Double>::value, FieldT>::type
get_root_of_unity(const size_t n)
{
const size_t logn = log2(n);
if (n != (1u << logn)) throw std::invalid_argument("libff::get_root_of_unity: expected n == (1u << logn)");
if (logn > FieldT::s) throw std::invalid_argument("libff::get_root_of_unity: expected logn <= FieldT::s");
printf("get_root_of_unity FieldT::s: %zu, logn: %zu", FieldT::s, logn);
FieldT omega = FieldT::root_of_unity;
omega.print();
for (size_t i = FieldT::s; i > logn; --i)
{
omega *= omega;
}
omega.print();
return omega;
}
对应的打印日志为:
get_root_of_unity FieldT::s: 28, logn: 11
19103219067921713944291392827692070036145651957329286315305642004821462161904 //root_of_unity
1120550406532664055539694724667294622065367841900378087843176726913374367458
背后的计算逻辑为:
//对应n=2048,logn=11,FieldT::s=28的情况:
sage: r=218882428718392752222464057452572750885483644004160343436982041865758084
....: 95617
sage: root=191032190679217139442913928276920700361456519573292863153056420048214
....: 62161904
sage: omega=power_mod(root,2^(28-11),r)
sage: omega
1120550406532664055539694724667294622065367841900378087843176726913374367458
sage: power_mod(omega,2^11,r)
1
//其他
sage: power_mod(root,2^21,r)
10359452186428527605436343203440067497552205259388878191021578220384701716497
sage: power_mod(root,2^18,r)
3161067157621608152362653341354432744960400845131437947728257924963983317266
sage: random=9183792848062807763466123715213696437742106827693609448424338413206
....: 273385681
sage: power_mod(random,1024,r)
18100781649685386403619787846080363133221994176323226106964186882182808276643
//在Finite Field有限域r内求1024的倒数。FieldT(1024).inverse()。
sage: inverse_mod(1024,r)
21866867634659744680037180739646672280844703888306253060159436409049855557633
sage: mod(1024* inverse_mod(1024,r),r) // 1024*(1/1024)=1
1
sage: mod(inverse_mod(1024,r)*18100781649685386403619787846080363133221994176323
....: 226106964186882182808276642,r)
7499009557415460658388536912779420550903466342111264889063804347835659943108
sage: