-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
3,704 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package bign | ||
|
||
import ( | ||
"sync" | ||
"math/big" | ||
"encoding/asn1" | ||
"crypto/elliptic" | ||
) | ||
|
||
var ( | ||
OIDNamedCurveP256v1 = asn1.ObjectIdentifier{1, 2, 112, 0, 2, 0, 34, 101, 45, 3, 1} | ||
OIDNamedCurveP384v1 = asn1.ObjectIdentifier{1, 2, 112, 0, 2, 0, 34, 101, 45, 3, 2} | ||
OIDNamedCurveP512v1 = asn1.ObjectIdentifier{1, 2, 112, 0, 2, 0, 34, 101, 45, 3, 3} | ||
) | ||
|
||
var ( | ||
once sync.Once | ||
p256v1, p384v1, p512v1 *elliptic.CurveParams | ||
) | ||
|
||
func initAll() { | ||
initP256v1() | ||
initP384v1() | ||
initP512v1() | ||
} | ||
|
||
func initP256v1() { | ||
p256v1 = new(elliptic.CurveParams) | ||
p256v1.P = bigFromHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff43") | ||
p256v1.N = bigFromHex("ffffffffffffffffffffffffffffffffd95c8ed60dfb4dfc7e5abf99263d6607") | ||
p256v1.B = bigFromHex("77ce6c1515f3a8edd2c13aabe4d8fbbe4cf55069978b9253b22e7d6bd69c03f1") | ||
p256v1.Gx = bigFromHex("0000000000000000000000000000000000000000000000000000000000000000") | ||
p256v1.Gy = bigFromHex("6bf7fc3cfb16d69f5ce4c9a351d6835d78913966c408f6521e29cf1804516a93") | ||
p256v1.BitSize = 256 | ||
p256v1.Name = "BIGN256V1" | ||
} | ||
|
||
func initP384v1() { | ||
p384v1 = new(elliptic.CurveParams) | ||
p384v1.P = bigFromHex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec3") | ||
p384v1.N = bigFromHex("fffffffffffffffffffffffffffffffffffffffffffffffe6cccc40373af7bbb8046dae7a6a4ff0a3db7dc3ff30ca7b7") | ||
p384v1.B = bigFromHex("3c75dfe1959cef2033075aab655d34d2712748bb0ffbb196a6216af9e9712e3a14bde2f0f3cebd7cbca7fc236873bf64") | ||
p384v1.Gx = bigFromHex("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") | ||
p384v1.Gy = bigFromHex("5d438224a82e9e9e6330117e432dbf893a729a11dc86ffa00549e79e66b1d35584403e276b2a42f9ea5ecb31f733c451") | ||
p384v1.BitSize = 384 | ||
p384v1.Name = "BIGN384V1" | ||
} | ||
|
||
func initP512v1() { | ||
p512v1 = new(elliptic.CurveParams) | ||
p512v1.P = bigFromHex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc7") | ||
p512v1.N = bigFromHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb2c0092c0198004ef26bebb02e2113f4361bcae59556df32dcffad490d068ef1") | ||
p512v1.B = bigFromHex("6cb45944933b8c43d88c5d6a60fd58895bc6a9eedd5d255117ce13e3daadb0882711dcb5c4245e952933008c87aca243ea8622273a49a27a09346998d6139c90") | ||
p512v1.Gx = bigFromHex("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") | ||
p512v1.Gy = bigFromHex("a826ff7ae4037681b182e6f7a0d18fabb0ab41b3b361bce2d2edf81b00cccada6973dde20efa6fd2ff777395eee8226167aa83b9c94c0d04b792ae6fceefedbd") | ||
p512v1.BitSize = 512 | ||
p512v1.Name = "BIGN512V1" | ||
} | ||
|
||
func P256v1() elliptic.Curve { | ||
once.Do(initAll) | ||
return p256v1 | ||
} | ||
|
||
func P384v1() elliptic.Curve { | ||
once.Do(initAll) | ||
return p384v1 | ||
} | ||
|
||
func P512v1() elliptic.Curve { | ||
once.Do(initAll) | ||
return p512v1 | ||
} | ||
|
||
func bigFromHex(s string) *big.Int { | ||
b, ok := new(big.Int).SetString(s, 16) | ||
if !ok { | ||
panic("go-cryptobin/bign: internal error: invalid encoding") | ||
} | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,255 @@ | ||
package bign | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"encoding/hex" | ||
"crypto/rand" | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
) | ||
|
||
func fromHex(s string) []byte { | ||
h, _ := hex.DecodeString(s) | ||
return h | ||
} | ||
|
||
func testCurve(t *testing.T, curve elliptic.Curve) { | ||
priv, err := ecdsa.GenerateKey(curve, rand.Reader) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
msg := []byte("test") | ||
r, s, err := ecdsa.Sign(rand.Reader, priv, msg) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !ecdsa.Verify(&priv.PublicKey, msg, r, s) { | ||
t.Fatal("signature didn't verify.") | ||
} | ||
} | ||
|
||
func Test_All(t *testing.T) { | ||
t.Run("P256v1", func(t *testing.T) { | ||
testCurve(t, P256v1()) | ||
}) | ||
|
||
t.Run("P384v1", func(t *testing.T) { | ||
testCurve(t, P384v1()) | ||
}) | ||
|
||
t.Run("P512v1", func(t *testing.T) { | ||
testCurve(t, P512v1()) | ||
}) | ||
} | ||
|
||
func Test_ScalarBaseMult1(t *testing.T) { | ||
for _, td := range testKeys1 { | ||
x, y := P256v1().ScalarBaseMult(td.key) | ||
|
||
xx := x.Bytes() | ||
yy := y.Bytes() | ||
|
||
if !bytes.Equal(xx, td.px) { | ||
t.Errorf("make x fail, got %x, want %x", xx, td.px) | ||
} | ||
if !bytes.Equal(yy, td.py) { | ||
t.Errorf("make y fail, got %x, want %x", yy, td.py) | ||
} | ||
} | ||
} | ||
|
||
func Test_ScalarBaseMult2(t *testing.T) { | ||
for _, td := range testKeys2 { | ||
x, y := P256v1().ScalarBaseMult(td.key) | ||
|
||
xx := x.Bytes() | ||
yy := y.Bytes() | ||
|
||
if !bytes.Equal(xx, td.px) { | ||
t.Errorf("make x fail, got %x, want %x", xx, td.px) | ||
} | ||
if !bytes.Equal(yy, td.py) { | ||
t.Errorf("make y fail, got %x, want %x", yy, td.py) | ||
} | ||
} | ||
} | ||
|
||
type testKey struct { | ||
key []byte | ||
px []byte | ||
py []byte | ||
} | ||
|
||
var testKeys1 = []testKey{ | ||
{ | ||
fromHex("01"), | ||
fromHex(""), | ||
fromHex("6BF7FC3CFB16D69F5CE4C9A351D6835D78913966C408F6521E29CF1804516A93"), | ||
}, | ||
{ | ||
fromHex("02"), | ||
fromHex("2D8419E3D905D21E3611261DADC95BB35583090C2CADD0C807F53BB3828F0995"), | ||
fromHex("E62690D81BDC754EFAF5B2770054564849621E8A933A7ECD6C02387649CF258A"), | ||
}, | ||
{ | ||
fromHex("4978C521DD195389ABC5B9C762482FCF1EAD50BF87E1209C744E50AE71596936"), | ||
fromHex("85DF197A07D0D83B5FDF0021E2494451FFB66127E7CA3645512AAD9DBF86715A"), | ||
fromHex("C5F6A22BE1583E65A6A1F4CB0309F8FFDD5C0B757FAB111D05AA40DE05D5F724"), | ||
}, | ||
{ | ||
fromHex("03"), | ||
fromHex("37C58ED5CDF4EFFB78272CF0662E92F5E012060C2B1C234BCC9D10854989824E"), | ||
fromHex("FD9BC9B3DACB85B94160E112D35F312702C169757A1E7AE3F0EF177B32283E84"), | ||
}, | ||
{ | ||
fromHex("04"), | ||
fromHex("A9D6F12F1D785C67F9B4D24B79910E5E73CCB26A1D2F8399CDD386FEF301E913"), | ||
fromHex("EEDD42E79372C116A1312E079707ED43AECCD0E92EED529B6DDC7532B8436DE5"), | ||
}, | ||
{ | ||
fromHex("05"), | ||
fromHex("5EA08F541F6EF3C7B1F27749009FFFBBCC73111F11120D3E1B6E9C7A3612F746"), | ||
fromHex("D1AAABE31BD369F378F8A0BC9D70EEBEC7825976283D482130239EECC4F3A751"), | ||
}, | ||
{ | ||
fromHex("06"), | ||
fromHex("A0FB2B18D0D8EC130266C201FD8E183AEE98BF9572BE4DC0C8EBDF11AD0042CC"), | ||
fromHex("271CCC56A71BAC6BCE3967AD0F4D55AA04064BC8A9A10B78F3B37B543800891B"), | ||
}, | ||
{ | ||
fromHex("07"), | ||
fromHex("0BBBCD4AA459206972B08AF9C2347B54B9B644F2255F6C4B65F739A8255F9844"), | ||
fromHex("BB717EE3A1ADC18B42F39F89AB9AEC0230CF648505069A267686DBF5339135EC"), | ||
}, | ||
{ | ||
fromHex("08"), | ||
fromHex("D2DDA2788DDE65AA2C69A2C0385B83FD1E71C5F4597473A4EB882152DF36A2F2"), | ||
fromHex("22E0DD3784B2F9CCB8A0B6BE1BC346C5B43B483BE921514A9C144CA1A16AB769"), | ||
}, | ||
{ | ||
fromHex("09"), | ||
fromHex("79F11C784808A342FE222B63C88569133F277364F8FF99C9F8471B9FF356C80E"), | ||
fromHex("505D4B3F56A5DA430C1CB5F96D48BCD21949B9E50148D7D1D64833AE8E8D516B"), | ||
}, | ||
{ | ||
fromHex("0a"), | ||
fromHex("BE4F901ABEB39DC3296E2F7E0280161C73233FCDEFDDF4AA8F4FF06609BE626E"), | ||
fromHex("1F07AC58BBBFA32FAF018B57C1ABF695EB1D94D68238B8AAE5C64C6309010C6C"), | ||
}, | ||
{ | ||
fromHex("0b"), | ||
fromHex("114A842FCC2FA6134609628A1134BAE6E66E7A55B68DDEBC228A0EA683053728"), | ||
fromHex("92A593D312418AB18A477BE0FFBC3E57D90B7B9F926BA341FDC820B760D77C1B"), | ||
}, | ||
{ | ||
fromHex("0c"), | ||
fromHex("7D977BA61750C5FF7268EF42BB61F82CAB5C29C2A66F3125431E849B7F3CCC92"), | ||
fromHex("DE12ABD5AE3D05C3029FF5D67FBE8B4DC1F265D0F2621B1856DD87DA21863E53"), | ||
}, | ||
} | ||
|
||
var testKeys2 = []testKey{ | ||
{ | ||
fromHex("CED4907163D8C2250299A2FE6A9D4F8A676501B90D570D50999B9E17FD993DE6"), | ||
fromHex("7F494925F5EEA6252467D1F97951A3E00ABE75D5DC2E9837BEA227DA4FE93F32"), | ||
fromHex("7608EB9FF53DB721B99009B60058CD72BD8A1E8CFFDA35154B3B487C0330B6A3"), | ||
}, | ||
{ | ||
fromHex("8F70C9391D8143F5A0CC3F56D3132868DE940F01EF55293E61054A0DCDA13C6C"), | ||
fromHex("96F6FB31F578A3C70402655B4E18FBA6ED82052AE38465D54EF3B392EEB599DB"), | ||
fromHex("36989A75343AB812BC7EBE61DBBC8433BEF72E994BEFF339F82E9B2FC908977E"), | ||
}, | ||
{ | ||
fromHex("4978C521DD195389ABC5B9C762482FCF1EAD50BF87E1209C744E50AE71596936"), | ||
fromHex("85DF197A07D0D83B5FDF0021E2494451FFB66127E7CA3645512AAD9DBF86715A"), | ||
fromHex("C5F6A22BE1583E65A6A1F4CB0309F8FFDD5C0B757FAB111D05AA40DE05D5F724"), | ||
}, | ||
{ | ||
fromHex("5803E676B9ACA28E76B8F41E8E06DE3063964DF566BC36677A9B02383ABB652C"), | ||
fromHex("B6DC581C5BBE45D94670577696E3C260C401AF2F16D53E9886EE68993CE4A68B"), | ||
fromHex("B851D966A2C9DC5EB9CB51893BBC68356E25D70DDEC59A777E85726651114382"), | ||
}, | ||
{ | ||
fromHex("33486286DC0682170C1864E85A38519127246F677AAF74124C2363E223E68E50"), | ||
fromHex("11106D2AEF58FAA4894185A37F84F8EB01B5173EB9D97EC9A2B59C89654A963B"), | ||
fromHex("9F9790B9AD5CCE7B491831F8D8B34C5957716351D46BBBB644C5E04899389459"), | ||
}, | ||
{ | ||
fromHex("FAD252070188E443F0C030E7C7ECA079851AA45EC38EA5591205A92BF72E7A8A"), | ||
fromHex("4F34B166D0F6E21398FDADDF90BD92A9466D5DC84807D12F0B26F63E6A6A6199"), | ||
fromHex("1B34BD337D078D6E3F0B1757C1EABD6E20C05903B146E307FC103E4C9D98AE86"), | ||
}, | ||
{ | ||
fromHex("6766501368501B7AECB273B176A1CB1734D10D8D680FA0DFDDD1AC655B9B1B1E"), | ||
fromHex("9F767B0EA1289232BC18599F54E3EFD3566BBC2F871A5277F8AFD8FAA4676191"), | ||
fromHex("A8F7B696569B7936C29C46087AC8DB0D22E4B0A98DA2A2E79073304341147EA8"), | ||
}, | ||
{ | ||
fromHex("BDE9C057B02F2A875920A93EA7E90E0ACFCFA61B725FF4273AC6EBF8F86D26C1"), | ||
fromHex("5C707BAD698506E2C8596B88F8D9E438A3CEEB0C513D95F171152522CD6F6BBB"), | ||
fromHex("AD37F0B3114D548DFE61AF0D2D20A17CF46F5FFA3E42624F0CC49A095AEF0327"), | ||
}, | ||
{ | ||
fromHex("D818D5F9167D7015E615FA914B43E926113D91F83B430E382337F9D3FE89DB44"), | ||
fromHex("331C4AEC55CFFBB0F4864532C5B21E5017A093A3848ADD18558D8B1706198E37"), | ||
fromHex("C4F73684630EA2800C5587AA6B5BAA6ECDB364DAA342D00E88BD79D73BF0DF42"), | ||
}, | ||
{ | ||
fromHex("C3550F59124BDE3BE6BEC2591D694E2528ED1410678B286DE6F626BF88FE5027"), | ||
fromHex("9029C4C081273D5708952B6F36428D16F3E0935F79187FE6992C4B494DC114A5"), | ||
fromHex("5F47621BCEE3C63EB7EF6C7E1B1301BF951928D43F91089CD48DF7B058285291"), | ||
}, | ||
{ | ||
fromHex("09A2AF8364E9C20C12CD68ADF99C77CFE016D602B29E702AF83233E980DCE1F4"), | ||
fromHex("C6BA1BB73E92B552D58A7585F4CBC911E05239EBFFED6681AB56C366873A204E"), | ||
fromHex("25B15BF7B8D3BE06249ACD3CBF928B79E21542FDABB2F3196D8A0561445A632A"), | ||
}, | ||
{ | ||
fromHex("DBC71EB3CEAB0EBE1E4DFC804EF0B64866720EB440A5D09FA017CBC399F62AF2"), | ||
fromHex("347262446EDF9B71B20CE9FB73CD8211959E8C5D0AC84B1FF3A3A139CA058567"), | ||
fromHex("9688F4CF75D5B8956DEABDBB420EFCB51B9F0727068B4A6D852B902B135F0A7B"), | ||
}, | ||
{ | ||
fromHex("E868A23B0DA36443B119E863657D918FB2783C03191838B67D0D704747AFE887"), | ||
fromHex("43694CB57D57CF1F8B4145577888AB9A33E45778A2CAFC078A529991269A4BEE"), | ||
fromHex("6686270715CC7F94C433A4DFC8036D6C3DA4384597AEA7E4DD5685034ACACB49"), | ||
}, | ||
{ | ||
fromHex("16763333BC3B7CDE3C19370CC0CECEDE456DF4F4CB1BE13F9BD4E3BCE2BC8F00"), | ||
fromHex("711AC9EB8D78CAA22CDBC82C8E777D686F22DC6AE236BC90E0A598AF8F720FA7"), | ||
fromHex("72E338F8D83E1D17A5E5A55D9D26539CFD4EEFC501FDCA6DEF36B8726FF1E85D"), | ||
}, | ||
{ | ||
fromHex("47CD1553152E9D5194FCC4BBBD32E98F046A063D64B506E1CE01FF79DA7693E0"), | ||
fromHex("F34C6657ACF2C1FE67036FA836C82BFBFEBCDF93D22E346CC9049F24B8C512C3"), | ||
fromHex("B394717C5C37D5A08128588D5922F0798D479D4B900BC29D74EC0C76EB8622EC"), | ||
}, | ||
{ | ||
fromHex("09307AFD97C5D3854F3D513B8BA6AE4FE246C46F4A3DFA746492BB4D948E4850"), | ||
fromHex("C91132EB3ACC4FA84FC8DF11A14FE24C828947F8299696A027E49B57BA5E6F5A"), | ||
fromHex("BA5BB5DB0F6B5FFC95FCD27F9267D5004FD79539BB3C6345E4C5E5CFDF89A5B6"), | ||
}, | ||
{ | ||
fromHex("C42B1903984390E20C403560927832D02BE36B1C7EE33F1987C5241CAB8A0F63"), | ||
fromHex("D218ADD38586BD8531F45ACDFDBE8B45D5489A1CCCC8C7AA005A65B07937C96F"), | ||
fromHex("DDC12E336442E1C7BD369CDF020E7F91C9B367178C212C331A15C9E012355658"), | ||
}, | ||
{ | ||
fromHex("F368D42DD35F918D4694AED51097784DE1B8F1CD204789B4E018DD36135F0CB4"), | ||
fromHex("D62675D5E732555270D6427F3C91319B0FC659F88CE45BDFB88C25B77F4BC2BA"), | ||
fromHex("C4F6ABADC81431C99E4E710F5AECFDE0B7AF4AC95DFB1635692B91EA85F4CBAF"), | ||
}, | ||
{ | ||
fromHex("B6E9E1F518C801082FCC37B8573A0D4C5E49E65EE7E7577082F024C8A6A3C798"), | ||
fromHex("6D682C005148718B620D0C169C324C3B407BC6F1125B7623694ED7704E888832"), | ||
fromHex("0D8C0BB56FBD8174C916955580D27D40F220E27F4C514999EB3D3ADFAAC1C52E"), | ||
}, | ||
{ | ||
fromHex("B352F9B096B909607E52853CF78FBAC0F106917BD705645AF3DB53E4CAC1868D"), | ||
fromHex("F8596F4A17518F117120018C537E59AA2AEB66817DC9EEA6EC1E5AABC1A4AB77"), | ||
fromHex("DDD08D4CA61ED8B7F3F06403134CF0309ECABCD611024CF7C77BE3D8CF8E3610"), | ||
}, | ||
} |
Oops, something went wrong.