|
| 1 | +package zkencryption |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha3" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "filippo.io/edwards25519" |
| 8 | + "github.com/gagliardetto/solana-go" |
| 9 | + "github.com/tyler-smith/go-bip39" |
| 10 | +) |
| 11 | + |
| 12 | +// ElGamalSecretKeyLen is the canonical length of an ElGamal secret scalar |
| 13 | +// encoded in little-endian form (matches curve25519-dalek Scalar::as_bytes). |
| 14 | +const ElGamalSecretKeyLen = 32 |
| 15 | + |
| 16 | +// elGamalSigningDomain is the domain-separation prefix prepended to the |
| 17 | +// public seed before signing. It must match b"ElGamalSecretKey" in |
| 18 | +// solana-zk-sdk. |
| 19 | +const elGamalSigningDomain = "ElGamalSecretKey" |
| 20 | + |
| 21 | +// minElGamalSeedLen / maxElGamalSeedLen mirror the bounds enforced in |
| 22 | +// solana-zk-sdk's ElGamalSecretKey::from_seed implementation. |
| 23 | +const ( |
| 24 | + minElGamalSeedLen = ElGamalSecretKeyLen |
| 25 | + maxElGamalSeedLen = 65535 |
| 26 | +) |
| 27 | + |
| 28 | +// ElGamalSecretKey is a canonical little-endian encoding of a Ristretto/Ed25519 |
| 29 | +// scalar mod ell. It is the Token-2022 confidential-transfer ElGamal private |
| 30 | +// key; byte-for-byte equivalent to ElGamalSecretKey::as_bytes in solana-zk-sdk. |
| 31 | +type ElGamalSecretKey [ElGamalSecretKeyLen]byte |
| 32 | + |
| 33 | +// ElGamalSecretKeyFromSeed derives an ElGamal secret key from an entropy seed |
| 34 | +// by computing Scalar::from_bytes_mod_order_wide(SHA3-512(seed)), matching |
| 35 | +// curve25519-dalek's Scalar::hash_from_bytes::<Sha3_512>. |
| 36 | +func ElGamalSecretKeyFromSeed(seed []byte) (ElGamalSecretKey, error) { |
| 37 | + if len(seed) < minElGamalSeedLen { |
| 38 | + return ElGamalSecretKey{}, ErrSeedTooShort |
| 39 | + } |
| 40 | + if len(seed) > maxElGamalSeedLen { |
| 41 | + return ElGamalSecretKey{}, ErrSeedTooLong |
| 42 | + } |
| 43 | + |
| 44 | + h := sha3.Sum512(seed) |
| 45 | + // SetUniformBytes only errors on wrong input length; Sum512 always |
| 46 | + // returns 64 bytes, so this branch is unreachable in practice but kept |
| 47 | + // to avoid an implicit panic if the upstream contract ever changes. |
| 48 | + s, err := edwards25519.NewScalar().SetUniformBytes(h[:]) |
| 49 | + if err != nil { |
| 50 | + return ElGamalSecretKey{}, ErrInvalidScalarEncoding |
| 51 | + } |
| 52 | + |
| 53 | + var out ElGamalSecretKey |
| 54 | + copy(out[:], s.Bytes()) |
| 55 | + return out, nil |
| 56 | +} |
| 57 | + |
| 58 | +// ElGamalSecretKeyFromSignature derives an ElGamal secret key from an ed25519 |
| 59 | +// signature by using SHA3-512(signature) as the seed. Mirrors |
| 60 | +// ElGamalSecretKey::seed_from_signature + from_seed in solana-zk-sdk. |
| 61 | +func ElGamalSecretKeyFromSignature(sig solana.Signature) (ElGamalSecretKey, error) { |
| 62 | + h := sha3.Sum512(sig[:]) |
| 63 | + return ElGamalSecretKeyFromSeed(h[:]) |
| 64 | +} |
| 65 | + |
| 66 | +// ElGamalSecretKeyFromSigner deterministically derives an ElGamal secret key |
| 67 | +// from a Solana signer and a public seed. The signer signs |
| 68 | +// b"ElGamalSecretKey" || publicSeed; the signature is hashed with SHA3-512 |
| 69 | +// and fed into ElGamalSecretKeyFromSeed. An all-zero (default) signature is |
| 70 | +// rejected to match the Rust implementation. |
| 71 | +func ElGamalSecretKeyFromSigner(signer Signer, publicSeed []byte) (ElGamalSecretKey, error) { |
| 72 | + msg := make([]byte, 0, len(elGamalSigningDomain)+len(publicSeed)) |
| 73 | + msg = append(msg, elGamalSigningDomain...) |
| 74 | + msg = append(msg, publicSeed...) |
| 75 | + |
| 76 | + sig, err := signer.Sign(msg) |
| 77 | + if err != nil { |
| 78 | + return ElGamalSecretKey{}, fmt.Errorf("zkencryption: sign ElGamalSecretKey public seed: %w", err) |
| 79 | + } |
| 80 | + if sig == (solana.Signature{}) { |
| 81 | + return ElGamalSecretKey{}, ErrDefaultSignature |
| 82 | + } |
| 83 | + return ElGamalSecretKeyFromSignature(sig) |
| 84 | +} |
| 85 | + |
| 86 | +// ElGamalSecretKeyFromSeedPhraseAndPassphrase derives an ElGamal secret key |
| 87 | +// from a BIP39 mnemonic and an optional passphrase, matching |
| 88 | +// solana_seed_phrase's PBKDF2-HMAC-SHA512 derivation. Solana does not |
| 89 | +// validate the mnemonic checksum at this layer, and neither do we. |
| 90 | +func ElGamalSecretKeyFromSeedPhraseAndPassphrase(mnemonic, passphrase string) (ElGamalSecretKey, error) { |
| 91 | + return ElGamalSecretKeyFromSeed(bip39.NewSeed(mnemonic, passphrase)) |
| 92 | +} |
0 commit comments