mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Make ARC presentation verification cost independent of the nonce (#478)
### Motivation: ARC presentation verification derives the tag using a loop that scales with the client-provided nonce, but it has everything it needs to recompute it with a scalar multiplication. ### Modifications: - Add a failing test. - Recompute `m1Tag` as `T - nonceScalar * self.tag`, the same way the prover does. ### Result: ARC presentation verification computation independent of magnitude of nonce.
This commit is contained in:
1 parent
8c018d0882
commit
25847ff705
2 files changed
+57
-2
No files matched your search
@@ -108,8 +108,8 @@ extension ARC {
|
||||
let T = H2G.hashToGroup(presentationContext, domainSeparationString: Data(("HashToGroup-" + ciphersuite.domain + "Tag").utf8))
|
||||
|
||||
// Recompute m1Tag = H2G(presentationContext) - nonce * tag
|
||||
var m1Tag = T
|
||||
for _ in 0..<nonce { m1Tag = m1Tag - self.tag }
|
||||
let nonceScalar = try Group.Scalar(bytes: I2OSP(value: nonce, outputByteCount: ciphersuite.scalarByteCount), reductionIsModOrder: true)
|
||||
let m1Tag = T - nonceScalar * self.tag
|
||||
|
||||
// Create a verifier, and allocate variables for the constrained scalars.
|
||||
var verifier = Verifier<H2G>(label: ciphersuite.domain + ciphersuite.domain + "CredentialPresentation")
|
||||
|
||||
@@ -158,6 +158,61 @@ class ARCTests: XCTestCase {
|
||||
// try endToEndWorkflow(CurveType: P521.self)
|
||||
}
|
||||
|
||||
func testVerifyCostIsIndependentOfTheNonce() async throws {
|
||||
let ciphersuite = ARC.Ciphersuite(HashToCurveImpl<P256>.self)
|
||||
let (generatorG, generatorH) = ARC.getGenerators(suite: ciphersuite)
|
||||
let x0 = GroupImpl<P256>.Scalar.random
|
||||
let x1 = GroupImpl<P256>.Scalar.random
|
||||
let x2 = GroupImpl<P256>.Scalar.random
|
||||
let x0Blinding = GroupImpl<P256>.Scalar.random
|
||||
let serverPrivateKey = ARC.ServerPrivateKey(x0: x0, x1: x1, x2: x2, x0Blinding: x0Blinding)
|
||||
let server = ARC.Server(ciphersuite: ciphersuite, x0: x0, x1: x1, x2: x2, x0Blinding: x0Blinding)
|
||||
let requestContext = Data("test request context".utf8)
|
||||
let precredential = try ARC.Precredential(
|
||||
ciphersuite: ciphersuite,
|
||||
m1: GroupImpl<P256>.Scalar.random,
|
||||
requestContext: requestContext,
|
||||
r1: GroupImpl<P256>.Scalar.random,
|
||||
r2: GroupImpl<P256>.Scalar.random,
|
||||
serverPublicKey: server.serverPublicKey
|
||||
)
|
||||
let credential = try precredential.makeCredential(
|
||||
credentialResponse: try server.respond(credentialRequest: precredential.credentialRequest)
|
||||
)
|
||||
|
||||
// The nonce comes from the client, so presentation verification cost must not scale with nonce.
|
||||
// This test uses such a large nonce that the test would hit the timeout if verification was correlated.
|
||||
let nonce = 1 << 62
|
||||
let presentationContext = Data("0123456789".utf8)
|
||||
let presentation = try ARC.Presentation(
|
||||
credential: credential,
|
||||
presentationContext: presentationContext,
|
||||
nonce: nonce,
|
||||
generatorG: generatorG,
|
||||
generatorH: generatorH
|
||||
)
|
||||
|
||||
let verificationResult: LockedBox<Bool?> = .init(initialValue: nil)
|
||||
let finished = expectation(description: "verification finished")
|
||||
let thread = Thread {
|
||||
let result = try? presentation.verify(
|
||||
serverPrivateKey: serverPrivateKey,
|
||||
X1: x1 * generatorH,
|
||||
m2: precredential.clientSecrets.m2,
|
||||
presentationContext: presentationContext,
|
||||
presentationLimit: Int.max,
|
||||
nonce: nonce,
|
||||
generatorG: generatorG,
|
||||
generatorH: generatorH,
|
||||
ciphersuite: ciphersuite)
|
||||
verificationResult.withLockedValue { $0 = result }
|
||||
finished.fulfill()
|
||||
}
|
||||
thread.start()
|
||||
await fulfillment(of: [finished], timeout: 10)
|
||||
XCTAssertEqual(verificationResult.withLockedValue { $0 }, true)
|
||||
}
|
||||
|
||||
func testPresentationState() throws {
|
||||
let context1 = Data("context1".utf8)
|
||||
let context2 = Data("context2".utf8)
|
||||
|
||||
Reference in new issue
Block a user