|
1 | 1 | package org.tron.common.crypto.zksnark;
|
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.Arrays; |
| 8 | +import org.tron.common.utils.ByteArray; |
| 9 | + |
3 | 10 | public class VerifyingKey {
|
4 | 11 |
|
5 | 12 | private G2Point A;
|
@@ -90,4 +97,210 @@ public static VerifyingKey getInstance() {
|
90 | 97 | }
|
91 | 98 | return vk;
|
92 | 99 | }
|
| 100 | + |
| 101 | + public static void assertAequalsB(byte a, byte b) throws IOException { |
| 102 | + if (a != b) { |
| 103 | + byte[] A = {a}; |
| 104 | + byte[] B = {b}; |
| 105 | + throw new IOException( |
| 106 | + "Need " + ByteArray.toHexString(A) + " but found " + ByteArray.toHexString(B)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public static VerifyingKey loadVk() { |
| 111 | + if (vk == null) { |
| 112 | + File file = new File("sprout-verifying.key"); |
| 113 | + Long filelength = file.length(); |
| 114 | + byte[] filecontent = new byte[filelength.intValue()]; |
| 115 | + try { |
| 116 | + FileInputStream in = new FileInputStream(file); |
| 117 | + in.read(filecontent); |
| 118 | + in.close(); |
| 119 | + vk = new VerifyingKey(); |
| 120 | + int offset = 0; |
| 121 | + assertAequalsB((byte) (0x30), filecontent[offset++]); |
| 122 | + vk.A = Pairing.G2Point(Arrays.copyOfRange(filecontent, offset, offset + 128)); |
| 123 | + offset += 128; |
| 124 | + assertAequalsB((byte) (0x30), filecontent[offset++]); |
| 125 | + vk.B = Pairing.G1Point(Arrays.copyOfRange(filecontent, offset, offset + 64)); |
| 126 | + offset += 64; |
| 127 | + assertAequalsB((byte) (0x30), filecontent[offset++]); |
| 128 | + vk.C = Pairing.G2Point(Arrays.copyOfRange(filecontent, offset, offset + 128)); |
| 129 | + offset += 128; |
| 130 | + assertAequalsB((byte) (0x30), filecontent[offset++]); |
| 131 | + vk.gamma = Pairing.G2Point(Arrays.copyOfRange(filecontent, offset, offset + 128)); |
| 132 | + offset += 128; |
| 133 | + assertAequalsB((byte) (0x30), filecontent[offset++]); |
| 134 | + vk.gammaBeta1 = Pairing.G1Point(Arrays.copyOfRange(filecontent, offset, offset + 64)); |
| 135 | + offset += 64; |
| 136 | + assertAequalsB((byte) (0x30), filecontent[offset++]); |
| 137 | + vk.gammaBeta2 = Pairing.G2Point(Arrays.copyOfRange(filecontent, offset, offset + 128)); |
| 138 | + offset += 128; |
| 139 | + assertAequalsB((byte) (0x30), filecontent[offset++]); |
| 140 | + vk.Z = Pairing.G2Point(Arrays.copyOfRange(filecontent, offset, offset + 128)); |
| 141 | + offset += 128; |
| 142 | + vk.IC = new G1Point[10]; |
| 143 | + assertAequalsB((byte) (0x30), filecontent[offset++]); |
| 144 | + vk.IC[0] = Pairing.G1Point(Arrays.copyOfRange(filecontent, offset, offset + 64)); |
| 145 | + offset += 88; |
| 146 | + for (int i = 1; i < 10; i++) { |
| 147 | + assertAequalsB((byte) (0x30), filecontent[offset++]); |
| 148 | + vk.IC[i] = Pairing.G1Point(Arrays.copyOfRange(filecontent, offset, offset + 64)); |
| 149 | + offset += 64; |
| 150 | + } |
| 151 | + } catch (FileNotFoundException e) { |
| 152 | + e.printStackTrace(); |
| 153 | + } catch (IOException e) { |
| 154 | + e.printStackTrace(); |
| 155 | + } |
| 156 | + } |
| 157 | + return vk; |
| 158 | + } |
| 159 | + |
| 160 | + public static VerifyingKey initVk() { |
| 161 | + if (vk == null) { |
| 162 | + vk = new VerifyingKey(); |
| 163 | + vk.A = new G2Point( |
| 164 | + "14752851163271972921165116810778899752274893127848647655434033030151679466487", |
| 165 | + "2146841959437886920191033516947821737903543682424168472444605468016078231160", |
| 166 | + "19774899457345372253936887903062884289284519982717033379297427576421785416781", |
| 167 | + "8159591693044959083845993640644415462154314071906244874217244895511876957520"); |
| 168 | + vk.B = new G1Point( |
| 169 | + "21163380042281667028194921861846440787793088615342153907557220755287297358850", |
| 170 | + "1730005633951488561162401768080055521907218407650744548610087024095695199108"); |
| 171 | + vk.C = new G2Point( |
| 172 | + "21049232722760520884910305096518213957309396732908002623546433288382066807275", |
| 173 | + "752476689148090443252690606274719847522796924289184281944322016120845872819", |
| 174 | + "9346016947773545029940290874113526292203330783138316933543286726319309993747", |
| 175 | + "10657101118636466197534311304303971390099046792106599174009327086566056805776"); |
| 176 | + vk.Z = new G2Point( |
| 177 | + "15147055940817099984713168864119185960995485721468434382981947300642935581737", |
| 178 | + "4631727067030503710010688256995134761045201948838871620017875546783390086460", |
| 179 | + "17623047202600292659611642134440671974256480551657416500487803939184025704533", |
| 180 | + "21638878652776235365545898652250152098523031965244840843903617079107891864279"); |
| 181 | + vk.gamma = new G2Point( |
| 182 | + "17174171333098854828033634539500164902488935492941049779522958919502622588081", |
| 183 | + "15684072703239714088748884492940919778409948011906556607893998678768263898205", |
| 184 | + "2875949754418862832249955782188169764124161746616276218844232725526931423080", |
| 185 | + "3086697999584045732786424745914779370086036468911841736272013630524944011227"); |
| 186 | + vk.gammaBeta1 = new G1Point( |
| 187 | + "9712873799510369170966410452086991795283841876597505062330138725142728449825", |
| 188 | + "9433999572643313025031367487888933220352848413609488855427374525420646960237"); |
| 189 | + vk.gammaBeta2 = new G2Point( |
| 190 | + "14123988352319117906018970862315159679452117471849989000282265698764599106398", |
| 191 | + "18203970449465878141055527247672796515569702004956673464850250575302350363961", |
| 192 | + "9070274571799942693810232181825350369966811716654884009331987967306715939422", |
| 193 | + "3344169380239392314048474373026629561296701202792428464496597755254021991380"); |
| 194 | + vk.IC = new G1Point[10]; |
| 195 | + vk.IC[0] = new G1Point( |
| 196 | + "4944125736493822447335225095051526251764804673819722614680138374080051759962", |
| 197 | + "1935192491180648890600311215252271941452272522684171010354270378941282184111"); |
| 198 | + vk.IC[1] = new G1Point( |
| 199 | + "13671710343712145123751755431743289257188978742535474396465034058168696864220", |
| 200 | + "20826522333544227498944395534998927652160019773231902383997070883222962120651"); |
| 201 | + vk.IC[2] = new G1Point( |
| 202 | + "12761764339888541584683044940570653033593327533706847447891003583652324561983", |
| 203 | + "1890169332711480046756085850376547686758361763522376714890812739379155117275"); |
| 204 | + vk.IC[3] = new G1Point( |
| 205 | + "10780973691118990463572234139590032304523642666772690042271497225434343505093", |
| 206 | + "1225111119988715799286416715484295049110336342693607646598445309214414972833"); |
| 207 | + vk.IC[4] = new G1Point( |
| 208 | + "6813397648435401772315777392068447297731770034820995962730599333379629935197", |
| 209 | + "5543296837108785826068557070639753614637117027747616223928176042346981863803"); |
| 210 | + vk.IC[5] = new G1Point( |
| 211 | + "15888176973130579702136584647732320447911977285971866534948545524629530339320", |
| 212 | + "19353498956202835216323577418447420319043865439124272799134659660891866791335"); |
| 213 | + vk.IC[6] = new G1Point( |
| 214 | + "4391160655333174988591015543994926300076235924540378279303074871686753798142", |
| 215 | + "11171604800461778651579303937810014677300582907756707298692424617842305602740"); |
| 216 | + vk.IC[7] = new G1Point( |
| 217 | + "5577412546328490241391307238739013120425748898832356403880504969857771119690", |
| 218 | + "11257371099238762117045275690719175766562617360639429481885451902339768879934"); |
| 219 | + vk.IC[8] = new G1Point( |
| 220 | + "4717266903818752750408066803467256739157750721583295354208673132324161744458", |
| 221 | + "2253904876039028511475843193830314875944384935466740271927641917713621346414"); |
| 222 | + vk.IC[9] = new G1Point( |
| 223 | + "7742642460569273216539674856471756904887522145302233146876244281004809392495", |
| 224 | + "13338610944590869762446817049541912676528855874207736821753831893421715974960"); |
| 225 | + } |
| 226 | + |
| 227 | + return vk; |
| 228 | + } |
| 229 | + |
| 230 | + |
| 231 | + public static boolean checkG1Poin(G1Point g1) { |
| 232 | + BN128G1 g11 = g1.toBN128G1(); |
| 233 | + if (g11 == null) { |
| 234 | + return false; |
| 235 | + } |
| 236 | + return true; |
| 237 | + } |
| 238 | + |
| 239 | + public static boolean checkG2Poin(G2Point g2) { |
| 240 | + BN128G2 g21 = g2.toBN128G2(); |
| 241 | + if (g21 == null) { |
| 242 | + return false; |
| 243 | + } |
| 244 | + return true; |
| 245 | + } |
| 246 | + |
| 247 | + |
| 248 | + public static void sort(byte[] bytes) { |
| 249 | + int len = bytes.length / 2; |
| 250 | + for (int i = 0; i < len; i++) { |
| 251 | + byte b = bytes[i]; |
| 252 | + bytes[i] = bytes[bytes.length - i - 1]; |
| 253 | + bytes[bytes.length - i - 1] = b; |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + public static G1Point G1Point(byte[] x, byte[] y) { |
| 258 | +// sort(x); |
| 259 | +// sort(y); |
| 260 | + return new G1Point(x, y); |
| 261 | + } |
| 262 | + |
| 263 | + public static void test() { |
| 264 | + byte[] x = ByteArray |
| 265 | + .fromHexString("2eca0c7238bf16e83e7a1e6c5d49540685ff51380f309842a98561558019fc02"); |
| 266 | + byte[] y = ByteArray |
| 267 | + .fromHexString("03d3260361bb8451de5ff5ecd17f010ff22f5c31cdf184e9020b06fa5997db84"); |
| 268 | + G1Point p = G1Point(x, y); |
| 269 | + if (checkG1Poin(p)) { |
| 270 | + System.out.println("1"); |
| 271 | + } else { |
| 272 | + System.out.println("2"); |
| 273 | + } |
| 274 | + |
| 275 | + } |
| 276 | + |
| 277 | + public static void main(String[] args) throws Exception { |
| 278 | + initVk(); |
| 279 | + if (!checkG2Poin(vk.A)) { |
| 280 | + throw new Exception("Check false!"); |
| 281 | + } |
| 282 | + if (!checkG1Poin(vk.B)) { |
| 283 | + throw new Exception("Check false!"); |
| 284 | + } |
| 285 | + if (!checkG2Poin(vk.C)) { |
| 286 | + throw new Exception("Check false!"); |
| 287 | + } |
| 288 | + if (!checkG2Poin(vk.gamma)) { |
| 289 | + throw new Exception("Check false!"); |
| 290 | + } |
| 291 | + if (!checkG1Poin(vk.gammaBeta1)) { |
| 292 | + throw new Exception("Check false!"); |
| 293 | + } |
| 294 | + if (!checkG2Poin(vk.gammaBeta2)) { |
| 295 | + throw new Exception("Check false!"); |
| 296 | + } |
| 297 | + if (!checkG2Poin(vk.Z)) { |
| 298 | + throw new Exception("Check false!"); |
| 299 | + } |
| 300 | + for (int i = 0; i < 10; i++) { |
| 301 | + if (!checkG1Poin(vk.IC[i])) { |
| 302 | + throw new Exception("Check false!"); |
| 303 | + } |
| 304 | + } |
| 305 | + } |
93 | 306 | }
|
0 commit comments