github.com/annchain/OG@v0.0.9/deprecated/ogcrypto_interface/crypto.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package ogcrypto_interface 15 16 import ( 17 "fmt" 18 "github.com/annchain/OG/arefactor/common/hexutil" 19 ) 20 21 type CryptoType int8 22 23 const ( 24 CryptoTypeEd25519 CryptoType = iota 25 CryptoTypeSecp256k1 26 ) 27 28 var CryptoNameMap = map[string]CryptoType{ 29 "secp256k1": CryptoTypeSecp256k1, 30 "ed25519": CryptoTypeEd25519, 31 } 32 33 type PrivateKey struct { 34 Type CryptoType 35 KeyBytes []byte 36 } 37 38 func (p *PrivateKey) ToBytes() []byte { 39 var bytes []byte 40 bytes = append(bytes, byte(p.Type)) 41 bytes = append(bytes, p.KeyBytes...) 42 return bytes 43 } 44 45 func (p *PrivateKey) DebugString() string { 46 return fmt.Sprintf("privk%d:%s", p.Type, hexutil.ToHex(p.KeyBytes)) 47 } 48 49 func (p *PrivateKey) String() string { 50 return hexutil.ToHex(p.ToBytes()) 51 } 52 53 type PublicKey struct { 54 Type CryptoType 55 KeyBytes []byte 56 } 57 58 func (p *PublicKey) ToBytes() []byte { 59 var bytes []byte 60 bytes = append(bytes, byte(p.Type)) 61 bytes = append(bytes, p.KeyBytes...) 62 return bytes 63 } 64 65 func (p *PublicKey) String() string { 66 return hexutil.ToHex(p.ToBytes()) 67 } 68 69 func (p *PublicKey) DebugString() string { 70 return fmt.Sprintf("pubk%d:%s", p.Type, hexutil.ToHex(p.KeyBytes)) 71 } 72 73 type Signature struct { 74 Type CryptoType 75 SignatureBytes []byte 76 } 77 78 func (p *Signature) ToBytes() []byte { 79 var bytes []byte 80 bytes = append(bytes, byte(p.Type)) 81 bytes = append(bytes, p.SignatureBytes...) 82 return bytes 83 } 84 85 func (p *Signature) String() string { 86 return hexutil.ToHex(p.ToBytes()) 87 } 88 89 func (p *Signature) DebugString() string { 90 return fmt.Sprintf("sig%d:%s", p.Type, hexutil.ToHex(p.SignatureBytes)) 91 } 92 93 // 94 //type KyberEd22519PrivKey struct { 95 // PrivateKey kyber.Scalar 96 // Suit *edwards25519.SuiteEd25519 97 //} 98 // 99 //func (p *KyberEd22519PrivKey) Decrypt(cipherText []byte) (m []byte, err error) { 100 // return ecies.Decrypt(p.Suit, p.PrivateKey, cipherText, p.Suit.Hash) 101 //} 102 // 103 //func (p *PrivateKey) ToKyberEd25519PrivKey() *KyberEd22519PrivKey { 104 // var edPrivKey [32]byte 105 // var curvPrivKey [64]byte 106 // copy(curvPrivKey[:], p.KeyBytes[:64]) 107 // extra25519.PrivateKeyToCurve25519(&edPrivKey, &curvPrivKey) 108 // privateKey, err := edwards25519.UnmarshalBinaryScalar(edPrivKey[:32]) 109 // suite := edwards25519.NewBlakeSHA256Ed25519() 110 // if err != nil { 111 // panic(err) 112 // } 113 // return &KyberEd22519PrivKey{ 114 // PrivateKey: privateKey, 115 // Suit: suite, 116 // } 117 //} 118 119 func (c CryptoType) String() string { 120 if c == CryptoTypeEd25519 { 121 return "ed25519" 122 } else if c == CryptoTypeSecp256k1 { 123 return "secp256k1" 124 } 125 return "unknown" 126 } 127 128 type PublicKeys []PublicKey 129 130 func (h PublicKeys) Len() int { 131 return len(h) 132 } 133 func (h PublicKeys) Less(i, j int) bool { 134 return h[i].String() < h[j].String() 135 } 136 137 func (h PublicKeys) Swap(i, j int) { 138 h[i], h[j] = h[j], h[i] 139 }