Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zero Knowledge Proofs #2

Merged
merged 30 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
74af495
- equality proofs + tests
dssei Oct 10, 2024
67f138d
- transcript tests
dssei Oct 10, 2024
6fe1020
- transcript docs
dssei Oct 10, 2024
87f5d6a
- ciphertext commitment proofs + tests
dssei Oct 10, 2024
ad1472b
- ciphertext validity + proofs
dssei Oct 11, 2024
0dbb17d
- pubkey validity proofs + tests
dssei Oct 11, 2024
4690e7a
- rename method for consistency with the rest of the lib
dssei Oct 11, 2024
1e07854
- range proofs + tests
dssei Oct 11, 2024
3049ff8
- marshaling test
dssei Oct 11, 2024
a1594f3
- zero balance proofs + tests
dssei Oct 11, 2024
7d5d44c
- invalid inputs tests
dssei Oct 11, 2024
c28a285
- invalid inputs tests for NewCiphertextCiphertextEqualityProof
dssei Oct 11, 2024
d6d7568
- invalid inputs tests for NewCiphertextCommitmentEqualityProof
dssei Oct 11, 2024
3b0ef00
- more input validation tests
dssei Oct 11, 2024
ba3ce73
- input validation for NewCiphertextValidityProof
dssei Oct 11, 2024
1c4fcde
- input validation for VerifyCiphertextValidityProof
dssei Oct 11, 2024
9d1da51
- input validation for NewPubKeyValidityProof & VerifyPubKeyValidityP…
dssei Oct 11, 2024
b116f1b
- input validation for NewRangeProof & VerifyRangeProof
dssei Oct 11, 2024
83c64dc
- fix linting errors
dssei Oct 14, 2024
acb5f41
- fix workflow
dssei Oct 14, 2024
9edaaa0
- fix workflow - 2
dssei Oct 14, 2024
3ac0ad7
Switch to github.com/coinbase/kryptology sei fork
dssei Oct 15, 2024
8472431
Addressing comments - part 1
dssei Oct 17, 2024
695054e
Addressing comments - part 2
dssei Oct 17, 2024
8d07ac5
Addressing comments - part 3
dssei Oct 17, 2024
939d474
Refactoring transcript to a neutral name
dssei Oct 17, 2024
bd8bc81
Addressing comments - part 4
dssei Oct 17, 2024
693c1c3
Addressing comments - part 5
dssei Oct 17, 2024
5a3b716
Address review comments
dssei Oct 22, 2024
a953dd9
Move GenerateKey to test utils
dssei Oct 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,10 @@ jobs:
~/.cache/go-build
key: ${{ needs.setup.outputs.cache-key }}

- name: Determine packages to test (excluding elgamal)
id: packages
- name: Run general unit tests (excluding elgamal)
run: |
PACKAGES=$(go list ./... | grep -v '/pkg/encryption/elgamal')
echo "PACKAGES=$PACKAGES" >> $GITHUB_ENV

- name: Run general unit tests
run: |
go test -mod=readonly -race -v -timeout 5m $PACKAGES
echo "Determining packages to test (excluding elgamal)..."
go list ./... | grep -v '/pkg/encryption/elgamal$' | xargs go test -mod=readonly -race -v -timeout 5m

# Define a matrix for ElGamal package test subsets
elgamal-tests:
Expand Down
14 changes: 9 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ go 1.21
require (
github.com/bwesterb/go-ristretto v1.2.3
github.com/coinbase/kryptology v1.8.0
github.com/ethereum/go-ethereum v1.10.26
github.com/ethereum/go-ethereum v1.13.15
github.com/gtank/merlin v0.1.1
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.27.0
)

require (
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/btcsuite/btcd v0.21.0-beta.0.20201114000516-e9c7a5ac6401 // indirect
github.com/consensys/gnark-crypto v0.5.3 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.25.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/coinbase/kryptology => github.com/sei-protocol/coinbase-kryptology v0.0.0-20241015231206-08f61b7965cd
81 changes: 20 additions & 61 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,86 +1,45 @@
filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU=
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btcd v0.21.0-beta.0.20201114000516-e9c7a5ac6401 h1:0tjUthKCaF8zwF9Qg7lfnep0xdo4n8WiFUfQPaMHX6g=
github.com/btcsuite/btcd v0.21.0-beta.0.20201114000516-e9c7a5ac6401/go.mod h1:Sv4JPQ3/M+teHz9Bo5jBpkNcP0x6r7rdihlNL/7tTAs=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
github.com/btcsuite/btcutil v1.0.2/go.mod h1:j9HUFwoQRsZL3V4n+qG+CUnEGHOarIxfC3Le2Yhbcts=
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=
github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY=
github.com/btcsuite/goleveldb v1.0.0/go.mod h1:QiK9vBlgftBg6rWQIj6wFzbPfRjiykIEhBH4obrXJ/I=
github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
github.com/btcsuite/snappy-go v1.0.0/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/btcsuite/btcd/btcec/v2 v2.3.4 h1:3EJjcN70HCu/mwqlUsGK8GcNVyLVxFDlWurTXGPFfiQ=
github.com/btcsuite/btcd/btcec/v2 v2.3.4/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
github.com/bwesterb/go-ristretto v1.2.3 h1:1w53tCkGhCQ5djbat3+MH0BAQ5Kfgbt56UZQ/JMzngw=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/coinbase/kryptology v1.8.0 h1:Aoq4gdTsJhSU3lNWsD5BWmFSz2pE0GlmrljaOxepdYY=
github.com/coinbase/kryptology v1.8.0/go.mod h1:RYXOAPdzOGUe3qlSFkMGn58i3xUA8hmxYHksuq+8ciI=
github.com/consensys/bavard v0.1.8-0.20210915155054-088da2f7f54a/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI=
github.com/consensys/gnark-crypto v0.5.3 h1:4xLFGZR3NWEH2zy+YzvzHicpToQR8FXFbfLNvpGB+rE=
github.com/consensys/gnark-crypto v0.5.3/go.mod h1:hOdPlWQV1gDLp7faZVeg8Y0iEPFaOUnCc4XeCCk96p0=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218=
github.com/ethereum/go-ethereum v1.10.26 h1:i/7d9RBBwiXCEuyduBQzJw/mKmnvzsN14jqBmytw72s=
github.com/ethereum/go-ethereum v1.10.26/go.mod h1:EYFyF19u3ezGLD4RqOkLq+ZCXzYbLoNDdZlMt7kyKFg=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
github.com/ethereum/go-ethereum v1.13.15 h1:U7sSGYGo4SPjP6iNIifNoyIAiNjrmQkz6EwQG+/EZWo=
github.com/ethereum/go-ethereum v1.13.15/go.mod h1:TN8ZiHrdJwSe8Cb6x+p0hs5CxhJZPbqB7hHkaUXcmIU=
github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is=
github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 h1:hLDRPB66XQT/8+wG9WsDpiCvZf1yKO7sz7scAjSlBa0=
github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/sei-protocol/coinbase-kryptology v0.0.0-20241015231206-08f61b7965cd h1:R/g4pa6pgegLAAt1NTrO1qVJ3uZH9hfcMcc4yLz1cgg=
github.com/sei-protocol/coinbase-kryptology v0.0.0-20241015231206-08f61b7965cd/go.mod h1:vAKKp7/qgfMtPXMseamOlZMqK7BytjfOm0rFKWph5c4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA=
7 changes: 7 additions & 0 deletions pkg/encryption/elgamal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package elgamal

import (
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"io"

"github.com/coinbase/kryptology/pkg/core/curves"
Expand All @@ -12,6 +14,11 @@ import (
// H_STRING H is a random point on the elliptic curve that is unrelated to G.
const H_STRING = "gPt25pi0eDphSiXWu0BIeIvyVATCtwhslTqfqvNhW2c"

// GenerateKey generates a new ECDSA key pair.
func GenerateKey() (*ecdsa.PrivateKey, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be useful more as a testing function? Should we still export this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do want to export it we could consider renaming it since it may be misleading

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used for testing, but across multiple packages.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, any way to rename this or move this to some test utils file?

This is exposed to user as elgamal.GenerateKey(), which might be confusing (since it might suggest the generation of a el gamal Private Key or el gamal Public Key). Maybe just rename to GenerateEcdsaKey?

return ecdsa.GenerateKey(secp256k1.S256(), rand.Reader)
}

// KeyGen generates a new key pair for the Twisted ElGamal encryption scheme.
func (teg TwistedElGamal) KeyGen(privateKey ecdsa.PrivateKey, denom string) (*KeyPair, error) {
// Fixed base point H
Expand Down
34 changes: 14 additions & 20 deletions pkg/encryption/elgamal/encryption_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package elgamal

import (
"crypto/ecdsa"
"crypto/rand"
"github.com/coinbase/kryptology/pkg/core/curves"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/stretchr/testify/require"
"math"
"testing"
)

const DefaultTestDenom = "factory/sei1239081236472sd/testToken"

func generateKey() (*ecdsa.PrivateKey, error) {
return ecdsa.GenerateKey(secp256k1.S256(), rand.Reader)
}

func TestKeyGeneration(t *testing.T) {
privateKey, err := generateKey()
privateKey, err := GenerateKey()
require.Nil(t, err)

eg := NewTwistedElgamal()
Expand All @@ -42,16 +36,16 @@ func TestKeyGeneration(t *testing.T) {
require.NotEqual(t, keyPair, keyPairDiffSalt, "PK should be different for different salt")

// Test that different privateKey should generate different PK
altPrivateKey, err := generateKey()
altPrivateKey, err := GenerateKey()
require.Nil(t, err)
keyPairDiffPK, err := eg.KeyGen(*altPrivateKey, altDenom)
require.Nil(t, err)
require.NotEqual(t, keyPair, keyPairDiffPK, "PK should be different for different ESDCA Private Key")
}

func TestEncryptionDecryption(t *testing.T) {
privateKey, _ := generateKey()
altPrivateKey, _ := generateKey()
privateKey, _ := GenerateKey()
altPrivateKey, _ := GenerateKey()

eg := NewTwistedElgamal()

Expand Down Expand Up @@ -86,7 +80,7 @@ func TestEncryptionDecryption(t *testing.T) {

// Due to the size of 48 bit numbers, this test takes a really long time (~1hr) to run.
func Test48BitEncryptionDecryption(t *testing.T) {
privateKey, err := generateKey()
privateKey, err := GenerateKey()
require.Nil(t, err)

eg := NewTwistedElgamal()
Expand Down Expand Up @@ -127,8 +121,8 @@ func Test48BitEncryptionDecryption(t *testing.T) {
}

func TestAddCiphertext(t *testing.T) {
privateKey, _ := generateKey()
altPrivateKey, _ := generateKey()
privateKey, _ := GenerateKey()
altPrivateKey, _ := GenerateKey()

eg := NewTwistedElgamal()

Expand Down Expand Up @@ -175,7 +169,7 @@ func TestAddCiphertext(t *testing.T) {

func TestTwistedElGamal_InvalidCiphertext(t *testing.T) {
eg := NewTwistedElgamal()
privateKey, _ := generateKey()
privateKey, _ := GenerateKey()
keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom)

invalidCt := &Ciphertext{}
Expand All @@ -190,7 +184,7 @@ func TestTwistedElGamal_NilPrivateKey(t *testing.T) {
eg := NewTwistedElgamal()

// Generate a valid key pair for comparison
privateKey, _ := generateKey()
privateKey, _ := GenerateKey()
keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom)

// Encrypt a value with a valid public key
Expand All @@ -209,7 +203,7 @@ func TestTwistedElGamal_EncryptDecryptWithRand(t *testing.T) {
eg := NewTwistedElgamal()

// Generate a valid key pair for comparison
privateKey, _ := generateKey()
privateKey, _ := GenerateKey()
keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom)

message := uint64(555555555)
Expand All @@ -227,7 +221,7 @@ func TestTwistedElGamal_EncryptMessageTwice(t *testing.T) {
eg := NewTwistedElgamal()

// Generate a valid key pair for comparison
privateKey, _ := generateKey()
privateKey, _ := GenerateKey()
keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom)

message := uint64(555555555)
Expand All @@ -243,7 +237,7 @@ func TestTwistedElGamal_DecryptWithZeroBits(t *testing.T) {
eg := NewTwistedElgamal()

// Generate a valid key pair for comparison
privateKey, _ := generateKey()
privateKey, _ := GenerateKey()
keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom)

message := uint64(555555555)
Expand All @@ -269,7 +263,7 @@ func TestTwistedElGamal_EncryptInvalidRandomFactor(t *testing.T) {
eg := NewTwistedElgamal()

// Generate a valid key pair for comparison
privateKey, _ := generateKey()
privateKey, _ := GenerateKey()
keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom)

// Test with nil public key
Expand All @@ -282,7 +276,7 @@ func TestTwistedElGamal_EncryptBoundaryValues(t *testing.T) {
eg := NewTwistedElgamal()

// Generate a valid key pair for comparison
privateKey, _ := generateKey()
privateKey, _ := GenerateKey()
keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom)

// Test with the smallest possible value (0)
Expand Down
2 changes: 1 addition & 1 deletion pkg/encryption/elgamal/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestCiphertext_MarshalJSON(t *testing.T) {
privateKey, _ := generateKey()
privateKey, _ := GenerateKey()
eg := NewTwistedElgamal()

keys, _ := eg.KeyGen(*privateKey, DefaultTestDenom)
Expand Down
Loading
Loading