import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.nio.file.*; import java.security.*; import java.security.spec.*; import java.util.*; /** Автономная проверка Rust/Java/JS фикстур; не зависит от Solana SDK и сетевого RPC. */ public final class RegistrationV2VectorsCheck { private static final BigInteger P=BigInteger.ONE.shiftLeft(255).subtract(BigInteger.valueOf(19)); private static final BigInteger D=BigInteger.valueOf(-121665).multiply(BigInteger.valueOf(121666).modInverse(P)).mod(P); private static final String B58="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; private static byte[] utf8(String s){return s.getBytes(StandardCharsets.UTF_8);} private static byte[] sha(byte[] b){try{return MessageDigest.getInstance("SHA-256").digest(b);}catch(Exception e){throw new IllegalStateException(e);}} private static byte[] join(byte[]... array){int n=0;for(var a:array)n+=a.length;byte[] out=new byte[n];int i=0;for(var a:array){System.arraycopy(a,0,out,i,a.length);i+=a.length;}return out;} private static String hex(byte[] b){return HexFormat.of().formatHex(b);} private static byte[] fromHex(String s){return HexFormat.of().parseHex(s);} private static byte[] base58decode(String text){ BigInteger n=BigInteger.ZERO, b=BigInteger.valueOf(58); for(char c:text.toCharArray()) {int digit=B58.indexOf(c);if(digit<0)throw new IllegalArgumentException();n=n.multiply(b).add(BigInteger.valueOf(digit));} byte[] raw=n.equals(BigInteger.ZERO)?new byte[0]:n.toByteArray();if(raw.length>0 && raw[0]==0)raw=Arrays.copyOfRange(raw,1,raw.length); int zero=0;while(zero0){BigInteger[] div=n.divideAndRemainder(b);result.append(B58.charAt(div[1].intValue()));n=div[0];} int zero=0;while(zero>7;ybytes[31]&=127; for(int i=0;i<16;i++){byte x=ybytes[i];ybytes[i]=ybytes[31-i];ybytes[31-i]=x;} BigInteger y=new BigInteger(1,ybytes);if(y.compareTo(P)>=0)return false; BigInteger yy=y.multiply(y).mod(P); BigInteger xx=yy.subtract(BigInteger.ONE).multiply(D.multiply(yy).add(BigInteger.ONE).mod(P).modInverse(P)).mod(P); if(xx.equals(BigInteger.ZERO))return sign==0; return xx.modPow(P.subtract(BigInteger.ONE).shiftRight(1),P).equals(BigInteger.ONE); } private static String[] pda(byte[] seed,byte[] program){ for(int bump=255;bump>=0;bump--){byte[] bytes=sha(join(seed,new byte[]{(byte)bump},program,utf8("ProgramDerivedAddress"))); if(!onCurve(bytes))return new String[]{base58encode(bytes),Integer.toString(bump)};} throw new IllegalStateException("PDA unavailable"); } private static void check(boolean condition,String text){if(!condition)throw new AssertionError(text);} public static void main(String[] argv)throws Exception{ Path path=argv.length>0?Path.of(argv[0]):Path.of("tests/fixtures/registration-v2.properties"); Properties props=new Properties();try(var reader=Files.newBufferedReader(path)){props.load(reader);} byte[] program=base58decode(props.getProperty("program_id"));check(program.length==32,"program id"); for(String name:List.of("alice@shine","bob@free","a_b@shine","a@shine","aaaaaaaaaaaaaaaaaaaaaaaa@shine","aaaaaaaaaaaaaaaaaaaaaaaaa@shine")){ String key=name.replace("a_b","a_underscore_b").replace('@','_'); byte[] seed=sha(utf8("user_login="+name));check(hex(seed).equals(props.getProperty("user_"+key+"_seed")),"seed " + name); String[] result=pda(seed,program); check(result[0].equals(props.getProperty("user_"+key+"_pda")),"user pda " + name); check(result[1].equals(props.getProperty("user_"+key+"_bump")),"user bump " + name); } for(String name:List.of("shine","free")){ byte[] seed=sha(utf8("namespace="+name)); check(hex(seed).equals(props.getProperty("namespace_"+name+"_seed")),"namespace seed"); String[] result=pda(seed,program); check(result[0].equals(props.getProperty("namespace_"+name+"_pda")),"namespace pda"); check(result[1].equals(props.getProperty("namespace_"+name+"_bump")),"namespace bump"); } byte[] raw=fromHex(props.getProperty("registrar_pubkey_hex")); byte[] der=join(fromHex("302a300506032b6570032100"),raw); PublicKey signer=KeyFactory.getInstance("Ed25519").generatePublic(new X509EncodedKeySpec(der)); byte[] msg=fromHex(props.getProperty("inner_message_hex")),signature=fromHex(props.getProperty("inner_signature_hex")); Signature verifier=Signature.getInstance("Ed25519");verifier.initVerify(signer);verifier.update(msg); check(verifier.verify(signature),"inner signature"); byte[] bad=msg.clone();bad[bad.length-1]^=1; verifier.initVerify(signer);verifier.update(bad);check(!verifier.verify(signature),"tamper should fail"); check(hex(sha(utf8("alice.example+tag@example.com"))).equals(props.getProperty("email_hash_hex")),"email hash"); System.out.println("OK: SHA-256 + user/namespace PDA + Ed25519 + email vector"); } }