├── migrations ├── 1_initial_migration.js └── 2_deploy_contracts.js ├── .gitignore ├── ethpm.json ├── truffle.js ├── contracts ├── Migrations.sol └── Arithmetic.sol ├── test ├── ThrowProxy.sol ├── TestArithmetic.sol └── arithmetic.js ├── package.json ├── README.md └── LICENSE /migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /migrations/2_deploy_contracts.js: -------------------------------------------------------------------------------- 1 | var Arithmetic = artifacts.require("./Arithmetic.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Arithmetic); 5 | }; 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Directories used by tools like mocha & istanbul 6 | coverage 7 | shippable 8 | 9 | # Dependency directory 10 | installed_contracts 11 | node_modules 12 | 13 | # Generated files 14 | config/development/contracts.json 15 | config/development/*.sol.js 16 | config/test/contracts.json 17 | build 18 | dist 19 | -------------------------------------------------------------------------------- /ethpm.json: -------------------------------------------------------------------------------- 1 | { 2 | "package_name": "arithmetic", 3 | "version": "0.1.0", 4 | "description": "A solidity library for performing arithmetic.", 5 | "authors": [ 6 | "Alan Lu " 7 | ], 8 | "keywords": [ 9 | "ethereum", 10 | "ethereum-contract", 11 | "solidity", 12 | "arithmetic", 13 | "library", 14 | "truffle", 15 | "multiplication", 16 | "division" 17 | ], 18 | "dependencies": {}, 19 | "license": "LGPL-3.0" 20 | } 21 | -------------------------------------------------------------------------------- /truffle.js: -------------------------------------------------------------------------------- 1 | var HDWalletProvider = require("truffle-hdwallet-provider"); 2 | 3 | // 12-word mnemonic 4 | var mnemonic = "congress drastic play goose cactus shine begin fork label panel churn potato"; 5 | 6 | module.exports = { 7 | networks: { 8 | development: { 9 | host: "localhost", 10 | port: 8545, 11 | network_id: "*" // Match any network id 12 | }, 13 | ropsten: { 14 | provider: new HDWalletProvider(mnemonic, "https://ropsten.infura.io/"), 15 | network_id: 3 // official id of the ropsten network 16 | } 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity >=0.4.24 <0.6.0; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | modifier restricted() { 8 | if (msg.sender == owner) _; 9 | } 10 | 11 | constructor() public { 12 | owner = msg.sender; 13 | } 14 | 15 | function setCompleted(uint completed) public restricted { 16 | last_completed_migration = completed; 17 | } 18 | 19 | function upgrade(address new_address) public restricted { 20 | Migrations upgraded = Migrations(new_address); 21 | upgraded.setCompleted(last_completed_migration); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /test/ThrowProxy.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.4; 2 | 3 | /* 4 | This is for testing if a transaction would throw. 5 | 6 | Contract calls rethrow when it encounters errors. 7 | Raw calls do not. 8 | You wrap your contract you want to test in a ThrowProxy. 9 | You prime it by calling the fallback function. 10 | Then executing it. 11 | 12 | False will be returned if it threw. 13 | True will be return it it did not throw or OOG. 14 | */ 15 | contract ThrowProxy { 16 | 17 | address public target; 18 | bytes data; 19 | 20 | function ThrowProxy(address _target) { 21 | target = _target; 22 | } 23 | 24 | //prime the data using the fallback function. 25 | function() { 26 | data = msg.data; 27 | } 28 | 29 | function execute() returns (bool) { 30 | return target.call(data); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "solidity-arithmetic", 3 | "version": "0.1.0", 4 | "description": "A solidity library for performing arithmetic.", 5 | "main": "truffle.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "dependencies": {}, 10 | "devDependencies": { 11 | "bignumber.js": "^4.0.0", 12 | "truffle-hdwallet-provider": "0.0.3" 13 | }, 14 | "scripts": { 15 | "test": "truffle test" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/gnosis/solidity-arithmetic.git" 20 | }, 21 | "author": "Alan Lu", 22 | "keywords": [ 23 | "ethereum", 24 | "ethereum-contract", 25 | "solidity", 26 | "arithmetic", 27 | "library", 28 | "truffle", 29 | "multiplication", 30 | "division" 31 | ], 32 | "license": "LGPL-3.0", 33 | "bugs": { 34 | "url": "https://github.com/gnosis/solidity-arithmetic/issues" 35 | }, 36 | "homepage": "https://github.com/gnosis/solidity-arithmetic#readme" 37 | } 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # solidity-arithmetic 2 | A solidity library for performing arithmetic. 3 | 4 | ## Usage 5 | The contract source is in the repo at `contracts/Arithmetic.sol`. Simply copy 6 | the contract file into your source tree and import it with: 7 | 8 | ```clike 9 | import "./path/to/Arithmetic.sol"; 10 | 11 | contract MyContract { 12 | function myFunction() { 13 | uint a, b, d; 14 | // your code here 15 | uint result = Arithmetic.overflowResistantFraction(a, b, d); 16 | } 17 | } 18 | ``` 19 | 20 | ## `library Arithmetic` API 21 | ```clike 22 | function mul256By256(uint a, uint b) 23 | constant 24 | returns (uint ab32, uint ab1, uint ab0) 25 | ``` 26 | This function takes two unsigned 256-bit integers and multiplies them, returning 27 | a 512-bit result split into a high 256-bit limb, a middle 128-bit limb, and a 28 | low 128-bit limb. 29 | 30 | ```clike 31 | function div256_128By256(uint a21, uint a0, uint b) 32 | constant 33 | returns (uint q, uint r) 34 | ``` 35 | This function takes a unsigned 384-bit integer and divides it by a 256-bit 36 | integer, returning a high-bits truncated 256-bit quotient and a remainder. The 37 | 384-bit dividend is represented as a high 256-bit limb and a low 128-bit limb. 38 | 39 | ```clike 40 | function overflowResistantFraction(uint a, uint b, uint divisor) 41 | returns (uint) 42 | ``` 43 | This function returns a 256-bit truncated `a * b / divisor`, where the division 44 | is integer division. The overflow from `a * b` is handled in a 512-bit buffer, 45 | so this method calculates the expression correctly for high values of `a` and `b`. 46 | -------------------------------------------------------------------------------- /contracts/Arithmetic.sol: -------------------------------------------------------------------------------- 1 | pragma solidity >=0.4.24 <0.6.0; 2 | 3 | // Arithmetic library 4 | library Arithmetic { 5 | function mul256By256(uint a, uint b) 6 | internal pure 7 | returns (uint ab32, uint ab1, uint ab0) 8 | { 9 | uint ahi = a >> 128; 10 | uint alo = a & 2**128-1; 11 | uint bhi = b >> 128; 12 | uint blo = b & 2**128-1; 13 | ab0 = alo * blo; 14 | ab1 = (ab0 >> 128) + (ahi * blo & 2**128-1) + (alo * bhi & 2**128-1); 15 | ab32 = (ab1 >> 128) + ahi * bhi + (ahi * blo >> 128) + (alo * bhi >> 128); 16 | ab1 &= 2**128-1; 17 | ab0 &= 2**128-1; 18 | } 19 | 20 | // I adapted this from Fast Division of Large Integers by Karl Hasselström 21 | // Algorithm 3.4: Divide-and-conquer division (3 by 2) 22 | // Karl got it from Burnikel and Ziegler and the GMP lib implementation 23 | function div256_128By256(uint a21, uint a0, uint b) 24 | internal pure 25 | returns (uint q, uint r) 26 | { 27 | uint qhi = (a21 / b) << 128; 28 | a21 %= b; 29 | 30 | uint shift = 0; 31 | while(b >> shift > 0) shift++; 32 | shift = 256 - shift; 33 | a21 = (a21 << shift) + (shift > 128 ? a0 << (shift - 128) : a0 >> (128 - shift)); 34 | a0 = (a0 << shift) & 2**128-1; 35 | b <<= shift; 36 | (uint256 b1, uint256 b0) = (b >> 128, b & 2**128-1); 37 | 38 | uint rhi; 39 | q = a21 / b1; 40 | rhi = a21 % b1; 41 | 42 | uint rsub0 = (q & 2**128-1) * b0; 43 | uint rsub21 = (q >> 128) * b0 + (rsub0 >> 128); 44 | rsub0 &= 2**128-1; 45 | 46 | while(rsub21 > rhi || rsub21 == rhi && rsub0 > a0) { 47 | q--; 48 | a0 += b0; 49 | rhi += b1 + (a0 >> 128); 50 | a0 &= 2**128-1; 51 | } 52 | 53 | q += qhi; 54 | r = (((rhi - rsub21) << 128) + a0 - rsub0) >> shift; 55 | } 56 | 57 | function overflowResistantFraction(uint a, uint b, uint divisor) 58 | internal pure 59 | returns (uint) 60 | { 61 | uint ab32_q1; uint ab1_r1; uint ab0; 62 | if(b <= 1 || b != 0 && a * b / b == a) { 63 | return a * b / divisor; 64 | } else { 65 | (ab32_q1, ab1_r1, ab0) = mul256By256(a, b); 66 | (ab32_q1, ab1_r1) = div256_128By256(ab32_q1, ab1_r1, divisor); 67 | (a, b) = div256_128By256(ab1_r1, ab0, divisor); 68 | return (ab32_q1 << 128) + a; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /test/TestArithmetic.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.2; 2 | 3 | import "truffle/Assert.sol"; 4 | import "truffle/DeployedAddresses.sol"; 5 | import "../contracts/Arithmetic.sol"; 6 | import "./ThrowProxy.sol"; 7 | 8 | contract ArithmeticThrower { 9 | function doNotThrowOnValidDiv() { 10 | Arithmetic.div256_128By256( 11 | 0xc14e772707388bb8558b1b24b0dd11f6cf50f61fcc9ad3e671189b2bc28270a7, 12 | 0x4ad41f3028d19ee5b1058090facc4f6a, 13 | 0xc21d87c491ba579f4cfbd2a65e868eca4e7f0ac566709f92998f26fdb0777c7); 14 | } 15 | 16 | function doThrowOnDivByZero() { 17 | Arithmetic.div256_128By256( 18 | 0xc14e772707388bb8558b1b24b0dd11f6cf50f61fcc9ad3e671189b2bc28270a7, 19 | 0x4ad41f3028d19ee5b1058090facc4f6a, 0); 20 | } 21 | } 22 | 23 | contract TestArithmetic { 24 | using Assert for *; 25 | 26 | uint constant a = 0xcafef00dcafef00dcafef00dcafef00dcafef00dcafef00dcafef00dcafef00d; 27 | uint constant b = 0xf7bdef7bdef7bdef7bdef7bdef7bdef7bdef7bdef7bdef7bdef7bdef7bde0000; 28 | 29 | function testMul256By256() { 30 | var (ab32, ab1, ab0) = Arithmetic.mul256By256(a, b); 31 | ab32.equal(0xc47295bac47295bac47295bac47295bac47295bac47295bac47295bac471d147, "high 256 bits of product wrong"); 32 | ab1.equal(0x6a453b8d6a453b8d6a453b8d6a453b8d, "next 128 bits of product wrong"); 33 | ab0.equal(0x6a453b8d6a453b8d6a453b8d6a460000, "low 128 bits of product wrong"); 34 | 35 | (ab32, ab1, ab0) = Arithmetic.mul256By256(2**256-1, 2**256-1); 36 | ab32.equal(2**256-2, "high 256 bits of max product wrong"); 37 | ab1.equal(0, "next 128 bits of max product wrong"); 38 | ab0.equal(1, "low 128 bits of max product wrong"); 39 | 40 | (ab32, ab1, ab0) = Arithmetic.mul256By256( 41 | 0x9014f6307009d2d5df0cb71c7f97859a04b335daea24bdf92e515f149837a8e6, 42 | 0xd72618b69d8c48d5d775dc419ecfc219551e515f3af0963ad4688920ce79c269 43 | ); 44 | ab32.equal(0x79170bc7f7f041e813f86c6c7852db20d88f13727f2e083410a19526a57f22ba, "high bits of some product wrong"); 45 | ab1.equal(0x4f96f469620c8051bfc75631846830fb, "next bits of some product wrong"); 46 | ab0.equal(0x0b08e4d8c0c1fde6126e89c485889256, "next bits of some product wrong"); 47 | } 48 | 49 | function testdiv256_128By256() { 50 | var (q, r) = Arithmetic.div256_128By256( 51 | 0xc47295bac47295bac47295bac47295bac47295bac47295bac47295bac471d147, 52 | 0x6a453b8d6a453b8d6a453b8d6a453b8d, 53 | 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000); 54 | 55 | q.equal(0xc47295bac47295bac47295bac47295ba, "wrong quotient"); 56 | r.equal(0xc47295bac47295bac47295bac47295b9ffffffffffffffffffffffffffff3b8d, "wrong remainder"); 57 | 58 | (q, r) = Arithmetic.div256_128By256(2**256-1, 2**128-1, 2**256-1); 59 | 60 | q.equal(2**128, "wrong quotient for maximal division args"); 61 | r.equal(2**128-1, "wrong remainder for maximal division args"); 62 | 63 | (q, r) = Arithmetic.div256_128By256( 64 | 0xc14e772707388bb8558b1b24b0dd11f6cf50f61fcc9ad3e671189b2bc28270a7, 65 | 0x4ad41f3028d19ee5b1058090facc4f6a, 66 | 0xc21d87c491ba579f4cfbd2a65e868eca4e7f0ac566709f92998f26fdb0777c7); 67 | 68 | q.equal(0xfeeeec0de9b8e3642b7e41db43cd76305, "wrong quotient for some division"); 69 | r.equal(0x4cb9d73e6c7e49b14dde63623d22354587735d56d2be2a21ff30c6994340387, "wrong remainder for some division"); 70 | 71 | (q, r) = Arithmetic.div256_128By256( 72 | 0xc14e772707388bb8558b1b24b0dd11f6cf50f61fcc9ad3e671189b2bc28270a7, 73 | 0x4ad41f3028d19ee5b1058090facc4f6a, 74 | 0x1a4e7f0ac566709f92998f26fdb0777c7); 75 | 76 | q.equal(0x75924065957af22f3caef35d592d3e6387fa560075ec52525de8eea7216708e5, "wrong quotient for dividing some 384-bit by 129-bit"); 77 | r.equal(0x16a825313ed69004e12d1e866f94ef267, "wrong remainder for dividing some 384-bit by 129-bit"); 78 | 79 | ArithmeticThrower thrower = new ArithmeticThrower(); 80 | ThrowProxy throwProxy = new ThrowProxy(address(thrower)); 81 | 82 | ArithmeticThrower(address(throwProxy)).doNotThrowOnValidDiv(); 83 | throwProxy.execute.gas(200000)().isTrue("should not throw when dividing normally"); 84 | 85 | ArithmeticThrower(address(throwProxy)).doThrowOnDivByZero(); 86 | throwProxy.execute.gas(200000)().isFalse("should throw on zero division"); 87 | } 88 | 89 | function testOverflowResistantFraction() { 90 | // a * b / d = c 91 | uint c = 0xc47295bac47295bac47295bac47295bac47295bac47295bac47295bac47295ba; 92 | uint d = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000; 93 | 94 | c.equal(Arithmetic.overflowResistantFraction(a, b, d), "lolwut"); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /test/arithmetic.js: -------------------------------------------------------------------------------- 1 | var BigNumber = require('bignumber.js'); 2 | BigNumber.config({ 3 | ROUNDING_MODE: BigNumber.ROUND_FLOOR 4 | }); 5 | 6 | let crypto = require('crypto'); 7 | 8 | var Arithmetic = artifacts.require("./Arithmetic.sol"); 9 | const pow2To256 = new BigNumber(2).pow(256), 10 | pow2To128 = new BigNumber(2).pow(128); 11 | 12 | function randBigNumber(numBytes) { 13 | return new BigNumber(crypto.randomBytes(numBytes).toString('hex'), 16); 14 | } 15 | 16 | function truncate256(value) { 17 | return value.sub(value.divToInt(pow2To256).mul(pow2To256)); 18 | } 19 | 20 | const numTimes = 100; 21 | 22 | contract('Arithmetic', function(accounts) { 23 | it(`should calculate a * b correctly ${numTimes} times`, function() { 24 | return Arithmetic.deployed().then(function(instance) { 25 | return Promise.all([...Array(numTimes)].map(function(_, i) { 26 | var a = randBigNumber(32 - Math.floor(i*31/numTimes)), 27 | b = randBigNumber(32 - i%31); 28 | 29 | return instance.mul256By256(a, b).then(function(cvals) { 30 | var res = a.times(b), 31 | c = cvals[0].times(pow2To256).plus(cvals[1].times(pow2To128)).plus(cvals[2]); 32 | 33 | assert(c.equals(res), `Contract value doesn't match BigNumber value:\n0x${c.toString(16)} !=\n0x${res.toString(16)}\nformat(\n 0x${a.toString(16)} *\n 0x${b.toString(16)}\n, '02x')`); 34 | }).then(function() { 35 | return instance.mul256By256.sendTransaction(a, b).then(function(txHash) { 36 | return Promise.resolve(Arithmetic.web3.eth.getTransactionReceipt(txHash).gasUsed); 37 | }); 38 | }); 39 | })).then(function(gasCosts) { 40 | gasCosts.sort(); 41 | console.log(`mul256By256 gas cost:\n range: ${gasCosts[0]}-${gasCosts[numTimes-1]}\n median: ${(gasCosts[Math.floor(numTimes/2)]+gasCosts[Math.ceil(numTimes/2)])/2}\n mean: ${gasCosts.reduce((a,b)=>a+b)/numTimes}`); 42 | }); 43 | }); 44 | }); 45 | 46 | it(`should calculate a / b correctly ${numTimes} times`, function() { 47 | return Arithmetic.deployed().then(function(instance) { 48 | return Promise.all([...Array(numTimes)].map(function(_, i) { 49 | var a = randBigNumber(48 - Math.floor(i*47/numTimes)), 50 | a21 = a.divToInt(pow2To128), 51 | a0 = a.sub(a21.mul(pow2To128)), 52 | b = randBigNumber(32 - i%31); 53 | return instance.div256_128By256.call(a21, a0, b).then(function(qr) { 54 | var resQ = truncate256(a.divToInt(b)), 55 | resR = truncate256(a.mod(b)); 56 | assert(qr[0].equals(resQ), `Contract q value doesn't match BigNumber q value:\n0x${qr[0].toString(16)} !=\n0x${resQ.toString(16)}\nformat(\n 0x${a.toString(16)} //\n 0x${b.toString(16)}\n, '02x')`); 57 | assert(qr[1].equals(resR), `Contract r value doesn't match BigNumber r value:\n0x${qr[1].toString(16)} !=\n0x${resQ.toString(16)}\nformat(\n 0x${a.toString(16)} %\n 0x${b.toString(16)}\n, '02x')`); 58 | }).then(function() { 59 | return instance.div256_128By256.sendTransaction(a21, a0, b).then(function(txHash) { 60 | return Promise.resolve(Arithmetic.web3.eth.getTransactionReceipt(txHash).gasUsed); 61 | }); 62 | }); 63 | })).then(function(gasCosts) { 64 | gasCosts.sort(); 65 | console.log(`div256_128By256 gas cost:\n range: ${gasCosts[0]}-${gasCosts[numTimes-1]}\n median: ${(gasCosts[Math.floor(numTimes/2)]+gasCosts[Math.ceil(numTimes/2)])/2}\n mean: ${gasCosts.reduce((a,b)=>a+b)/numTimes}`); 66 | }); 67 | }); 68 | }); 69 | 70 | it(`should calculate a * b / d correctly ${numTimes} times`, function() { 71 | return Arithmetic.deployed().then(function(instance) { 72 | return Promise.all([...Array(numTimes)].map(function(_, i) { 73 | var a = randBigNumber(32 - Math.floor(i*31/numTimes)), 74 | b = randBigNumber(32 - i%31), 75 | d = randBigNumber(32 - 2*(i%15)); 76 | return instance.overflowResistantFraction.call(a, b, d).then(function(c) { 77 | var res = truncate256(a.times(b).divToInt(d)); 78 | 79 | assert(c.equals(res), `Contract value doesn't match BigNumber value:\n0x${c.toString(16)} !=\n0x${res.toString(16)}\nformat(\n 0x${a.toString(16)} *\n 0x${b.toString(16)} //\n 0x${d.toString(16)}\n, '02x')`); 80 | }).then(function() { 81 | return instance.overflowResistantFraction.sendTransaction(a, b, d).then(function(txHash) { 82 | return Promise.resolve(Arithmetic.web3.eth.getTransactionReceipt(txHash).gasUsed); 83 | }); 84 | }); 85 | })).then(function(gasCosts) { 86 | gasCosts.sort(); 87 | console.log(`overflowResistantFraction gas cost:\n range: ${gasCosts[0]}-${gasCosts[numTimes-1]}\n median: ${(gasCosts[Math.floor(numTimes/2)]+gasCosts[Math.ceil(numTimes/2)])/2}\n mean: ${gasCosts.reduce((a,b)=>a+b)/numTimes}`); 88 | }); 89 | }); 90 | }); 91 | }); 92 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. --------------------------------------------------------------------------------