diff --git a/build.sbt b/build.sbt index 7fd9727c9a..f9f58c0c91 100644 --- a/build.sbt +++ b/build.sbt @@ -69,10 +69,10 @@ dynverSonatypeSnapshots in ThisBuild := true // use "-" instead of default "+" dynverSeparator in ThisBuild := "-" -val bouncycastleBcprov = "org.bouncycastle" % "bcprov-jdk15on" % "1.64" -val scrypto = "org.scorexfoundation" %% "scrypto" % "2.1.10" -val scorexUtil = "org.scorexfoundation" %% "scorex-util" % "0.1.8" -val debox = "org.scorexfoundation" %% "debox" % "0.9.0" +val bouncycastleBcprov = "org.bouncycastle" % "bcprov-jdk15on" % "1.66" +val scrypto = "org.scorexfoundation" %% "scrypto" % "2.3.0-RC1" +val scorexUtil = "org.scorexfoundation" %% "scorex-util" % "0.2.0" +val debox = "org.scorexfoundation" %% "debox" % "0.10.0" val spireMacros = "org.typelevel" %% "spire-macros" % "0.17.0-M1" val fastparse = "com.lihaoyi" %% "fastparse" % "2.3.3" val guava = "com.google.guava" % "guava" % "30.1.1-jre" diff --git a/sigmastate/src/main/java/gf2t/GF2_192.java b/sigmastate/src/main/java/gf2t/GF2_192.java deleted file mode 100644 index dc0c166bcf..0000000000 --- a/sigmastate/src/main/java/gf2t/GF2_192.java +++ /dev/null @@ -1,580 +0,0 @@ -/* - By Leonid Reyzin - - This is free and unencumbered software released into the public domain. - - Anyone is free to copy, modify, publish, use, compile, sell, or - distribute this software, either in source code form or as a compiled - binary, for any purpose, commercial or non-commercial, and by any - means. - - In jurisdictions that recognize copyright laws, the author or authors - of this software dedicate any and all copyright interest in the - software to the public domain. We make this dedication for the benefit - of the public at large and to the detriment of our heirs and - successors. We intend this dedication to be an overt act of - relinquishment in perpetuity of all present and future rights to this - software under copyright law. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR - OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - OTHER DEALINGS IN THE SOFTWARE. - - For more information, please refer to - */ -package gf2t; - -import java.util.Arrays; - -public class GF2_192 { - private final long [] word = new long[3]; - - /** - * - * @param obj the field element with which to compare - * @return true if and only if this and that represent the same field element - */ - @Override - public boolean equals(Object obj) { - if (this == obj) return true; // equal references - if (obj instanceof GF2_192) { - GF2_192 that = (GF2_192)obj; - return this.word[0]==that.word[0] && this.word[1]==that.word[1] && this.word[2]==that.word[2]; - } - return false; // different types - } - - @Override - public int hashCode() { - return Arrays.hashCode(word); - } - - // using irreducible polynomial x^192+x^7+x^2+x+1 - // We need only the last word - static private final long irredPentanomial = (1L<<7) | (1L<<2) | (1L<<1) | 1L; - - // irredPentanomial times 0, 1, x, x+1, x^2, x^2+1, x^2+x, x^2+x+1, x^3, x^3+1, x^3+x, x^3+x+1, x^3+x^2, x^3+x^2+1, x^3+x^2+x, x^3+x^2x+1, - // Need only the last word, because the leading two words are 0 - static private final long [] irredMuls = {0L, irredPentanomial, irredPentanomial<<1, (irredPentanomial<<1)^irredPentanomial, - irredPentanomial<<2, (irredPentanomial<<2)^irredPentanomial, (irredPentanomial<<2)^(irredPentanomial<<1), (irredPentanomial<<2)^(irredPentanomial<<1)^irredPentanomial, - irredPentanomial<<3, (irredPentanomial<<3)^irredPentanomial, (irredPentanomial<<3)^(irredPentanomial<<1), (irredPentanomial<<3)^(irredPentanomial<<1)^irredPentanomial, - (irredPentanomial<<3)^(irredPentanomial<<2), (irredPentanomial<<3)^(irredPentanomial<<2)^irredPentanomial, (irredPentanomial<<3)^(irredPentanomial<<2)^(irredPentanomial<<1), (irredPentanomial<<3)^(irredPentanomial<<2)^(irredPentanomial<<1)^irredPentanomial - - }; - - /** - * returns the 0 field element - */ - public GF2_192() { - } - - /** - * returns a copy of the field element - * @param that element to copy - */ - public GF2_192(GF2_192 that) { - this.word[0] = that.word[0]; - this.word[1] = that.word[1]; - this.word[2] = that.word[2]; - } - - /** - * returns the field element whose 32 least significant bits are bits of that and rest are 0 - * @param that lower 32 bits - */ - public GF2_192(int that) { - this.word[0] = ((long) that) & 0xFFFFFFFFL; - } - - /** - * returns the field element whose bits are given by the long array - * @param that must be length 3 - */ - public GF2_192(long [] that) { - assert (that.length == 3); - this.word[0] = that[0]; - this.word[1] = that[1]; - this.word[2] = that[2]; - } - - /** - * returns the field element whose bits are given by the byte array that - * @param that must be length 24 - */ - public GF2_192(byte [] that) { - this(that, 0); - } - - /** - * returns the field element whose bits are given by the byte array that[pos]...that[pos+23] - * @param that must be length at least pos+24 - */ - public GF2_192(byte [] that, int pos) { - assert (that.length >= pos+24); - for (int i = 0; i<8; i++) { - word[0] |= (((long)that[i+pos] & 0xFF))<<(i<<3); - } - for (int i = 0; i<8; i++) { - word[1] |= (((long)that[i+pos+8] & 0xFF))<<(i<<3); - } - for (int i = 0; i<8; i++) { - word[2] |= (((long)that[i+pos+16] & 0xFF))<<(i<<3); - } - } - - /** - * - * @return long array of length 3 containing the three words of the field element - */ - public long [] toLongArray() { - long [] ret = new long[3]; - ret[0] = word[0]; - ret[1] = word[1]; - ret[2] = word[2]; - return ret; - } - - /** - * - * @return byte array of length 24 containing the two words of the field element - */ - public byte[] toByteArray() { - byte [] ret = new byte[24]; - toByteArray(ret, 0); - return ret; - } - - /** - * @param ret bytes of the field element will go into ret[pos]...ret[pos+23] - */ - public void toByteArray(byte[] ret, int pos) { - assert(ret.length>=pos+24); - for (int j = 0; j<3; j++) { - for (int i = 0; i < 8; i++) { - ret[pos+i+8*j] = (byte) ((word[j] >> (i << 3)) & 0xFF); - } - } - } - - - - /** - * - * @return true if this == 0, false otherwise - */ - public boolean isZero () { - return word[0]==0L && word[1]==0L && word[2]==0L; - } - - /** - * - * @return true if this == 1, false otherwise - */ - public boolean isOne () { - return word[0]==1L && word[1]==0L && word[2]==0L; - } - - /** - * Computes a plus b and puts the result into res. - * @param res output; must be not null; may be equal to a and/or b - * @param a multiplicand; may be equal to res, in which case will get overwritten - * @param b multiplier; may be equal to res, in which case will get overwritten - */ - - public static void add (GF2_192 res, GF2_192 a, GF2_192 b) { - res.word[0] = a.word[0]^b.word[0]; - res.word[1] = a.word[1]^b.word[1]; - res.word[2] = a.word[2]^b.word[2]; - } - - - - /** - * Computes a times b and puts the result into res. - * Uses table lookups, which may not preserve - * the secrecy of the inputs in case of side-channel attacks. - * - * @param res output; must be not null; may be equal to a and/or b - * @param a multiplicand; may be equal to res, in which case will get overwritten - * @param b multiplier; may be equal to res, in which case will get overwritten - */ - public static void mul (GF2_192 res, GF2_192 a, GF2_192 b) { - - // Implements a sort of times-x-and-add algorithm, except instead of multiplying by x - // we multiply by x^4 and then add one of possible 16 precomputed values - - // contains a*0, a*1, a*x, a*(x+1), a*x^2, a*(x^2+1), a*(x^2+x), a*(x^2+x+1) - // a*x^3, a*(x^3+1), a*(x^3+x), a*(x^3+x+1), a*(x^3+x^2), a*(x^3+x^2+1), a*(x^3+x^2+x), a*(x^3+x^2+x+1), all mod reduced - // First word of each is in a0 muls, second word of each is in a1muls, third word of each is in a2muls - long [] a0muls = new long[16]; - long [] a1muls = new long[16]; - long [] a2muls = new long[16]; - - // a0muls[0], a1muls[0] and a2muls[0] are already correctly initialized to 0 - - a0muls[1] = a.word[0]; - a1muls[1] = a.word[1]; - a2muls[1] = a.word[2]; - - // a*x, a*x^2, a*x^3 - for (int i = 2; i<=8; i*=2) { - // multiply a*x^{log_2 i/2} by x to get a*x^{log_2 i} - int prev = i / 2; - a0muls[i] = a0muls[prev] << 1; - a1muls[i] = (a1muls[prev] << 1) | (a0muls[prev] >>> 63); - a2muls[i] = (a2muls[prev] << 1) | (a1muls[prev] >>> 63); - // mod reduce - a0muls[i] ^= irredMuls[(int) (a2muls[prev] >>> 63)]; - } - - // a*(x+1) - a0muls[3] = a0muls[1] ^ a0muls[2]; - a1muls[3] = a1muls[1] ^ a1muls[2]; - a2muls[3] = a2muls[1] ^ a2muls[2]; - - - // a*(x^2+1), a*(x^2+x), a*(x^2+x+1) - for (int i = 1; i<4; i++) { - a0muls[4|i] = a0muls[4]^a0muls[i]; - a1muls[4|i] = a1muls[4]^a1muls[i]; - a2muls[4|i] = a2muls[4]^a2muls[i]; - } - - // a*(x^3+1), a*(x^3+x), a*(x^3+x+1), a*(x^3+x^2), a*(x^3+x^2+1), a*(x^3+x^2+x), a*(x^3+x^2+x+1) - for (int i = 1; i<8; i++) { - a0muls[8|i] = a0muls[8]^a0muls[i]; - a1muls[8|i] = a1muls[8]^a1muls[i]; - a2muls[8|i] = a2muls[8]^a2muls[i]; - } - - long w0 = 0, w1 = 0, w2 = 0; - for (int j = 2; j>=0; j--) { - long multiplier = b.word[j]; - for (int i = 60; i >= 0; i -= 4) { - // Multiply by x^4 - int modReduceIndex = (int) (w2 >>> 60); - w2 = (w2 << 4) | (w1 >>> 60); - w1 = (w1 << 4) | (w0 >>> 60); - // MOD REDUCE ACCORDING TO modReduceIndex by XORing the right value - w0 = (w0 << 4) ^ irredMuls[modReduceIndex]; - //w0 = (w0<<4)^(irredPentanomial*(modReduceIndex&8))^(irredPentanomial*(modReduceIndex&4))^(irredPentanomial*(modReduceIndex&2))^(irredPentanomial*(modReduceIndex&1)); - - // Add the correct multiple of a - int index = (int) ((multiplier >>> i) & 15); - w0 ^= a0muls[index]; - w1 ^= a1muls[index]; - w2 ^= a2muls[index]; - } - } - res.word[0] = w0; - res.word[1] = w1; - res.word[2] = w2; - } - - /** - * Computes a times b and puts the result into res. More efficient than mul(res, a, new GF2_192(b)) - * @param res output; must be not null; may be equal to a and/or b - * @param a multiplicand; may be equal to res, in which case will get overwritten - * @param b multiplier; may be equal to res, in which case will get overwritten - */ - public static void mul (GF2_192 res, GF2_192 a, byte b) { - - long w0 = 0, w1 = 0, w2 = 0, w3=0; - - for (int i = 7; i >= 0; i--) { - w3 = w2 >>> 63; - w2 = (w2 << 1) | (w1 >>> 63); - w1 = (w1 << 1) | (w0 >>> 63); - w0 <<= 1; - long t = (b >>> i) & 1; - w2 ^= a.word[2] * t; - w1 ^= a.word[1] * t; - w0 ^= (a.word[0] * t) ^ (irredPentanomial * w3); // mod reduce - } - res.word[0] = w0; - res.word[1] = w1; - res.word[2] = w2; - } - - - public static void invert (GF2_192 res, GF2_192 z) { - // Computes z^{2^192-2} = z^{exponent written in binary as 191 ones followed by a single zero} - // (by Fermat's little theorem, this is the correct inverse) - - // contains z raised to the power whose binary representation is 2^k ones - GF2_192 zTo2ToK1s = new GF2_192(z); - - // Square res to get its exponent to be 10 in binary - mul(res, z, z); - - // contains z raised to the power whose binary representation is 2^k ones followed by 2^k zeros - GF2_192 zTo2ToK1s2ToK0s = new GF2_192(res); - - - // Loop invariant - // res contains z raised to the power whose binary representation is 2^{k+1}-1 ones followed by a single zero - // zTo2ToK1s contains z raised to the power whose binary representation is 2^k ones - // zTo2ToK1s2ToK0s contains z raised to the power whose binary representation is 2^k ones followed by 2^k zeros - int k = 0; - while (k<6) { - k++; - // Fill in the zeros in the exponent of zTo2ToK1s2ToK0s with ones - mul(zTo2ToK1s, zTo2ToK1s2ToK0s, zTo2ToK1s); - // zTo2ToK1s2ToK0s = power2To2ToK with 2^k zeros appended to the exponent - power2To2ToK(zTo2ToK1s2ToK0s, zTo2ToK1s, k); - // prepend 2^k ones to res - mul(res, res, zTo2ToK1s2ToK0s); - } - // Prepened another 64 ones to res - power2To2ToK(zTo2ToK1s2ToK0s, zTo2ToK1s2ToK0s, k); - mul(res, res, zTo2ToK1s2ToK0s); - } - - - - // These tables are used in power2To2ToK and sqr -- see explanation in power2To2ToK. They take up about about 14KB. - - private static final long [][] powTable0 = { - {1L,4L,16L,64L,256L,1024L,4096L,16384L,65536L,262144L,1048576L,4194304L,16777216L,67108864L,268435456L,1073741824L,4294967296L,17179869184L,68719476736L,274877906944L,1099511627776L,4398046511104L,17592186044416L,70368744177664L,281474976710656L,1125899906842624L,4503599627370496L,18014398509481984L,72057594037927936L,288230376151711744L,1152921504606846976L,4611686018427387904L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,135L,540L,2160L,8640L,34560L,138240L,552960L,2211840L,8847360L,35389440L,141557760L,566231040L,2264924160L,9059696640L,36238786560L,144955146240L,579820584960L,2319282339840L,9277129359360L,37108517437440L,148434069749760L,593736278999040L,2374945115996160L,9499780463984640L,37999121855938560L,151996487423754240L,607985949695016960L,2431943798780067840L,-8718968878589280256L,2017612633061982208L,8070450532247928832L,-4611686018427387904L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,270L,1080L,4199L,}, - {1L,16L,256L,4096L,65536L,1048576L,16777216L,268435456L,4294967296L,68719476736L,1099511627776L,17592186044416L,281474976710656L,4503599627370496L,72057594037927936L,1152921504606846976L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,135L,2160L,34560L,552960L,8847360L,141557760L,2264924160L,36238786560L,579820584960L,9277129359360L,148434069749760L,2374945115996160L,37999121855938560L,607985949695016960L,-8718968878589280256L,8070450532247928832L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,1080L,16405L,262480L,4199680L,67194880L,1075118080L,17201889280L,275230228480L,4403683655680L,70458938490880L,1127343015854080L,18037488253665280L,288599812058644480L,4617596992938311680L,94575592174780416L,1513209474796486656L,5764607523034234880L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,540L,8640L,138375L,2214635L,35434160L,566946560L,9071144960L,145138319360L,2322213109760L,37155409756160L,594486556098560L,9511784897576960L,152188558361231360L,2435016933779701760L,2066782793056124928L,-3824963458521104384L,-5859183115209015296L,-1513209474796486656L,-5764607523034234880L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,270L,4199L,65620L,1048694L,16777290L,}, - {1L,65536L,4294967296L,281474976710656L,0L,0L,0L,0L,0L,0L,0L,0L,135L,8847360L,579820584960L,37999121855938560L,0L,0L,0L,0L,0L,0L,0L,0L,16405L,1075118080L,70458938490880L,4617596992938311680L,0L,0L,0L,0L,0L,0L,0L,0L,2214635L,145138319360L,9511784897576960L,-3824963458521104384L,0L,0L,0L,0L,0L,0L,0L,4199L,268435729L,17592203935744L,1152922677132918784L,76842668642009088L,0L,0L,0L,0L,0L,0L,0L,552960L,36238823415L,2374947531325440L,8070608823267622912L,-8072983807038324736L,0L,0L,0L,0L,0L,0L,1080L,67194880L,4403688133701L,288600105530228736L,5783840476780036096L,6072259672578981888L,0L,0L,0L,0L,0L,0L,138375L,9071137756L,594486085783387L,2066751970480947200L,-7784595809881817088L,-8116893903405187072L,0L,0L,0L,0L,0L,270L,16777290L,1099511627845L,72057594037993729L,4311810048L,282578783305728L,72339069014638592L,0L,0L,0L,0L,0L,34560L,2264924160L,148434069749895L,-8718968878580398201L,582094356480L,38148135746273280L,-8680969756733341696L,0L,0L,0L,0L,0L,4199680L,275230228480L,18037488253681685L,1513209475875820821L,70735243837440L,4635704940130467840L,6130806467734798336L,0L,0L,0L,0L,8640L,566946560L,37155409756160L,2435016933781924651L,-1513209329119911445L,9547060033028096L,-1513172181595455488L,2444047222778626048L,0L,0L,0L,0L,1048694L,68719546624L,4503604207554663L,300166943871232L,1224996759836561425L,1157443864920915968L,1229500363472633856L,1157706579210928128L,0L,0L,0L,2160L,141557895L,9277138794240L,607986568019867760L,40522537422618480L,-646134085450172169L,8680969174647242752L,-38147594580393984L,8716435603798884352L,0L,0L,0L,262480L,17201906733L,1127344162226488L,94650720833590632L,4923631783780892776L,4685244537110860101L,6130877201840930816L,4635630379498209280L,1244400872037810176L,0L,0L,540L,35434160L,2322211272620L,152188437960410523L,-5867073703402003989L,-608753751841641685L,5021550739930207323L,2435017087666290688L,-1503124363304501248L,-3144919914788159488L,0L,0L,65620L,4294967417L,281474976710691L,16L,118L,65641L,}, - {1L,0L,0L,268435729L,0L,0L,72057594037993729L,0L,8640L,1224996759836561425L,0L,2322211272620L,4295032833L,0L,-3824963458521104384L,1152940269605290257L,67194880L,-8116893903405048697L,72340168543043841L,18037488253681685L,2444084377648024256L,1152940269335812193L,4923631783780892776L,8706684044243276764L,575525617665L,0L,2214635L,6917686146403139857L,0L,594486068876182L,109923151000961281L,0L,-1508668585977835444L,-42651198216466415L,17201907249L,-1503124363304500630L,37718226699878401L,4617597137004134400L,-3834029160147512970L,-79200059939786479L,5542457468742751288L,72653179618366162L,-8718686828065942650L,6157755589565773013L,-427524231241125731L,-7636548200255303383L,2349380831670242920L,-1503122247285515780L,1L,16405L,36240966940L,268435729L,4403688133701L,-8718374392512378660L,72057594037993729L,1513209475874772323L,2019996756866529500L,1224996759976022166L,4684963044965832011L,5021552416478667632L,37999126150971393L,4684939972385783829L,-4989366934530337413L,-6920043528899271987L,72339069031416121L,-8680822410973408226L,-6391613922127811155L,1153203052884328721L,5891559298491384716L,-7528422813418094127L,35172261L,-608824726141909180L,575525617798L,9441498026213649L,3487211355411353591L,6917686182105090782L,-4416945885376472588L,6063073012436278090L,-8753160638328035514L,-5605497289716500368L,-5610075583180945114L,90149276037394458L,339026464814167628L,369965729453364929L,-3860411569319034860L,-7219661873698413202L,-7217141859998432150L,1806743557951716919L,-8790878027314019100L,-8686366285709925551L,-9018771827274447141L,-3500033589960484657L,-6239661070421212278L,-7022774006793486301L,-5486222289147287496L,6557835543537224962L,1L,4403688117328L,268435729L,268435729L,1513213877407514960L,72057594037993729L,72057594179551622L,6054338822752200080L,1224996759836570065L,1262995881831960726L,4685176399308498684L,2326506170314L,-8110982924599230463L,-7012597109688626096L,-2672024288427437773L,1760926221464557073L,-8193431732091728425L,-8188932805459047229L,-702248183304599069L,2358520128326754357L,3596724480578929564L,2653562374675748729L,7558329885135416404L,8706684602589025248L,611764439881L,-4354323463508992325L,6917686146405354490L,-1801282968903807655L,9071577101557338822L,109381790017557217L,8696092877002101742L,-4668327226503048164L,1475308329743286364L,128146801688724317L,-8390330629760215797L,-1531869438861569413L,-4617320090730090391L,5541920254930687988L,3761689569789112577L,-2327319826622658605L,-6182158405047794621L,-41970731974321979L,-4950437438052009930L,-3752274221890333889L,1476151931308439890L,5613176216134850767L,289726897646273736L,-1503122247017080678L,594453066058452L,4437244711257L,72057630278829085L,7782810071065752468L,-7925740857026315255L,-7637528022863553386L,-3839220191352824283L,6705067445074182019L,6637312287860931618L,-2154173082950753382L,-8117068327749574457L,4645236792527042077L,3567261728918198996L,-1570715894964224286L,3851762367547029496L,-1282481202581286367L,604372413954671920L,7336273916715742398L,8984291365106642911L,-3520346153016046579L,-3001672072993760708L,5487223282412062341L,3220238017403266817L,-7526201328351032492L,-6651737113209978849L,-876842992662428725L,2730222910065286284L,4666291884953713058L,27885208784216435L,-3464689086048100177L,9074463908856611112L,6089525621414736771L,-344374184071824754L,-4138339726157814851L,90460192285484837L,7003316425532605053L,2325362866034789830L,6377579351440235774L,-5484290407194710110L,3204252260295655770L,4952583157325034207L,-8782555314696362611L,-2316049681279009322L,-1127600795797440159L,7751601582170687883L,-6463181785702448409L,7121056025054547704L,6485777949499231279L,}, - {1L,-3006172399682371634L,2600966637154804214L,8641L,8525113116679455864L,-2145144163292000430L,2019996756799457565L,-1850476282776554867L,-4889822090580977265L,909561413152654253L,-25001132819151504L,7726439241120171362L,7303489125464870298L,4332131768403656981L,2962048663402138678L,-2892279489278463047L,-4733651509025142792L,1063143929388429494L,-1197895461363724390L,3525840681984842667L,-4695277565342099683L,5146529997701558317L,7435951619486932058L,-2724786844429777139L,40504930336318985L,876946985023164340L,5608484252067120944L,-6816077621382648259L,-4033654809715777901L,-4081572395912436435L,-8636364450456842826L,7120123665156854152L,-2739360841521830815L,4179210361336297309L,-5035580621240300693L,5024856697532411216L,-5583726119146644383L,1447126495187071743L,-2586987284404803098L,8837846401203718243L,6684981609574125625L,1540466829568951558L,-4848578993616133096L,531411902399629160L,-8097992882751386397L,5836923291160128631L,2657822116024759531L,5082038816076700909L,7237486007293790522L,5252165852497928096L,-6943891580396708852L,-2358876157997055033L,2004344792661408502L,6922719691033397220L,-6115771057843578790L,-882977230851385180L,4445218069992821756L,5905786485590667688L,-8952895028275913749L,6335857422495481096L,-1336747599306330073L,-295017526585807725L,6427842712075527612L,6136969228259728187L,8790718804228268581L,-7649524091336109629L,-5994405249073919640L,4313678309655720149L,1583534994170157977L,5606445082714174951L,6853837681080604556L,-8485637026077231303L,7790525872849806729L,5940658092022763209L,8602473577714766584L,2936249914771413453L,-5521212179187796747L,4711968172455986094L,-4922856425548406499L,130141881968551392L,3536227681426168034L,1538274929948045556L,2319096648797621624L,-390137264770768181L,8029329512101034001L,-7020589058068074770L,3265911137432880136L,8114665243288440240L,1727024161912275448L,7211866768629281013L,-3130723513389854793L,-7216856759928143716L,3533195878158403511L,3984449705890310687L,-8379049565896909567L,-8388101802220698225L,-1001505570555522767L,-2621811112096768870L,603025228237669646L,-4855786637640829169L,-8861801597506211630L,5171117103964917160L,-6016899467464669218L,574486107801798403L,1346751909104125183L,6265319601178024276L,3065092470199993307L,-3544361000120591516L,-6311655751958887780L,5850322626671860616L,-2175699856332693179L,2849507709866937883L,-5020421862032512214L,-4568858551591370457L,-2431265188066595845L,-8732538833975482925L,49029378622487761L,948188484528056399L,685766231005340987L,-6108229294291709748L,-6619503736528295873L,2499319692519687998L,-8241104250166498648L,-6283823462790380478L,-1799922584861092054L,-6240401189323759144L,5497064944286443280L,6723566846653152186L,-7200823809060633542L,-8991399666593498102L,3696845228942259783L,5525150667579715560L,949595271893899961L,4431634865357028469L,-7719973862985677490L,-6034475957088628470L,-4268733492881789978L,6756633516157056278L,-1957554544632307580L,7121101677918762223L,-2926994538817486764L,5070148293018691908L,-3164346027824263021L,-8065527601021753218L,-7393579279100902671L,1578925030408104064L,-5470498100570909442L,-9059484745764947078L,8728828886428453065L,876414647339389056L,-4691346483920214801L,271068101201614328L,-1328948676306083414L,-601927494426833593L,-1580410200329916588L,-4326051892200391110L,5551955200782384943L,214408676534699021L,892732170117421280L,-6779792031755998944L,8533716818059729780L,873315600166044946L,8844237596764485904L,4304845159865160138L,5606488036825769410L,425090181770096226L,-1135909175464444990L,-4725116773226449403L,5897643684611700155L,1650595387261410562L,-7317473568888247260L,-8317376280642525522L,524782793988048123L,-6713202495631135192L,-4939556292506913402L,-5609869695293186861L,-4230589026387944683L,-724065909912518572L,-2110301323783658314L,1029673643630214818L,-5297873999180537788L,-8928073349900829853L,7277641038077141425L,6527157727335254212L,-3946003823558145331L,8063768130289328687L,4522053259152859193L,675627983587702560L,-1137165482193899852L,8414663288495567717L,-8436820189876267996L,7876837133135388570L,}, - {1L,-5169860002514781635L,-2589809952505784534L,8777364680166470481L,-8830656417429304044L,3176782973934494582L,109797415759816827L,1076956885061792565L,7914398690249482831L,8011904680480159185L,8410793115243855507L,6266439534501642392L,-336875557465114730L,4038138302048025623L,-8101148793053570270L,1124933003446523927L,8391412732637477197L,2199881538445059432L,719775327569887587L,-4825086212853447750L,-7314323185577824224L,4891665885256984545L,7598126349271893834L,-3239373303059569532L,-659203789875424936L,7692138112394859210L,-193023354580355438L,1661409395237152970L,-867709405024660660L,-4774852438416084051L,1013518333030173816L,3485843496410831696L,188315317670329959L,-7626348485928744434L,4785373727715537048L,-2557360640145528783L,815573462730278215L,2725495643400585416L,763815832452911292L,-1234416280134845253L,3031628907175299520L,6164952807644926203L,-6906157987463386683L,8750285495619286011L,-2944268397760272112L,-8571087382226582932L,7349937386463951048L,7786267339579184516L,337223540666190292L,1366638621193514276L,5979097440737272663L,-8229072190234731287L,-7464767416008336370L,2196188713781264658L,-6606956754260663431L,8497668204436713713L,7806277383834821447L,591641631205476095L,6045490875774248095L,456224709346991991L,-7218871130131209394L,580163022797824109L,-3693397793970743538L,-1855672627986562453L,1785325366189117080L,-5612555377205332105L,-294341367983285122L,2445160339032774813L,7132278192002140706L,878326058396851309L,3921174184439993950L,5853022697017928950L,-2825405099755624515L,-7030859599219471492L,-8473806211642996024L,-8498195465917087535L,-4182645819142121229L,726866375401350017L,-4123860299531460321L,67135272333132065L,238539200704091067L,9069168069279185101L,-8554967853203633945L,-1798135605252717890L,-359730572749547446L,4493519132539194015L,4958647882027071589L,-911149761898967393L,-5156659928190548061L,1295304005530223213L,-6977343558102121156L,6922621878872799870L,6666583055979266042L,7292171483456095852L,-1969457853973942746L,-6898392144047603629L,5756338153240882170L,6504592230467093280L,169954543499997878L,8244762795686741102L,-7702335274149853320L,170612181012260678L,-1167924627447415726L,6330133554527618692L,-670458599026908667L,-5475429905606927886L,-4407322762187867157L,-8580599370684241560L,3285212811372363965L,7894963086517678484L,8569304869073834915L,-6544807253825008820L,-5996237358893605511L,-3042468804887085356L,2374190430796422836L,3894653610967932827L,3898642019578338045L,-5362019755573200863L,4402246476084717913L,3291977739464152007L,3674469931224113236L,-44278192132845207L,-146130534644405364L,5382065438553845456L,6785308159791913441L,4538149016807450869L,8422045206362267535L,-751406355315423454L,8999771730341663411L,-990169186945997877L,1777800203227879507L,-4856230533325857618L,6591273902673649307L,4756065680746518619L,-8504746896788247085L,4474109455375415963L,7512977545114097664L,-4994667515641620176L,-4624712674055432963L,-7581435093848090518L,-3374583368016299959L,-6772163028506140599L,-4597352927609652442L,7074576231879668500L,8699761606660221174L,-1460827062303528567L,8015141181709918285L,-1169566320418753181L,-4781112204804977330L,1888326456844245676L,8052564071464611668L,8677658026519325509L,8196795344534159240L,-6189127939919551760L,-756836619992549632L,3275904360037766197L,8063439441889441129L,-7331287930982412507L,5909316323417099098L,7555764543220595192L,5975647989155847506L,3542169932787954825L,3572787110578921974L,-4910033269362933281L,4127048709984103372L,-8249546761957136187L,2136513283148217767L,6914147571605817471L,-1401967832836965144L,8407424200159619086L,3198875950909199105L,2570623872214327259L,6598776223519113019L,-8548723600459949403L,1542874467657402904L,5132064706814079464L,-8697292868739094825L,7457266453233910833L,-1093418532865160497L,3247028061820159553L,-1424542897552112257L,5068257691790424406L,5880873780866162489L,-3102632713002543235L,1263997674554416274L,5364397535996490566L,-6980107079961147736L,-3471995143361337385L,6641708940048027257L,1135429108619021836L,5387343206428796059L,-6804247660018484977L,}, - {1L,1073223059898794450L,-2038813369256918412L,5304323081406240818L,3303538589997961255L,-8242543733483560526L,-3361776038394087875L,-7966401800695747134L,3661948208390050674L,6880986284844289330L,2763308461018415133L,1781656665212037754L,6138182394991053453L,-7512225217435664149L,-2696439080506022889L,-2583065046819908147L,-3131915636955022563L,-2889357217172249387L,-5802605454962117101L,-2436895282174351119L,-1766568650463350318L,-377122117525974673L,1776581853777064707L,-308611149542073169L,-540314814780534625L,8467843478952328360L,3468583091456007095L,505744836808219017L,-5428914123150493339L,-7776987036728855764L,-6102268671949303457L,3494341367960380577L,2418782979535889063L,4679473505413000143L,-6441695285033044392L,6265164336385959504L,-7246211974952246812L,-531598530841805554L,7617543382550044362L,7304030873573479981L,-32281324903020730L,5445701030230371763L,252311267739668938L,7731413305529340107L,4792829355971906114L,3012742833337443711L,7376323673068462242L,2937445380659696069L,9159469984575429466L,4180661836353763848L,9102205873245014544L,-9157654341755851144L,-5917184808139238532L,-913012161319772394L,3520965615244733154L,1105108204046932414L,3640346287517683383L,2610854371735641718L,3369140582718399805L,-167503450741461836L,7612537899180910062L,4647612969614095840L,3109561378106990004L,8745335350416839074L,-298291418130746473L,-2193685336892597509L,-3205646479549694138L,-6912700853529572321L,-5285985482282474664L,-8011420405948338685L,-20608671900365787L,2461277845240147045L,-8794182113545320389L,-4143692325726628162L,-5206060059250865124L,-7341828269574673748L,5997074990730119543L,-6983875851493091721L,5190016632970933241L,-1133665158084916157L,-7711757776416462528L,958657918611969015L,2998347888694802671L,-176777484057504605L,5157771292309956459L,-8999461217390585327L,4257577863211723393L,-3385462713578387432L,5581805654425926531L,-6981668768611461067L,6619449109488021267L,8273870493873208839L,6305391702152098821L,-7738026482851978733L,-7424370079506352431L,1130290113791392248L,5736936187682728875L,-1889540322116193194L,6918453404219483036L,-258362754443186269L,1740351069027290592L,2990762037699987674L,2598835903464008375L,-6081928830422087295L,5388584501900900268L,-3101635458397415306L,5560630914118636896L,5540181403154305832L,-5050504706493546867L,2482421493273885212L,8345872426056290435L,-114929197746482490L,-1411535166620560130L,1955447843789378400L,-4415727728018118178L,174849678320512726L,-5578523943478722759L,5785969583518444021L,-2232487152186233036L,3347165569852915762L,8597084251364200641L,-8637378221001354125L,1905787365039574668L,-5764297333239831537L,-6887855556896782846L,-7466419285184801288L,3561228892785125799L,-6025847770670435502L,7880903838892214728L,4831723645980851507L,-8700410925103051535L,5167047152144011193L,-5589950774695842572L,-6975863374495850197L,8636325743490381438L,-5393462740365964783L,758109935525139289L,8449824526047483324L,2882040775719318333L,8722862970638278922L,652288902771973384L,-5889783177719394690L,-3211436433657712007L,1362735929528070085L,8025013999877916656L,8057121196213509424L,-597152196419816458L,-3731606026680532941L,-6814641654656904693L,-2513538531416010165L,-1449817420573805572L,7983703381157839238L,4136435098211692276L,-1331213617861613522L,-289183771021882356L,-3100859045837937046L,536889653442631647L,1933334157013614393L,-3378838522188481907L,59059052034778512L,-9070778615901588520L,-8561634908355121490L,8646082716902511799L,-7190394898230747028L,2223190096772479452L,-625200368095748196L,-7695499964415613072L,-2925720374223602748L,7859320007538329711L,-8590024973885119823L,8447867896928955011L,-8341487853447061366L,1319006032904755853L,5631657808401138749L,2597687022492864406L,7575706244535104411L,-5270921382190007475L,3486972365953290955L,67197056587467249L,-542829327793657434L,-8188837871581890476L,-3908705513843534551L,-8566981112648480966L,-6798624665298914527L,9174760941175519307L,-5792872140589943835L,-2219700069287782459L,-6440926725540882731L,687123600828336407L,6695459205124198333L,5752186679888935902L,3823361293471306013L,}, - }; - private static final long [][] powTable1 = { - {0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,1L,4L,16L,64L,256L,1024L,4096L,16384L,65536L,262144L,1048576L,4194304L,16777216L,67108864L,268435456L,1073741824L,4294967296L,17179869184L,68719476736L,274877906944L,1099511627776L,4398046511104L,17592186044416L,70368744177664L,281474976710656L,1125899906842624L,4503599627370496L,18014398509481984L,72057594037927936L,288230376151711744L,1152921504606846976L,4611686018427387904L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,2L,8L,33L,135L,540L,2160L,8640L,34560L,138240L,552960L,2211840L,8847360L,35389440L,141557760L,566231040L,2264924160L,9059696640L,36238786560L,144955146240L,579820584960L,2319282339840L,9277129359360L,37108517437440L,148434069749760L,593736278999040L,2374945115996160L,9499780463984640L,37999121855938560L,151996487423754240L,607985949695016960L,2431943798780067840L,-8718968878589280256L,2017612633061982208L,8070450532247928832L,-4611686018427387904L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,}, - {0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,1L,16L,256L,4096L,65536L,1048576L,16777216L,268435456L,4294967296L,68719476736L,1099511627776L,17592186044416L,281474976710656L,4503599627370496L,72057594037927936L,1152921504606846976L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,8L,135L,2160L,34560L,552960L,8847360L,141557760L,2264924160L,36238786560L,579820584960L,9277129359360L,148434069749760L,2374945115996160L,37999121855938560L,607985949695016960L,-8718968878589280256L,8070450532247928832L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,4L,64L,1025L,16405L,262480L,4199680L,67194880L,1075118080L,17201889280L,275230228480L,4403683655680L,70458938490880L,1127343015854080L,18037488253665280L,288599812058644480L,4617596992938311680L,94575592174780416L,1513209474796486656L,5764607523034234880L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,2L,33L,540L,8650L,138414L,2214635L,35434160L,566946560L,9071144960L,145138319360L,2322213109760L,37155409756160L,594486556098560L,9511784897576960L,152188558361231360L,2435016933779701760L,2066782793056124928L,-3824963458521104384L,-5859183115209015296L,-1513209474796486656L,-5764607523034234880L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,}, - {0L,0L,0L,0L,1L,65536L,4294967296L,281474976710656L,0L,0L,0L,0L,0L,0L,0L,0L,135L,8847360L,579820584960L,37999121855938560L,0L,0L,0L,0L,0L,0L,0L,0L,16405L,1075118080L,70458938490880L,4617596992938311680L,0L,0L,0L,0L,0L,0L,0L,33L,2214635L,145138319360L,9511784897576960L,-3824963458521104384L,0L,0L,0L,0L,0L,0L,0L,4096L,268435729L,17592203935744L,1152922677132918784L,76842668642009088L,0L,0L,0L,0L,0L,0L,8L,552960L,36238823415L,2374947531325440L,8070608823267622912L,-8072983807038324736L,0L,0L,0L,0L,0L,0L,1025L,67194948L,4403688133701L,288600105530228736L,5783840476780036096L,6072259672578981888L,0L,0L,0L,0L,0L,2L,138414L,9071137783L,594486085783387L,2066751970480947200L,-7784595809881817088L,-8116893903405187072L,0L,0L,0L,0L,0L,256L,16777216L,1099511627777L,72057594037993729L,4311810048L,282578783305728L,72339069014638592L,0L,0L,0L,0L,0L,34560L,2264924160L,148434069749895L,-8718968878580398201L,582094356480L,38148135746273280L,-8680969756733341696L,0L,0L,0L,0L,64L,4199680L,275230228480L,18037488253681749L,1513209475875820821L,70735243837440L,4635704940130467840L,6130806467734798336L,0L,0L,0L,0L,8650L,566946560L,37155409756193L,2435016933781924608L,-1513209329119911445L,9547060033028096L,-1513172181595455488L,2444047222778626048L,0L,0L,0L,16L,1048577L,68719546624L,4503604207554576L,300166943871248L,1224996759836561425L,1157443864920915968L,1229500363472633856L,1157706579210928128L,0L,0L,0L,2160L,141557903L,9277138794248L,607986568019867768L,40522537422618488L,-646134085450172169L,8680969174647242752L,-38147594580393984L,8716435603798884352L,0L,0L,4L,262480L,17201906772L,1127344162226437L,94650720833590549L,4923631783780892757L,4685244537110860101L,6130877201840930816L,4635630379498209280L,1244400872037810176L,0L,0L,540L,35434131L,2322211272589L,152188437960410544L,-5867073703402004022L,-608753751841641693L,5021550739930207323L,2435017087666290688L,-1503124363304501248L,-3144919914788159488L,0L,1L,65536L,4294967296L,}, - {0L,135L,0L,0L,36238823415L,0L,0L,-8718968878580398201L,0L,1048577L,-646134085450172169L,0L,281474976710656L,579829432455L,33L,76842668642009088L,8072983807037771767L,9071137783L,72339069031416064L,-8680821320398633081L,2435016933781924608L,1153203052884328721L,8072983770657683879L,-608753751841642177L,1L,70995809403015L,0L,268435737L,2325224901419503607L,0L,72057596286140673L,-4047941590536714361L,8650L,1828479723647826040L,5198580908436257015L,2322211206545L,281479271743623L,4652851823974482055L,-3824945721178980319L,1229782974218235903L,-8359203940994719823L,-7785153702193098917L,-8790878036300495359L,1551279595255904722L,1157478821306368048L,-1231840126630824042L,-2244067483645952701L,-5867073703402004022L,16404L,135L,2214635L,4403419698516L,36238823415L,594486085783387L,1441151881837827092L,-8718968878580398137L,-1513209329246797650L,5766090787059466581L,-646134068292338525L,4987494515535258582L,351930695303188L,4617597572767748231L,4989366934530869954L,4995636651726742868L,2616027333969501107L,-8680969754468382976L,6148982769688008083L,-3255113792571260030L,8108459549707964399L,6110225865917094366L,2616023978973555122L,4330401300L,9440922232176661L,70995809419410L,1162434420959906814L,-6650882093460728755L,2325229302423405341L,1510550969824680745L,-4696143385879816939L,-3255272293030243662L,299538383934670116L,899585657599261095L,-6421298510395728011L,4987212465024951234L,-8439804607159910222L,4722974330230397517L,2604241862179011578L,2945765058557585494L,-4172586336866215245L,-7908298668497926605L,3292917456527349333L,-1778416807812019587L,-2687845494699988046L,-1568533776661182301L,-7153308993320401295L,-751561123640744348L,135L,135L,594486083601840L,36238823415L,36238823351L,-1512615122307666256L,-8718968878580398201L,-8718968861378493549L,-5859145822004578895L,-646134085451220746L,-5251909112208802653L,5012764771064815280L,282054806696071L,1463670458724844711L,-8184224933876447855L,8149678041610583920L,8167559115579133249L,-8179721325835109289L,-8788221579835704968L,-2069077196422502013L,-4705347465926991951L,6955256496823371726L,-7524108394299359031L,7563110548872886158L,70995809403014L,75399497649272L,-8947147165825350487L,2325224901151067886L,3838472271485441641L,-8942843533514592335L,-4119999182185003507L,4329109129124827895L,-6421336103236948990L,5891272723926220804L,-1804055540449515934L,8460564669008640934L,-3753081440134336504L,-3860138431806531030L,-7856653449343714193L,-7595651990768072831L,-4052283364874674317L,5532806268997719779L,5288704387223848300L,-8744239686539652327L,-8855053725810232034L,6006015969448097578L,1347435800541178025L,2211897868760481646L,36238839779L,72061997459758636L,590093403544079L,-8718964475161218279L,360860506022940844L,-72057449430616578L,-2087283856937846165L,2955844742563665725L,1563985813813261386L,-7320240988429897084L,-6688021649094603968L,123717882122878262L,-370210050093239481L,6069896046897945042L,-8123643723470492874L,3483026182173742943L,-7835963269163963083L,629692079040326967L,-2397767918274165203L,2307919561648830965L,-44914365399010368L,599544380208996432L,-597294467434364901L,-6964430688229133911L,2334920026170350992L,-5605342300067343405L,3551021258077608539L,609035323518161084L,-6721022405297278944L,3504310508963389432L,-7283152471191799613L,-8722198899053206729L,-1250874794737731711L,9074608179003201540L,1810628635239662178L,-6451776899373758723L,-4615694538393549295L,-4362275103790198702L,-3792222432303245256L,-1559692666614209616L,-98431217577796539L,-9029920801466639058L,-7109365235188542407L,-946677770089042793L,-182592247182753593L,-2637452241563314774L,6124864149863968072L,5021550739898464339L,}, - {0L,-675700754464725196L,-7400245184483749183L,2325224865180680192L,4308854149351096230L,4087632387066765743L,-7275283868590567591L,998815193966931967L,-2143796421407463834L,6056625504733376525L,-4045721824789864836L,-858530229944418774L,94575875809256316L,-7495074221927422752L,-3628564631334774117L,-6872602957773652666L,-8837670072557268635L,2150855273413160557L,5842706246122142201L,-8874354114744897075L,-7498620451270880714L,8345255364170114647L,8675725534108615393L,-628718379448202296L,7213058728673621187L,8697600085574001635L,569941992076422209L,2101729217045010600L,-869272563270581351L,6349834165615373008L,-8278423197265695468L,-289667629372155284L,-4996188247313713002L,-4193590892988300734L,-7808747067947116470L,5740222723455656079L,-1004293179227928657L,-7378027571087326870L,1850676172401126584L,4102071215454560899L,7259757191991660866L,-3263750637324313573L,-7101095927535849009L,2960944838505603741L,7960409200978162151L,-1380919195139420544L,-7265787777315546970L,8696134004788876257L,4459615944721376107L,357711267766598467L,-6591463393386029767L,6213350576813261001L,2844246054154448404L,-3029083139417290328L,2245638225067189479L,8617601719125400457L,2128220270181946136L,3470499785866555218L,132066871132521002L,5517882414738609434L,-2003374013673959323L,3739836398962945633L,4448468942318558050L,5616161409774106646L,-1642714353533004129L,3432917397076393183L,-7631234578304800051L,1381553953950798900L,4023962110129311253L,8644710097507238632L,-1197417248055046378L,-9214470001404003712L,1609372195130461718L,-1978500250962778826L,-4911048255582930948L,6141651141653126100L,-2210543824408560838L,-6093993786564968581L,-2331300188278471014L,301965068498387180L,-4793576488484320667L,-5125824936144379883L,-5544211551818536164L,4552272197317892247L,-7837278238339183834L,-8761637332466461638L,147364084060044539L,-6064825681333790105L,-9198755709530655411L,-1778124120876162064L,-1861492150822659393L,4745713929342438769L,6861768606120208380L,5182389685193931296L,-8534340902764341615L,-3590390582013687815L,30073544289745672L,5662918870480130927L,8401074218837039520L,-3162537045663256753L,-3696184205999945570L,-4122514099598642296L,-7789756672013896695L,-287966903401783401L,-6507490048846866302L,-4916032157138151070L,-5141347076922627105L,-5155040494742911856L,7430428299516436592L,7568914190360801521L,8976947262451327251L,-823811253246772689L,3192421721340291099L,-2321904427011346487L,-7001545068850142391L,-8141745164608837714L,-8484695803346689573L,-5018197261793538831L,-4680980064014602375L,-3442898850200611643L,-5222684558521265456L,7664587417491999280L,-1184664264514778096L,-6690427770995712658L,-4105408088494675476L,-4179651232527412888L,4672902529451387521L,-4929725195951149496L,1568605474300434685L,9132692739037808736L,1067330377231330106L,6234823338025250317L,5513535159769997480L,-6296592138893575335L,1857768573899923229L,-7795052510154389180L,-7305898577268848196L,-4018600913292308304L,5464682203804131280L,4913993492957390835L,7057555747515730451L,-4522471330477326797L,4668369156558985260L,4477286195984166923L,7381389311118714077L,-7376279152378820185L,2001470970870984953L,-6267077529255448638L,2308580872094892292L,-3732951464203912308L,6769597213437287237L,7233920586946777352L,2580562194308495651L,-5163189941835227305L,6315713421482690203L,4098234801449939167L,-209812842733548776L,-7894469765614518540L,-2554059219807878625L,1903444257061664607L,9182030862387586589L,-3109633788736425272L,6389430618268296347L,2129494461042385878L,6329472897410366361L,-91499458815143634L,1221682685580272771L,9196639794739742553L,3102002862952173801L,-2224431388243524627L,346567882230063653L,-5437991707734288744L,-2709888611820883348L,-2920666756321254869L,4930390194078211174L,-6753455264900793669L,8186033625824413668L,2517429022687892375L,-591051196767852614L,-4262657608111575498L,-3159880638279040943L,-5917782714799358545L,-2681282764713948442L,-2318918853231809968L,-6811170076728669460L,3266419467001559315L,-515756142318178599L,-3869312023589911625L,1976731212099439882L,-8842932614801981568L,3225246888378129325L,8664993276476894618L,}, - {0L,6888918647020560317L,6616704076946383887L,72307319579008765L,5019028484594594082L,-3189487582568884878L,2134511505384836107L,-8664850887478530773L,4653976163382426028L,-7552953056849816027L,6722895949646321714L,-6956540475216209151L,-5858062716999869069L,-6264590843552634074L,5887677734908074113L,2474629020427595354L,-7967281225152887102L,185220160216387872L,-5760623994813845340L,-2535719245014115912L,5111362823887419820L,8505227612259241925L,9012934128161153438L,-3454844884687058304L,2688606500979393393L,257493182509576689L,3669454563593192799L,-5918660207704940910L,989805058886100772L,-7949845614554438917L,-8650932953258294805L,3719293863967856435L,5524498832277079871L,-8204898049315048076L,7429706311125713887L,-3392350091536387004L,8803967679232678927L,-6709573047928675008L,3993787092655773756L,-3869147372389115171L,-5407630961212543773L,-3635696971192372537L,-4123255486571580111L,7821261793194743720L,-8259701755427194463L,8778804536940933206L,-2390355563131639991L,-6022268794344148517L,-7704290986577360513L,-5411054657730165929L,6563199634835578546L,-6179985290346013130L,-1779710405126312658L,3860946749005281310L,2894361089265489575L,3315776251307050326L,6320628998837644363L,-4720130451255879792L,-7396858832460579381L,4947792566457792928L,-771597949057082277L,-6283874120936860908L,7751617974029105049L,-3171781932687118992L,-4986583824628389869L,7554891199116256466L,440872917012743287L,8832529013462318719L,6930378999847853276L,-6172476313572563251L,6597889588190964693L,-3923014422236362697L,4997044127822541429L,-7107806937807475144L,-6289965583375946716L,-3044570048596622809L,-582961432760897161L,-5961388502855958672L,-7194944488185970118L,-2808201162094710660L,2825505208148432617L,-7328788671423443242L,-981277169914054735L,5488327868523545998L,8289151770336172410L,4907413581236102050L,-5687370805778312995L,59381471987291070L,-6444452561966471140L,8634477550854188789L,6207648744164223412L,-101963300189371393L,7848638498504197321L,3498348962566120926L,-5579914395407433825L,-2545139183283670919L,5130648017855629422L,563110260262788563L,2135475898455702366L,7684314690515735551L,-7002326702731443230L,3880977581557303704L,-8637342854207977051L,8496120004406519552L,8560768772188447329L,2594230810807943255L,4337654442994597095L,-4585888647597749639L,-7892855638664835779L,1565051773613738620L,-1693518853642867805L,5567975317132003788L,2962765293309317965L,-3913616984206243549L,-5558808830930587106L,-8050075131445890472L,-8457438218982837752L,-2386212332821275776L,6035074888470767028L,-7849778513431525815L,3298946049141257047L,7780989528292148686L,7259205837724911376L,4482330118862142166L,-6537500962732474747L,-3022706756077921906L,7148848233164372316L,5077131755656950340L,7912726786975152439L,4848423276587831069L,-3830309290120570642L,4508033649949774926L,6846300513888811901L,6242882741110337747L,4170945278743029343L,-8127216801805486969L,-7852480183891607774L,3990619336327845960L,-5983515027675199463L,-5175175724494917100L,-1281849133123778903L,6285841989812053065L,-6067528458241089448L,7576477052240298304L,-36164364831076984L,-60994174189450074L,-8529533350870615239L,-4817148324041097753L,-4640927619693149134L,1872208642623965689L,5240297214938900007L,1572132602803588846L,-650696111471122406L,-6683156123433455837L,4545617032206292763L,-5495059938487344780L,6556517553578965713L,-2511396774410513113L,-2176872386175367579L,-8143603626268702413L,-8219576026727867109L,5228455090572155705L,-8048532819979278350L,-2942421454182291603L,-7739452100314543781L,5339545143011211455L,431828781909893989L,6504224558731934448L,1325434730767337764L,9044277964768096581L,-2012199912615250454L,2239476923138147060L,5446470907191488828L,-8988608972356041587L,318526050097438911L,4497994325831131702L,2421970446587647365L,8944762431294272394L,-1575644653468915716L,-7480808359693534377L,-8386442038249850993L,-4826695226202864144L,-2040524201209064254L,1185655104167734601L,9208155472300323928L,5181480307949496453L,7213432351073135570L,3512466954668360697L,-4638794086271207228L,-5040741222494255775L,2246055029297786978L,-2407449019420128263L,}, - {0L,4331831946986922092L,5039137575310318944L,6572365103720100214L,-3372926667923484384L,-804959452957560038L,5319341503317224386L,6670307960034794050L,9122331677505366914L,8132210050240438928L,1856212684185378577L,-2418012329048383413L,2221935190602818642L,-686940626615845117L,1911412200771311721L,-8073430757900621106L,3214975954359082910L,-4390346519891573401L,-3501449563817128233L,2931745286610574473L,6196837566508069630L,-7985185842868269260L,-242763400354519042L,-4762972334960522653L,-1128709856992935914L,-3133210263897755440L,-6300878854527293186L,7341789762184517091L,-742546840681255590L,-7168808549419220445L,-4984050358805078354L,5072732112695304142L,2078982868474422173L,-6351627917229293982L,-5889560863851129253L,-6131615849942787808L,6542639951406636963L,8480740463682186660L,183168288556720330L,-336660061516138115L,-1410356938699911649L,926150233093626212L,-2881764498566629282L,-1145487279366622181L,2351884447002867207L,-5662126631020168770L,-2882202962071256822L,6804440515561204260L,-7645812423207814611L,4621144061789925334L,-2353433576646516057L,-664832534192041661L,-554535101160684318L,-2377767015672774851L,2162245627601598447L,7482066041309690120L,8259346317988151315L,-3846765727327599638L,1731326869372517225L,-1980000664251113326L,800694689332165758L,2515205999128895054L,-1997432734072953419L,4672602758053323554L,6952202226827626829L,1161365306867490106L,-8384523961905918605L,-733116767785239559L,5682995397931390212L,-3325241677131986932L,-7594435646578169790L,1216932035765251873L,7076597346109630370L,4335245905675328559L,-8507624828826261297L,-6137869528790051691L,-2990757269081501860L,8207981314338567695L,-211809688264439807L,-3940005235808670096L,1182929724269347747L,4877641380646887291L,-7648008948030491945L,6628446266965226465L,-2970852163554202170L,-4670164336893474473L,-153630879029846201L,-3309814009518565346L,1753932513542224524L,-5251791855420722187L,-6759963098607779538L,-8872083893346595943L,-3120155793324540715L,5978602003840494141L,7512687430188190283L,562835420304739569L,4904579120205325277L,-3683422392650008109L,7042500995660740987L,-1972180833418385981L,6346437281268254654L,-2243185610911458224L,9026737040177775190L,4993647529943791743L,288019624394432352L,-4353118648656339247L,-5420873398923132591L,1549704053713244983L,3614498467424513972L,-9027385653029153084L,1629718615812967730L,-2759637422881662616L,8432531436503595927L,611355542547437884L,-3281181767178975456L,-6653389075112192169L,-6767829238387607677L,-18377277181334064L,3726999606378779346L,8982897182547291075L,-7270818405218871784L,7385860370779663565L,8974928614734018492L,291006976587948877L,5300536609199764342L,-5751424882775853301L,4349796007363489739L,-6969537113022603028L,6155372982161835515L,2778638126408133418L,-5347543214912549170L,-9027306700095574549L,30249368683249826L,7438665593684814940L,2922690888626912628L,-8180978412153428577L,-7237995582212279627L,416623024707852752L,-6898030889740364442L,7422011222211524550L,-1171936184535606480L,-6766055360615975733L,1731128074959168128L,-4607795217355720589L,-3213029979791539367L,-5255952104659057192L,-2314414383739670603L,4396632171704966086L,3006711910774356251L,5778933548260329613L,4157127632653091975L,7318697691457423286L,569409338780015498L,4153168956437659929L,-1756599119278225307L,-3314455936932684387L,342118048129659290L,-7569984731692337361L,7663926015038805676L,-7070277473562255822L,289945662752184113L,7974238619106446240L,738352690694983924L,831937267866542901L,-7662231404878724479L,-6989417225990538386L,4271423021483837384L,-4850149730195086768L,3731768127773306728L,7364897548779496026L,-2756767884644196684L,-1753950093378288022L,7143078809577818863L,4597090775987743317L,7848087705354908100L,1905884943523277834L,-6153674769862497703L,7016308115898147045L,-1899771811690011877L,7221196628836046154L,-4199539233852873704L,-8773468848938902681L,3961256666769655818L,-3407671319284954837L,3336711503507566638L,1628300123613068774L,-6944467362566050167L,5759866442005474216L,-2680310850292804557L,-8904643492625661829L,6625111154478727695L,-8756613211059540532L,}, - }; - private static final long [][] powTable2 = { - {0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,1L,4L,16L,64L,256L,1024L,4096L,16384L,65536L,262144L,1048576L,4194304L,16777216L,67108864L,268435456L,1073741824L,4294967296L,17179869184L,68719476736L,274877906944L,1099511627776L,4398046511104L,17592186044416L,70368744177664L,281474976710656L,1125899906842624L,4503599627370496L,18014398509481984L,72057594037927936L,288230376151711744L,1152921504606846976L,4611686018427387904L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,2L,8L,33L,135L,540L,2160L,8640L,34560L,138240L,552960L,2211840L,8847360L,35389440L,141557760L,566231040L,2264924160L,9059696640L,36238786560L,144955146240L,579820584960L,2319282339840L,9277129359360L,37108517437440L,148434069749760L,593736278999040L,2374945115996160L,9499780463984640L,37999121855938560L,151996487423754240L,607985949695016960L,2431943798780067840L,-8718968878589280256L,2017612633061982208L,8070450532247928832L,-4611686018427387904L,}, - {0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,1L,16L,256L,4096L,65536L,1048576L,16777216L,268435456L,4294967296L,68719476736L,1099511627776L,17592186044416L,281474976710656L,4503599627370496L,72057594037927936L,1152921504606846976L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,8L,135L,2160L,34560L,552960L,8847360L,141557760L,2264924160L,36238786560L,579820584960L,9277129359360L,148434069749760L,2374945115996160L,37999121855938560L,607985949695016960L,-8718968878589280256L,8070450532247928832L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,4L,64L,1025L,16405L,262480L,4199680L,67194880L,1075118080L,17201889280L,275230228480L,4403683655680L,70458938490880L,1127343015854080L,18037488253665280L,288599812058644480L,4617596992938311680L,94575592174780416L,1513209474796486656L,5764607523034234880L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,2L,33L,540L,8650L,138414L,2214635L,35434160L,566946560L,9071144960L,145138319360L,2322213109760L,37155409756160L,594486556098560L,9511784897576960L,152188558361231360L,2435016933779701760L,2066782793056124928L,-3824963458521104384L,-5859183115209015296L,-1513209474796486656L,-5764607523034234880L,}, - {0L,0L,0L,0L,0L,0L,0L,0L,1L,65536L,4294967296L,281474976710656L,0L,0L,0L,0L,0L,0L,0L,0L,135L,8847360L,579820584960L,37999121855938560L,0L,0L,0L,0L,0L,0L,0L,0L,16405L,1075118080L,70458938490880L,4617596992938311680L,0L,0L,0L,0L,0L,0L,0L,33L,2214635L,145138319360L,9511784897576960L,-3824963458521104384L,0L,0L,0L,0L,0L,0L,0L,4096L,268435729L,17592203935744L,1152922677132918784L,76842668642009088L,0L,0L,0L,0L,0L,0L,8L,552960L,36238823415L,2374947531325440L,8070608823267622912L,-8072983807038324736L,0L,0L,0L,0L,0L,0L,1025L,67194948L,4403688133701L,288600105530228736L,5783840476780036096L,6072259672578981888L,0L,0L,0L,0L,0L,2L,138414L,9071137783L,594486085783387L,2066751970480947200L,-7784595809881817088L,-8116893903405187072L,0L,0L,0L,0L,0L,256L,16777216L,1099511627777L,72057594037993729L,4311810048L,282578783305728L,72339069014638592L,0L,0L,0L,0L,0L,34560L,2264924160L,148434069749895L,-8718968878580398201L,582094356480L,38148135746273280L,-8680969756733341696L,0L,0L,0L,0L,64L,4199680L,275230228480L,18037488253681749L,1513209475875820821L,70735243837440L,4635704940130467840L,6130806467734798336L,0L,0L,0L,0L,8650L,566946560L,37155409756193L,2435016933781924608L,-1513209329119911445L,9547060033028096L,-1513172181595455488L,2444047222778626048L,0L,0L,0L,16L,1048577L,68719546624L,4503604207554576L,300166943871248L,1224996759836561425L,1157443864920915968L,1229500363472633856L,1157706579210928128L,0L,0L,0L,2160L,141557903L,9277138794248L,607986568019867768L,40522537422618488L,-646134085450172169L,8680969174647242752L,-38147594580393984L,8716435603798884352L,0L,0L,4L,262480L,17201906772L,1127344162226437L,94650720833590549L,4923631783780892757L,4685244537110860101L,6130877201840930816L,4635630379498209280L,1244400872037810176L,0L,0L,540L,35434131L,2322211272589L,152188437960410544L,-5867073703402004022L,-608753751841641693L,5021550739930207323L,2435017087666290688L,-1503124363304501248L,-3144919914788159488L,}, - {0L,0L,16405L,0L,0L,4403688133701L,0L,0L,1513209475875820821L,0L,141557903L,4685244537110860101L,0L,37999121855938560L,70460013625365L,4096L,-8072983807038324728L,6072259672646177861L,1099511627777L,-8680969754468382976L,6148844231222908181L,300166943871248L,8108459549707964415L,6072264093493673942L,0L,135L,9441497757794325L,0L,36238822398L,-4344883889898761147L,0L,-8718968601253017721L,-4731747198132988651L,1048577L,-132789517139225323L,-1552739068514332347L,281474985492480L,37999701685387285L,-8439804064927760332L,79200023969402880L,-4437309755392L,5541920255199135408L,72340746325590273L,-7908297577503485241L,-6110106313622404075L,8676465578921629935L,-326184747196674646L,-3477858389994040487L,1L,2214508L,16405L,268435729L,594453068185772L,4403688133701L,72057594037993793L,7782220301781673580L,1513209475875828959L,1224996777003865156L,-5568602317731943389L,4685246687521203912L,4617315522256633857L,46947281589357164L,-3824892998506958796L,4995636651659171088L,8116893894401741024L,-2657538287028282471L,6130806742969226560L,27400700751995177L,6195590254004767622L,1157782445580441636L,-3782275202737413550L,-2658220304921737954L,575525634069L,1162363457633372907L,9441497759976190L,6917681753729944802L,-5498814577540363438L,-4345476997192759376L,1551110792602277236L,5891520892378292651L,6144410812500239428L,634601418098493862L,1774375360755843061L,132652009187043017L,4653133841193683532L,-2595690203251959175L,72110664981553453L,-1230337829717287256L,-6188909812282035538L,6456071231484419289L,-2320958359203443879L,1835508870757419369L,2040370669282281878L,6661217468141030217L,-8423640389371565403L,-8499405260925408371L,268435728L,16405L,16405L,72057594306428944L,4403688133701L,4403688142223L,1152939165798699280L,1513209475875820821L,1513211798084813650L,1224996764271055007L,4685244537241932234L,-8363651757808097592L,1190939387166195984L,38069581936595025L,5021584044531862713L,-6992383705976074216L,-2597990945295154084L,-363348139508661757L,-7600368554163986847L,-7582301154678812143L,894948077087235353L,6955255921298015887L,6959836035952832632L,3211452494295328458L,6917685570877522327L,9441497757794450L,10016896989796619L,7027313512938572782L,-4344883925063770117L,2927870116789082016L,8786239407365985943L,4131336578943587486L,-4726948581212862695L,127865309502915844L,6074982671291646492L,-3775267067956559256L,-79200059939802875L,-8744009164630305692L,4615764723498800113L,-8718682424587484521L,-6158288432344557974L,-5261638342178675313L,-9059672762503245484L,5519878846588365914L,-4436464087703865944L,1518927025456563280L,1468085383922929905L,2639105496201891805L,270650237L,4403690315305L,-8718374425263534908L,72652047106048429L,1513803926786430456L,-7346953228811164402L,9007217061455705778L,3243811299122139894L,6753230224894884636L,-5602660768557635367L,-883519440648577041L,-116177230604955516L,-2323053257641285469L,-8482307462680691891L,-5549874877820869559L,958020971772365464L,-4991544269364360344L,6363309982556444943L,7331190440433981494L,5015726139678737532L,-2043388114386413778L,5482647037605624617L,1206745860518513047L,2086309373276999127L,8079745281175912599L,-3273465508891764574L,315336160138416834L,3834475922849262099L,38021128702995733L,3474027448873074912L,-4358775760477723763L,-5618657038833202186L,-1545654340300590546L,-2631227852331219336L,-8776825519658677635L,-2327823662365475282L,5194557473419685442L,-4092526298777937177L,-916312845293823853L,-2363175478922554126L,-4978193684293897722L,-6320440829497657480L,-2670705166203992950L,8762236708630736962L,-5599128477180867553L,5323899568776667617L,-5564129564103000826L,-3382740581917399352L,}, - {0L,2716622788963031847L,40846672993527025L,1513209509967159019L,4621053680045158388L,-2903996173352235569L,-8072983807038316094L,-7575455417369363805L,8071326617522633051L,-7258922398541384564L,8704697640774031947L,-6681570071551204221L,-1562641326068657855L,-883532926705604284L,956268130454475388L,1215539162648774541L,-426414134220978363L,5270683299884762747L,2330181287926686005L,6134726476362434106L,-2325959145524659347L,4329716150192313480L,-8392881078967907522L,-1802165835817182982L,-8100388729951545868L,1867951006267200589L,7919618595933494384L,-8980269885778783131L,4071040395359672573L,-312862961573357955L,-2941873227506952960L,-8115294874401740510L,5303426865933611246L,2958139355704065308L,1463694019776869652L,-8976074711962363925L,5268644657139266566L,6964861625476310538L,-1517608123673286513L,-2915846685277558710L,5550751346972347727L,2112780311210561748L,-1828007150056065159L,6384500303152980510L,-906389597981947895L,4410709334475405688L,-6483486400246371702L,-8147255517704438781L,-6120938892653523797L,3199011783477797964L,-6761726043452623010L,-7047017470580974912L,1462717182584096488L,-7023268205675315115L,-2989303706422286108L,3180460850976781138L,-298416058831618501L,-7266836423948921205L,6663350365655515731L,-8359063233001334572L,9056022989288353575L,7798619345308693609L,-44616847615569961L,3192155203687896874L,1239389723220221729L,-4986248400106757818L,4108171716624422170L,-4173733134437352158L,-8125352421416488169L,-1185374074307245661L,-2966673101544610810L,2704683786991607858L,1515177657563554330L,5601341054180475457L,2983614494542029933L,3582348994995344418L,-7227381997475091214L,375800204207924206L,2993383011336056468L,-8189001996079290037L,682068580983257903L,948844863510518846L,-5027244184536956601L,1827155836971693192L,-1870457681157527590L,1442861833621761616L,-7501553589451368346L,7570278778816122784L,2996106664946480472L,-8781991581739643752L,-1791985321944155905L,-867682135916918265L,-3280503815586869540L,323928290362912952L,-5799577295755596577L,7265376777009662712L,-5900142203173808917L,-1517427944723435699L,8119832848073844820L,-3536200579017813566L,-3797880052796029066L,2953127755774818207L,7848593138406357335L,-3561467074012037304L,-7337175485261886469L,5807093610353856481L,4150266656318619307L,5586315349794142210L,3173004702088493577L,-4041319562043822090L,3864667468337058586L,3273021938862095623L,1830503478934844635L,3749676212788847170L,3555683139611306856L,2307286949526856026L,-6923475262585040711L,-701796416470938392L,-1858770742650766417L,5567697607330176830L,-7615393280240676601L,8367577308384162142L,1289903960876064367L,2964316419676655924L,103719735714153803L,115707662404946645L,7038009716978659739L,6993618381930301556L,-4413073683489083550L,-5009032023647056184L,8984906013653963103L,4426963583202880421L,-6178068367693439722L,3477129417051532148L,-6090672487045910366L,-6135241880902941518L,-1212340631615108657L,-3815125365865109228L,-9071677335252123805L,-1160367325517043426L,2955609817225945308L,-1003634050197601548L,5820534109579904411L,-2426268040301780073L,-8146151533873453013L,606046764860835901L,6365109532573370147L,-6149510524360813181L,8739343454238347465L,-2972886873342710445L,-8078313491105835340L,-8453826761140217942L,2610148900125924178L,4094047797743140623L,1798485114169935380L,4998884408623716905L,8950537777569088369L,1259580490436590913L,-315040158391152316L,4404501065226543846L,8146418735010981422L,-3207553747699863543L,8675392909711600921L,2405040524590938945L,3011427288135051459L,-7505026985532373362L,-717246331947472893L,-1249163693485939994L,-5296945757900320134L,4406943265523691209L,-7262817264707461431L,4641142375242104030L,974365924473300426L,5883429614278840526L,1752533021582360374L,4682777309199078800L,3204063093503561410L,-363155732334907633L,-2346835733169282527L,-8137667428439140060L,-5206973815505988715L,-6974909107915889500L,8764043288183918255L,-4092038371708175209L,3554060350791641111L,8798581772638267674L,683285472572572424L,7214985925390295205L,-2157639133813705666L,-1448249137206045231L,3845284593116750432L,-491254275194118919L,}, - {0L,-9067886894117383031L,-8765631554449913793L,-351960140370164101L,-3809828891435867380L,709347176573105019L,6367995667762707277L,-2306580641000201140L,-3294298272065609853L,4371465194278568200L,2607306921379681548L,-417323118857018121L,-7881922716238547623L,1742624878807396150L,6417633233153038438L,7245011226171800446L,8165326897970180555L,-3243122380933127165L,-6714040292093312106L,5299517670423801760L,1162621650998540887L,-2644879706039864385L,5228558976169295831L,-4391072266260069004L,5007015542307306646L,4682900706152011413L,-2123901141179325738L,-1563119241267841448L,-7901984116490304069L,3479726911851668567L,2328631765004444895L,4163098372279135427L,-1162220268661065413L,7829988552306176969L,3774955113097750561L,-2924095674636243058L,-3804005518905654993L,4738269630527809961L,5850935874683467236L,8655960374699796185L,-9059189580654183421L,4147746414725146890L,24509238664879977L,-7300123086118785535L,4751556328478914134L,1176138900156266839L,-2121425120505405076L,7247247000762229134L,9000592214487314811L,-889874608502765498L,7236795550799772071L,4974622482278556577L,-420560834611630453L,-3768646734241421044L,-6934105751774839201L,2969065130293732380L,1157896836976407312L,-1559291870109505818L,-2713062981234471322L,-7531022569765588290L,885491871452046438L,8154994685434777085L,-6183517998680799133L,-1006560964259624252L,-3294584533985251232L,8399396859432285260L,4941554285858435526L,-6156845871586263088L,-3284695794597124922L,1532126679216399071L,8710324980140137084L,-5829657872686309788L,-8769115779063538072L,6935866645975431778L,3597238061932133376L,2900308539208720779L,-2048705180801093564L,7055281952541030950L,-7002220757486340324L,964568490678066690L,-8767317763140366030L,-7051357021415343791L,-4942724421035409788L,-3480567531822663066L,97030438992350687L,6148848201715609829L,8190673576143502363L,-582873467951437833L,2647318604324937637L,-6741961242409679499L,-3005976745528477871L,6997691635910859134L,-1554393429201478263L,5312146010243409080L,8436439012567294277L,-6630723209210577509L,-8673971494977105192L,3562527997978101964L,3222754741725930251L,-5514361182259644100L,8086871624387515496L,-1467927774684365475L,8787210120862734018L,3868688879557973489L,6456800047903216186L,2070752891678043017L,-2941591783186713651L,-8183443290798012978L,8647945886161088410L,-1841819063379755142L,7860120982846312859L,-8493947801835772681L,-8772774970693045973L,-5241215312398964897L,-2611583395517621586L,-6102100345907364588L,1496816916737236359L,-7037938107874011031L,1848097601417926664L,-3280842067707319343L,9059361377927179923L,3282174138875773780L,-4916481664416226618L,3886169231649272478L,-1162360497370278599L,637220995047853939L,2885978966316747573L,6728781912350490111L,2328479630147269651L,5554304721491051724L,-6390247834560005501L,678119673988273692L,8651416760092589714L,3253782092936323383L,-1172981379019092788L,-2681934234447846106L,2685869215896638688L,3821621354052583845L,-2684383087845706054L,8084097633656028849L,-8363538709557862805L,-4959083379007699006L,-636423994845221856L,-6174337185251636501L,-8181624222422079238L,5775773648593308303L,2958061024555050145L,-5902970918704504061L,-2311021221920430097L,-1163971532460119016L,7608815371737216021L,-2151017459333583787L,-4990542649899559495L,-7513884523432741003L,4393693670759726766L,-6124974036012512535L,4359543309307998296L,-7037830908455507891L,9041027053538546397L,-4965851250845680310L,-2710962042488783523L,-8360351447962823382L,8718758072633673592L,6091137861253238579L,3170648231776465081L,7255519673286422939L,-7053661224901986133L,-915340883967460252L,58498294164417086L,-3484407801438382424L,-5784099705119286635L,4125777477825819372L,-6146926288831583310L,-3273005006573409355L,3494948349508696583L,-5006780570108823652L,1464644271341482461L,8662100360049866867L,-8213718696027725861L,6111700097284965138L,8732551933992222890L,-2625466519519768621L,7604239518435423389L,-7828716348124971154L,-7235290890666697460L,5948056974595294574L,1526856277675902747L,6112243656016507212L,-5496701154568696152L,-3510748376081434273L,-8169496792421316254L,-8879479780209390885L,}, - {0L,3140177619151565L,1460417631189212726L,2353554947493303907L,-8202113219766144441L,-6445610874707668273L,608805251577783707L,5576331011745724676L,-4616645954249110408L,-8150056064724481814L,-4375043884813768089L,61489915732671441L,2417003783109232201L,-5797177228338315992L,2030419241542275643L,3535467551177747619L,8160953148558434627L,3783933582983994877L,-395558155134838237L,-9037492967799507046L,-5583543374553761415L,-3766787778222337249L,83690989555392614L,586532773118624438L,6753062925187026745L,-8948265279136309565L,-6410106965476177336L,4682508874368457020L,-295508894803101770L,8755364544425811426L,-6946865691886399246L,-4998699326132353090L,-4913666944514812110L,6656002279105587104L,-4102330896140732563L,4073773542208959172L,1868016903708504679L,5519576265667760424L,-4364728854253647198L,-4053216048524447777L,6663500710558038858L,6972529717731021777L,-3207571042298769577L,-3888399221174842362L,420006135991670627L,8470075279405824658L,7027685758317034910L,2040544246752732537L,-3800680914792011602L,2040759759708548109L,-9015248103296315503L,652011287933867814L,-7782739132326567829L,-2317801922460293066L,2611886844620986012L,-8984631646102328554L,891896526125902634L,-367951637747449086L,-3497113700860363823L,5236614907167581294L,4114633954038032258L,-7512497826449542169L,3242179150310062402L,5331743182358224881L,7796816641120626796L,-2424392677330210781L,-8144516314316042367L,675811259852555579L,-4634618573840023378L,4925946102011935067L,-4976229192152584784L,-3306578665043743720L,-6377989030567179047L,3293903018441646805L,618512784046332063L,53396408973512940L,-972771490037569515L,-6449609039165015841L,-4713122490770203220L,5807317894024892376L,-2670957669784138572L,3253385083202136062L,4051471174117632380L,-6471306451500783400L,6958488026378472944L,-5240312948719888335L,-8730122111509150856L,-391530967442739522L,5304728157073167586L,-8727565006975506202L,-2051755253763667777L,-3562176666377319713L,7612155015414030601L,1518046426766570808L,-307960163954445386L,-4612626700850755125L,-7636305212757699943L,697133298319656099L,-4630649231468148194L,5297521963269776703L,-9050284098059359704L,-9005170998800333392L,2690681881609407239L,-6345572204046605232L,5811140775354848418L,-7583858705664828398L,883333908996047580L,-3023430280093746546L,1253866782730544252L,-2652541341293732240L,-3271892520967689019L,-3752005443895063020L,4447917552968482513L,-3824236721243846818L,5600139313482945383L,-999779136651389228L,-8469897042294769427L,7796143214715526711L,406420894930862997L,-2962130523947052332L,-1278282830806339692L,-8447450042751963291L,634287000685320862L,-4988625188634368073L,7264114668740343559L,604237143730786090L,1470443674382315935L,7627286917302141493L,6193255306513896806L,-88995703250039083L,5251413380424095294L,-896519829376623449L,-1255840563362049523L,-6375440828517507443L,2331875313404929916L,8954805250915502220L,7208472503953793396L,-4921350084320109520L,8682056056407108854L,-588986527289762379L,9058761110104874970L,5619968217006612529L,3556136729690226244L,2636161296644079035L,-8961930654928536483L,-5855381532408164884L,8378124878485911957L,-2362426267959965117L,2730324786590578227L,-7828512552827125787L,6135910401929572633L,7539770217393522009L,2966065950377957666L,-2096713473244396849L,-7604673598994665387L,-7219768014399086113L,2704497122794833704L,-2967228398298959366L,-6762901644464291495L,-1553630643759993924L,1564603397139288648L,6116811891197961186L,3562368697804772802L,-3199395829836276360L,-1165308188865718640L,-2427793488252231210L,-8760734042314058841L,-7857272506160709120L,3203259669013418168L,-946026787142432929L,4945843407304527756L,-3777254025118911417L,-8496811611764847693L,6997957085954944184L,6393220067418700989L,1296680119038768392L,5852192073241482937L,-4072182146803896176L,-6966297004098237683L,-5487642568473235337L,-1519244559474694756L,3811374754853013692L,-2605325301426890003L,-2885808776371324742L,6661350203815069651L,1327657210385814471L,-3825923588202089762L,-7915987217397706762L,6735184013381996252L,1519213888176055875L,8382345159179417209L,1911513965813618260L,}, - }; - - //The tables above were generated by the code below. The code is no longer needed. - - /* ******************************************************************************************* - - - static long [][] powTable0 = new long [7] []; - static long [][] powTable1 = new long [7] []; - static long [][] powTable2 = new long [7] []; - - public static void genPowTable() { - GF2_192 z = new GF2_192(); - int i; - - powTable0[0] = new long [192]; - powTable1[0] = new long [192]; - powTable2[0] = new long [192]; - i = 0; - for (; i<64; i++) { - z.word[0] = 1L<=7) { - // By Fermat's little theorem, z^{2^{2^k}} = z^{2^{2^k} mod (2^{192}-1)} - // If k>=7, then 2^{2^k} mod (2^{192}-1) = 2^64 when k is even and 2^128 when k is odd (proof below), - // so that's what we compute. - // Note that we have no precomputed table for k=7 (i.e., 2^128), because we don't expect - // this to be needed -- only up to k=6 is used in inversion. - // Here's the proof: let m = 64. 2^{2^k} mod (2^{192}-1) = 2^{mn} mod (2^{3m}-1) for n = 2^{k-6}. - // Let d = n div 3 and r = n mod 3. - // Then 2^{mn} = (2^{3m}-1) (2^{m(n-3}}+2^{m(n-6)}+...+2^{m-nd})+2^{nr} - // So the remainder is 2^{nr}. r is 2 when k is odd and 1 when k is even. - - power2To2ToK (res, z, 6); - if (k%2 == 1) { - power2To2ToK(res, res, 6); - } - } - else { - // powTable0[k][i] contains the result of raising x^i to the power 2^k for i = 0...63 - // powTable0[k][i-64] contains the result of raising x^i to the power 2^k for i = 64...127 - // powTable0[k][i-128] contains the result of raising x^i to the power 2^k for i = 128...191 - // Because raising to the power 2^k is linear over any field of characteristic 2, - // we just need to XOR the values in these tables at indices i where z is 1. - // This selection is done via multiplication by 0 or 1, to avoid having an input-dependent path - // through the code, thus reducing the chance of side-channel attacks. - // - // Note that more efficient tables can be precomputed -- for example, the result of raising - // every one of 16 possible 4-bit nibbles at every one of 32 possible nibble positions. - // But indexing into these tables will be input-dependent, which may make side-channel attacks easier. - - long t0 = 0; - long t1 = 0; - long t2 = 0; - int maxIndex = 0, i = 0; - for (long w : z.word) { - maxIndex += 64; - for (; i < maxIndex; i++) { - long multiplier = (w & 1); - // No "if w&1 == 0" here, to avoid a data-dependent path through the code, - // thus reducing the chance of side channel attacks - t0 ^= powTable0[k][i] * multiplier; - t1 ^= powTable1[k][i] * multiplier; - t2 ^= powTable2[k][i] * multiplier; - w >>>= 1; - } - } - res.word[0] = t0; - res.word[1] = t1; - res.word[2] = t2; - } - } - - - /** - * - * @return bits of this in hexadecimal notation, most significant on the left - */ - public String toString() { - return String.format("%016X", word[2])+String.format("%016X", word[1])+String.format("%016X", word[0]); - } - - -} diff --git a/sigmastate/src/main/java/gf2t/GF2_192_Poly.java b/sigmastate/src/main/java/gf2t/GF2_192_Poly.java deleted file mode 100644 index d3ccd61fc3..0000000000 --- a/sigmastate/src/main/java/gf2t/GF2_192_Poly.java +++ /dev/null @@ -1,247 +0,0 @@ -/* - By Leonid Reyzin - - This is free and unencumbered software released into the public domain. - - Anyone is free to copy, modify, publish, use, compile, sell, or - distribute this software, either in source code form or as a compiled - binary, for any purpose, commercial or non-commercial, and by any - means. - - In jurisdictions that recognize copyright laws, the author or authors - of this software dedicate any and all copyright interest in the - software to the public domain. We make this dedication for the benefit - of the public at large and to the detriment of our heirs and - successors. We intend this dedication to be an overt act of - relinquishment in perpetuity of all present and future rights to this - software under copyright law. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR - OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - OTHER DEALINGS IN THE SOFTWARE. - - For more information, please refer to - */ - -package gf2t; - -import java.util.Arrays; - -public class GF2_192_Poly { - private final GF2_192 [] c; // must be not null and of length at least 1 - - private int deg; // must be >=0. actual degree is <= deg. c[deg+1]...c[c.length-1] must be 0 or null - // deg of the 0 polynomial is 0 - - /** - * Constructs the polynomial given the byte array representation of the coefficients. - * Coefficient of degree zero is given separately. Each coefficient should be given - * as a 24-byte representation of a GF2_192 value. Coefficient of degree 1 should - * start at moreCoeffs[0]. - * @param coeff0 byte array representing lowest coefficient (24 bytes) - * @param moreCoeffs byte array with concatenation of byte-converted coefficients - * (24 bytes each) from degree 1 to the highest - */ - public GF2_192_Poly (byte[] coeff0, byte[] moreCoeffs) { - deg = moreCoeffs.length/24; - c = new GF2_192[deg+1]; - c[0] = new GF2_192(coeff0); - for (int i = 1; i<=deg; i++) { - c[i] = new GF2_192(moreCoeffs, (i-1)*24); - } - } - - /** - * Factory constructor -- same as GF2_192_Poly(coeff0, moreCoeffs) - * @param coeff0 byte array representing lowest coefficient (24 bytes) - * @param moreCoeffs byte array with concatenation of byte-converted coefficients - * (24 bytes each) from degree 1 to the highest - * @return new polynomial with the given coefficients - */ - public static GF2_192_Poly fromByteArray(byte[] coeff0, byte[] moreCoeffs) { - return new GF2_192_Poly(coeff0, moreCoeffs); - } - - - /** - * Interpolates the polynomial at given points (and at point 0, if valueAt0!=null). - * If points are not all distinct, or if 0 is in the points array and valueAt0!=null, behavior is undefined. - * valueAt0 is separated only for efficiency reason; the caller can treat 0 like any other point instead - * (i.e., the points array can include 0 if valueAt0==null, but computation will be slightly less efficient). - * If points is null, or values is null, or if lengths of points and values arrays differ, - * or if the arrays are 0 length and valueAt0 is null, returns null. - * - * @param points the set of distinct inputs to the returned polynomial - * (last byte of the field element only; all other bits are assumed to be 0) - * @param values values[i] will be the result evaluating the returned polynomial at points[i]. values[i] must not be null. - * @param valueAt0 if not null, then valueAt0 will be the result of evaluating the returned polynomial at 0 - * @return the unique lowest-degree polynomial p such that for every i, p(points[i]) = values[i] and p(0)=valueAt0 - * (if valueAt0!=null) - */ - public static GF2_192_Poly interpolate (byte[] points, GF2_192 [] values, GF2_192 valueAt0) { - if (points == null || values == null || (values.length == 0 && valueAt0 == null) || values.length!=points.length) return null; - - int resultDegree = values.length-1; - if (valueAt0!=null) { - resultDegree++; - } - - GF2_192_Poly result = new GF2_192_Poly(resultDegree, 0); - GF2_192_Poly vanishingPoly = new GF2_192_Poly(resultDegree, 1); - - for (int i = 0; i < points.length; i++) { - GF2_192 t = result.evaluate(points[i]); - GF2_192 s = vanishingPoly.evaluate(points[i]); - - // need to find r such that currentValue+r*valueOfVanishingPoly = values[i] - GF2_192.add(t, t, values[i]); - GF2_192.invert(s, s); - GF2_192.mul(t, t, s); - - result.addMonicTimesConstantTo(vanishingPoly, t); - - if (i < points.length - 1 || valueAt0!=null) { - vanishingPoly.monicTimesMonomial(points[i]); - } - } - - if (valueAt0!=null) { // the last point is 0 - GF2_192 t = new GF2_192(result.c[0]); // evaluating at 0 is easy - GF2_192 s = new GF2_192(vanishingPoly.c[0]); // evaluating at 0 is easy - - // need to find r such that currentValue+r*valueOfVanishingPoly = valueAt0] - GF2_192.add(t, t, valueAt0); - GF2_192.invert(s, s); - GF2_192.mul(t, t, s); - result.addMonicTimesConstantTo(vanishingPoly, t); - } - return result; - } - - /** - * Evaluates the polynomial at a given point - * @param x the last byte of a field element (all other bits are assumed to be 0) - * @return the value of this polynomial evaluated at the field element - */ - public GF2_192 evaluate (byte x) { - GF2_192 res = new GF2_192(c[deg]); - for (int d = deg-1; d>=0; d--) { - GF2_192.mul(res, res, x); - GF2_192.add(res, res, c[d]); - } - return res; - } - - /** - * adds r*p to this; assumes p is monic, c.length>p.deg, and (p.deg == this.deg+1, or this==0 and p==1) - * @param p the monic polynomial being added to this - * @param r the constant by which p is multiplied before being added - */ - private void addMonicTimesConstantTo (GF2_192_Poly p, GF2_192 r) { - GF2_192 t = new GF2_192(); - for (int i = 0; i 0; i--) { - // c[i] = c[i-1]+r*c[i] - GF2_192.mul(c[i], c[i], r); - GF2_192.add(c[i], c[i], c[i - 1]); - } - GF2_192.mul(c[0], c[0], r); - } - - - /** - * Constructs a constant polynomial - * - * @param maxDeg the maximum degree this polynomial could possibly have (to allocate space) - * @param constantTerm the polynomial is initially created with degree 0 and given constantTerm - */ - private GF2_192_Poly (int maxDeg, int constantTerm) { - c = new GF2_192[maxDeg+1]; - c[0] = new GF2_192(constantTerm); - deg = 0; - } - - /** - * - * @return this represented in usual polynomial notation (but possibly leading 0s), with X as the free variable - */ - public String toString() { - String ret = ""; - if (deg>=2) { - ret+= c[deg].toString() + "*X^"+deg; - int i; - for (i = deg - 1; i >= 2; i--) { - ret += " + " + c[i]+"*X^"+i; - } - ret+= " + "; - } - if (deg>=1) { - ret += c[1] + "*X" + " + "; - } - ret+=c[0]; - return ret; - } - - - /** - * Returns a byte array that contains the concatenation of all the coefficients - * (except possibly the degree-0 coefficient, which is omitted if coeff0 is false). - * Lowest-degree coefficient (0 or 1 depending on coeff0) starts at index 0 of the returned array. - * Each coefficient takes 24 bytes, for a total of degree*24 bytes if coeff0 is false, - * or (degree+1)*24 bytes if coeff0 is true - * @param coeff0 whether to include coeff0 - * @return array of all coefficients (except possibly 0th depending on coeff0) - */ - public byte[] toByteArray(Boolean coeff0) { - int c0; - if (coeff0) c0 = 0; - else c0=1; - byte [] ret = new byte[(deg+1-c0)*24]; - for (int i=c0; i<=deg; i++) { - c[i].toByteArray(ret, (i-c0)*24); - - } - return ret; - } - - /** - * @return The degree-0 coefficient, converted to an array of 24 bytes - */ - public byte[] coeff0Bytes() { - return c[0].toByteArray(); - } - - @Override - public int hashCode() { - return 31 * Arrays.deepHashCode(c) + deg; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj instanceof GF2_192_Poly) { - GF2_192_Poly that = (GF2_192_Poly)obj; - return Arrays.deepEquals(c, that.c) && deg == that.deg; - } - return false; - } -} diff --git a/sigmastate/src/main/scala/org/ergoplatform/ErgoBox.scala b/sigmastate/src/main/scala/org/ergoplatform/ErgoBox.scala index e097a6302f..f1c883ab60 100644 --- a/sigmastate/src/main/scala/org/ergoplatform/ErgoBox.scala +++ b/sigmastate/src/main/scala/org/ergoplatform/ErgoBox.scala @@ -61,7 +61,7 @@ class ErgoBox( import ErgoBox._ /** Blake2b256 hash of the serialized `bytes`. */ - lazy val id: BoxId = ADKey @@ Blake2b256.hash(bytes) + lazy val id: BoxId = ADKey @@@ Blake2b256.hash(bytes) override def get(identifier: RegisterId): Option[Value[SType]] = { identifier match { diff --git a/sigmastate/src/main/scala/sigmastate/SigSerializer.scala b/sigmastate/src/main/scala/sigmastate/SigSerializer.scala index 045274e75b..92c18463dc 100644 --- a/sigmastate/src/main/scala/sigmastate/SigSerializer.scala +++ b/sigmastate/src/main/scala/sigmastate/SigSerializer.scala @@ -1,8 +1,7 @@ package sigmastate import com.typesafe.scalalogging.LazyLogging -import gf2t.GF2_192_Poly -import sigmastate.crypto.BigIntegers +import sigmastate.crypto.{BigIntegers, GF2_192_Poly} import scorex.util.encode.Base16 import sigmastate.Values.SigmaBoolean import sigmastate.basics.DLogProtocol.{ProveDlog, SecondDLogProverMessage} @@ -11,7 +10,7 @@ import sigmastate.basics.{ProveDHTuple, SecondDiffieHellmanTupleProverMessage} import sigmastate.interpreter.ErgoTreeEvaluator.{fixedCostOp, perItemCostOp} import sigmastate.interpreter.{CryptoConstants, ErgoTreeEvaluator, NamedDesc, OperationCostInfo} import sigmastate.lang.exceptions.SerializerException -import sigmastate.serialization.{SigmaSerializer} +import sigmastate.serialization.SigmaSerializer import sigmastate.util.safeNewArray import sigmastate.utils.{Helpers, SigmaByteReader, SigmaByteWriter} import debox.cfor diff --git a/sigmastate/src/main/scala/sigmastate/UncheckedTree.scala b/sigmastate/src/main/scala/sigmastate/UncheckedTree.scala index f932f7d77f..365a1f85a7 100644 --- a/sigmastate/src/main/scala/sigmastate/UncheckedTree.scala +++ b/sigmastate/src/main/scala/sigmastate/UncheckedTree.scala @@ -4,8 +4,8 @@ import java.util.Arrays import sigmastate.basics.DLogProtocol.{FirstDLogProverMessage, ProveDlog, SecondDLogProverMessage} import sigmastate.basics.VerifierMessage.Challenge import sigmastate.Values.SigmaBoolean -import gf2t.GF2_192_Poly import sigmastate.basics.{FirstDiffieHellmanTupleProverMessage, ProveDHTuple, SecondDiffieHellmanTupleProverMessage} +import sigmastate.crypto.GF2_192_Poly sealed trait UncheckedTree extends ProofTree diff --git a/sigmastate/src/main/scala/sigmastate/UnprovenTree.scala b/sigmastate/src/main/scala/sigmastate/UnprovenTree.scala index 8d4dc83f6d..6e600b927b 100644 --- a/sigmastate/src/main/scala/sigmastate/UnprovenTree.scala +++ b/sigmastate/src/main/scala/sigmastate/UnprovenTree.scala @@ -1,8 +1,6 @@ package sigmastate import java.math.BigInteger - -import gf2t.GF2_192_Poly import sigmastate.Values.{ErgoTree, SigmaBoolean, SigmaPropConstant} import sigmastate.basics.DLogProtocol.{FirstDLogProverMessage, ProveDlog} import sigmastate.basics.VerifierMessage.Challenge @@ -13,7 +11,7 @@ import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer import sigmastate.serialization.SigmaSerializer import sigmastate.utils.SigmaByteWriter import debox.cfor - +import sigmastate.crypto.GF2_192_Poly import scala.language.existentials object ConjectureType extends Enumeration { diff --git a/sigmastate/src/main/scala/sigmastate/crypto/GF2_192.scala b/sigmastate/src/main/scala/sigmastate/crypto/GF2_192.scala new file mode 100644 index 0000000000..36e8343e2b --- /dev/null +++ b/sigmastate/src/main/scala/sigmastate/crypto/GF2_192.scala @@ -0,0 +1,577 @@ +/* + By Leonid Reyzin + + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to + */ +package sigmastate.crypto + +import debox.cfor + +import java.util + +class GF2_192 extends AnyRef { + private[crypto] val word: Array[Long] = new Array[Long](3) + + /** + * returns a copy of the field element + * + * @param that element to copy + */ + def this(that: GF2_192) { + this() + this.word(0) = that.word(0) + this.word(1) = that.word(1) + this.word(2) = that.word(2) + } + + /** + * returns the field element whose 32 least significant bits are bits of that and rest are 0 + * + * @param that lower 32 bits + */ + def this(that: Int) { + this() + this.word(0) = that.toLong & 0xFFFFFFFFL + } + + /** + * returns the field element whose bits are given by the long array + * + * @param that must be length 3 + */ + def this(that: Array[Long]) { + this() + require(that.length == 3) + this.word(0) = that(0) + this.word(1) = that(1) + this.word(2) = that(2) + } + + /** + * returns the field element whose bits are given by the byte array that[pos]...that[pos+23] + * + * @param that must be length at least pos+24 + */ + def this(that: Array[Byte], pos: Int) { + this() + assert(that.length >= pos + 24) + cfor(0)(_ < 8, _ + 1) { i => + word(0) |= (that(i + pos).toLong & 0xFF) << (i << 3) + } + cfor(0)(_ < 8, _ + 1) { i => + word(1) |= (that(i + pos + 8).toLong & 0xFF) << (i << 3) + } + cfor(0)(_ < 8, _ + 1) { i => + word(2) |= (that(i + pos + 16).toLong & 0xFF) << (i << 3) + } + } + + /** + * returns the field element whose bits are given by the byte array that + * + * @param that must be length 24 + */ + def this(that: Array[Byte]) { + this(that, 0) + } + + + /** + * + * @param obj the field element with which to compare + * @return true if and only if this and that represent the same field element + */ + override def equals(obj: Any): Boolean = { + if (this eq obj.asInstanceOf[AnyRef]) return true // equal references + obj match { + case that: GF2_192 => + this.word(0) == that.word(0) && this.word(1) == that.word(1) && this.word(2) == that.word(2) + case _ => false + } + } + + override def hashCode = util.Arrays.hashCode(word) + + /** + * + * @return long array of length 3 containing the three words of the field element + */ + def toLongArray: Array[Long] = { + val ret = new Array[Long](3) + ret(0) = word(0) + ret(1) = word(1) + ret(2) = word(2) + ret + } + + /** + * + * @return byte array of length 24 containing the two words of the field element + */ + def toByteArray: Array[Byte] = { + val ret = new Array[Byte](24) + toByteArray(ret, 0) + ret + } + + /** + * @param ret bytes of the field element will go into ret[pos]...ret[pos+23] + */ + def toByteArray(ret: Array[Byte], pos: Int): Unit = { + assert(ret.length >= pos + 24) + cfor (0)(_ < 3, _ + 1) { j => + cfor(0)(_ < 8, _ + 1) { i => + ret(pos + i + 8 * j) = ((word(j) >> (i << 3)) & 0xFF).toByte + } + } + } + + /** + * + * @return true if this == 0, false otherwise + */ + def isZero: Boolean = word(0) == 0L && word(1) == 0L && word(2) == 0L + + /** + * + * @return true if this == 1, false otherwise + */ + def isOne: Boolean = word(0) == 1L && word(1) == 0L && word(2) == 0L + + //The tables above were generated by the code below. The code is no longer needed. + /* ******************************************************************************************* + + + static long [][] powTable0 = new long [7] []; + static long [][] powTable1 = new long [7] []; + static long [][] powTable2 = new long [7] []; + + public static void genPowTable() { + GF2_192 z = new GF2_192(); + int i; + + powTable0[0] = new long [192]; + powTable1[0] = new long [192]; + powTable2[0] = new long [192]; + i = 0; + for (; i<64; i++) { + z.word[0] = 1L<= 0, _ - 1) { i => + w3 = w2 >>> 63 + w2 = (w2 << 1) | (w1 >>> 63) + w1 = (w1 << 1) | (w0 >>> 63) + w0 <<= 1 + val t: Long = (b >>> i) & 1 + w2 ^= a.word(2) * t + w1 ^= a.word(1) * t + w0 ^= (a.word(0) * t) ^ (irredPentanomial * w3) // mod reduce + } + res.word(0) = w0 + res.word(1) = w1 + res.word(2) = w2 + } + + /** + * Computes a plus b and puts the result into res. + * + * @param res output; must be not null; may be equal to a and/or b + * @param a multiplicand; may be equal to res, in which case will get overwritten + * @param b multiplier; may be equal to res, in which case will get overwritten + */ + def add(res: GF2_192, a: GF2_192, b: GF2_192): Unit = { + res.word(0) = a.word(0) ^ b.word(0) + res.word(1) = a.word(1) ^ b.word(1) + res.word(2) = a.word(2) ^ b.word(2) + } + + /** + * Computes a times b and puts the result into res. + * Uses table lookups, which may not preserve + * the secrecy of the inputs in case of side-channel attacks. + * + * @param res output; must be not null; may be equal to a and/or b + * @param a multiplicand; may be equal to res, in which case will get overwritten + * @param b multiplier; may be equal to res, in which case will get overwritten + */ + def mul(res: GF2_192, + a: GF2_192, + b: GF2_192): Unit = { + // Implements a sort of times-x-and-add algorithm, except instead of multiplying by x + // we multiply by x^4 and then add one of possible 16 precomputed values + // contains a*0, a*1, a*x, a*(x+1), a*x^2, a*(x^2+1), a*(x^2+x), a*(x^2+x+1) + // a*x^3, a*(x^3+1), a*(x^3+x), a*(x^3+x+1), a*(x^3+x^2), a*(x^3+x^2+1), a*(x^3+x^2+x), a*(x^3+x^2+x+1), all mod reduced + // First word of each is in a0 muls, second word of each is in a1muls, third word of each is in a2muls + val a0muls = new Array[Long](16) + val a1muls = new Array[Long](16) + val a2muls = new Array[Long](16) + + // a0muls[0], a1muls[0] and a2muls[0] are already correctly initialized to 0 + a0muls(1) = a.word(0) + a1muls(1) = a.word(1) + a2muls(1) = a.word(2) + // a*x, a*x^2, a*x^3 + var i = 2 + while (i <= 8) { + // multiply a*x^{log_2 i/2} by x to get a*x^{log_2 i} + val prev = i / 2 + a0muls(i) = a0muls(prev) << 1 + a1muls(i) = (a1muls(prev) << 1) | (a0muls(prev) >>> 63) + a2muls(i) = (a2muls(prev) << 1) | (a1muls(prev) >>> 63) + // mod reduce + a0muls(i) ^= irredMuls((a2muls(prev) >>> 63).toInt) + i *= 2 + } + + // a*(x+1) + a0muls(3) = a0muls(1) ^ a0muls(2) + a1muls(3) = a1muls(1) ^ a1muls(2) + a2muls(3) = a2muls(1) ^ a2muls(2) + + // a*(x^2+1), a*(x^2+x), a*(x^2+x+1) + cfor(1)(_ < 4, _ + 1) { i => + a0muls(4 | i) = a0muls(4) ^ a0muls(i) + a1muls(4 | i) = a1muls(4) ^ a1muls(i) + a2muls(4 | i) = a2muls(4) ^ a2muls(i) + } + + // a*(x^3+1), a*(x^3+x), a*(x^3+x+1), a*(x^3+x^2), a*(x^3+x^2+1), a*(x^3+x^2+x), a*(x^3+x^2+x+1) + cfor(1)(_ < 8, _ + 1) { i => + a0muls(8 | i) = a0muls(8) ^ a0muls(i) + a1muls(8 | i) = a1muls(8) ^ a1muls(i) + a2muls(8 | i) = a2muls(8) ^ a2muls(i) + } + var w0: Long = 0 + var w1: Long = 0 + var w2: Long = 0 + cfor(2)(_ >= 0, _ - 1) { j => + val multiplier = b.word(j) + var i = 60 + while (i >= 0) { + // Multiply by x^4 + val modReduceIndex = (w2 >>> 60).toInt + w2 = (w2 << 4) | (w1 >>> 60) + w1 = (w1 << 4) | (w0 >>> 60) + // MOD REDUCE ACCORDING TO modReduceIndex by XORing the right value + w0 = (w0 << 4) ^ irredMuls(modReduceIndex) + //w0 = (w0<<4)^(irredPentanomial*(modReduceIndex&8))^(irredPentanomial*(modReduceIndex&4))^(irredPentanomial*(modReduceIndex&2))^(irredPentanomial*(modReduceIndex&1)); + // Add the correct multiple of a + val index = ((multiplier >>> i) & 15).toInt + w0 ^= a0muls(index) + w1 ^= a1muls(index) + w2 ^= a2muls(index) + i -= 4 + } + } + res.word(0) = w0 + res.word(1) = w1 + res.word(2) = w2 + } + + def invert(res: GF2_192, + z: GF2_192): Unit = { + // Computes z^{2^192-2} = z^{exponent written in binary as 191 ones followed by a single zero} + // (by Fermat's little theorem, this is the correct inverse) + // contains z raised to the power whose binary representation is 2^k ones + val zTo2ToK1s = new GF2_192(z) + + // Square res to get its exponent to be 10 in binary + mul(res, z, z) + // contains z raised to the power whose binary representation is 2^k ones followed by 2^k zeros + val zTo2ToK1s2ToK0s = new GF2_192(res) + // Loop invariant + // res contains z raised to the power whose binary representation is 2^{k+1}-1 ones followed by a single zero + // zTo2ToK1s contains z raised to the power whose binary representation is 2^k ones + // zTo2ToK1s2ToK0s contains z raised to the power whose binary representation is 2^k ones followed by 2^k zeros + var k = 0 + while (k < 6) { + k += 1 + // Fill in the zeros in the exponent of zTo2ToK1s2ToK0s with ones + mul(zTo2ToK1s, zTo2ToK1s2ToK0s, zTo2ToK1s) + // zTo2ToK1s2ToK0s = power2To2ToK with 2^k zeros appended to the exponent + power2To2ToK(zTo2ToK1s2ToK0s, zTo2ToK1s, k) + // prepend 2^k ones to res + mul(res, res, zTo2ToK1s2ToK0s) + } + // Prepened another 64 ones to res + power2To2ToK(zTo2ToK1s2ToK0s, zTo2ToK1s2ToK0s, k) + mul(res, res, zTo2ToK1s2ToK0s) + } + + /** + * Squares z and puts the result into res. Same as power2To2ToK(res, z, 0). + * About same efficiency as mul(res, z, z) (more efficient implementations are possible, + * but not provided here because of risk of side-channel attacks) + * + * @param res output; must be not null; may be equal to z + * @param z input to be squared; may be equal to res, in which case will get overwritten + */ + def sqr(res: GF2_192, + z: GF2_192): Unit = { + // It is possible to precompute the square of every byte value in every byte position + // (only bytes a few byte positions are needed -- squares of bytes in other positions + // can be figured out by shifting squares of bytes in positions 0 and 16). This will result in a slightly + // bigger table and several times more efficient squaring. However, because table lookups will + // be input-dependent, this gives a higher risk of side-channel attacks that reveal z. + // Hence, it's not implemented. + // + power2To2ToK(res, z, 0) + } + + /** + * Raises z to the power 2^{2^k} and puts the result into res. Same sqr(z, z) 2^k times. + * Takes only about as much time as mul(res, z, z) (even more efficient implementations are possible, + * but not provided here because of risk of side-channel attacks) + * @param res output; must be not null; may be equal to z + * @param z input to be squared; may be equal to res, in which case will get overwritten + */ + def power2To2ToK(res: GF2_192, z: GF2_192, k: Int): Unit = { + if (k >= 7) { + // By Fermat's little theorem, z^{2^{2^k}} = z^{2^{2^k} mod (2^{192}-1)} + // If k>=7, then 2^{2^k} mod (2^{192}-1) = 2^64 when k is even and 2^128 when k is odd (proof below), + // so that's what we compute. + // Note that we have no precomputed table for k=7 (i.e., 2^128), because we don't expect + // this to be needed -- only up to k=6 is used in inversion. + // Here's the proof: let m = 64. 2^{2^k} mod (2^{192}-1) = 2^{mn} mod (2^{3m}-1) for n = 2^{k-6}. + // Let d = n div 3 and r = n mod 3. + // Then 2^{mn} = (2^{3m}-1) (2^{m(n-3}}+2^{m(n-6)}+...+2^{m-nd})+2^{nr} + // So the remainder is 2^{nr}. r is 2 when k is odd and 1 when k is even. + power2To2ToK(res, z, 6) + if (k % 2 == 1) { + power2To2ToK(res, res, 6) + } + } else { + // powTable0[k][i] contains the result of raising x^i to the power 2^k for i = 0...63 + // powTable0[k][i-64] contains the result of raising x^i to the power 2^k for i = 64...127 + // powTable0[k][i-128] contains the result of raising x^i to the power 2^k for i = 128...191 + // Because raising to the power 2^k is linear over any field of characteristic 2, + // we just need to XOR the values in these tables at indices i where z is 1. + // This selection is done via multiplication by 0 or 1, to avoid having an input-dependent path + // through the code, thus reducing the chance of side-channel attacks. + // + // Note that more efficient tables can be precomputed -- for example, the result of raising + // every one of 16 possible 4-bit nibbles at every one of 32 possible nibble positions. + // But indexing into these tables will be input-dependent, which may make side-channel attacks easier. + var t0: Long = 0 + var t1: Long = 0 + var t2: Long = 0 + var maxIndex = 0 + var i = 0 + cfor(0)(_ < z.word.length, _ + 1) { iW => + var w = z.word(iW) + maxIndex += 64 + while (i < maxIndex) { + val multiplier: Long = w & 1 + // No "if w&1 == 0" here, to avoid a data-dependent path through the code, + // thus reducing the chance of side channel attacks + t0 ^= powTable0(k)(i) * multiplier + t1 ^= powTable1(k)(i) * multiplier + t2 ^= powTable2(k)(i) * multiplier + w >>>= 1 + i += 1 + } + } + res.word(0) = t0 + res.word(1) = t1 + res.word(2) = t2 + } + } +} diff --git a/sigmastate/src/main/scala/sigmastate/crypto/GF2_192_Poly.scala b/sigmastate/src/main/scala/sigmastate/crypto/GF2_192_Poly.scala new file mode 100644 index 0000000000..dc0617ea29 --- /dev/null +++ b/sigmastate/src/main/scala/sigmastate/crypto/GF2_192_Poly.scala @@ -0,0 +1,257 @@ +/* + By Leonid Reyzin + + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to + */ + +package sigmastate.crypto + +import debox.cfor + +import java.util +import java.util.Arrays + +class GF2_192_Poly { + final private var c: Array[GF2_192] = null // must be not null and of length at least 1 + + private var deg: Int = 0 // must be >=0. actual degree is <= deg. c[deg+1]...c[c.length-1] must be 0 or null + // deg of the 0 polynomial is 0 + + /** + * Constructs the polynomial given the byte array representation of the coefficients. + * Coefficient of degree zero is given separately. Each coefficient should be given + * as a 24-byte representation of a GF2_192 value. Coefficient of degree 1 should + * start at moreCoeffs[0]. + * + * @param coeff0 byte array representing lowest coefficient (24 bytes) + * @param moreCoeffs byte array with concatenation of byte-converted coefficients + * (24 bytes each) from degree 1 to the highest + */ + def this(coeff0: Array[Byte], moreCoeffs: Array[Byte]) { + this() + deg = moreCoeffs.length / 24 + c = new Array[GF2_192](deg + 1) + c(0) = new GF2_192(coeff0) + cfor(1)(_ <= deg, _ + 1) { i => + c(i) = new GF2_192(moreCoeffs, (i - 1) * 24) + } + } + + /** + * Evaluates the polynomial at a given point + * + * @param x the last byte of a field element (all other bits are assumed to be 0) + * @return the value of this polynomial evaluated at the field element + */ + def evaluate(x: Byte): GF2_192 = { + val res = new GF2_192(c(deg)) + cfor(deg - 1)(_ >= 0, _ - 1) { d => + GF2_192.mul(res, res, x) + GF2_192.add(res, res, c(d)) + } + res + } + + /** + * adds r*p to this; assumes p is monic, c.length>p.deg, and (p.deg == this.deg+1, or this==0 and p==1) + * + * @param p the monic polynomial being added to this + * @param r the constant by which p is multiplied before being added + */ + private def addMonicTimesConstantTo(p: GF2_192_Poly, r: GF2_192): Unit = { + val t: GF2_192 = new GF2_192 + cfor(0)(_ < p.deg, _ + 1) { i => + GF2_192.mul(t, p.c(i), r) + GF2_192.add(c(i), c(i), t) + } + deg = p.deg + c(deg) = new GF2_192(r) + } + + /** + * multiplies this by (x+r), assuming this is monic of degree deg (i.e. assumed c[deg]==1) + * + * @param r the constant term of the monomial + */ + private def monicTimesMonomial(r: Byte): Unit = { + deg += 1 + c(deg) = new GF2_192(1) + cfor(deg - 1)(_ > 0, _ - 1) { i => + // c[i] = c[i-1]+r*c[i] + GF2_192.mul(c(i), c(i), r) + GF2_192.add(c(i), c(i), c(i - 1)) + } + GF2_192.mul(c(0), c(0), r) + } + + /** + * Constructs a constant polynomial + * + * @param maxDeg the maximum degree this polynomial could possibly have (to allocate space) + * @param constantTerm the polynomial is initially created with degree 0 and given constantTerm + */ + def this(maxDeg: Int, constantTerm: Int) { + this() + c = new Array[GF2_192](maxDeg + 1) + c(0) = new GF2_192(constantTerm) + deg = 0 + } + + /** + * + * @return this represented in usual polynomial notation (but possibly leading 0s), with X as the free variable + */ + override def toString: String = { + var ret: String = "" + if (deg >= 2) { + ret += c(deg).toString + "*X^" + deg + cfor(deg - 1)(_ >= 2, _ - 1) { i => + ret += " + " + c(i) + "*X^" + i + } + ret += " + " + } + if (deg >= 1) { + ret += c(1) + "*X" + " + " + } + ret += c(0) + ret + } + + /** + * Returns a byte array that contains the concatenation of all the coefficients + * (except possibly the degree-0 coefficient, which is omitted if coeff0 is false). + * Lowest-degree coefficient (0 or 1 depending on coeff0) starts at index 0 of the returned array. + * Each coefficient takes 24 bytes, for a total of degree*24 bytes if coeff0 is false, + * or (degree+1)*24 bytes if coeff0 is true + * + * @param coeff0 whether to include coeff0 + * @return array of all coefficients (except possibly 0th depending on coeff0) + */ + def toByteArray(coeff0: Boolean): Array[Byte] = { + val c0 = if (coeff0) 0 else 1 + val ret: Array[Byte] = new Array[Byte]((deg + 1 - c0) * 24) + cfor(c0)(_ <= deg, _ + 1) { i => + c(i).toByteArray(ret, (i - c0) * 24) + } + ret + } + + /** + * @return The degree-0 coefficient, converted to an array of 24 bytes + */ + def coeff0Bytes: Array[Byte] = c(0).toByteArray + + override def hashCode: Int = { + 31 * util.Arrays.deepHashCode(c.asInstanceOf[Array[AnyRef]]) + deg + } + + override def equals(obj: Any): Boolean = { + if (this eq obj.asInstanceOf[AnyRef]) return true + obj match { + case that: GF2_192_Poly => + util.Arrays.deepEquals(c.asInstanceOf[Array[AnyRef]], that.c.asInstanceOf[Array[AnyRef]]) && + deg == that.deg + case _ => false + } + } +} + +object GF2_192_Poly { + /** + * Factory constructor -- same as GF2_192_Poly(coeff0, moreCoeffs) + * + * @param coeff0 byte array representing lowest coefficient (24 bytes) + * @param moreCoeffs byte array with concatenation of byte-converted coefficients + * (24 bytes each) from degree 1 to the highest + * @return new polynomial with the given coefficients + */ + // deg of the 0 polynomial is 0 + def fromByteArray(coeff0: Array[Byte], moreCoeffs: Array[Byte]): GF2_192_Poly = { + val res = new GF2_192_Poly(coeff0, moreCoeffs) + res + } + + /** + * Interpolates the polynomial at given points (and at point 0, if valueAt0!=null). + * If points are not all distinct, or if 0 is in the points array and valueAt0!=null, behavior is undefined. + * valueAt0 is separated only for efficiency reason; the caller can treat 0 like any other point instead + * (i.e., the points array can include 0 if valueAt0==null, but computation will be slightly less efficient). + * If points is null, or values is null, or if lengths of points and values arrays differ, + * or if the arrays are 0 length and valueAt0 is null, returns null. + * + * @param points the set of distinct inputs to the returned polynomial + * (last byte of the field element only; all other bits are assumed to be 0) + * @param values values[i] will be the result evaluating the returned polynomial at points[i]. values[i] must not be null. + * @param valueAt0 if not null, then valueAt0 will be the result of evaluating the returned polynomial at 0 + * @return the unique lowest-degree polynomial p such that for every i, p(points[i]) = values[i] and p(0)=valueAt0 + * (if valueAt0!=null) + */ + def interpolate(points: Array[Byte], + values: Array[GF2_192], + valueAt0: GF2_192): GF2_192_Poly = { + if (points == null || values == null || + (values.length == 0 && valueAt0 == null) || + values.length != points.length) return null + + var resultDegree = values.length - 1 + if (valueAt0 != null) resultDegree += 1 + + val result = new GF2_192_Poly(resultDegree, 0) + val vanishingPoly = new GF2_192_Poly(resultDegree, 1) + + cfor (0)(_ < points.length, _ + 1) { i => + val t = result.evaluate(points(i)) + val s = vanishingPoly.evaluate(points(i)) + + // need to find r such that currentValue+r*valueOfVanishingPoly = values[i] + GF2_192.add(t, t, values(i)) + GF2_192.invert(s, s) + GF2_192.mul(t, t, s) + + result.addMonicTimesConstantTo(vanishingPoly, t) + + if (i < points.length - 1 || valueAt0 != null) { + vanishingPoly.monicTimesMonomial(points(i)) + } + } + + if (valueAt0 != null) { + // the last point is 0 + val t = new GF2_192(result.c(0)) // evaluating at 0 is easy + val s = new GF2_192(vanishingPoly.c(0)) + + // need to find r such that currentValue+r*valueOfVanishingPoly = valueAt0] + GF2_192.add(t, t, valueAt0) + GF2_192.invert(s, s) + GF2_192.mul(t, t, s) + result.addMonicTimesConstantTo(vanishingPoly, t) + } + + result + } + +} + diff --git a/sigmastate/src/main/scala/sigmastate/interpreter/ProverInterpreter.scala b/sigmastate/src/main/scala/sigmastate/interpreter/ProverInterpreter.scala index c6aead2b29..d49cba76f3 100644 --- a/sigmastate/src/main/scala/sigmastate/interpreter/ProverInterpreter.scala +++ b/sigmastate/src/main/scala/sigmastate/interpreter/ProverInterpreter.scala @@ -1,6 +1,5 @@ package sigmastate.interpreter -import gf2t.{GF2_192, GF2_192_Poly} import sigmastate.kiama.rewriting.Rewriter.{everywherebu, everywheretd, rule} import sigmastate.kiama.rewriting.Strategy import scalan.util.CollectionUtil._ @@ -11,6 +10,7 @@ import sigmastate._ import sigmastate.basics.DLogProtocol._ import sigmastate.basics.VerifierMessage.Challenge import sigmastate.basics._ +import sigmastate.crypto.{GF2_192, GF2_192_Poly} import sigmastate.lang.exceptions.InterpreterException import sigmastate.utils.Helpers @@ -469,7 +469,7 @@ trait ProverInterpreter extends Interpreter with ProverUtils { val newChildren = t.children.foldLeft(Seq[ProofTree](), 1) { case ((s, count), child) => val newChild = child match { - case r: UnprovenTree if r.real => r.withChallenge(Challenge @@ q.evaluate(count.toByte).toByteArray()) + case r: UnprovenTree if r.real => r.withChallenge(Challenge @@ q.evaluate(count.toByte).toByteArray) case p: ProofTree => p } (s :+ newChild, count + 1) diff --git a/sigmastate/src/test/java/gf2t/GF2_192Test.java b/sigmastate/src/test/java/gf2t/GF2_192Test.java index 46a4110b82..f438907efe 100644 --- a/sigmastate/src/test/java/gf2t/GF2_192Test.java +++ b/sigmastate/src/test/java/gf2t/GF2_192Test.java @@ -30,6 +30,8 @@ import org.junit.Test; import org.junit.runner.RunWith; +import sigmastate.crypto.GF2_192; +import sigmastate.crypto.GF2_192_Poly; import java.util.Random; import java.util.Arrays; diff --git a/sigmastate/src/test/scala/sigmastate/SigmaProtocolSpecification.scala b/sigmastate/src/test/scala/sigmastate/SigmaProtocolSpecification.scala index b5381c2780..96931f5e84 100644 --- a/sigmastate/src/test/scala/sigmastate/SigmaProtocolSpecification.scala +++ b/sigmastate/src/test/scala/sigmastate/SigmaProtocolSpecification.scala @@ -1,7 +1,7 @@ package sigmastate -import gf2t.{GF2_192_Poly, GF2_192} import sigmastate.basics.VerifierMessage.Challenge +import sigmastate.crypto.{GF2_192, GF2_192_Poly} import special.sigma.SigmaTestingData class SigmaProtocolSpecification extends SigmaTestingData { diff --git a/sigmastate/src/test/scala/sigmastate/helpers/SigmaPPrint.scala b/sigmastate/src/test/scala/sigmastate/helpers/SigmaPPrint.scala index f9a5e64885..3122e2f5f0 100644 --- a/sigmastate/src/test/scala/sigmastate/helpers/SigmaPPrint.scala +++ b/sigmastate/src/test/scala/sigmastate/helpers/SigmaPPrint.scala @@ -1,7 +1,6 @@ package sigmastate.helpers import java.math.BigInteger -import gf2t.GF2_192_Poly import org.ergoplatform.ErgoBox import org.ergoplatform.ErgoBox.RegisterId import org.ergoplatform.settings.ErgoAlgos @@ -11,6 +10,7 @@ import scalan.RType.PrimitiveType import sigmastate.SCollection._ import sigmastate.Values.{ConstantNode, ErgoTree, FuncValue, ValueCompanion} import sigmastate._ +import sigmastate.crypto.GF2_192_Poly import sigmastate.interpreter.CryptoConstants.EcPointType import sigmastate.lang.SigmaTyper import sigmastate.lang.Terms.MethodCall @@ -111,7 +111,7 @@ object SigmaPPrint extends PPrinter { Tree.Apply("BigInt", treeifyMany(v.toString(16), 16)) case poly: GF2_192_Poly => - val c0 = poly.coeff0Bytes() + val c0 = poly.coeff0Bytes val others = poly.toByteArray(false) // don't output Tree.Apply("GF2_192_Poly.fromByteArray", treeifyMany(c0, others)) diff --git a/sigmastate/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala b/sigmastate/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala index 3c09275a1b..ed3382fa10 100644 --- a/sigmastate/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala +++ b/sigmastate/src/test/scala/sigmastate/serialization/AvlTreeSpecification.scala @@ -45,7 +45,7 @@ class AvlTreeSpecification extends SerializationSpecification { val k = Blake2b256.hash("1") val v = k - avlProver.performOneOperation(Insert(ADKey @@ k, ADValue @@ v)) + avlProver.performOneOperation(Insert(ADKey @@@ k, ADValue @@@ v)) val proof = avlProver.generateProof() val resTree = tree.insert(Array(k.toColl -> v.toColl).toColl, proof.toColl).get diff --git a/sigmastate/src/test/scala/sigmastate/serialization/DeserializationResilience.scala b/sigmastate/src/test/scala/sigmastate/serialization/DeserializationResilience.scala index d8733d89a9..22be862da2 100644 --- a/sigmastate/src/test/scala/sigmastate/serialization/DeserializationResilience.scala +++ b/sigmastate/src/test/scala/sigmastate/serialization/DeserializationResilience.scala @@ -403,10 +403,10 @@ class DeserializationResilience extends SerializationSpecification val tree = SigmaDsl.avlTree(treeData) val k = Blake2b256.hash("1") val v = k - avlProver.performOneOperation(Insert(ADKey @@ k, ADValue @@ v)) + avlProver.performOneOperation(Insert(ADKey @@@ k, ADValue @@@ v)) val proof = avlProver.generateProof() val verifier = tree.createVerifier(Colls.fromArray(proof)) - verifier.performOneOperation(Insert(ADKey @@ k, ADValue @@ v)).isFailure shouldBe true + verifier.performOneOperation(Insert(ADKey @@@ k, ADValue @@@ v)).isFailure shouldBe true // NOTE, even though performOneOperation fails, some AvlTree$ methods used in Interpreter // (remove_eval, update_eval, contains_eval) won't throw, while others will. } diff --git a/sigmastate/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala b/sigmastate/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala index 57a578174a..dbaa64f57e 100644 --- a/sigmastate/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala +++ b/sigmastate/src/test/scala/sigmastate/serialization/SigSerializerSpecification.scala @@ -2,8 +2,6 @@ package sigmastate.serialization import java.math.BigInteger import java.util - -import gf2t.GF2_192_Poly import org.ergoplatform.settings.ErgoAlgos import org.scalacheck.{Arbitrary, Gen} import org.scalatest.Assertion @@ -12,6 +10,7 @@ import sigmastate._ import sigmastate.basics.DLogProtocol.{ProveDlog, SecondDLogProverMessage} import sigmastate.basics.VerifierMessage.Challenge import sigmastate.basics.{ProveDHTuple, SecondDiffieHellmanTupleProverMessage} +import sigmastate.crypto.GF2_192_Poly import sigmastate.helpers.{ContextEnrichingTestProvingInterpreter, ErgoLikeContextTesting, ErgoLikeTransactionTesting, SigmaTestingCommons} import sigmastate.interpreter.Interpreter import sigmastate.serialization.generators.ObjectGenerators diff --git a/sigmastate/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala b/sigmastate/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala index 537c616b05..1693a302e4 100644 --- a/sigmastate/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala +++ b/sigmastate/src/test/scala/sigmastate/serialization/generators/ObjectGenerators.scala @@ -188,7 +188,7 @@ trait ObjectGenerators extends TypeGenerators def additionalTokensGen(cnt: Int): Seq[Gen[(Digest32, Long)]] = (0 until cnt).map { _ => for { - id <- Digest32 @@ boxIdGen + id <- Digest32 @@@ boxIdGen amt <- Gen.oneOf(1, 500, 20000, 10000000, Long.MaxValue) } yield id -> amt } @@ -252,7 +252,7 @@ trait ObjectGenerators extends TypeGenerators flags <- avlTreeFlagsGen keyLength <- unsignedIntGen vl <- arbOption[Int](Arbitrary(unsignedIntGen)).arbitrary - } yield AvlTreeData(ADDigest @@ digest, flags, keyLength, vl) + } yield AvlTreeData(ADDigest @@@ digest, flags, keyLength, vl) def avlTreeGen: Gen[AvlTree] = avlTreeDataGen.map(SigmaDsl.avlTree) diff --git a/sigmastate/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala b/sigmastate/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala index e7abf54a0a..a9b07f8b00 100644 --- a/sigmastate/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala +++ b/sigmastate/src/test/scala/sigmastate/utxo/AVLTreeScriptsSpecification.scala @@ -31,8 +31,8 @@ class AVLTreeScriptsSpecification extends SigmaTestingCommons private val reg1 = ErgoBox.nonMandatoryRegisters(0) private val reg2 = ErgoBox.nonMandatoryRegisters(1) - def genKey(str: String): ADKey = ADKey @@ Blake2b256("key: " + str) - def genValue(str: String): ADValue = ADValue @@ Blake2b256("val: " + str) + def genKey(str: String): ADKey = ADKey @@@ Blake2b256("key: " + str) + def genValue(str: String): ADValue = ADValue @@@ Blake2b256("val: " + str) val inKey = genKey("init key") val inValue = genValue("init value") @@ -231,7 +231,7 @@ class AVLTreeScriptsSpecification extends SigmaTestingCommons property("avl tree - contains key satisfying condition") { val elements = Seq(123, 22) - val treeElements = elements.map(i => Longs.toByteArray(i)).map(s => (ADKey @@ Blake2b256(s), ADValue @@ s)) + val treeElements = elements.map(i => Longs.toByteArray(i)).map(s => (ADKey @@@ Blake2b256(s), ADValue @@ s)) val avlProver = new BatchAVLProver[Digest32, Blake2b256.type](keyLength = 32, None) treeElements.foreach(s => avlProver.performOneOperation(Insert(s._1, s._2))) avlProver.generateProof() diff --git a/sigmastate/src/test/scala/sigmastate/utxo/examples/IcoExample.scala b/sigmastate/src/test/scala/sigmastate/utxo/examples/IcoExample.scala index 790cf69afc..a52c4d2999 100644 --- a/sigmastate/src/test/scala/sigmastate/utxo/examples/IcoExample.scala +++ b/sigmastate/src/test/scala/sigmastate/utxo/examples/IcoExample.scala @@ -440,7 +440,7 @@ class IcoExample extends SigmaTestingCommons val projectBoxBeforeClosing = testBox(10, issuanceTree, 0, Seq(), Map(R4 -> ByteArrayConstant(Array.emptyByteArray), R5 -> AvlTreeConstant(openTreeData))) - val tokenId = Digest32 @@ projectBoxBeforeClosing.id + val tokenId = Digest32 @@@ projectBoxBeforeClosing.id val closedTreeData = SigmaDsl.avlTree(new AvlTreeData(digest, AvlTreeFlags.RemoveOnly, 32, None)) val projectBoxAfterClosing = testBox(1, withdrawalTree, 0, @@ -481,7 +481,7 @@ class IcoExample extends SigmaTestingCommons } funderKvs.foreach { case (k, v) => - avlProver.performOneOperation(Insert(ADKey @@ k, ADValue @@ v)) + avlProver.performOneOperation(Insert(ADKey @@@ k, ADValue @@ v)) } val digest = avlProver.digest val fundersTree = new AvlTreeData(digest, AvlTreeFlags.RemoveOnly, 32, None) @@ -492,12 +492,12 @@ class IcoExample extends SigmaTestingCommons avlProver.generateProof() withdrawals.foreach { case (k, _) => - avlProver.performOneOperation(Lookup(ADKey @@ k)) + avlProver.performOneOperation(Lookup(ADKey @@@ k)) } val lookupProof = avlProver.generateProof() withdrawals.foreach { case (k, _) => - avlProver.performOneOperation(Remove(ADKey @@ k)) + avlProver.performOneOperation(Remove(ADKey @@@ k)) } val removalProof = avlProver.generateProof() diff --git a/sigmastate/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala b/sigmastate/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala index 2639522f64..63182e2a1a 100644 --- a/sigmastate/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala +++ b/sigmastate/src/test/scala/sigmastate/utxo/examples/LetsSpecification.scala @@ -291,9 +291,9 @@ class LetsSpecification extends SigmaTestingCommons with CrossVersionProps { sui R4 -> AvlTreeConstant(SigmaDsl.avlTree(initTreeData)), R5 -> SigmaPropConstant(TrivialProp.TrueProp))) - val userTokenId = Digest32 @@ projectBoxBefore.id + val userTokenId = Digest32 @@@ projectBoxBefore.id - avlProver.performOneOperation(Insert(ADKey @@ userTokenId, ADValue @@ Array.emptyByteArray)) + avlProver.performOneOperation(Insert(ADKey @@@ userTokenId, ADValue @@ Array.emptyByteArray)) val proof = avlProver.generateProof() val endTree = new AvlTreeData(avlProver.digest, AvlTreeFlags.InsertOnly, 32, None) @@ -330,14 +330,14 @@ class LetsSpecification extends SigmaTestingCommons with CrossVersionProps { sui val userTokenId1 = Digest32 @@ Array.fill(32)(Random.nextInt(100).toByte) val avlProver = new BatchAVLProver[Digest32, Blake2b256.type](keyLength = 32, None) - avlProver.performOneOperation(Insert(ADKey @@ userTokenId0, ADValue @@ Array.emptyByteArray)) - avlProver.performOneOperation(Insert(ADKey @@ userTokenId1, ADValue @@ Array.emptyByteArray)) + avlProver.performOneOperation(Insert(ADKey @@@ userTokenId0, ADValue @@ Array.emptyByteArray)) + avlProver.performOneOperation(Insert(ADKey @@@ userTokenId1, ADValue @@ Array.emptyByteArray)) val digest = avlProver.digest avlProver.generateProof() val initTreeData = new AvlTreeData(digest, AvlTreeFlags.InsertOnly, 32, None) - avlProver.performOneOperation(Lookup(ADKey @@ userTokenId0)) - avlProver.performOneOperation(Lookup(ADKey @@ userTokenId1)) + avlProver.performOneOperation(Lookup(ADKey @@@ userTokenId0)) + avlProver.performOneOperation(Lookup(ADKey @@@ userTokenId1)) val proof = avlProver.generateProof() val managementTree = mkTestErgoTree(managementScript) diff --git a/sigmastate/src/test/scala/sigmastate/utxo/examples/MASTExampleSpecification.scala b/sigmastate/src/test/scala/sigmastate/utxo/examples/MASTExampleSpecification.scala index 3a5d28b45c..2de3395431 100644 --- a/sigmastate/src/test/scala/sigmastate/utxo/examples/MASTExampleSpecification.scala +++ b/sigmastate/src/test/scala/sigmastate/utxo/examples/MASTExampleSpecification.scala @@ -83,7 +83,7 @@ class MASTExampleSpecification extends SigmaTestingCommons val allSecrets = (0 until 5).map(_ => Random.nextString(32).getBytes("UTF-8")) val scriptBranches = allSecrets.map(s => EQ(ByteArrayConstant(s), GetVarByteArray(secretId).get)) val scriptBranchesBytes = scriptBranches.map(b => ValueSerializer.serialize(b)) - val treeElements: Seq[(ADKey, ADValue)] = scriptBranchesBytes.map(s => (ADKey @@ Blake2b256(s), ADValue @@ s)) + val treeElements: Seq[(ADKey, ADValue)] = scriptBranchesBytes.map(s => (ADKey @@@ Blake2b256(s), ADValue @@ s)) val knownSecretTreeKey = treeElements.head._1 val knownSecret = ByteArrayConstant(allSecrets.head) diff --git a/sigmastate/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala b/sigmastate/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala index 3774e5ddc2..178d053bd2 100644 --- a/sigmastate/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala +++ b/sigmastate/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala @@ -114,7 +114,7 @@ class OracleExamplesSpecification extends SigmaTestingCommons val avlProver = new BatchAVLProver[Digest32, Blake2b256.type](keyLength = 32, None) - avlProver.performOneOperation(Insert(ADKey @@ oracleBox.id, ADValue @@ oracleBox.bytes)) + avlProver.performOneOperation(Insert(ADKey @@@ oracleBox.id, ADValue @@ oracleBox.bytes)) avlProver.generateProof() val lastBlockUtxoDigest = avlProver.digest @@ -153,7 +153,7 @@ class OracleExamplesSpecification extends SigmaTestingCommons ), contractLogic) - avlProver.performOneOperation(Lookup(ADKey @@ oracleBox.id)) + avlProver.performOneOperation(Lookup(ADKey @@@ oracleBox.id)) val proof = avlProver.generateProof() val newBox1 = testBox(20, alicePubKey, 0, boxIndex = 2)