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

Fix test vectors for bign256 crate #1065

Merged
merged 15 commits into from
Jul 27, 2024
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bign256/src/ecdsa/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{BignP256, FieldBytes, NonZeroScalar, ProjectivePoint, PublicKey, Sca
use belt_hash::{BeltHash, Digest};
use core::fmt::{self, Debug};
use elliptic_curve::{
array::{consts::U32, typenum::Unsigned, Array},
array::{sizes::U32, typenum::Unsigned, Array},
ops::{MulByGenerator, Reduce},
point::AffineCoordinates,
subtle::{Choice, ConstantTimeEq},
Expand Down
2 changes: 1 addition & 1 deletion bign256/src/ecdsa/verifying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use belt_hash::{
BeltHash,
};
use elliptic_curve::{
array::{consts::U32, typenum::Unsigned, Array},
array::{sizes::U32, typenum::Unsigned, Array},
group::GroupEncoding,
ops::{LinearCombination, Reduce},
Curve, Field, Group,
Expand Down
230 changes: 105 additions & 125 deletions bign256/src/test_vectors/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use hex_literal::hex;
/// These are the first 20 test vectors, generated by sagemath snippet.
pub const ADD_TEST_VECTORS: &[([u8; 32], [u8; 32])] = &[
// k = 1,..,20, p += G
(
hex!("0000000000000000000000000000000000000000000000000000000000000000"),
hex!("6BF7FC3CFB16D69F5CE4C9A351D6835D78913966C408F6521E29CF1804516A93"),
),
(
hex!("2D8419E3D905D21E3611261DADC95BB35583090C2CADD0C807F53BB3828F0995"),
hex!("E62690D81BDC754EFAF5B2770054564849621E8A933A7ECD6C02387649CF258A"),
Expand Down Expand Up @@ -83,175 +87,151 @@ pub const ADD_TEST_VECTORS: &[([u8; 32], [u8; 32])] = &[
hex!("1230402EBB4A981C19506708F779C265D1695DF2132F17099D0E54BA1C46D945"),
hex!("FBB7A79A4E5F631E1FD59501F221FA2FAE93F8E2CD7D4D87E1003324C327EEB7"),
),
(
hex!("006A714979F622DBC2685A296CACA8067694D3ADC75F9F9A97F3BBA424DD2670"),
hex!("F4FD78EDF21FB1C4A23DD0B0FFD281FC35184F13D2B5D779DB7253E88097F121"),
),
];

// Snippet for generation:
// p = 2**256 - 189
// a = 2**256 - 192
// b = 0x77CE6C1515F3A8EDD2C13AABE4D8FBBE4CF55069978B9253B22E7D6BD69C03F1
// Gx = 0
// Gy = 0x6BF7FC3CFB16D69F5CE4C9A351D6835D78913966C408F6521E29CF1804516A93
// h = 1
//
// # Create the finite field and the elliptic curve
// F = GF(p)
// E = EllipticCurve(F, [a, b])
//
// # Define the base point
// G = E(Gx, Gy)
// n = G.order()
//
// # Generate test vectors
// def generate_mul_test_vectors(num_vectors):
// vectors = []
// for _ in range(num_vectors):
// k = ZZ.random_element(1, n)
// P = k * G
// vectors.append((k, P))
// return vectors
//
// # Number of test vectors to generate
// num_vectors = 20
// test_vectors = generate_mul_test_vectors(num_vectors)
//
// # Print the test vectors in the required format
// for k, P in test_vectors:
// k_hex = f"{k:064X}"
// Px_hex = f"{int(P[0]):064X}"
// Py_hex = f"{int(P[1]):064X}"
// print(f' (')
// print(f' hex!("{k_hex}"),')
// print(f' hex!("{Px_hex}"),')
// print(f' hex!("{Py_hex}"),')
// print(f' ),')

/// Scalar multiplication with the generator.
///
/// These are the test vectors from sagemath snippet,that are not part of [`ADD_TEST_VECTORS`].
/// k values is from NIST-P256 test vectors
/// k values is generated randomly
pub const MUL_TEST_VECTORS: &[([u8; 32], [u8; 32], [u8; 32])] = &[
(
hex!("BCE6EE467AA910ADD6FB47DF33EC4825582596B8C694DB7B403129C9427CF647"),
hex!("1D89D067B0EB566FBA7CA700A48AA90026F9584AFAA33139B890BCB21729140E"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
),
(
hex!("321D9319C3BE889BA0FD29C2E2688ABF39308A712A1D81446ED1ABC789E52CA9"),
hex!("E3B297D31A709BCD61B9861069EAEB8162237F970C11245C7E1761E2FC802B7A"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
),
(
hex!("132175D5AE957336F608DC53257205D81B088C5BDF048AF2C83B47784D70617C"),
hex!("1ADF305FCF83BA7C5B035615290644A70D381F50666D9B907673385CBCBD4791"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
),
(
hex!("BC01F7A6067E4529252C7E6F8F7D6EFA7889B8455ED042F0DE0870B419653658"),
hex!("65037BB63611DFE97DC043ED085181A4AA3A0D1D54C77C36135182ACF444265D"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
),
(
hex!("8E13BE6EAB0F05B8F0C07509DC8C17922B354ED5CF5E88D732E4EE031735319B"),
hex!("F0F23BA15E570754D21773E9D7B3D471B239415A29BB23D2C33DB3082D32966D"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
),
(
hex!("5859FC33184C7A38EE37FA61F13A47C96FF3E2D1A21347DF32297E3A7FA19FCD"),
hex!("F37B8AFF67104403A992853E307C988CE8F148E5A5D9F0091B094EC3E11CF0C0"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
),
(
hex!("1CF0FC9B67C3496E6104C6FE4CFEB03C582D6121E17B27FF5F163697021B1CA3"),
hex!("7ED1D7E43E06686AD6A050982927E54894022A55A872165CEDD82F1936800AC9"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
),
(
hex!("5251712A9696E4D3F4CC1C4B26877C200335311B03C9D53C8FC2ACE8ECECA49C"),
hex!("B04C72917AF4AAFA772314D49A2B899CD5D1D4F71FD0F18882050E61CEC361F9"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
),
(
hex!("7EF4573793DE5E95C749C810952408750B669FBFC4F177C27D74E66B95617258"),
hex!("6F7372A2DA5690F539834B9A8DD1910120E148C74F9376391F2DD41F8529DB57"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
),
(
hex!("85E00E5546713D9F2F5C8E605CA49AA16FA77CE3A3323B463599C31289697E37"),
hex!("CC258D7CA2CB8ACD629701844FE26AFE2029E127A59024E9885174E8BC35BB3F"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
),
(
hex!("796F1AA06F3366EF6C74822CD69D274D73FD7947D99E5A388D87525613C0164F"),
hex!("69FB53A5AE70C701BFCE800A78676265E87F3EDF6A1059A438B2A16A291DFEB9"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
),
(
hex!("660CEBCB17AAC38F454210254037CDE84ED2671B7C6B3531995DFAA478F01F20"),
hex!("3E752FDD0749DE0128C2A9237875413FB0C7866E9CD0F86B248272676728B6DF"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
),
(
hex!("108E86DBB15D670F952C36DDBADC381B7EE06052DED0C1FC369BC721F537BC21"),
hex!("35CA247AF507BE8B5FA662AFB35EB9F3DA55CB4E67FF048FA2757EBCF8ABF046"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("CED4907163D8C2250299A2FE6A9D4F8A676501B90D570D50999B9E17FD993DE6"),
hex!("7F494925F5EEA6252467D1F97951A3E00ABE75D5DC2E9837BEA227DA4FE93F32"),
hex!("7608EB9FF53DB721B99009B60058CD72BD8A1E8CFFDA35154B3B487C0330B6A3"),
),
(
hex!("62B8EBD7808F1A50B851D9080984B4A23181AF1CC60F2DE11F80CABBE27E213E"),
hex!("52A44CCB834C87D5CAA20C1B42260F9EDC1C30731C2F2F14AE2D52A7AF243794"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("8F70C9391D8143F5A0CC3F56D3132868DE940F01EF55293E61054A0DCDA13C6C"),
hex!("96F6FB31F578A3C70402655B4E18FBA6ED82052AE38465D54EF3B392EEB599DB"),
hex!("36989A75343AB812BC7EBE61DBBC8433BEF72E994BEFF339F82E9B2FC908977E"),
),
(
hex!("DD219D558AF61D9099822705054BA6F23F1378A5F482B1EC5AAD69138065B1D6"),
hex!("2C51E0EDE3618C1057E0360BECF3A550FE95E6096B9CD9326C0FEF0B68506859"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("4978C521DD195389ABC5B9C762482FCF1EAD50BF87E1209C744E50AE71596936"),
hex!("85DF197A07D0D83B5FDF0021E2494451FFB66127E7CA3645512AAD9DBF86715A"),
hex!("C5F6A22BE1583E65A6A1F4CB0309F8FFDD5C0B757FAB111D05AA40DE05D5F724"),
),
(
hex!("6C9E47CE0CCA48F4EDB53D0A02459C8D00BC670C82A4D091ADA36A158DB74DCB"),
hex!("7896936848785A4CCBBE5B39A3111EB3D36895B4ECDD911F3CBC4F08B0CE0FFC"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("5803E676B9ACA28E76B8F41E8E06DE3063964DF566BC36677A9B02383ABB652C"),
hex!("B6DC581C5BBE45D94670577696E3C260C401AF2F16D53E9886EE68993CE4A68B"),
hex!("B851D966A2C9DC5EB9CB51893BBC68356E25D70DDEC59A777E85726651114382"),
),
(
hex!("A320F2ED3C408FEB68BB456A75E14B943DA7B02B0C8A76A5737DE66F008148C4"),
hex!("C6AC14D4F31C17D720AD8A4711BD0DB84D9AE8FC6070289EA1883775D410C690"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("33486286DC0682170C1864E85A38519127246F677AAF74124C2363E223E68E50"),
hex!("11106D2AEF58FAA4894185A37F84F8EB01B5173EB9D97EC9A2B59C89654A963B"),
hex!("9F9790B9AD5CCE7B491831F8D8B34C5957716351D46BBBB644C5E04899389459"),
),
(
hex!("84221BA012EFC0694EC2DDB3B6B003FB92CD329F193D2A0A316050391DF99667"),
hex!("E91DA84671D47149BD8343291CB00F4639E90828A005176F58541F2A0DCDF185"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("FAD252070188E443F0C030E7C7ECA079851AA45EC38EA5591205A92BF72E7A8A"),
hex!("4F34B166D0F6E21398FDADDF90BD92A9466D5DC84807D12F0B26F63E6A6A6199"),
hex!("1B34BD337D078D6E3F0B1757C1EABD6E20C05903B146E307FC103E4C9D98AE86"),
),
(
hex!("F4BD16F5E8BE79055EE7B62FF8FE9381A20D2182CCE62CA77C740C7B627C0384"),
hex!("38B1E87028B46FF7742999AABB634D713DA2810C159A004E8A2C13CAF2428D96"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("6766501368501B7AECB273B176A1CB1734D10D8D680FA0DFDDD1AC655B9B1B1E"),
hex!("9F767B0EA1289232BC18599F54E3EFD3566BBC2F871A5277F8AFD8FAA4676191"),
hex!("A8F7B696569B7936C29C46087AC8DB0D22E4B0A98DA2A2E79073304341147EA8"),
),
(
hex!("99773AFF98782D076D6D759943BC1F3E9DD57A464E622A7E23ADC96413A55841"),
hex!("9BA741511B2EB4465A9CB0B21133A47A1FD9163B7946289C31221EC60E1FF8B5"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("BDE9C057B02F2A875920A93EA7E90E0ACFCFA61B725FF4273AC6EBF8F86D26C1"),
hex!("5C707BAD698506E2C8596B88F8D9E438A3CEEB0C513D95F171152522CD6F6BBB"),
hex!("AD37F0B3114D548DFE61AF0D2D20A17CF46F5FFA3E42624F0CC49A095AEF0327"),
),
(
hex!("1666D4DA3F23A9CF7EB2CE3DEF0ACB5C781F7EF080F169DF887EB39ED0CCBD48"),
hex!("C4779DF3C1F5DAAE8C0DB77BE0CF9D49F249647360A867C36A20047CF5D18CDC"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("D818D5F9167D7015E615FA914B43E926113D91F83B430E382337F9D3FE89DB44"),
hex!("331C4AEC55CFFBB0F4864532C5B21E5017A093A3848ADD18558D8B1706198E37"),
hex!("C4F73684630EA2800C5587AA6B5BAA6ECDB364DAA342D00E88BD79D73BF0DF42"),
),
(
hex!("F7D96B2CE1766AC5DF7C0CF5CDE463101334182908635F43F24B61975DE671BF"),
hex!("04B069AD735EE63E50AE27E5D7EDD6257134C6E8D8CA3AE3040001A9855682A2"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("C3550F59124BDE3BE6BEC2591D694E2528ED1410678B286DE6F626BF88FE5027"),
hex!("9029C4C081273D5708952B6F36428D16F3E0935F79187FE6992C4B494DC114A5"),
hex!("5F47621BCEE3C63EB7EF6C7E1B1301BF951928D43F91089CD48DF7B058285291"),
),
(
hex!("2F78EF54DD714BA141A893B9DEF9C14AA47900440C8C42ED90AE05D2E8665E21"),
hex!("1DDE14D0959610C2966258889FD025666B746765FCA36F6025309026145EFD3A"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("09A2AF8364E9C20C12CD68ADF99C77CFE016D602B29E702AF83233E980DCE1F4"),
hex!("C6BA1BB73E92B552D58A7585F4CBC911E05239EBFFED6681AB56C366873A204E"),
hex!("25B15BF7B8D3BE06249ACD3CBF928B79E21542FDABB2F3196D8A0561445A632A"),
),
(
hex!("3B1514BF33F6EC2C5FEEA324D04F06ECBC516B358400D67B67181029F4AF8AC9"),
hex!("0F7917B7EEAF7E7836D5FD8BDB488858CFE2E78BD9FA66757EF4D89BFAA27F69"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("DBC71EB3CEAB0EBE1E4DFC804EF0B64866720EB440A5D09FA017CBC399F62AF2"),
hex!("347262446EDF9B71B20CE9FB73CD8211959E8C5D0AC84B1FF3A3A139CA058567"),
hex!("9688F4CF75D5B8956DEABDBB420EFCB51B9F0727068B4A6D852B902B135F0A7B"),
),
(
hex!("67CC0FDF4E1735A5FCB98168158945DDE241FBA39BE2B35A5CD904CA5EB88A52"),
hex!("2C9B912969C98E7B7ED01CCDB9F32951A529BA77F40DC79B2241DA3D6FAD6C00"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("E868A23B0DA36443B119E863657D918FB2783C03191838B67D0D704747AFE887"),
hex!("43694CB57D57CF1F8B4145577888AB9A33E45778A2CAFC078A529991269A4BEE"),
hex!("6686270715CC7F94C433A4DFC8036D6C3DA4384597AEA7E4DD5685034ACACB49"),
),
(
hex!("97C9D36D07BDAA3846CDAE30E68EA4D5A84896A7A94056B4424B08B58E41BB84"),
hex!("B528D08C013362D444A9A345570E59B2E5265720B8B4F128ED6700994618990B"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("16763333BC3B7CDE3C19370CC0CECEDE456DF4F4CB1BE13F9BD4E3BCE2BC8F00"),
hex!("711AC9EB8D78CAA22CDBC82C8E777D686F22DC6AE236BC90E0A598AF8F720FA7"),
hex!("72E338F8D83E1D17A5E5A55D9D26539CFD4EEFC501FDCA6DEF36B8726FF1E85D"),
),
(
hex!("AED8D377B262039F5FA3625CBE7DDAD5129B350B9A54CE58B4E51C36DB2355B5"),
hex!("8A8B1F284CE3D769477334C6569FCFB6BAE71E8E5E1509873366A7695961B06C"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("47CD1553152E9D5194FCC4BBBD32E98F046A063D64B506E1CE01FF79DA7693E0"),
hex!("F34C6657ACF2C1FE67036FA836C82BFBFEBCDF93D22E346CC9049F24B8C512C3"),
hex!("B394717C5C37D5A08128588D5922F0798D479D4B900BC29D74EC0C76EB8622EC"),
),
(
hex!("5C01A3FEB3B295E76F091B20E652ED48A59F9AEC46CBA86160A96B59BFF6276D"),
hex!("502DBD20E1F58654546891CB66480C52155A0A45F7BD2FDE2563F2E0D5F5CD2B"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("09307AFD97C5D3854F3D513B8BA6AE4FE246C46F4A3DFA746492BB4D948E4850"),
hex!("C91132EB3ACC4FA84FC8DF11A14FE24C828947F8299696A027E49B57BA5E6F5A"),
hex!("BA5BB5DB0F6B5FFC95FCD27F9267D5004FD79539BB3C6345E4C5E5CFDF89A5B6"),
),
(
hex!("FA2879ADDCD74E29E049EAAD5279A01987D08EF515693E721572EEF5D0445278"),
hex!("332FEE155643CEFD408350C49CC776E1589770F217B2F47389FFBAE3AA8F9DE4"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("C42B1903984390E20C403560927832D02BE36B1C7EE33F1987C5241CAB8A0F63"),
hex!("D218ADD38586BD8531F45ACDFDBE8B45D5489A1CCCC8C7AA005A65B07937C96F"),
hex!("DDC12E336442E1C7BD369CDF020E7F91C9B367178C212C331A15C9E012355658"),
),
(
hex!("D0B331A63DD4815CEF494557A5CA040296E19737E0D84A8AB95C0B7404799584"),
hex!("4E982EF880233592068F58E117BE4A13EC544FB10248E10821EE4037A5466C86"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("F368D42DD35F918D4694AED51097784DE1B8F1CD204789B4E018DD36135F0CB4"),
hex!("D62675D5E732555270D6427F3C91319B0FC659F88CE45BDFB88C25B77F4BC2BA"),
hex!("C4F6ABADC81431C99E4E710F5AECFDE0B7AF4AC95DFB1635692B91EA85F4CBAF"),
),
(
hex!("51A0F158FF49D20AE469441FC627516DCB1D1161119A7B9F9531D52C8F26A36A"),
hex!("5047FC9B8057392D20057845073F58AF80DE2C1DB80D8C814AFE6C21A4033581"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("B6E9E1F518C801082FCC37B8573A0D4C5E49E65EE7E7577082F024C8A6A3C798"),
hex!("6D682C005148718B620D0C169C324C3B407BC6F1125B7623694ED7704E888832"),
hex!("0D8C0BB56FBD8174C916955580D27D40F220E27F4C514999EB3D3ADFAAC1C52E"),
),
(
hex!("F02838F64C02057A1191C490C51501A5C261A3201A0330E1A31B67FE121C9434"),
hex!("172B364C2952F8ED8771B3B152DF85A8039E9F6D7B2669E90078D2D47393902B"),
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
hex!("B352F9B096B909607E52853CF78FBAC0F106917BD705645AF3DB53E4CAC1868D"),
hex!("F8596F4A17518F117120018C537E59AA2AEB66817DC9EEA6EC1E5AABC1A4AB77"),
hex!("DDD08D4CA61ED8B7F3F06403134CF0309ECABCD611024CF7C77BE3D8CF8E3610"),
),
];
36 changes: 16 additions & 20 deletions bign256/tests/projective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@

#![cfg(all(feature = "arithmetic", feature = "test-vectors"))]

// TODO(tarcieri): these are failing
//
// use bign256::{
// test_vectors::group::{ADD_TEST_VECTORS, MUL_TEST_VECTORS},
// AffinePoint, ProjectivePoint, Scalar,
// };
// use elliptic_curve::{
// group::{ff::PrimeField, GroupEncoding},
// sec1::{self, ToEncodedPoint},
// };
// use primeorder::{impl_projective_arithmetic_tests, Double};
//
// impl_projective_arithmetic_tests!(
// AffinePoint,
// ProjectivePoint,
// Scalar,
// ADD_TEST_VECTORS,
// MUL_TEST_VECTORS
// );
use bign256::{
test_vectors::group::{ADD_TEST_VECTORS, MUL_TEST_VECTORS},
AffinePoint, ProjectivePoint, Scalar,
};
use elliptic_curve::{
group::{ff::PrimeField, GroupEncoding},
sec1::{self, ToEncodedPoint},
};
use primeorder::{impl_projective_arithmetic_tests, Double};

use bign256::{elliptic_curve::group::GroupEncoding, ProjectivePoint};
impl_projective_arithmetic_tests!(
AffinePoint,
ProjectivePoint,
Scalar,
ADD_TEST_VECTORS,
MUL_TEST_VECTORS
);

#[test]
fn projective_identity_to_bytes() {
Expand Down
Loading